lexical-ui 0.1.3

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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
//! `formatList.ts`: inserting and removing lists, merging them, numbering
//! their items, indenting and outdenting items, and Enter in an empty item.
//!
//! Where `NodeStructureExtension` enforces node structure, `$insertList`
//! keeps a block that holds what a list item cannot (`$keptTops`,
//! `$listTarget`), and the normalization joins nested lists at a seam only
//! where an item's only child is the nested list. Like upstream, neither
//! `$insertList` nor `$removeList` converts a named slot's value.

use super::ListItemNode;
use super::list_item_node::{create_list_item_node, is_list_item_node};
use super::list_node::{ListType, create_list_node, is_list_node};
use super::utils::{
    get_all_list_items, get_new_list_start, get_top_list_node, is_nested_list_node,
    remove_highest_empty_list_parent,
};
use crate::LexicalError;
use crate::caret::CaretDirection;
use crate::caret_utils::{get_child_caret_at_index, normalize_caret, set_point_from_caret};
use crate::error::invariant;
use crate::lexical_selection::{PointType, SelectionRef, Which, get_selection};
use crate::lexical_utils::copy_node;
use crate::node::NodeRef;
use crate::nodes::line_break::LineBreakNode;
use crate::nodes::paragraph::create_paragraph_node;
use crate::selection_deletion::is_js_whitespace;
use crate::selection_insert_nodes::is_block_container;
use crate::utils::get_nearest_node_of_type;
use std::collections::HashMap;

/// `$isLeafNode(node)`: text, a line break or a decorator.
fn is_leaf_node(node: &NodeRef) -> bool {
    node.is_text() || node.get_latest().is::<LineBreakNode>() || node.is_decorator()
}

/// `$isSelectingEmptyListItem(anchorNode, nodes)`.
fn is_selecting_empty_list_item(anchor_node: &NodeRef, nodes: &[NodeRef]) -> bool {
    is_list_item_node(anchor_node)
        && (nodes.is_empty()
            || (nodes.len() == 1
                && anchor_node == &nodes[0]
                && anchor_node.get_children_size() == 0))
}

/// `$newListFrom(list, listType)`: a copy of the list with the new type, or
/// a new list for anything else.
fn new_list_from(list: &NodeRef, list_type: ListType) -> NodeRef {
    if is_list_node(list) {
        let copy = copy_node(list);
        copy.set_list_type(list_type);
        copy
    } else {
        create_list_node(list_type, 1.0)
    }
}

/// `append(node, nodesToAppend)`: `ElementNode.splice` at the end, so that
/// `ListItemNode.append`'s merging doesn't apply.
fn append(node: &NodeRef, nodes_to_append: &[NodeRef]) -> Result<(), LexicalError> {
    node.splice(node.get_children_size(), 0, nodes_to_append)
}

/// `$insertList(listType)`: turns the selected blocks into items of a list
/// of `list_type` (merging with a list of the type next to them), and the
/// selected lists into lists of that type.
pub fn insert_list(list_type: ListType) -> Result<(), LexicalError> {
    let Some(selection) = get_selection() else {
        return Ok(());
    };
    let mut nodes = selection.get_nodes()?;
    if let Some(anchor) = selection.point(Which::Anchor) {
        let anchor_node = anchor.get_node()?;
        let anchor_node_parent = anchor_node.get_parent();
        if is_block_container(&anchor_node) {
            match anchor_node.get_first_child() {
                Some(first_child) => nodes = first_child.select_start()?.get_nodes()?,
                None => {
                    let paragraph = create_paragraph_node();
                    anchor_node.append(std::slice::from_ref(&paragraph))?;
                    nodes = paragraph.select(None, None)?.get_nodes()?;
                }
            }
        } else if is_selecting_empty_list_item(&anchor_node, &nodes) {
            if anchor_node_parent.as_ref().is_some_and(is_block_container) {
                let list = create_list_node(list_type, 1.0);
                anchor_node.replace(&list, false)?;
                let list_item = create_list_item_node(None);
                if anchor_node.is_element() {
                    list_item.set_element_format(anchor_node.element_data().get_format_type());
                    list_item.set_indent(anchor_node.get_indent()?)?;
                }
                list.append(&[list_item])?;
            } else if is_list_item_node(&anchor_node) {
                let parent = anchor_node.get_parent_or_throw()?;
                // A named-slot value has no __parent (its up-link is
                // __slotHost); its slot assignment is managed by the node or
                // extension that owns the slot, so converting it is a no-op
                // rather than a replace (which would throw).
                if crate::slot::get_slot_host(&parent).is_none() {
                    let list = new_list_from(&parent, list_type);
                    append(&list, &parent.get_children())?;
                    parent.replace(&list, false)?;
                }
            }
            return Ok(());
        }
    }

    let mut handled: Vec<String> = Vec::new();
    let mut kept = kept_tops(&nodes);
    for node in &nodes {
        if node.is_element()
            && node.is_empty()
            && !is_list_item_node(node)
            // An empty container the selection passes over stays a container.
            && !is_block_container(node)
            // A named-slot value's slot assignment is managed by the node or
            // extension that owns the slot, so it is not eligible for list
            // conversion (see $setBlocksType).
            && crate::slot::get_slot_host(node).is_none()
            && !handled.iter().any(|k| k == node.get_key())
        {
            create_list_or_merge(node, list_type)?;
            continue;
        }

        let mut parent = if is_leaf_node(node) {
            node.get_parent()
        } else if is_list_item_node(node) && node.is_empty() {
            Some(node.clone())
        } else {
            None
        };

        while let Some(p) = parent {
            if is_block_container(&p) {
                break;
            }
            let parent_key = p.get_key().to_owned();
            if is_list_node(&p) {
                // A list occupying a named slot is left as-is: its slot
                // assignment is managed by the node or extension that owns
                // the slot.
                if !handled.contains(&parent_key) && crate::slot::get_slot_host(&p).is_none() {
                    let new_list_node = new_list_from(&p, list_type);
                    append(&new_list_node, &p.get_children())?;
                    p.replace(&new_list_node, false)?;
                    handled.push(parent_key);
                }
                break;
            }
            let next_parent = p.get_parent();
            if next_parent.as_ref().is_some_and(is_block_container) {
                if let Some(target) = list_target(&p, node, &mut kept)
                    && !handled.iter().any(|k| k == target.get_key())
                {
                    handled.push(target.get_key().to_owned());
                    create_list_or_merge(&target, list_type)?;
                }
                break;
            }
            parent = next_parent;
        }
    }
    Ok(())
}

/// `$isListItemContent(node)`: whether a list item can hold `node` where
/// `NodeStructureExtension` enforces declared structure: inline content as
/// the normalization counts it, a list, or a plain paragraph (which becomes a
/// line of the item). Anything else is lifted out of the item.
fn is_list_item_content(node: &NodeRef) -> bool {
    crate::internal_is_inline_content(node)
        || is_list_node(node)
        || (node
            .get_latest()
            .is::<crate::nodes::paragraph::ParagraphNode>()
            && !node.get_latest().is_shadow_root())
}

/// `$keptTops(nodes)`: where `NodeStructureExtension` enforces declared
/// structure, for the node in the nearest block container that holds each
/// selected node, whether it stays and only the blocks holding the selected
/// text inside it become lists (see [`list_target`]). Decided for the whole
/// command before anything is converted.
fn kept_tops(nodes: &[NodeRef]) -> HashMap<String, bool> {
    let mut kept = HashMap::new();
    if !crate::structure::is_structure_enforced_on_active_editor() {
        return kept;
    }
    for node in nodes {
        let mut top = Some(node.clone());
        while let Some(t) = top.clone() {
            if t.get_parent().is_some_and(|p| is_block_container(&p)) {
                break;
            }
            top = t.get_parent();
        }
        if let Some(top) = top.filter(|t| t.is_element() && !is_block_container(t))
            && !kept.contains_key(top.get_key())
        {
            let keep = !top.get_children().iter().all(is_list_item_content);
            kept.insert(top.get_key().to_owned(), keep);
        }
    }
    kept
}

