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. 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; diff --git a/packages/indent-list/src/lib/transforms/toggleIndentList.ts b/packages/indent-list/src/lib/transforms/toggleIndentList.ts index 38bc22ef45..0b89ca882c 100644 --- a/packages/indent-list/src/lib/transforms/toggleIndentList.ts +++ b/packages/indent-list/src/lib/transforms/toggleIndentList.ts @@ -1,4 +1,6 @@ import { + type ElementEntryOf, + type ElementOf, type SlateEditor, type TElement, getBlockAbove, @@ -11,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,13 +27,17 @@ import { toggleIndentListSet } from './toggleIndentListSet'; import { toggleIndentListUnset } from './toggleIndentListUnset'; /** 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,11 @@ export const toggleIndentList = ( return; } - setIndentListSiblingNodes(editor, entry, { - getSiblingIndentListOptions, + setIndentListSiblingNodes(editor, entry as ElementEntryOf, { + getSiblingIndentListOptions: { + ..._getSiblingIndentListOptions, + ...getSiblingIndentListOptions, + } as GetSiblingIndentListOptions, E>, listStyleType, }); 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;