From a7839b72532a8e288032ca4baaa8907a807de763 Mon Sep 17 00:00:00 2001 From: Aman Harwara Date: Sat, 27 Apr 2024 17:54:28 +0530 Subject: [PATCH 1/4] set default `checked` boolean value when creating a check list --- packages/lexical-list/src/formatList.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/packages/lexical-list/src/formatList.ts b/packages/lexical-list/src/formatList.ts index 2263edfb757..ff9c098c267 100644 --- a/packages/lexical-list/src/formatList.ts +++ b/packages/lexical-list/src/formatList.ts @@ -83,7 +83,9 @@ export function insertList(editor: LexicalEditor, listType: ListType): void { if ($isRootOrShadowRoot(anchorNodeParent)) { anchorNode.replace(list); - const listItem = $createListItemNode(); + const listItem = $createListItemNode( + listType === 'check' ? false : undefined, + ); if ($isElementNode(anchorNode)) { listItem.setFormat(anchorNode.getFormatType()); listItem.setIndent(anchorNode.getIndent()); @@ -156,7 +158,9 @@ function createListOrMerge(node: ElementNode, listType: ListType): ListNode { const previousSibling = node.getPreviousSibling(); const nextSibling = node.getNextSibling(); - const listItem = $createListItemNode(); + const listItem = $createListItemNode( + listType === 'check' ? false : undefined, + ); listItem.setFormat(node.getFormatType()); listItem.setIndent(node.getIndent()); append(listItem, node.getChildren()); From 65eb5bfe627bebd9cd955fc6ec7edadb24c4ab08 Mon Sep 17 00:00:00 2001 From: Aman Harwara Date: Sun, 12 May 2024 22:53:32 +0530 Subject: [PATCH 2/4] return boolean from getChecked if list type is check --- packages/lexical-list/src/LexicalListItemNode.ts | 8 +++++++- packages/lexical-list/src/formatList.ts | 8 ++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/packages/lexical-list/src/LexicalListItemNode.ts b/packages/lexical-list/src/LexicalListItemNode.ts index 497583adf88..ca9b8013508 100644 --- a/packages/lexical-list/src/LexicalListItemNode.ts +++ b/packages/lexical-list/src/LexicalListItemNode.ts @@ -321,7 +321,13 @@ export class ListItemNode extends ElementNode { getChecked(): boolean | undefined { const self = this.getLatest(); - return self.__checked; + const parent = this.getParent(); + if (!$isListNode(parent)) { + invariant(false, 'getChecked: list node is not parent of list item node'); + } + const listType = parent.getListType(); + + return listType === 'check' ? Boolean(self.__checked) : undefined; } setChecked(checked?: boolean): void { diff --git a/packages/lexical-list/src/formatList.ts b/packages/lexical-list/src/formatList.ts index ff9c098c267..2263edfb757 100644 --- a/packages/lexical-list/src/formatList.ts +++ b/packages/lexical-list/src/formatList.ts @@ -83,9 +83,7 @@ export function insertList(editor: LexicalEditor, listType: ListType): void { if ($isRootOrShadowRoot(anchorNodeParent)) { anchorNode.replace(list); - const listItem = $createListItemNode( - listType === 'check' ? false : undefined, - ); + const listItem = $createListItemNode(); if ($isElementNode(anchorNode)) { listItem.setFormat(anchorNode.getFormatType()); listItem.setIndent(anchorNode.getIndent()); @@ -158,9 +156,7 @@ function createListOrMerge(node: ElementNode, listType: ListType): ListNode { const previousSibling = node.getPreviousSibling(); const nextSibling = node.getNextSibling(); - const listItem = $createListItemNode( - listType === 'check' ? false : undefined, - ); + const listItem = $createListItemNode(); listItem.setFormat(node.getFormatType()); listItem.setIndent(node.getIndent()); append(listItem, node.getChildren()); From 35eae1c5ba7bdb56eca08a1b5d42f771b23c080f Mon Sep 17 00:00:00 2001 From: Aman Harwara Date: Sun, 12 May 2024 23:12:04 +0530 Subject: [PATCH 3/4] refactor implementation --- packages/lexical-list/src/LexicalListItemNode.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/packages/lexical-list/src/LexicalListItemNode.ts b/packages/lexical-list/src/LexicalListItemNode.ts index ca9b8013508..c2c20dcf646 100644 --- a/packages/lexical-list/src/LexicalListItemNode.ts +++ b/packages/lexical-list/src/LexicalListItemNode.ts @@ -6,7 +6,7 @@ * */ -import type {ListNode} from './'; +import type {ListNode, ListType} from './'; import type { BaseSelection, DOMConversionMap, @@ -321,11 +321,12 @@ export class ListItemNode extends ElementNode { getChecked(): boolean | undefined { const self = this.getLatest(); + let listType: ListType | undefined; + const parent = this.getParent(); - if (!$isListNode(parent)) { - invariant(false, 'getChecked: list node is not parent of list item node'); + if ($isListNode(parent)) { + listType = parent.getListType(); } - const listType = parent.getListType(); return listType === 'check' ? Boolean(self.__checked) : undefined; } From 8e9a96650395350d1167675a67fde0854c32e67c Mon Sep 17 00:00:00 2001 From: Aman Harwara Date: Wed, 5 Jun 2024 14:22:46 +0530 Subject: [PATCH 4/4] fix: Make sure list item __checked value gets unset if parent list type changes to bullet or number --- packages/lexical-list/src/formatList.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/packages/lexical-list/src/formatList.ts b/packages/lexical-list/src/formatList.ts index 565887c57d5..6d4a5cb41b5 100644 --- a/packages/lexical-list/src/formatList.ts +++ b/packages/lexical-list/src/formatList.ts @@ -297,7 +297,7 @@ export function updateChildrenListItemValue(list: ListNode): void { if (child.getValue() !== value) { child.setValue(value); } - if (isNotChecklist && child.getChecked() != null) { + if (isNotChecklist && child.getLatest().__checked != null) { child.setChecked(undefined); } if (!$isListNode(child.getFirstChild())) {