/// `$listTarget(top, leaf, kept)`: the node `$insertList` turns into a list
/// item for the selected `leaf`, where `top` is its ancestor in the nearest
/// block container, or `None` for none. Usually `top`; where
/// `NodeStructureExtension` enforces declared structure and `top` also holds
/// a block a list item cannot hold, `top` stays, and the block holding the
/// selected text becomes the list instead, in place, inside `top`.
fn list_target(top: &NodeRef, leaf: &NodeRef, kept: &mut HashMap<String, bool>) -> Option<NodeRef> {
    if !crate::structure::is_structure_enforced_on_active_editor() {
        return Some(top.clone());
    }
    let keep = *kept
        .entry(top.get_key().to_owned())
        .or_insert_with(|| !top.get_children().iter().all(is_list_item_content));
    if !keep {
        return Some(top.clone());
    }
    // The block that holds the selected text, never inline content.
    crate::selection_insert_nodes::find_matching_parent(leaf, |n| {
        crate::lexical_utils::is_block(n) && !crate::internal_is_inline_content(n)
    })
    .filter(NodeRef::is_element)
}

/// `$createListOrMerge(node, listType)`: the block becomes an item of the
/// list of `list_type` next to it, or of a new list in its place.
fn create_list_or_merge(node: &NodeRef, list_type: ListType) -> Result<NodeRef, LexicalError> {
    if is_list_node(node) {
        return Ok(node.clone());
    }
    let previous_sibling = node.get_previous_sibling();
    let next_sibling = node.get_next_sibling();
    let list_item = create_list_item_node(None);
    append(&list_item, &node.get_children())?;

    let same_type = |n: &Option<NodeRef>| {
        n.as_ref()
            .filter(|n| is_list_node(n) && n.get_list_type() == Some(list_type))
            .cloned()
    };
    let target_list = if let Some(previous_sibling) = same_type(&previous_sibling) {
        previous_sibling.append(std::slice::from_ref(&list_item))?;
        // if the same type of list is on both sides, merge them.
        if let Some(next_sibling) = same_type(&next_sibling) {
            append(&previous_sibling, &next_sibling.get_children())?;
            next_sibling.remove(false)?;
        }
        previous_sibling
    } else if let Some(next_sibling) = same_type(&next_sibling) {
        let first = next_sibling.get_first_child().ok_or_else(|| {
            LexicalError::new(format!(
                "Expected node {} to have a first child.",
                next_sibling.get_key()
            ))
        })?;
        first.insert_before(&list_item, true)?;
        next_sibling
    } else {
        let list = create_list_node(list_type, 1.0);
        list.append(std::slice::from_ref(&list_item))?;
        node.replace(&list, false)?;
        list
    };
    // listItem needs to be attached to root prior to setting indent
    list_item.set_element_format(node.element_data().get_format_type());
    list_item.set_indent(node.get_indent()?)?;

    // Preserve element-anchored selections by updating them to anchor to the
    // listItem instead of the listNode.
    if let Some(selection) = get_selection().filter(SelectionRef::is_range) {
        for which in [Which::Anchor, Which::Focus] {
            let point = selection.point(which).expect("a range selection");
            if point.key == target_list.get_key() {
                selection.point_set(
                    which,
                    list_item.get_key(),
                    point.offset,
                    PointType::Element,
                )?;
            }
        }
    }

    node.remove(false)?;
    Ok(target_list)
}

/// `mergeLists(list1, list2)`: appends the second list's items to the
/// first's, joining nested lists of the same type where they meet, and
/// removes the second.
pub fn merge_lists(list1: &NodeRef, list2: &NodeRef) -> Result<(), LexicalError> {
    join_lists(list1, list2, false)
}

