From: Shannon Sterz <s.sterz@proxmox.com>
To: pdm-devel@lists.proxmox.com
Subject: [pdm-devel] [PATCH yew-comp v3 3/3] utils: split out clipboard helpers into their own modules
Date: Fri, 17 Oct 2025 14:46:44 +0200 [thread overview]
Message-ID: <20251017124646.294343-6-s.sterz@proxmox.com> (raw)
In-Reply-To: <20251017124646.294343-2-s.sterz@proxmox.com>
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@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
_______________________________________________
pdm-devel mailing list
pdm-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pdm-devel
next prev parent reply other threads:[~2025-10-17 12:47 UTC|newest]
Thread overview: 19+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-10-17 12:46 [pdm-devel] [PATCH datacenter-manager/yew-comp/yew-widget-toolkit v3 0/7] add better token support for pdm Shannon Sterz
2025-10-17 12:46 ` [pdm-devel] [PATCH yew-widget-toolkit v3 1/1] props: add readonly to field_std_props Shannon Sterz
2025-10-21 18:52 ` [pdm-devel] applied: " Thomas Lamprecht
2025-10-17 12:46 ` [pdm-devel] [PATCH yew-comp v3 1/3] utils/tfa add recover/token panel: add copy_text_to_clipboard function Shannon Sterz
2025-10-22 17:03 ` [pdm-devel] applied: " Thomas Lamprecht
2025-10-17 12:46 ` [pdm-devel] [PATCH yew-comp v3 2/3] token panel: improve token secret dialog layout and hide password Shannon Sterz
2025-10-22 17:03 ` [pdm-devel] applied: " Thomas Lamprecht
2025-10-17 12:46 ` Shannon Sterz [this message]
2025-10-17 12:51 ` [pdm-devel] [PATCH yew-comp v3 3/3] utils: split out clipboard helpers into their own modules Shannon Sterz
2025-10-22 17:03 ` [pdm-devel] applied: " Thomas Lamprecht
2025-10-17 12:46 ` [pdm-devel] [PATCH datacenter-manager v3 1/3] ui: add a token panel and a token acl edit menu in the permissions panel Shannon Sterz
2025-10-17 12:52 ` Shannon Sterz
2025-10-22 17:22 ` [pdm-devel] applied: " Thomas Lamprecht
2025-10-17 12:46 ` [pdm-devel] [PATCH datacenter-manager v3 2/3] server: access: use token endpoints from proxmox-access-control Shannon Sterz
2025-10-17 12:52 ` Shannon Sterz
2025-10-22 17:22 ` [pdm-devel] applied: " Thomas Lamprecht
2025-10-17 12:46 ` [pdm-devel] [PATCH datacenter-manager v3 3/3] server: clean up acl tree entries and api tokens when deleting users Shannon Sterz
2025-10-17 12:52 ` Shannon Sterz
2025-10-22 17:22 ` [pdm-devel] applied: " Thomas Lamprecht
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20251017124646.294343-6-s.sterz@proxmox.com \
--to=s.sterz@proxmox.com \
--cc=pdm-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox