lexical-ui 0.1.2

A Rust port of Lexical's model and headless packages, at parity with the JS implementation.
Documentation
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//! `@lexical/react`'s auto-embed without React (the `auto-embed` feature):
//! `LexicalAutoEmbedPluginUtils.ts` ([`EmbedMatchResult`], [`EmbedConfig`],
//! [`URL_MATCHER`](url_matcher), [`INSERT_EMBED_COMMAND`],
//! [`AutoEmbedOption`]) and the logic of `LexicalAutoEmbedPlugin.tsx`
//! ([`register_auto_embed`]): a pasted link that an embed config recognizes
//! (a video, a post) opens a node menu at it that offers to replace it with
//! the embed. React keeps the match in component state and renders
//! `LexicalNodeMenuPlugin`; here [`register_auto_embed`] keeps it and
//! drives a [`NodeMenu`], which the app's view draws.
//!
//! `parseUrl` may return a promise in JS, which the plugin awaits; here an
//! [`EmbedConfig`] answers synchronously ([`EmbedConfig::parse_url`]) or
//! later, through the [`ParseUrlDone`] that
//! [`EmbedConfig::parse_url_async`] is given. A synchronous answer opens
//! the menu in the paste's mutation listener, where JS's
//! `await Promise.resolve(result)` opens it a microtask later
//! (rust/PARITY.md).

use crate::lexical_selection::get_selection;
use crate::link::is_link_node;
use crate::menu::{MenuOption, MenuSelectOption, NodeMenu};
use crate::merge_register::merge_register;
use crate::updates::on_update;
use crate::{
    COMMAND_PRIORITY_BEFORE_EDITOR, COMMAND_PRIORITY_EDITOR, Editor, EditorReadMode,
    LexicalCommand, LexicalError, MutationListenerOptions, NodeKey, NodeMutation, NodeRef,
    PASTE_COMMAND, PASTE_TAG, PasteCommandType, Unregister, UpdateOptions, get_node_by_key,
};
use std::any::Any;
use std::cell::{Cell, RefCell};
use std::rc::Rc;

/// `EmbedMatchResult`: the matched `url`, the `id` of the embedded
/// resource, and optional provider `data`.
#[derive(Clone)]
pub struct EmbedMatchResult {
    pub url: String,
    pub id: String,
    pub data: Option<Rc<dyn Any>>,
}

/// `EmbedConfig`: a kind of embed, with its `type`, `parseUrl`, which
/// decides whether a URL is one and extracts its data, and `insertNode`,
/// which inserts its node. A config implements `parse_url`, or
/// `parse_url_async` when it answers later (JS's `parseUrl` returning a
/// promise).
pub trait EmbedConfig {
    /// `type`.
    fn embed_type(&self) -> &str;
    /// `parseUrl(text)`, answered now. `None` unless implemented.
    fn parse_url(&self, _text: &str) -> Option<EmbedMatchResult> {
        None
    }
    /// `parseUrl(text)` returning a promise: the config resolves `done`
    /// with the result when it has one, now or later; dropping it is a
    /// rejected promise, which ends the check. By default, resolves it
    /// with [`parse_url`](Self::parse_url).
    fn parse_url_async(&self, text: &str, done: ParseUrlDone) {
        done.resolve(self.parse_url(text));
    }
    /// `insertNode(editor, result)`, in an update.
    fn insert_node(&self, editor: &Editor, result: &EmbedMatchResult) -> Result<(), LexicalError>;
}

/// What settles a [`EmbedConfig::parse_url_async`], as the promise JS's
/// `parseUrl` returns does: [`resolve`](Self::resolve) it with the result,
/// or drop it to reject. Only the latest paste's check opens the menu, so
/// a parse that resolves after a newer paste is ignored, as is one whose
/// link was removed or given another URL meanwhile.
pub struct ParseUrlDone(Option<Box<dyn FnOnce(Option<EmbedMatchResult>)>>);