/// `joinLists(list1, list2, onlyChild)`.
fn join_lists(list1: &NodeRef, list2: &NodeRef, only_child: bool) -> Result<(), LexicalError> {
    merge_nested_lists_at_seam(list1.get_last_child(), list2.get_first_child(), only_child)?;
    let to_merge = list2.get_children();
    if !to_merge.is_empty() {
        list1.append(&to_merge)?;
    }
    list2.remove(false)
}

/// `mergeNestedListsAtSeam(listItem1, listItem2, onlyChild)`: two items,
/// one at the end of a list and one at the start of the next, that wrap
/// nested lists of the same type become one. With `only_child` (the node
/// structure normalization), an item counts as one that only wraps a nested
/// list when that list is its only child.
pub(super) fn merge_nested_lists_at_seam(
    list_item1: Option<NodeRef>,
    list_item2: Option<NodeRef>,
    only_child: bool,
) -> Result<(), LexicalError> {
    let (Some(list_item1), Some(list_item2)) = (list_item1, list_item2) else {
        return Ok(());
    };
    if !is_nested_list_node(&list_item1) || !is_nested_list_node(&list_item2) {
        return Ok(());
    }
    if only_child && (list_item1.get_children_size() != 1 || list_item2.get_children_size() != 1) {
        return Ok(());
    }
    let inner1 = list_item1.get_first_child().expect("a nested list");
    let inner2 = list_item2.get_first_child().expect("a nested list");
    // A nested <ul> must not swallow a nested <ol>, same rule as
    // mergeNextSiblingListIfSameType applies at the top level.
    if inner1.get_list_type() == inner2.get_list_type() {
        join_lists(&inner1, &inner2, only_child)?;
        if !only_child {
            return list_item2.remove(false);
        }
        // ElementNode's own remove. The normalization calls this once the
        // second list's items are already in the first, so
        // ListItemNode.remove would also join the item after this one.
        crate::node::remove(&list_item2, false)?;
    }
    Ok(())
}

/// `$removeList()`: the lists holding the selection become paragraphs, one
/// per item that holds content.
pub fn remove_list() -> Result<(), LexicalError> {
    let Some(selection) = get_selection().filter(SelectionRef::is_range) else {
        return Ok(());
    };
    let mut list_nodes: Vec<NodeRef> = Vec::new();
    let nodes = selection.get_nodes()?;
    let anchor_node = selection
        .point(Which::Anchor)
        .expect("a range selection")
        .get_node()?;
    if is_selecting_empty_list_item(&anchor_node, &nodes) {
        list_nodes.push(get_top_list_node(&anchor_node)?);
    } else {
        for node in &nodes {
            if is_leaf_node(node)
                && let Some(list_item_node) = get_nearest_node_of_type::<ListItemNode>(node)
            {
                let top = get_top_list_node(&list_item_node)?;
                if !list_nodes.contains(&top) {
                    list_nodes.push(top);
                }
            }
        }
    }

    for list_node in &list_nodes {
        // A list occupying a named slot is left as-is: unwinding it would
        // insert siblings through the tree API (which would fail) and vacate
        // a slot whose assignment the node or extension that owns it manages.
        if crate::slot::get_slot_host(list_node).is_some() {
            continue;
        }
        let mut insertion_point = list_node.clone();
        for list_item_node in get_all_list_items(list_node) {
            let (format, style) = selection
                .with_range(|r| (r.format, r.style.clone()))
                .expect("a range selection");
            let paragraph = create_paragraph_node();
            paragraph.set_text_style(&style);
            paragraph.set_text_format(format);
            // The paragraph stands in for the list item, so it keeps the
            // item's own element state.
            let item = list_item_node.element_data();
            paragraph.set_element_format(item.get_format_type());
            paragraph.set_indent(list_item_node.get_indent()?)?;
            paragraph.set_direction(item.get_direction());
            paragraph.set_element_style(item.get_style());

            append(&paragraph, &list_item_node.get_children())?;

            insertion_point.insert_after(&paragraph, true)?;
            insertion_point = paragraph.clone();

            // Points in the item's children move with them. A point on the
            // item itself (an empty item, or a caret beside an inline
            // decorator) moves to the same offset in the paragraph, which
            // holds the same children in the same order.
            for which in [Which::Anchor, Which::Focus] {
                let point = selection.point(which).expect("a range selection");
                if point.key == list_item_node.get_key() {
                    let caret = normalize_caret(&get_child_caret_at_index(
                        &paragraph,
                        point.offset,
                        CaretDirection::Next,
                    ));
                    set_point_from_caret(&selection, which, &caret)?;
                }
            }

            list_item_node.remove(false)?;
        }
        list_node.remove(false)?;
    }
    Ok(())
}

