From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from firstgate.proxmox.com (firstgate.proxmox.com [IPv6:2a01:7e0:0:424::9]) by lore.proxmox.com (Postfix) with ESMTPS id 0BC121FF15C for ; Fri, 17 Oct 2025 14:47:40 +0200 (CEST) Received: from firstgate.proxmox.com (localhost [127.0.0.1]) by firstgate.proxmox.com (Proxmox) with ESMTP id 99EB128FC; Fri, 17 Oct 2025 14:48:00 +0200 (CEST) From: Shannon Sterz To: pdm-devel@lists.proxmox.com Date: Fri, 17 Oct 2025 14:46:44 +0200 Message-ID: <20251017124646.294343-6-s.sterz@proxmox.com> X-Mailer: git-send-email 2.47.3 In-Reply-To: <20251017124646.294343-2-s.sterz@proxmox.com> References: <20251017124646.294343-2-s.sterz@proxmox.com> MIME-Version: 1.0 X-Bm-Milter-Handled: 55990f41-d878-4baa-be0a-ee34c49e34d2 X-Bm-Transport-Timestamp: 1760705239951 X-SPAM-LEVEL: Spam detection results: 0 AWL 0.056 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DMARC_MISSING 0.1 Missing DMARC policy KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment SPF_HELO_NONE 0.001 SPF: HELO does not publish an SPF Record SPF_PASS -0.001 SPF: sender matches SPF record Subject: [pdm-devel] [PATCH yew-comp v3 3/3] utils: split out clipboard helpers into their own modules X-BeenThere: pdm-devel@lists.proxmox.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: Proxmox Datacenter Manager development discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: Proxmox Datacenter Manager development discussion Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: pdm-devel-bounces@lists.proxmox.com Sender: "pdm-devel" to make the utils module a little bit easier to navigate split out some functionality into their own submodules and re-export their functions. start with the clipboard helpers for now. Signed-off-by: Shannon Sterz --- src/utils/clipboard.rs | 46 +++++++++++++++++++++++++++++++++ src/{utils.rs => utils/mod.rs} | 47 +++------------------------------- 2 files changed, 50 insertions(+), 43 deletions(-) create mode 100644 src/utils/clipboard.rs rename src/{utils.rs => utils/mod.rs} (92%) diff --git a/src/utils/clipboard.rs b/src/utils/clipboard.rs new file mode 100644 index 0000000..26dde05 --- /dev/null +++ b/src/utils/clipboard.rs @@ -0,0 +1,46 @@ +use wasm_bindgen::JsCast; +use yew::NodeRef; + +use pwt::convert_js_error; + +#[deprecated( + note = "This relies on the deprecated `execCommand` method. Please use `utils::copy_text_to_clipboard` instead." +)] +/// Copies the content of the passed `NodeRef` to the user's clipboard. The `NodeRef` should be +/// caste-able to `web_sys::HtmlInputElement`. +pub fn copy_to_clipboard(node_ref: &NodeRef) { + if let Some(el) = node_ref.cast::() { + let window = gloo_utils::window(); + let document = gloo_utils::document(); + + let selection = window.get_selection().unwrap().unwrap(); + let _ = selection.remove_all_ranges(); + + let range = document.create_range().unwrap(); + let _ = range.select_node_contents(&el); + + let _ = selection.add_range(&range); + + let document = document.dyn_into::().unwrap(); + let _ = document.exec_command("copy"); + } +} + +/// Copies `text` to a user's clipboard via the `Clipboard` API. +pub fn copy_text_to_clipboard(text: &str) { + let text = text.to_owned(); + + wasm_bindgen_futures::spawn_local(async move { + let future: wasm_bindgen_futures::JsFuture = gloo_utils::window() + .navigator() + .clipboard() + .write_text(&text) + .into(); + + let res = future.await.map_err(convert_js_error); + + if let Err(e) = res { + log::error!("could not copy to clipboard: {e:#}"); + } + }); +} diff --git a/src/utils.rs b/src/utils/mod.rs similarity index 92% rename from src/utils.rs rename to src/utils/mod.rs index e793414..c55431f 100644 --- a/src/utils.rs +++ b/src/utils/mod.rs @@ -2,16 +2,17 @@ use std::collections::HashMap; use std::fmt::Display; use std::sync::Mutex; -use pwt::convert_js_error; use serde_json::Value; -use wasm_bindgen::JsCast; use yew::prelude::*; -use yew::NodeRef; use crate::common_api_types::ProxmoxUpid; use pwt::tr; +mod clipboard; +#[allow(deprecated)] +pub use clipboard::{copy_text_to_clipboard, copy_to_clipboard}; + /// Somewhat like a human would tell durations, omit zero values and do not /// give seconds precision if we talk days already pub fn format_duration_human(ut: f64) -> String { @@ -339,46 +340,6 @@ pub fn json_array_to_flat_string(list: &[Value]) -> String { list.join(" ") } -#[deprecated( - note = "This relies on the deprecated `execCommand` method. Please use `utils::copy_text_to_clipboard` instead." -)] -pub fn copy_to_clipboard(node_ref: &NodeRef) { - if let Some(el) = node_ref.cast::() { - let window = gloo_utils::window(); - let document = gloo_utils::document(); - - let selection = window.get_selection().unwrap().unwrap(); - let _ = selection.remove_all_ranges(); - - let range = document.create_range().unwrap(); - let _ = range.select_node_contents(&el); - - let _ = selection.add_range(&range); - - let document = document.dyn_into::().unwrap(); - let _ = document.exec_command("copy"); - } -} - -/// Copies `text` to a user's clipboard via the `Clipboard` API. -pub fn copy_text_to_clipboard(text: &str) { - let text = text.to_owned(); - - wasm_bindgen_futures::spawn_local(async move { - let future: wasm_bindgen_futures::JsFuture = gloo_utils::window() - .navigator() - .clipboard() - .write_text(&text) - .into(); - - let res = future.await.map_err(convert_js_error); - - if let Err(e) = res { - log::error!("could not copy to clipboard: {e:#}"); - } - }); -} - /// Set the browser window.location.href pub fn set_location_href(href: &str) { let window = gloo_utils::window(); -- 2.47.3 _______________________________________________ pdm-devel mailing list pdm-devel@lists.proxmox.com https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel