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
//! Ports of `packages/lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts`.

use crate::constants::{IS_BOLD, IS_ITALIC};
use crate::{
    CreateEditorArgs, LexicalError, NodeRef, PointType, SelectionRef, UpdateOptions, Which,
    create_editor, create_paragraph_node, create_range_selection, create_text_node, get_root,
    get_selection, set_selection,
};

/// `buildEditorFromExtensions({name})`, then a discrete update.
fn run(name: &str, f: impl FnOnce() -> Result<(), LexicalError> + 'static) {
    let editor = create_editor(CreateEditorArgs {
        namespace: name.into(),
        ..CreateEditorArgs::default()
    });
    editor
        .update(
            f,
            UpdateOptions {
                discrete: true,
                ..UpdateOptions::default()
            },
        )
        .unwrap();
}

/// `$createTextNode(text).setFormat(format)`.
fn text_with_format(text: &str, format: &str) -> NodeRef {
    let node = create_text_node(text);
    node.set_format_type(format);
    node
}

/// `$createParagraphNode().append(...children)`.
fn paragraph(children: &[NodeRef]) -> Result<NodeRef, LexicalError> {
    let paragraph = create_paragraph_node();
    paragraph.append(children)?;
    Ok(paragraph)
}

/// `$getRoot().clear().append(...children)`.
fn set_root(children: &[NodeRef]) -> Result<(), LexicalError> {
    get_root().clear()?;
    get_root().append(children)
}

/// The `[start, end]` points: `[focus, anchor]` when backward.
fn ends(backward: bool) -> (Which, Which) {
    if backward {
        (Which::Focus, Which::Anchor)
    } else {
        (Which::Anchor, Which::Focus)
    }
}

fn point(selection: &SelectionRef, which: Which) -> crate::Point {
    selection.point(which).expect("a range selection")
}

/// `expect($getSelection()).toBe(selection)`.
fn assert_active(selection: &SelectionRef) {
    assert!(get_selection().is_some_and(|s| s.ptr_eq(selection)));
}

// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > format of a detached text range repairs the active element point
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > extract of a detached text range repairs the active element point
#[test]
fn detached_text_range_repairs_the_active_element_point() {
    for operation in ["format", "extract"] {
        run("slice-active-element", move || {
            let text = create_text_node("abcde");
            let following = text_with_format("after", "italic");
            let paragraph = paragraph(&[text.clone(), following.clone()])?;
            set_root(std::slice::from_ref(&paragraph))?;
            let active = paragraph.select(Some(2), Some(2))?;
            let selection = create_range_selection();
            selection.point_set(Which::Anchor, text.get_key(), 1, PointType::Text)?;
            selection.point_set(Which::Focus, text.get_key(), 4, PointType::Text)?;

            if operation == "format" {
                selection.format_text("bold", None)?;
            } else {
                selection.extract()?;
            }

            assert_eq!(selection.get_text_content()?, "bcd", "{operation}");
            assert_active(&active);
            assert!(active.range().unwrap().is_collapsed());
            let anchor = point(&active, Which::Anchor);
            assert_eq!(anchor.key, following.get_key(), "{operation}");
            assert_eq!(anchor.point_type, PointType::Text);
            assert_eq!(anchor.offset, 5);
            Ok(())
        });
    }
}

// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > styling retains the selected prefix at an element start (backward=false)
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > styling retains the selected prefix at an element start (backward=true)
#[cfg(feature = "selection")]
#[test]
fn styling_retains_the_selected_prefix_at_an_element_start() {
    use crate::selection::{StylePatchValue, patch_style_text};
    for backward in [false, true] {
        run("slice-style", move || {
            let text = create_text_node("abc");
            let paragraph = paragraph(std::slice::from_ref(&text))?;
            set_root(std::slice::from_ref(&paragraph))?;
            let selection = create_range_selection();
            let (start, end) = ends(backward);
            selection.point_set(start, paragraph.get_key(), 0, PointType::Element)?;
            selection.point_set(end, text.get_key(), 1, PointType::Text)?;
            set_selection(Some(selection.clone()));

            patch_style_text(&selection, &[("color", StylePatchValue::from("red"))])?;
            assert_eq!(selection.get_text_content()?, "a", "backward={backward}");
            patch_style_text(&selection, &[("color", StylePatchValue::from("blue"))])?;
            assert_eq!(
                paragraph.get_first_child_or_throw()?.get_text_content(),
                "a"
            );
            assert_eq!(
                point(&selection, Which::Anchor)
                    .get_node()?
                    .text_data()
                    .style,
                "color: blue;"
            );
            assert_eq!(
                paragraph.get_last_child_or_throw()?.get_text_content(),
                "bc"
            );
            Ok(())
        });
    }
}

// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > format retains detached element endpoints after a split
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > extract retains detached element endpoints after a split
#[test]
fn retains_detached_element_endpoints_after_a_split() {
    for operation in ["format", "extract"] {
        run("slice-detached-end", move || {
            let first = create_text_node("abc");
            let second = text_with_format("def", "italic");
            let paragraph = paragraph(&[first.clone(), second])?;
            let other = create_text_node("other");
            set_root(&[
                paragraph.clone(),
                self::paragraph(std::slice::from_ref(&other))?,
            ])?;
            let active = other.select(Some(2), Some(2))?;
            let selection = create_range_selection();
            selection.point_set(Which::Anchor, first.get_key(), 1, PointType::Text)?;
            selection.point_set(Which::Focus, paragraph.get_key(), 2, PointType::Element)?;

            if operation == "format" {
                selection.format_text("bold", None)?;
            } else {
                selection.extract()?;
            }
            assert_eq!(selection.get_text_content()?, "bcdef", "{operation}");
            let focus = point(&selection, Which::Focus);
            let focus_node = focus.get_node()?;
            let limit = if focus.point_type == PointType::Element {
                focus_node.get_children_size()
            } else {
                focus_node.get_text_content_size()
            };
            assert!(focus.offset <= limit, "{operation}");
            assert_active(&active);
            let anchor = point(&active, Which::Anchor);
            assert_eq!(anchor.key, other.get_key());
            assert_eq!(anchor.offset, 2);
            Ok(())
        });
    }
}

// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > extracts through an element endpoint between children (backward=false)
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > extracts through an element endpoint between children (backward=true)
#[test]
fn extracts_through_an_element_endpoint_between_children() {
    for backward in [false, true] {
        run("selection-text-slices", move || {
            let first = create_text_node("abcd");
            let second = text_with_format("efgh", "italic");
            let third = text_with_format("ijkl", "bold");
            let paragraph = paragraph(&[first.clone(), second, third])?;
            set_root(std::slice::from_ref(&paragraph))?;
            let selection = create_range_selection();
            let (start, end) = ends(backward);
            selection.point_set(start, first.get_key(), 1, PointType::Text)?;
            selection.point_set(end, paragraph.get_key(), 2, PointType::Element)?;
            set_selection(Some(selection.clone()));

            let extracted: Vec<String> = selection
                .extract()?
                .iter()
                .filter(|n| n.is_text())
                .map(NodeRef::get_text_content)
                .collect();
            assert_eq!(extracted, ["bcd", "efgh"], "backward={backward}");
            assert_eq!(paragraph.get_text_content(), "abcdefghijkl");
            assert_eq!(selection.is_backward()?, backward);
            Ok(())
        });
    }
}

// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > does not extract text from an empty mixed-point range (backward=false)
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > does not extract text from an empty mixed-point range (backward=true)
#[test]
fn does_not_extract_text_from_an_empty_mixed_point_range() {
    for backward in [false, true] {
        run("selection-text-slices", move || {
            let text = create_text_node("abcd");
            let following = text_with_format("efgh", "italic");
            let paragraph = paragraph(&[text.clone(), following.clone()])?;
            set_root(std::slice::from_ref(&paragraph))?;
            let selection = create_range_selection();
            let (element_point, text_point) = ends(backward);
            selection.point_set(element_point, paragraph.get_key(), 1, PointType::Element)?;
            selection.point_set(text_point, text.get_key(), 4, PointType::Text)?;
            set_selection(Some(selection.clone()));

            assert_eq!(selection.extract()?, [], "backward={backward}");
            assert_eq!(paragraph.get_children(), [text.clone(), following]);
            assert_eq!(text.get_text_content(), "abcd");
            Ok(())
        });
    }
}

// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > pending format excludes a final node selected at offset zero
#[test]
fn pending_format_excludes_a_final_node_selected_at_offset_zero() {
    run("selection-text-slices", || {
        let first = create_text_node("abcd");
        first.set_format(f64::from(IS_BOLD));
        let second = create_text_node("efgh");
        second.set_format(f64::from(IS_ITALIC));
        let paragraph = paragraph(&[first.clone(), second.clone()])?;
        set_root(std::slice::from_ref(&paragraph))?;
        let selection = create_range_selection();
        selection.point_set(Which::Anchor, first.get_key(), 0, PointType::Text)?;
        selection.point_set(Which::Focus, second.get_key(), 0, PointType::Text)?;
        selection.set_format(f64::from(IS_BOLD));
        set_selection(Some(selection.clone()));

        selection.format_text("bold", None)?;
        assert_eq!(first.text_data().format, 0.0);
        assert_eq!(second.text_data().format, f64::from(IS_ITALIC));
        assert_eq!(selection.range().unwrap().format, 0.0);
        Ok(())
    });
}

// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > formatting a detached selection retains its partial text range
#[test]
fn formatting_a_detached_selection_retains_its_partial_text_range() {
    run("selection-text-slices", || {
        let text = create_text_node("abcde");
        let other = create_text_node("other");
        let paragraph = paragraph(std::slice::from_ref(&text))?;
        set_root(&[
            paragraph.clone(),
            self::paragraph(std::slice::from_ref(&other))?,
        ])?;
        let active = other.select(Some(2), Some(2))?;
        let selection = create_range_selection();
        selection.point_set(Which::Anchor, text.get_key(), 4, PointType::Text)?;
        selection.point_set(Which::Focus, text.get_key(), 1, PointType::Text)?;

        selection.format_text("bold", None)?;
        let children: Vec<(String, Option<f64>)> = paragraph
            .get_children()
            .iter()
            .map(|n| {
                (
                    n.get_text_content(),
                    n.is_text().then(|| n.text_data().format),
                )
            })
            .collect();
        assert_eq!(
            children,
            [
                ("a".to_owned(), Some(0.0)),
                ("bcd".to_owned(), Some(f64::from(IS_BOLD))),
                ("e".to_owned(), Some(0.0)),
            ]
        );
        let anchor = point(&selection, Which::Anchor);
        assert_eq!(anchor.get_node()?.get_text_content(), "bcd");
        assert_eq!(anchor.offset, 3);
        assert_eq!(point(&selection, Which::Focus).offset, 0);
        assert_active(&active);
        let active_anchor = point(&active, Which::Anchor);
        assert_eq!(active_anchor.key, other.get_key());
        assert_eq!(active_anchor.offset, 2);
        Ok(())
    });
}

// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > formatting retains an element start (backward=false, active=false, preceding=false, end=1)
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > formatting retains an element start (backward=false, active=false, preceding=false, end=3)
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > formatting retains an element start (backward=false, active=false, preceding=true, end=1)
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > formatting retains an element start (backward=false, active=false, preceding=true, end=3)
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > formatting retains an element start (backward=false, active=true, preceding=false, end=1)
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > formatting retains an element start (backward=false, active=true, preceding=false, end=3)
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > formatting retains an element start (backward=false, active=true, preceding=true, end=1)
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > formatting retains an element start (backward=false, active=true, preceding=true, end=3)
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > formatting retains an element start (backward=true, active=false, preceding=false, end=1)
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > formatting retains an element start (backward=true, active=false, preceding=false, end=3)
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > formatting retains an element start (backward=true, active=false, preceding=true, end=1)
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > formatting retains an element start (backward=true, active=false, preceding=true, end=3)
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > formatting retains an element start (backward=true, active=true, preceding=false, end=1)
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > formatting retains an element start (backward=true, active=true, preceding=false, end=3)
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > formatting retains an element start (backward=true, active=true, preceding=true, end=1)
// upstream: lexical/src/__tests__/unit/LexicalSelectionCaretSlices.test.ts::selection text slices > formatting retains an element start (backward=true, active=true, preceding=true, end=3)
#[test]
fn formatting_retains_an_element_start() {
    for backward in [false, true] {
        for active in [false, true] {
            for preceding in [false, true] {
                for end_offset in [1, 3] {
                    formatting_retains_an_element_start_case(
                        backward, active, preceding, end_offset,
                    );
                }
            }
        }
    }
}

fn formatting_retains_an_element_start_case(
    backward: bool,
    active: bool,
    preceding: bool,
    end_offset: usize,
) {
    let case =
        format!("backward={backward}, active={active}, preceding={preceding}, end={end_offset}");
    run("selection-text-slices", move || {
        let text = create_text_node("abc");
        let paragraph = create_paragraph_node();
        if preceding {
            paragraph.append(&[text_with_format("before", "italic")])?;
        }
        paragraph.append(&[text.clone(), create_text_node("def")])?;
        let other = create_text_node("other");
        set_root(&[
            paragraph.clone(),
            self::paragraph(std::slice::from_ref(&other))?,
        ])?;
        let unrelated = other.select(Some(2), Some(2))?;
        let selection = create_range_selection();
        let (start, end) = ends(backward);
        selection.point_set(
            start,
            paragraph.get_key(),
            usize::from(preceding),
            PointType::Element,
        )?;
        selection.point_set(end, text.get_key(), end_offset, PointType::Text)?;
        if active {
            set_selection(Some(selection.clone()));
        }
        let expected_text = &"abc"[..end_offset];
        for expected_format in [IS_BOLD, 0] {
            selection.format_text("bold", None)?;
            let formatted = paragraph
                .get_child_at_index(usize::from(preceding))
                .expect("a child");
            assert!(formatted.is_text(), "{case}");
            assert_eq!(
                formatted.text_data().format,
                f64::from(expected_format),
                "{case}"
            );
            assert_eq!(formatted.get_text_content(), expected_text, "{case}");
            assert_eq!(selection.get_text_content()?, expected_text, "{case}");
            let start_point = point(&selection, start);
            let expected_start_key = if end_offset == 3 {
                paragraph.get_key()
            } else {
                formatted.get_key()
            };
            assert_eq!(start_point.key, expected_start_key, "{case}");
            assert_eq!(
                start_point.point_type,
                if end_offset == 3 {
                    PointType::Element
                } else {
                    PointType::Text
                },
                "{case}"
            );
            assert_eq!(
                start_point.offset,
                usize::from(end_offset == 3 && preceding),
                "{case}"
            );
            let end_point = point(&selection, end);
            assert_eq!(end_point.key, formatted.get_key(), "{case}");
            assert_eq!(end_point.offset, end_offset, "{case}");
            assert_eq!(selection.is_backward()?, backward, "{case}");
            if !active {
                assert_active(&unrelated);
                let anchor = point(&unrelated, Which::Anchor);
                assert_eq!(anchor.key, other.get_key(), "{case}");
                assert_eq!(anchor.offset, 2, "{case}");
            }
        }
        Ok(())
    });
}