impl ParseUrlDone {
    /// The promise resolved with `result`.
    pub fn resolve(mut self, result: Option<EmbedMatchResult>) {
        if let Some(resolve) = self.0.take() {
            resolve(result);
        }
    }
}

/// `INSERT_EMBED_COMMAND`: starts inserting the embed whose config has the
/// payload as its `type`.
pub const INSERT_EMBED_COMMAND: LexicalCommand<String> =
    LexicalCommand::constant(u64::MAX - 30, "INSERT_EMBED_COMMAND");

/// `AutoEmbedOption`: an item of the embed menu, its key its title, with
/// what choosing it does.
pub struct AutoEmbedOption {
    pub title: String,
    /// `onSelect(targetNode)`.
    pub on_select: Rc<dyn Fn(Option<NodeRef>) -> Result<(), LexicalError>>,
}

impl MenuOption for AutoEmbedOption {
    fn key(&self) -> &str {
        &self.title
    }
    fn title(&self) -> Option<&str> {
        Some(&self.title)
    }
}

fn is_word(unit: u16) -> bool {
    matches!(unit, 0x30..=0x39 | 0x41..=0x5A | 0x61..=0x7A | 0x5F)
}

/// `[-a-zA-Z0-9@:%._+~#=]`.
fn is_host_unit(unit: u16) -> bool {
    is_word(unit) && unit != 0x5F || b"-@:%._+~#=".iter().any(|&c| u16::from(c) == unit)
}

/// `[a-zA-Z0-9()]`.
fn is_tld_unit(unit: u16) -> bool {
    is_word(unit) && unit != 0x5F || unit == u16::from(b'(') || unit == u16::from(b')')
}

/// `[-a-zA-Z0-9()@:%_+.~#?&//=]`.
fn is_path_unit(unit: u16) -> bool {
    is_word(unit) || b"-()@:%+.~#?&/=".iter().any(|&c| u16::from(c) == unit)
}

/// The match of `URL_MATCHER` at `start`, its end, as `exec` finds it.
fn match_url_at(units: &[u16], start: usize) -> Option<usize> {
    let has = |at: usize, text: &str| {
        text.encode_utf16()
            .enumerate()
            .all(|(i, u)| units.get(at + i) == Some(&u))
    };
    // `((https?:\/\/(www\.)?)|(www\.))`, each alternative's ends in the order
    // the backtracking tries them.
    let mut prefixes = Vec::new();
    if has(start, "http") {
        let scheme = if has(start + 4, "s") {
            start + 5
        } else {
            start + 4
        };
        if has(scheme, "://") {
            let after = scheme + 3;
            if has(after, "www.") {
                prefixes.push(after + 4);
            }
            prefixes.push(after);
        }
    }
    if has(start, "www.") {
        prefixes.push(start + 4);
    }
    prefixes.into_iter().find_map(|prefix| {
        // `[-a-zA-Z0-9@:%._+~#=]{1,256}`, longest first.
        let host_max = units[prefix..]
            .iter()
            .take(256)
            .take_while(|&&u| is_host_unit(u))
            .count();
        (1..=host_max).rev().find_map(|host| {
            let dot = prefix + host;
            if units.get(dot) != Some(&u16::from(b'.')) {
                return None;
            }
            // `\.[a-zA-Z0-9()]{1,6}\b`, longest first.
            let tld_max = units[dot + 1..]
                .iter()
                .take(6)
                .take_while(|&&u| is_tld_unit(u))
                .count();
            (1..=tld_max).rev().find_map(|tld| {
                let end = dot + 1 + tld;
                let before = is_word(units[end - 1]);
                let after = units.get(end).is_some_and(|&u| is_word(u));
                (before != after).then(|| {
                    // `([-a-zA-Z0-9()@:%_+.~#?&//=]*)`.
                    end + units[end..]
                        .iter()
                        .take_while(|&&u| is_path_unit(u))
                        .count()
                })
            })
        })
    })
}

/// `URL_MATCHER.exec(text)`: the UTF-16 index and the text of the first
/// match of
/// `((https?:\/\/(www\.)?)|(www\.))[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&//=]*)`,
/// a general-purpose URL expression for a `parseUrl`, matched by hand as JS
/// matches it.
pub fn url_matcher(text: &str) -> Option<(usize, String)> {
    let units: Vec<u16> = text.encode_utf16().collect();
    (0..units.len()).find_map(|start| {
        match_url_at(&units, start).map(|end| (start, String::from_utf16_lossy(&units[start..end])))
    })
}

/// `/^\S+$/.test(text)`: whether a paste is one token, which JS's `\s`
/// (Unicode's white space, less U+0085 and U+180E, and U+FEFF) does not
/// split.
pub fn is_single_token(text: &str) -> bool {
    !text.is_empty()
        && !text
            .chars()
            .any(crate::selection_deletion::is_js_whitespace)
}

/// `getMenuOptions(activeEmbedConfig, embedFn, dismissFn)`.
pub type GetMenuOptions =
    Rc<dyn Fn(&Rc<dyn EmbedConfig>, Rc<dyn Fn()>, Rc<dyn Fn()>) -> Vec<Rc<AutoEmbedOption>>>;

/// `onOpenEmbedModalForConfig(embedConfig)`.
pub type OpenEmbedModalForConfig = Rc<dyn Fn(&Rc<dyn EmbedConfig>)>;

/// `$disableMenuFor(node)`.
pub type DisableMenuFor = Rc<dyn Fn(&NodeRef) -> bool>;

/// `registerAutoEmbed`'s arguments: `LexicalAutoEmbedPlugin`'s props
/// besides the view's.
pub struct AutoEmbedConfig {
    /// `embedConfigs`: when several match a URL, the last wins.
    pub embed_configs: Vec<Rc<dyn EmbedConfig>>,
    /// `getMenuOptions(activeEmbedConfig, embedFn, dismissFn)`: the menu's
    /// options for a match; `embedFn` replaces the link with the embed,
    /// `dismissFn` closes the menu.
    pub get_menu_options: GetMenuOptions,
    /// `onOpenEmbedModalForConfig`: handles `INSERT_EMBED_COMMAND` for a
    /// config's type.
    pub on_open_embed_modal_for_config: Option<OpenEmbedModalForConfig>,
    /// `$disableMenuFor`: no menu for a link where it returns true.
    pub disable_menu_for: Option<DisableMenuFor>,
    /// `menuCommandPriority`: the priority of the menu's keyboard commands
    /// (`COMMAND_PRIORITY_LOW` in JS's default).
    pub menu_command_priority: i8,
}

/// `EmbedMatch`.
#[derive(Clone)]
struct EmbedMatch {
    embed_config: Rc<dyn EmbedConfig>,
    key: NodeKey,
    result: EmbedMatchResult,
    url: String,
}

/// The plugin's state: its config, the current match, and the menu.
struct AutoEmbedState {
    editor: Editor,
    config: AutoEmbedConfig,
    menu: NodeMenu<AutoEmbedOption>,
    embed_match: RefCell<Option<EmbedMatch>>,
    is_single_token_paste: Cell<bool>,
    latest_check: Cell<u64>,
}

/// `$getCurrentLink(key, url)`: the link `key`, while it is attached and
/// its URL is still `url`.
fn get_current_link(key: &str, url: &str) -> Option<NodeRef> {
    get_node_by_key(key).filter(|link| {
        is_link_node(link) && link.is_attached() && link.get_url().as_deref() == Some(url)
    })
}

impl AutoEmbedState {
    /// `$isMenuDisabledForNode(linkNode, $disableMenuFor)`.
    fn is_menu_disabled_for_node(&self, link: &NodeRef) -> bool {
        self.config
            .disable_menu_for
            .as_ref()
            .is_some_and(|f| f(link))
    }

    /// `reset()`: `setEmbedMatch(null)`.
    fn reset(self: &Rc<Self>) {
        if self.embed_match.borrow_mut().take().is_some() {
            self.render();
        }
    }

