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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
//! Process-wide **rich clipboard slot** for [`RichTextEdit`](super::RichTextEdit).
//!
//! The system clipboard only carries plain text (see [`crate::clipboard`]), so a
//! styled Copy/Cut would lose every run's colour, size, and weight on paste.
//! This module keeps the last styled fragment in an in-process thread-local slot
//! alongside a **fingerprint** — the exact plain text that Copy wrote to the
//! system clipboard.
//!
//! On paste the editor asks [`matching`] for the stored fragment, passing the
//! plain text it just read back from the system clipboard. The fragment is
//! returned only when the fingerprints match, which means the clipboard has not
//! changed since our Copy (same session, nothing copied elsewhere in between).
//! On any mismatch the caller falls back to inserting the external plain text.
//!
//! The slot is process-wide (thread-local, and the UI runs on one thread on both
//! native and wasm), so a fragment copied from one `RichTextEdit` pastes with
//! full styling into another in the same app.
use RefCell;
use Block;
/// The stored styled fragment plus the plain-text fingerprint that must match
/// the system clipboard for the fragment to be reused.
thread_local!
/// Store a styled `fragment` under the plain-text `fingerprint` that Copy/Cut
/// simultaneously wrote to the system clipboard. Replaces any previous payload.
/// Return a clone of the stored fragment when its fingerprint matches `plain`
/// (the text just read from the system clipboard); otherwise `None`.
///
/// Line endings are normalized on *both* sides before comparing: native
/// clipboards (arboard on Windows in particular) round-trip `\n` as `\r\n`, so a
/// byte-exact match would spuriously fail on multi-line copies and drop the
/// styled fragment. Cloning (not taking) lets the same Copy be pasted
/// repeatedly.
/// Collapse `\r\n` and lone `\r` to `\n` so fingerprints compare equal
/// regardless of the platform clipboard's line-ending convention.
/// Drop any stored payload. Test-only hook so a fingerprint-mismatch case can't
/// be masked by a fragment left over from an earlier test on the same thread.