/// `updateChildrenListItemValue(list)`: numbers the list's items from its
/// start (an item that only wraps a nested list has no number), and clears
/// `checked` outside a check list.
pub fn update_children_list_item_value(list: &NodeRef) {
    let is_not_checklist = list.get_list_type() != Some(ListType::Check);
    let mut value = list.get_list_start().unwrap_or(1.0);
    for child in list.get_children() {
        if !is_list_item_node(&child) {
            continue;
        }
        if child.get_list_item_value() != Some(value) {
            child.set_list_item_value(value);
        }
        let checked = child
            .get_latest()
            .downcast_ref::<ListItemNode>()
            .and_then(ListItemNode::checked);
        if is_not_checklist && checked.is_some() {
            child.set_checked(None);
        }
        if !child.get_first_child().is_some_and(|c| is_list_node(&c)) {
            value += 1.0;
        }
    }
}

/// `mergeNextSiblingListIfSameType(list)`: `<ul>` merges with `<ul>`, but
/// not with `<ol>`.
pub fn merge_next_sibling_list_if_same_type(list: &NodeRef) -> Result<(), LexicalError> {
    if let Some(next_sibling) = list.get_next_sibling()
        && is_list_node(&next_sibling)
        && list.get_list_type() == next_sibling.get_list_type()
    {
        merge_lists(list, &next_sibling)?;
    }
    Ok(())
}

/// `$handleIndent(listItemNode)`: nests the item one level deeper, into the
/// nested list next to it or a new one.
pub fn handle_indent(list_item_node: &NodeRef) -> Result<(), LexicalError> {
    if is_nested_list_node(list_item_node) {
        return Ok(());
    }
    let parent = list_item_node.get_parent();
    let next_sibling = list_item_node
        .get_next_sibling()
        .filter(is_nested_list_node);
    let previous_sibling = list_item_node
        .get_previous_sibling()
        .filter(is_nested_list_node);
    // if there are nested lists on either side, merge them all together.
    match (&previous_sibling, &next_sibling) {
        (Some(previous_sibling), Some(next_sibling)) => {
            let inner_list = previous_sibling.get_first_child().expect("a nested list");
            inner_list.append(std::slice::from_ref(list_item_node))?;
            let next_inner_list = next_sibling.get_first_child().expect("a nested list");
            // Only pull in the following sublist when it is the same kind of
            // list, otherwise it loses its listType.
            if inner_list.get_list_type() == next_inner_list.get_list_type() {
                append(&inner_list, &next_inner_list.get_children())?;
                next_sibling.remove(false)?;
            }
        }
        (None, Some(next_sibling)) => {
            // if the ListItemNode is next to a nested ListNode, merge them
            let inner_list = next_sibling.get_first_child().expect("a nested list");
            if let Some(first_child) = inner_list.get_first_child() {
                first_child.insert_before(list_item_node, true)?;
            }
        }
        (Some(previous_sibling), None) => {
            let inner_list = previous_sibling.get_first_child().expect("a nested list");
            inner_list.append(std::slice::from_ref(list_item_node))?;
        }
        (None, None) => {
            // otherwise, we need to create a new nested ListNode
            if let Some(parent) = parent.filter(is_list_node) {
                let previous_sibling = list_item_node.get_previous_sibling();
                let next_sibling = list_item_node.get_next_sibling();
                let new_list_item = copy_node(list_item_node);
                let new_list = copy_node(&parent);
                new_list_item.append(std::slice::from_ref(&new_list))?;
                new_list.append(std::slice::from_ref(list_item_node))?;
                if let Some(previous_sibling) = previous_sibling {
                    previous_sibling.insert_after(&new_list_item, true)?;
                } else if let Some(next_sibling) = next_sibling {
                    next_sibling.insert_before(&new_list_item, true)?;
                } else {
                    parent.append(&[new_list_item])?;
                }
            }
        }
    }
    Ok(())
}

