diff --git a/hel/src/commands.rs b/hel/src/commands.rs index 370b732..81de786 100644 --- a/hel/src/commands.rs +++ b/hel/src/commands.rs @@ -14,6 +14,17 @@ use crate::utils::editor::password; #[cfg_attr(target_arch = "wasm32", allow(unused_imports))] use crate::utils::{call_cmd_with_input, get_cmd_args_from_command, get_copy_command_from_env, rnd}; +// In the browser `pb` copies through the host page's clipboard (navigator.clipboard) +// instead of shelling out to pbcopy/xclip. +#[cfg(target_arch = "wasm32")] +use wasm_bindgen::prelude::wasm_bindgen; +#[cfg(target_arch = "wasm32")] +#[wasm_bindgen] +extern "C" { + #[wasm_bindgen(js_name = hel_clipboard_write)] + fn hel_clipboard_write(text: &str); +} + impl<'a> LKEval<'a> { pub fn get_password(&self, name: &String) -> Option { match self.state.lock().borrow().ls.get(name) { @@ -218,7 +229,8 @@ impl<'a> LKEval<'a> { // In the browser the page provides a Copy button instead. #[cfg(target_arch = "wasm32")] { - out.e("error: pb (clipboard copy) is not available in the browser; use the Copy button".to_string()); + hel_clipboard_write(&data); + out.o(format!("Copied {} characters to the clipboard", data.chars().count())); } #[cfg(not(target_arch = "wasm32"))] { diff --git a/helwasm/index.html b/helwasm/index.html index aa7c92f..2b90007 100644 --- a/helwasm/index.html +++ b/helwasm/index.html @@ -221,6 +221,9 @@ }; window.hel_storage_get = (k) => localStorage.getItem("hel:" + k); window.hel_storage_set = (k, v) => localStorage.setItem("hel:" + k, v); + // `pb` copies to the system clipboard. Fire-and-forget: it runs inside the Enter + // keypress handler, so the user-gesture requirement for clipboard write is met. + window.hel_clipboard_write = (text) => { try { navigator.clipboard.writeText(text); } catch (e) {} }; // ---- helpers ---- const persist = () => hel_command("save " + CATALOG_KEY); diff --git a/helwasm/pkg/helwasm.js b/helwasm/pkg/helwasm.js index f49584d..838c873 100644 --- a/helwasm/pkg/helwasm.js +++ b/helwasm/pkg/helwasm.js @@ -142,6 +142,9 @@ function __wbg_get_imports() { const ret = arg0.getTimezoneOffset(); return ret; }, + __wbg_hel_clipboard_write_efa8177dd67c65ec: function(arg0, arg1) { + hel_clipboard_write(getStringFromWasm0(arg0, arg1)); + }, __wbg_hel_get_password_271a2beac04c29db: function(arg0, arg1, arg2) { const ret = hel_get_password(getStringFromWasm0(arg1, arg2)); const ptr1 = passStringToWasm0(ret, wasm.__wbindgen_malloc, wasm.__wbindgen_realloc); diff --git a/helwasm/pkg/helwasm_bg.wasm b/helwasm/pkg/helwasm_bg.wasm index 5fe3518..7aeaabb 100644 Binary files a/helwasm/pkg/helwasm_bg.wasm and b/helwasm/pkg/helwasm_bg.wasm differ