[pdm-devel] [PATCH yew-comp v3 3/3] utils: split out clipboard helpers into their own modules
Shannon Sterz
s.sterz at proxmox.com
Fri Oct 17 14:46:44 CEST 2025
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 <s.sterz at proxmox.com>
---
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::<web_sys::HtmlInputElement>() {
+ 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::<web_sys::HtmlDocument>().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::<web_sys::HtmlInputElement>() {
- 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::<web_sys::HtmlDocument>().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
More information about the pdm-devel
mailing list