From 07aea1f73f7a0a0df9ecadb7f1c4116a63c02365 Mon Sep 17 00:00:00 2001 From: natamox Date: Mon, 14 Oct 2024 17:26:53 +0800 Subject: [PATCH 1/5] Fixed the abnormal behavior of insertBreak when there is listRestart in the indented list node --- .../indent-list/src/lib/withIndentList.ts | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/packages/indent-list/src/lib/withIndentList.ts b/packages/indent-list/src/lib/withIndentList.ts index 0e9a33a511..3213de3826 100644 --- a/packages/indent-list/src/lib/withIndentList.ts +++ b/packages/indent-list/src/lib/withIndentList.ts @@ -11,6 +11,7 @@ import { BaseIndentPlugin } from '@udecode/plate-indent'; import { type BaseIndentListConfig, BaseIndentListPlugin, + INDENT_LIST_KEYS, } from './BaseIndentListPlugin'; import { shouldMergeNodesRemovePrevNodeIndentList, @@ -110,8 +111,53 @@ export const withIndentList: ExtendEditor = ({ } } + // When inserting a line break, normalize listStart if the node has a listRestart property + if ( + operation.type === 'split_node' && + (operation.properties as any)[BaseIndentListPlugin.key] && + (operation.properties as any)[INDENT_LIST_KEYS.listRestart] + ) { + const listReStart = (operation.properties as any)[ + INDENT_LIST_KEYS.listRestart + ]; + + (operation.properties as any)[INDENT_LIST_KEYS.listStart] = + listReStart + 1; + (operation.properties as any)[INDENT_LIST_KEYS.listRestart] = undefined; + + const node = getNode(editor, path); + + if (node) { + const nextNodeEntryBefore = getNextIndentList( + editor, + [node, path], + getSiblingIndentListOptions + ); + + if (nextNodeEntryBefore) { + nextIndentListPathRef = createPathRef(editor, nextNodeEntryBefore[1]); + } + } + } + apply(operation); + if (operation.type === 'split_node' && nextIndentListPathRef) { + const nextPath = nextIndentListPathRef.unref(); + + if (nextPath) { + const nextNode = getNode(editor, nextPath); + + if (nextNode) { + normalizeIndentListStart( + editor, + [nextNode, nextPath], + getSiblingIndentListOptions + ); + } + } + } + if (operation.type === 'merge_node') { const { properties } = operation; From dc3c4913af96eb5d084201d7c191d3d1eab5d307 Mon Sep 17 00:00:00 2001 From: natamox Date: Mon, 14 Oct 2024 18:53:12 +0800 Subject: [PATCH 2/5] feat: add breakOnListRestart option, early breakQuery execution & add current node parameters --- .../src/lib/queries/getSiblingIndentList.ts | 23 +++++++++++++++---- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/packages/indent-list/src/lib/queries/getSiblingIndentList.ts b/packages/indent-list/src/lib/queries/getSiblingIndentList.ts index dcf0bb13c6..ed6fda531d 100644 --- a/packages/indent-list/src/lib/queries/getSiblingIndentList.ts +++ b/packages/indent-list/src/lib/queries/getSiblingIndentList.ts @@ -9,7 +9,10 @@ import { } from '@udecode/plate-common'; import { BaseIndentPlugin } from '@udecode/plate-indent'; -import { BaseIndentListPlugin } from '../BaseIndentListPlugin'; +import { + BaseIndentListPlugin, + INDENT_LIST_KEYS, +} from '../BaseIndentListPlugin'; export interface GetSiblingIndentListOptions< N extends ElementOf, @@ -21,13 +24,14 @@ export interface GetSiblingIndentListOptions< getPreviousEntry?: ( entry: TNodeEntry> ) => TNodeEntry | undefined; + breakOnListRestart?: boolean; breakOnEqIndentNeqListStyleType?: boolean; breakOnLowerIndent?: boolean; - breakQuery?: (siblingNode: TNode) => boolean | undefined; + breakQuery?: (siblingNode: TNode, currentNode: TNode) => boolean | undefined; /** Query to break lookup */ eqIndent?: boolean; /** Query to validate lookup. If false, check the next sibling. */ - query?: (siblingNode: TNode) => boolean | undefined; + query?: (siblingNode: TNode, currentNode: TNode) => boolean | undefined; } /** @@ -42,6 +46,7 @@ export const getSiblingIndentList = < [node, path]: ElementEntryOf, { breakOnEqIndentNeqListStyleType = true, + breakOnListRestart = false, breakOnLowerIndent = true, breakQuery, eqIndent = true, @@ -65,8 +70,16 @@ export const getSiblingIndentList = < const indent = (node as any)[BaseIndentPlugin.key] as number; const nextIndent = (nextNode as any)[BaseIndentPlugin.key] as number; + if (breakQuery?.(nextNode, node)) return; if (!isDefined(nextIndent)) return; - if (breakQuery?.(nextNode)) return; + if (breakOnListRestart) { + if (getPreviousEntry && (node as any)[INDENT_LIST_KEYS.listRestart]) { + return; + } + if (getNextEntry && (nextNode as any)[INDENT_LIST_KEYS.listRestart]) { + return; + } + } if (breakOnLowerIndent && nextIndent < indent) return; if ( breakOnEqIndentNeqListStyleType && @@ -76,7 +89,7 @@ export const getSiblingIndentList = < ) return; - let valid = !query || query(nextNode as TNode); + let valid = !query || query(nextNode, node); if (valid) { valid = !eqIndent || nextIndent === indent; From 25a2f0c29695863fa38cfd3c4aa3298c80929033 Mon Sep 17 00:00:00 2001 From: natamox Date: Mon, 14 Oct 2024 18:54:05 +0800 Subject: [PATCH 3/5] feat: enable it to pass getSiblingIndentListOptions --- .../src/lib/transforms/toggleIndentList.ts | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/packages/indent-list/src/lib/transforms/toggleIndentList.ts b/packages/indent-list/src/lib/transforms/toggleIndentList.ts index 38bc22ef45..9ba170bc5c 100644 --- a/packages/indent-list/src/lib/transforms/toggleIndentList.ts +++ b/packages/indent-list/src/lib/transforms/toggleIndentList.ts @@ -1,4 +1,6 @@ import { + ElementEntryOf, + ElementOf, type SlateEditor, type TElement, getBlockAbove, @@ -22,15 +24,20 @@ import { setIndentListNodes } from './setIndentListNodes'; import { setIndentListSiblingNodes } from './setIndentListSiblingNodes'; import { toggleIndentListSet } from './toggleIndentListSet'; import { toggleIndentListUnset } from './toggleIndentListUnset'; +import { GetSiblingIndentListOptions } from '../queries'; /** Toggle indent list. */ -export const toggleIndentList = ( +export const toggleIndentList = < + N extends ElementOf, + E extends SlateEditor = SlateEditor, +>( editor: E, - options: IndentListOptions + options: IndentListOptions, + getSiblingIndentListOptions?: GetSiblingIndentListOptions ) => { const { listStyleType } = options; - const { getSiblingIndentListOptions } = + const { getSiblingIndentListOptions: _getSiblingIndentListOptions } = editor.getOptions(BaseIndentListPlugin); if (isCollapsed(editor.selection)) { @@ -44,8 +51,9 @@ export const toggleIndentList = ( return; } - setIndentListSiblingNodes(editor, entry, { - getSiblingIndentListOptions, + setIndentListSiblingNodes(editor, entry as ElementEntryOf, { + ..._getSiblingIndentListOptions, + ...getSiblingIndentListOptions, listStyleType, }); From 2701923dd0e52a2c11da1279c037be5c57547fc1 Mon Sep 17 00:00:00 2001 From: natamox Date: Tue, 15 Oct 2024 01:18:59 +0800 Subject: [PATCH 4/5] fix --- .../src/lib/transforms/toggleIndentList.ts | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/packages/indent-list/src/lib/transforms/toggleIndentList.ts b/packages/indent-list/src/lib/transforms/toggleIndentList.ts index 9ba170bc5c..0b89ca882c 100644 --- a/packages/indent-list/src/lib/transforms/toggleIndentList.ts +++ b/packages/indent-list/src/lib/transforms/toggleIndentList.ts @@ -1,6 +1,6 @@ import { - ElementEntryOf, - ElementOf, + type ElementEntryOf, + type ElementOf, type SlateEditor, type TElement, getBlockAbove, @@ -13,6 +13,7 @@ import { } from '@udecode/plate-common'; import { BaseIndentPlugin } from '@udecode/plate-indent'; +import type { GetSiblingIndentListOptions } from '../queries'; import type { IndentListOptions } from './indentList'; import { @@ -24,7 +25,6 @@ import { setIndentListNodes } from './setIndentListNodes'; import { setIndentListSiblingNodes } from './setIndentListSiblingNodes'; import { toggleIndentListSet } from './toggleIndentListSet'; import { toggleIndentListUnset } from './toggleIndentListUnset'; -import { GetSiblingIndentListOptions } from '../queries'; /** Toggle indent list. */ export const toggleIndentList = < @@ -52,8 +52,10 @@ export const toggleIndentList = < } setIndentListSiblingNodes(editor, entry as ElementEntryOf, { - ..._getSiblingIndentListOptions, - ...getSiblingIndentListOptions, + getSiblingIndentListOptions: { + ..._getSiblingIndentListOptions, + ...getSiblingIndentListOptions, + } as GetSiblingIndentListOptions, E>, listStyleType, }); From 4f0ec4b02d70848423d535b3fa16e9e1a2b62421 Mon Sep 17 00:00:00 2001 From: natamox <120485290+natamox@users.noreply.github.com> Date: Tue, 15 Oct 2024 01:21:19 +0800 Subject: [PATCH 5/5] Create silly-seas-compare.md --- .changeset/silly-seas-compare.md | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 .changeset/silly-seas-compare.md diff --git a/.changeset/silly-seas-compare.md b/.changeset/silly-seas-compare.md new file mode 100644 index 0000000000..67fdacf4b5 --- /dev/null +++ b/.changeset/silly-seas-compare.md @@ -0,0 +1,5 @@ +--- +"@udecode/plate-indent-list": patch +--- + +Fix the problem that the sequence number does not continue when inserting a line break in the listRestart node.