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
//! Ports of `packages/lexical-extension/src/__tests__/unit/SelectBlockExtension.test.ts`.
//!
//! JS mounts the editors on a `contenteditable` div; these have a
//! [`set_test_root_element`] root element.

#![cfg(feature = "rich-text")]

use super::super::support::{
    TestBlockableDecoratorNode, create_test_blockable_decorator_node, set_is_inline,
    set_test_root_element, update, update_with,
};
use super::support::paragraph;
use crate::extension::{
    GetParentEditor, LexicalEditorWithDispose, NestedEditorConfigOverrides,
    SelectBlockConfigOverrides, build_editor_from_extensions, get_extension_dependency_from_editor,
    nested_editor_extension, prevent_select_all_extension, select_block_extension,
};
use crate::{
    Editor, InitialEditorStateType, KeyboardEvent, LexicalError, LexicalExtension, NodeClass,
    NodeRef, PointType, SELECT_ALL_COMMAND, Which, config_extension, create_node_selection,
    create_paragraph_node, define_extension, get_root, get_selection, is_block_fully_selected,
    set_selection,
};
use std::rc::Rc;

/// `setUpEditor({cascadeSelection, disabled})`.
fn set_up_editor(cascade_selection: bool, disabled: bool) -> LexicalEditorWithDispose {
    let editor = build_editor_from_extensions([define_extension(LexicalExtension {
        initial_editor_state: Some(InitialEditorStateType::Update(Rc::new(|_| {
            get_root().append(&[paragraph("first"), paragraph("second")])?;
            Ok(())
        }))),
        dependencies: vec![config_extension(
            &select_block_extension(),
            [SelectBlockConfigOverrides {
                cascade_selection: Some(cascade_selection),
                disabled: Some(disabled),
            }],
        )],
        nodes: vec![NodeClass::of::<TestBlockableDecoratorNode>()],
        ..LexicalExtension::<(), (), ()>::new("select-block-test", ())
    })
    .into()])
    .unwrap();
    set_test_root_element(&editor);
    editor
}

fn default_editor() -> LexicalEditorWithDispose {
    set_up_editor(false, false)
}

/// `selectAllKeyboardEvent()`.
fn select_all_event() -> Rc<KeyboardEvent> {
    Rc::new(KeyboardEvent {
        key: "a".to_owned(),
        ctrl_key: true,
        ..KeyboardEvent::default()
    })
}

fn select_all(editor: &Editor) -> bool {
    editor
        .dispatch_command(&SELECT_ALL_COMMAND, select_all_event())
        .unwrap()
}

/// `$secondParagraph()`.
fn second_paragraph() -> NodeRef {
    let node = get_root().get_child_at_index(1).expect("second paragraph");
    assert!(node.is_element());
    node
}

/// Whether the selection is a range that fully selects `block`.
fn fully_selected(block: &NodeRef) -> bool {
    let range = get_selection()
        .and_then(|s| s.range())
        .expect("expecting a RangeSelection");
    is_block_fully_selected(block, &range).unwrap()
}

/// `$expectFullySelected($getRoot())`, read.
fn expect_root_fully_selected(editor: &Editor) {
    assert!(editor.read(|| fully_selected(&get_root())).unwrap());
}

/// A caret 3 characters into the second paragraph.
fn caret_in_second(editor: &Editor) {
    update(editor, || {
        let text = second_paragraph().get_first_descendant().unwrap();
        assert!(text.is_text());
        text.select(Some(3), Some(3))?;
        Ok(())
    });
}

fn points(editor: &Editor) -> (String, usize, String, usize) {
    editor
        .read(|| {
            let range = get_selection().and_then(|s| s.range()).unwrap();
            let (a, f) = (range.point(Which::Anchor), range.point(Which::Focus));
            (a.key.clone(), a.offset, f.key.clone(), f.offset)
        })
        .unwrap()
}

// upstream: lexical-extension/src/__tests__/unit/SelectBlockExtension.test.ts::SelectBlockExtension > selects the block first and then the whole document
#[test]
fn selects_the_block_then_the_document() {
    let editor = default_editor();
    caret_in_second(&editor);
    assert!(select_all(&editor));
    editor
        .read(|| {
            assert!(fully_selected(&second_paragraph()));
            assert!(!fully_selected(&get_root()));
        })
        .unwrap();
    assert!(select_all(&editor));
    expect_root_fully_selected(&editor);
    editor.dispose();
}

// upstream: lexical-extension/src/__tests__/unit/SelectBlockExtension.test.ts::SelectBlockExtension > repeated select all does not change the selection
#[test]
fn repeated_select_all_does_not_change_the_selection() {
    let editor = default_editor();
    caret_in_second(&editor);
    select_all(&editor);
    select_all(&editor);
    let before = points(&editor);
    select_all(&editor);
    assert_eq!(points(&editor), before);
    expect_root_fully_selected(&editor);
    editor.dispose();
}

// upstream: lexical-extension/src/__tests__/unit/SelectBlockExtension.test.ts::SelectBlockExtension > a selection spanning multiple blocks expands to the whole document
#[test]
fn a_selection_spanning_blocks_expands_to_the_document() {
    let editor = default_editor();
    update(&editor, || {
        let first = get_root().get_first_descendant().unwrap();
        let second = second_paragraph().get_first_descendant().unwrap();
        let selection = first.select(Some(2), Some(2))?;
        selection.point_set(Which::Focus, second.get_key(), 3, PointType::Text)?;
        Ok(())
    });
    assert!(select_all(&editor));
    expect_root_fully_selected(&editor);
    editor.dispose();
}

// upstream: lexical-extension/src/__tests__/unit/SelectBlockExtension.test.ts::SelectBlockExtension > a selection anchored on the root still selects all
#[test]
fn a_selection_on_the_root_selects_all() {
    let editor = default_editor();
    update(&editor, || {
        // A partial selection with points directly on the root.
        get_root().select(Some(0), Some(1))?;
        Ok(())
    });
    assert!(select_all(&editor));
    expect_root_fully_selected(&editor);
    editor.dispose();
}

// upstream: lexical-extension/src/__tests__/unit/SelectBlockExtension.test.ts::SelectBlockExtension > select all works after the document changes while fully selected
#[test]
fn select_all_after_the_document_changes() {
    let editor = default_editor();
    caret_in_second(&editor);
    select_all(&editor);
    select_all(&editor);
    // A remote or programmatic change that does not touch the selection.
    update(&editor, || {
        get_root().append(&[paragraph("third")])?;
        Ok(())
    });
    assert!(select_all(&editor));
    expect_root_fully_selected(&editor);
    editor.dispose();
}

// upstream: lexical-extension/src/__tests__/unit/SelectBlockExtension.test.ts::SelectBlockExtension > select all preserves a manually created full selection
#[test]
fn select_all_preserves_a_full_selection() {
    let editor = default_editor();
    update(&editor, || {
        let first = get_root().get_first_descendant().unwrap();
        let last = get_root().get_last_descendant().unwrap();
        // A backward selection of the whole document.
        let selection = last.select(Some(6), Some(6))?;
        selection.point_set(Which::Focus, first.get_key(), 0, PointType::Text)?;
        Ok(())
    });
    assert!(select_all(&editor));
    editor
        .read(|| {
            let selection = get_selection().unwrap();
            // Not rewritten by $selectAll.
            assert!(selection.is_backward().unwrap());
            assert!(fully_selected(&get_root()));
        })
        .unwrap();
    editor.dispose();
}

// upstream: lexical-extension/src/__tests__/unit/SelectBlockExtension.test.ts::SelectBlockExtension > select all in a single-block document
#[test]
fn select_all_in_a_single_block_document() {
    let editor = default_editor();
    update(&editor, || {
        let root = get_root();
        root.clear()?;
        root.append(&[paragraph("only")])?;
        root.get_first_descendant()
            .unwrap()
            .select(Some(2), Some(2))?;
        Ok(())
    });
    assert!(select_all(&editor));
    expect_root_fully_selected(&editor);
    assert!(select_all(&editor));
    expect_root_fully_selected(&editor);
    editor.dispose();
}

fn select_node(node: &NodeRef) {
    let selection = create_node_selection();
    selection.add(node.get_key());
    set_selection(Some(selection));
}

// upstream: lexical-extension/src/__tests__/unit/SelectBlockExtension.test.ts::SelectBlockExtension > a node selection of an inline decorator selects its block
#[test]
fn a_node_selection_of_an_inline_decorator_selects_its_block() {
    let editor = default_editor();
    update(&editor, || {
        let decorator = create_test_blockable_decorator_node();
        second_paragraph().append(std::slice::from_ref(&decorator))?;
        select_node(&decorator);
        Ok(())
    });
    assert!(select_all(&editor));
    assert!(editor.read(|| fully_selected(&second_paragraph())).unwrap());
    editor.dispose();
}

// upstream: lexical-extension/src/__tests__/unit/SelectBlockExtension.test.ts::SelectBlockExtension > a node selection of a top-level decorator selects all
#[test]
fn a_node_selection_of_a_top_level_decorator_selects_all() {
    let editor = default_editor();
    update(&editor, || {
        let decorator = set_is_inline(&create_test_blockable_decorator_node(), false);
        get_root().append(std::slice::from_ref(&decorator))?;
        select_node(&decorator);
        Ok(())
    });
    assert!(select_all(&editor));
    expect_root_fully_selected(&editor);
    editor.dispose();
}

// upstream: lexical-extension/src/__tests__/unit/SelectBlockExtension.test.ts::SelectBlockExtension > an empty node selection defers to other handlers
#[test]
fn an_empty_node_selection_defers() {
    let editor = default_editor();
    update(&editor, || {
        set_selection(Some(create_node_selection()));
        Ok(())
    });
    assert!(!select_all(&editor));
    editor.dispose();
}

// upstream: lexical-extension/src/__tests__/unit/SelectBlockExtension.test.ts::SelectBlockExtension > respects disabled config
#[test]
fn respects_disabled_config() {
    let editor = set_up_editor(false, true);
    caret_in_second(&editor);
    assert!(!select_all(&editor));
    editor.dispose();
}

// upstream: lexical-extension/src/__tests__/unit/SelectBlockExtension.test.ts::SelectBlockExtension > enables and disables PreventSelectAllExtension together
#[test]
fn enables_and_disables_prevent_select_all_together() {
    let editor = set_up_editor(false, true);
    let select_block = get_extension_dependency_from_editor(&editor, &select_block_extension())
        .unwrap()
        .output
        .unwrap();
    let prevent = get_extension_dependency_from_editor(&editor, &prevent_select_all_extension())
        .unwrap()
        .output
        .unwrap();
    assert!(prevent.disabled.value());
    select_block.disabled.set_value(false);
    assert!(!prevent.disabled.value());
    select_block.disabled.set_value(true);
    assert!(prevent.disabled.value());
    editor.dispose();
}

/// `setUpNestedEditor(parentEditor, $initialEditorState)`.
fn set_up_nested_editor(
    parent: &Editor,
    init: fn() -> Result<(), LexicalError>,
) -> LexicalEditorWithDispose {
    let parent = parent.clone();
    build_editor_from_extensions([define_extension(LexicalExtension {
        initial_editor_state: Some(InitialEditorStateType::Update(Rc::new(move |_| init()))),
        dependencies: vec![
            crate::rich_text::rich_text_extension().into(),
            config_extension(
                &nested_editor_extension(),
                [NestedEditorConfigOverrides {
                    get_parent_editor: Some(GetParentEditor(Rc::new(move || Ok(parent.clone())))),
                    ..Default::default()
                }],
            ),
        ],
        namespace: Some("nested".to_owned()),
        ..LexicalExtension::<(), (), ()>::new("nested-editor-test", ())
    })
    .into()])
    .unwrap()
}

fn caption() -> Result<(), LexicalError> {
    get_root().append(&[paragraph("caption")])?;
    Ok(())
}

// upstream: lexical-extension/src/__tests__/unit/SelectBlockExtension.test.ts::SelectBlockExtension with nested editors > ignores commands bubbled from a nested editor when cascadeSelection is off
#[test]
fn nested_ignores_bubbled_commands_without_cascade() {
    let editor = default_editor();
    let nested = set_up_nested_editor(&editor, caption);
    // A stale node selection in the parent editor must not hijack the
    // nested editor's select all.
    update(&editor, || {
        let decorator = create_test_blockable_decorator_node();
        second_paragraph().append(std::slice::from_ref(&decorator))?;
        select_node(&decorator);
        Ok(())
    });
    assert!(select_all(&nested));
    expect_root_fully_selected(&nested);
    assert!(
        editor
            .read(|| get_selection().is_some_and(|s| s.is_node()))
            .unwrap()
    );
    nested.dispose();
    editor.dispose();
}

// upstream: lexical-extension/src/__tests__/unit/SelectBlockExtension.test.ts::SelectBlockExtension with nested editors > cascadeSelection ignores the command until the nested editor is fully selected
#[test]
fn nested_cascade_waits_until_the_nested_editor_is_fully_selected() {
    let editor = set_up_editor(true, false);
    let nested = set_up_nested_editor(&editor, caption);
    update(&nested, || {
        get_root()
            .get_first_descendant()
            .unwrap()
            .select(Some(3), Some(3))?;
        Ok(())
    });
    assert!(select_all(&nested));
    expect_root_fully_selected(&nested);
    assert!(
        !editor
            .read(|| get_selection().is_some_and(|s| s.is_range()))
            .unwrap()
    );
    // The next press cascades to the parent editor.
    assert!(select_all(&nested));
    expect_root_fully_selected(&editor);
    nested.dispose();
    editor.dispose();
}

// upstream: lexical-extension/src/__tests__/unit/SelectBlockExtension.test.ts::SelectBlockExtension with nested editors > cascadeSelection works when dispatched from inside a nested editor update
#[test]
fn nested_cascade_from_inside_a_nested_update() {
    let editor = set_up_editor(true, false);
    let nested = set_up_nested_editor(&editor, caption);
    let inner = nested.editor().clone();
    let handled = update_with(&nested, move || {
        crate::select_all()?;
        // Dispatch while this update is still in progress.
        let handled = inner.dispatch_command(&SELECT_ALL_COMMAND, select_all_event())?;
        // The update continues after the dispatch returns.
        get_root().append(&[paragraph("after")])?;
        Ok(handled)
    });
    assert!(handled);
    let text = nested.read(|| get_root().get_text_content()).unwrap();
    assert!(text.contains("caption"));
    assert!(text.contains("after"));
    expect_root_fully_selected(&editor);
    nested.dispose();
    editor.dispose();
}

// upstream: lexical-extension/src/__tests__/unit/SelectBlockExtension.test.ts::SelectBlockExtension with nested editors > cascadeSelection handles the command from an empty nested editor
#[test]
fn nested_cascade_from_an_empty_nested_editor() {
    let editor = set_up_editor(true, false);
    let nested = set_up_nested_editor(&editor, || {
        let p = create_paragraph_node();
        get_root().append(std::slice::from_ref(&p))?;
        p.select_start()?;
        Ok(())
    });
    // An empty nested editor is fully selected by its caret.
    assert!(select_all(&nested));
    expect_root_fully_selected(&editor);
    nested.dispose();
    editor.dispose();
}