1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//! WASM-specific in-process clipboard buffer.
//!
//! Because `arboard` (the native clipboard crate) does not work in a browser
//! context, WASM clipboard operations use a thread-local `String` as the
//! in-process buffer. The JS harness in `demo-wasm` bridges this buffer to
//! the browser's system clipboard:
//!
//! * **Copy / Cut**: the Rust `clipboard_set` stub writes selected text here;
//! the JS `copy`/`cut` DOM event handler reads it via `wasm_clipboard_get()`
//! and places it in `event.clipboardData`, which lands in the system clipboard.
//!
//! * **Paste**: the JS `paste` DOM event handler reads the system clipboard text
//! from `event.clipboardData` and writes it here via `wasm_clipboard_set()`;
//! it then synthesises a Ctrl+V key event so Rust's paste handler picks it up.
//!
//! This module is compiled only when `target_arch = "wasm32"`.
use std::cell::RefCell;
thread_local! {
static BUFFER: RefCell<String> = RefCell::new(String::new());
static HTML_BUFFER: RefCell<String> = RefCell::new(String::new());
}
/// Read the current clipboard buffer. Returns `None` when the buffer is empty.
pub fn get() -> Option<String> {
BUFFER.with(|b| {
let s = b.borrow();
if s.is_empty() {
None
} else {
Some(s.clone())
}
})
}
/// Read the current HTML clipboard buffer. Returns `None` when empty.
pub fn get_html() -> Option<String> {
HTML_BUFFER.with(|b| {
let s = b.borrow();
if s.is_empty() {
None
} else {
Some(s.clone())
}
})
}
/// Overwrite the clipboard buffer with `text`.
pub fn set(text: &str) {
BUFFER.with(|b| *b.borrow_mut() = text.to_string());
HTML_BUFFER.with(|b| b.borrow_mut().clear());
}
/// Overwrite the clipboard buffers with plain text and rendered HTML.
pub fn set_rich(text: &str, html: &str) {
BUFFER.with(|b| *b.borrow_mut() = text.to_string());
HTML_BUFFER.with(|b| *b.borrow_mut() = html.to_string());
}