    /// What the component renders for the match: a node menu at its link
    /// with `getMenuOptions`' options, or nothing.
    fn render(self: &Rc<Self>) {
        let embed_match = self.embed_match.borrow().clone();
        match embed_match {
            None => {
                self.menu.set_node_key(None);
                self.menu.menu().set_options(Vec::new());
            }
            Some(embed_match) => {
                let (embed, dismiss) = (Rc::downgrade(self), Rc::downgrade(self));
                let options = (self.config.get_menu_options)(
                    &embed_match.embed_config,
                    Rc::new(move || {
                        if let Some(state) = embed.upgrade() {
                            state.embed_link_via_active_embed_config();
                        }
                    }),
                    Rc::new(move || {
                        if let Some(state) = dismiss.upgrade() {
                            state.reset();
                        }
                    }),
                );
                self.menu.set_node_key(Some(embed_match.key));
                self.menu.menu().set_options(options);
            }
        }
    }

    /// `checkIfLinkNodeIsEmbeddable(key)`.
    fn check_if_link_node_is_embeddable(self: &Rc<Self>, key: &str) {
        let check = self.latest_check.get() + 1;
        self.latest_check.set(check);
        let url = self.editor.get_editor_state().read(|| {
            let link = get_node_by_key(key).filter(is_link_node)?;
            if self.is_menu_disabled_for_node(&link) {
                return None;
            }
            link.get_url()
        });
        let Some(url) = url else { return };
        // The last config that matches wins, so they are tried from the end.
        self.try_embed_config(self.config.embed_configs.len(), check, key.to_owned(), url);
    }

    /// The loop of `checkIfLinkNodeIsEmbeddable` from the config before
    /// `index`, each `parseUrl` awaited before the next.
    fn try_embed_config(self: &Rc<Self>, index: usize, check: u64, key: NodeKey, url: String) {
        let Some(index) = index.checked_sub(1) else {
            return;
        };
        let embed_config = self.config.embed_configs[index].clone();
        let state = Rc::downgrade(self);
        let (parsed_config, parsed_url) = (embed_config.clone(), url.clone());
        embed_config.parse_url_async(
            &url,
            ParseUrlDone(Some(Box::new(move |result| {
                let Some(state) = state.upgrade() else { return };
                if check != state.latest_check.get() {
                    return;
                }
                let Some(result) = result else {
                    state.try_embed_config(index, check, key, parsed_url);
                    return;
                };
                // Edits made while parseUrl was pending may not be committed
                // yet, and the link may have moved into a disabled location.
                let embeddable = state
                    .editor
                    .read_with_mode(EditorReadMode::Pending, || {
                        get_current_link(&key, &parsed_url)
                            .is_some_and(|link| !state.is_menu_disabled_for_node(&link))
                    })
                    .unwrap_or(false);
                if embeddable {
                    *state.embed_match.borrow_mut() = Some(EmbedMatch {
                        embed_config: parsed_config,
                        key,
                        result,
                        url: parsed_url,
                    });
                    state.render();
                }
            }))),
        );
    }

    /// `embedLinkViaActiveEmbedConfig()`: in an update, the config inserts
    /// its node, then the link is removed (unless it was removed or its URL
    /// changed meanwhile).
    fn embed_link_via_active_embed_config(&self) {
        let Some(EmbedMatch {
            embed_config,
            key,
            result,
            url,
        }) = self.embed_match.borrow().clone()
        else {
            return;
        };
        let editor = self.editor.clone();
        let outcome = self.editor.update(
            move || {
                let Some(link) = get_current_link(&key, &url) else {
                    return Ok(());
                };
                if get_selection().is_none() {
                    link.select_end()?;
                }
                embed_config.insert_node(&editor, &result)?;
                if link.is_attached() {
                    link.remove(false)?;
                }
                Ok(())
            },
            UpdateOptions::default(),
        );
        // JS's `editor.update` reports an error to `onError`, which throws.
        let _ = outcome;
    }
}