/// `$handleOutdent(listItemNode)`: moves a nested item one level up, into
/// the list holding the item that wraps its list.
pub fn handle_outdent(list_item_node: &NodeRef) -> Result<(), LexicalError> {
    if is_nested_list_node(list_item_node) {
        return Ok(());
    }
    let parent_list = list_item_node.get_parent();
    let grandparent_list_item = parent_list.as_ref().and_then(NodeRef::get_parent);
    let great_grandparent_list = grandparent_list_item.as_ref().and_then(NodeRef::get_parent);
    // If it doesn't have these ancestors, it's not indented.
    let (Some(parent_list), Some(grandparent_list_item), Some(great_grandparent_list)) =
        (parent_list, grandparent_list_item, great_grandparent_list)
    else {
        return Ok(());
    };
    if !(is_list_node(&great_grandparent_list)
        && is_list_item_node(&grandparent_list_item)
        && is_list_node(&parent_list))
    {
        return Ok(());
    }
    let first_child = parent_list.get_first_child();
    let last_child = parent_list.get_last_child();
    let is_first = first_child.as_ref() == Some(list_item_node);
    let is_last = last_child.as_ref() == Some(list_item_node);
    // The general outdent below assumes the wrapper item holds only the
    // nested list: it removes or replaces the wrapper when the item is the
    // list's only item or a middle one, which would delete what else the
    // wrapper holds. Only those cases take the outdent that keeps it.
    if grandparent_list_item.get_children_size() > 1
        && (parent_list.get_children_size() == 1 || (!is_first && !is_last))
    {
        return outdent_from_item_with_content(
            list_item_node,
            &parent_list,
            &grandparent_list_item,
        );
    }
    if is_first {
        // if it's the first child in it's parent list, insert it into the
        // great grandparent list before the grandparent
        grandparent_list_item.insert_before(list_item_node, true)?;
        if parent_list.is_empty() {
            grandparent_list_item.remove(false)?;
        }
    } else if is_last {
        // if it's the last child in it's parent list, insert it into the
        // great grandparent list after the grandparent.
        grandparent_list_item.insert_after(list_item_node, true)?;
        if parent_list.is_empty() {
            grandparent_list_item.remove(false)?;
        }
    } else {
        // otherwise, we need to split the siblings into two new nested lists
        let previous_siblings_list_item = copy_node(list_item_node);
        let previous_siblings_list = copy_node(&parent_list);
        previous_siblings_list_item.append(std::slice::from_ref(&previous_siblings_list))?;
        for sibling in list_item_node.get_previous_siblings() {
            previous_siblings_list.append(&[sibling])?;
        }
        let next_siblings_list_item = copy_node(list_item_node);
        let next_siblings_list = copy_node(&parent_list);
        next_siblings_list_item.append(std::slice::from_ref(&next_siblings_list))?;
        append(&next_siblings_list, &list_item_node.get_next_siblings())?;
        // put the sibling nested lists on either side of the grandparent list
        // item in the great grandparent.
        grandparent_list_item.insert_before(&previous_siblings_list_item, true)?;
        grandparent_list_item.insert_after(&next_siblings_list_item, true)?;
        // replace the grandparent list item (now between the siblings) with
        // the outdented list item.
        grandparent_list_item.replace(list_item_node, false)?;
    }
    Ok(())
}

