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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
//! `NodeSelectionExtension.ts`.
use super::editor_state_extension::editor_state_extension;
use super::signals::{ReadonlySignal, Signal, computed, effect, signal};
use super::watched_signal::watched_signal;
use crate::extension_core::{ExtensionBuildState, LexicalExtension, define_extension};
use crate::{Editor, NodeKey, get_node_by_key, get_selection};
use std::cell::RefCell;
use std::collections::HashSet;
use std::rc::Rc;
/// The keys `watchNodeKey` was asked for, in the order first asked, each
/// with the signals it returned for it (JS's `Map<NodeKey,
/// Set<ReadonlySignal<boolean>>>`).
type WatchedNodeKeys = Rc<RefCell<Vec<(NodeKey, Vec<ReadonlySignal<bool>>)>>>;
/// `{watchedNodeKeys}`, the value of the watched node store. JS stores a
/// new object around the same map when a key is added, so the store's
/// signal notifies; this is the same only as itself.
#[derive(Clone)]
struct WatchedNodeStore {
watched_node_keys: WatchedNodeKeys,
identity: Rc<()>,
}
impl WatchedNodeStore {
fn new(watched_node_keys: WatchedNodeKeys) -> Self {
Self {
watched_node_keys,
identity: Rc::new(()),
}
}
}
impl PartialEq for WatchedNodeStore {
fn eq(&self, other: &Self) -> bool {
Rc::ptr_eq(&self.identity, &other.identity)
}
}
/// `watchNodeKey`.
type WatchNodeKey = Rc<dyn Fn(&str) -> ReadonlySignal<bool>>;
/// The output of `NodeSelectionExtension`.
#[derive(Clone)]
pub struct NodeSelectionOutput {
watch_node_key: WatchNodeKey,
}
impl NodeSelectionOutput {
/// `watchNodeKey(key)`: a signal of whether the node with `key` is
/// selected (`node.isSelected()`), kept up to date from the editor's
/// state while something watches it. Before then, and while there is
/// no selection, it is `false`.
pub fn watch_node_key(&self, key: &str) -> ReadonlySignal<bool> {
(self.watch_node_key)(key)
}
}
/// `NodeSelectionExtension`.
pub type NodeSelectionExtensionType = LexicalExtension<(), NodeSelectionOutput, ()>;
fn make() -> Rc<NodeSelectionExtensionType> {
define_extension(LexicalExtension {
build: Some(Rc::new(
|_: &Editor, _: &(), state: &ExtensionBuildState<()>| {
let editor_state_store = state
.get_dependency(&editor_state_extension())?
.output
.expect("EditorStateExtension has a build");
let watched_node_store =
signal(WatchedNodeStore::new(Rc::new(RefCell::new(Vec::new()))));
let selected_node_keys: Signal<Option<HashSet<NodeKey>>> = {
let watched_node_store = watched_node_store.clone();
watched_signal(
|| None,
move |selected_node_keys| {
let selected_node_keys = selected_node_keys.clone();
let editor_state_store = editor_state_store.clone();
let watched_node_store = watched_node_store.clone();
effect(move || {
let prev_selected_node_keys: Option<HashSet<NodeKey>> =
selected_node_keys.peek();
let watched_node_keys =
watched_node_store.value().watched_node_keys;
let mut next_selected_node_keys: Option<HashSet<NodeKey>> = None;
let mut did_change = false;
editor_state_store.value().read(|| {
if get_selection().is_none() {
return;
}
// We intentionally mutate this without
// firing a signal, to avoid re-triggering
// this effect. There are no subscribers so
// nothing can observe whether key was in
// the set or not.
watched_node_keys
.borrow_mut()
.retain(|(_, listeners)| !listeners.is_empty());
let keys: Vec<NodeKey> = watched_node_keys
.borrow()
.iter()
.map(|(key, _)| key.clone())
.collect();
for key in keys {
let is_selected =
get_node_by_key(&key).is_some_and(|node| {
matches!(node.is_selected(None), Ok(true))
});
did_change = did_change
|| is_selected
!= prev_selected_node_keys
.as_ref()
.is_some_and(|prev| prev.contains(&key));
if is_selected {
next_selected_node_keys
.get_or_insert_with(HashSet::new)
.insert(key);
}
}
});
let same = !did_change
&& match (&next_selected_node_keys, &prev_selected_node_keys) {
(Some(next), Some(prev)) => next.len() == prev.len(),
_ => false,
};
if !same {
selected_node_keys.set_value(next_selected_node_keys);
}
None
})
},
)
};
let watch_node_key = move |key: &str| -> ReadonlySignal<bool> {
let watcher = {
let selected_node_keys = selected_node_keys.clone();
let key = key.to_owned();
computed(move || {
selected_node_keys
.value()
.is_some_and(|keys| keys.contains(&key))
})
};
let watched_node_keys = watched_node_store.peek().watched_node_keys;
let had_listener = {
let mut entries = watched_node_keys.borrow_mut();
match entries.iter_mut().find(|(k, _)| k == key) {
Some((_, listeners)) => {
listeners.push(watcher.clone());
true
}
None => {
entries.push((key.to_owned(), vec![watcher.clone()]));
false
}
}
};
if !had_listener {
watched_node_store.set_value(WatchedNodeStore::new(watched_node_keys));
}
watcher
};
Ok(NodeSelectionOutput {
watch_node_key: Rc::new(watch_node_key),
})
},
)),
dependencies: vec![editor_state_extension().into()],
..LexicalExtension::new("@lexical/extension/NodeSelection", ())
})
}
thread_local! {
static NODE_SELECTION_EXTENSION: Rc<NodeSelectionExtensionType> = make();
}
/// `NodeSelectionExtension`: an output whose `watchNodeKey` returns a
/// signal of whether a node is selected, typically a decorator's (a
/// framework independent alternative to `useLexicalNodeSelection`).
pub fn node_selection_extension() -> Rc<NodeSelectionExtensionType> {
NODE_SELECTION_EXTENSION.with(Rc::clone)
}