/// `LexicalAutoEmbedPlugin`'s logic: watches the links (`LinkNode`,
/// `AutoLinkNode`) that a paste of one token creates, and when one of
/// `embed_configs` parses the URL, opens the returned menu at the link with
/// `get_menu_options`' options; choosing one runs its `on_select` with the
/// link. The match is cleared when the link changes again or the menu
/// closes. `on_open_embed_modal_for_config` handles `INSERT_EMBED_COMMAND`.
/// Returns the menu and what unregisters it; fails when the editor doesn't
/// register the link nodes.
pub fn register_auto_embed(
    editor: &Editor,
    config: AutoEmbedConfig,
) -> Result<(NodeMenu<AutoEmbedOption>, Unregister), LexicalError> {
    let on_select_option: MenuSelectOption<AutoEmbedOption> =
        Rc::new(|option, target_node, close_menu, _| {
            (option.on_select)(target_node)?;
            close_menu();
            Ok(())
        });
    let menu_command_priority = config.menu_command_priority;
    let state = Rc::new(AutoEmbedState {
        editor: editor.clone(),
        config,
        menu: NodeMenu::new(editor, on_select_option, menu_command_priority),
        embed_match: RefCell::new(None),
        is_single_token_paste: Cell::new(false),
        latest_check: Cell::new(0),
    });
    // `onClose={reset}`.
    let closing = Rc::downgrade(&state);
    *state.menu.menu().callbacks().on_close.borrow_mut() = Some(Rc::new(move || {
        if let Some(state) = closing.upgrade() {
            state.reset();
        }
    }));
    let pasting = Rc::downgrade(&state);
    let mut unregisters = vec![editor.register_command(
        &PASTE_COMMAND,
        move |payload, _| {
            let Some(state) = pasting.upgrade() else {
                return Ok(false);
            };
            let is_single_token_paste = match payload {
                PasteCommandType::Clipboard(event) => event
                    .clipboard_data
                    .as_ref()
                    .is_some_and(|data| is_single_token(&data.get_data("text/plain"))),
                PasteCommandType::Input(_) | PasteCommandType::Keyboard(_) => false,
            };
            state.is_single_token_paste.set(is_single_token_paste);
            if is_single_token_paste {
                let state = Rc::downgrade(&state);
                on_update(move || {
                    if let Some(state) = state.upgrade() {
                        state.is_single_token_paste.set(false);
                    }
                });
            }
            Ok(false)
        },
        COMMAND_PRIORITY_BEFORE_EDITOR,
    )];
    for node_type in ["link", "autolink"] {
        let listening = Rc::downgrade(&state);
        unregisters.push(editor.register_mutation_listener(
            node_type,
            move |mutations, payload| {
                let Some(state) = listening.upgrade() else {
                    return;
                };
                for (key, mutation) in mutations {
                    let node_key = state.embed_match.borrow().as_ref().map(|m| m.key.clone());
                    if *mutation == NodeMutation::Created
                        && payload.update_tags.iter().any(|t| t == PASTE_TAG)
                        && state.is_single_token_paste.get()
                    {
                        state.check_if_link_node_is_embeddable(key);
                    } else if node_key.as_ref() == Some(key) {
                        state.reset();
                    }
                }
            },
            MutationListenerOptions {
                skip_initialization: true,
            },
        )?);
    }
    if state.config.on_open_embed_modal_for_config.is_some() {
        let opening = Rc::downgrade(&state);
        unregisters.push(editor.register_command(
            &INSERT_EMBED_COMMAND,
            move |embed_config_type, _| {
                let Some(state) = opening.upgrade() else {
                    return Ok(false);
                };
                let Some(embed_config) = state
                    .config
                    .embed_configs
                    .iter()
                    .find(|c| c.embed_type() == embed_config_type)
                else {
                    return Ok(false);
                };
                if let Some(open) = &state.config.on_open_embed_modal_for_config {
                    open(embed_config);
                }
                Ok(true)
            },
            COMMAND_PRIORITY_EDITOR,
        ));
    }
    let menu = state.menu.clone();
    let unregister = merge_register(unregisters);
    Ok((
        menu,
        Box::new(move || {
            unregister();
            state.reset();
        }),
    ))
}