Skip to content

Commit

Permalink
Lexical: Aligned new empty item behaviour for nested lists
Browse files Browse the repository at this point in the history
- Makes enter on empty nested list item un-nest instead of just creating
  new list items.
- Also updated existing lists tests to use newer helper setup.
  • Loading branch information
ssddanbrown committed Dec 17, 2024
1 parent ace8af0 commit fca8f92
Show file tree
Hide file tree
Showing 4 changed files with 538 additions and 520 deletions.
1 change: 1 addition & 0 deletions resources/js/wysiwyg/lexical/core/__tests__/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,7 @@ export function dispatchKeydownEventForNode(node: LexicalNode, editor: LexicalEd
key,
});
nodeDomEl?.dispatchEvent(event);
editor.commitUpdates();
}

export function dispatchKeydownEventForSelectedNode(editor: LexicalEditor, key: string) {
Expand Down
11 changes: 9 additions & 2 deletions resources/js/wysiwyg/lexical/list/LexicalListItemNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -271,11 +271,18 @@ export class ListItemNode extends ElementNode {
insertNewAfter(
_: RangeSelection,
restoreSelection = true,
): ListItemNode | ParagraphNode {
): ListItemNode | ParagraphNode | null {

if (this.getTextContent().trim() === '' && this.isLastChild()) {
const list = this.getParentOrThrow<ListNode>();
if (!$isListItemNode(list.getParent())) {
const parentListItem = list.getParent();
if ($isListItemNode(parentListItem)) {
// Un-nest list item if empty nested item
parentListItem.insertAfter(this);
this.selectStart();
return null;
} else {
// Insert empty paragraph after list if adding after last empty child
const paragraph = $createParagraphNode();
list.insertAfter(paragraph, restoreSelection);
this.remove();
Expand Down
Loading

0 comments on commit fca8f92

Please sign in to comment.