/// `$outdentFromItemWithContent(listItemNode, parentList, wrapper)`: the
/// outdent when the item wrapping the nested list holds other content too,
/// which stays in reading order around the outdented item.
fn outdent_from_item_with_content(
    list_item_node: &NodeRef,
    parent_list: &NodeRef,
    wrapper: &NodeRef,
) -> Result<(), LexicalError> {
    let next_siblings = list_item_node.get_next_siblings();
    let after = parent_list.get_next_siblings();
    wrapper.insert_after(list_item_node, true)?;
    let mut last = list_item_node.clone();
    if !next_siblings.is_empty() {
        let next_siblings_list_item = copy_node(list_item_node);
        let next_siblings_list = copy_node(parent_list);
        next_siblings_list_item.append(std::slice::from_ref(&next_siblings_list))?;
        append(&next_siblings_list, &next_siblings)?;
        last.insert_after(&next_siblings_list_item, true)?;
        last = next_siblings_list_item;
    }
    if !after.is_empty() {
        let after_list_item = copy_node(wrapper);
        last.insert_after(&after_list_item, true)?;
        append(&after_list_item, &after)?;
    }
    if parent_list.is_empty() {
        parent_list.remove(false)?;
    }
    if wrapper.is_empty() {
        wrapper.remove(false)?;
    }
    Ok(())
}

/// `$handleListInsertParagraph(restoreNumbering)`: Enter in an empty (or
/// blank) item leaves the list, or a nested list, splitting it there.
/// Returns whether it handled the Enter.
pub fn handle_list_insert_paragraph(restore_numbering: bool) -> Result<bool, LexicalError> {
    let Some(selection) = get_selection().filter(SelectionRef::is_range) else {
        return Ok(false);
    };
    let anchor_point = selection.point(Which::Anchor).expect("a range selection");
    if Some(&anchor_point) != selection.point(Which::Focus).as_ref() {
        return Ok(false);
    }
    // Only run this code on empty list items (including whitespace-only)
    let anchor = anchor_point.get_node()?;
    let list_item = if is_list_item_node(&anchor) && anchor.get_children_size() == 0 {
        // Truly empty list item (element selection)
        Some(anchor)
    } else if anchor.is_text() {
        // Check if the entire list item contains only whitespace text nodes
        anchor.get_parent().filter(|parent| {
            is_list_item_node(parent)
                && parent.get_children().iter().all(|node| {
                    node.is_text() && node.get_text_content().chars().all(is_js_whitespace)
                })
        })
    } else {
        None
    };
    let Some(list_item) = list_item else {
        return Ok(false);
    };

    let top_list_node = get_top_list_node(&list_item)?;
    let parent = list_item.get_parent();
    invariant!(
        parent.as_ref().is_some_and(is_list_node),
        "A ListItemNode must have a ListNode for a parent."
    );
    let parent = parent.expect("checked above");
    let grandparent = parent.get_parent();

    let replacement_node = match &grandparent {
        Some(g) if is_block_container(g) => {
            let paragraph = create_paragraph_node();
            top_list_node.insert_after(&paragraph, true)?;
            paragraph
        }
        Some(g) if is_list_item_node(g) => {
            let item = copy_node(g);
            g.insert_after(&item, true)?;
            item
        }
        _ => return Ok(false),
    };
    let (format, style) = selection
        .with_range(|r| (r.format, r.style.clone()))
        .expect("a range selection");
    replacement_node.set_text_style(&style);
    replacement_node.set_text_format(format);
    replacement_node.select(None, None)?;

    let next_siblings = list_item.get_next_siblings();
    if !next_siblings.is_empty() {
        let new_start = if restore_numbering {
            get_new_list_start(&parent, &list_item)
        } else {
            1.0
        };
        let new_list = copy_node(&parent);
        new_list.set_list_start(new_start);
        if is_list_item_node(&replacement_node) {
            let new_list_item = copy_node(&replacement_node);
            new_list_item.append(std::slice::from_ref(&new_list))?;
            replacement_node.insert_after(&new_list_item, true)?;
        } else {
            replacement_node.insert_after(&new_list, true)?;
        }
        new_list.append(&next_siblings)?;
    }

    // Don't leave hanging nested empty lists
    remove_highest_empty_list_parent(&list_item)?;
    Ok(true)
}