Skip to content

Commit

Permalink
Merge pull request #3632 from udecode/fix/indent-list
Browse files Browse the repository at this point in the history
Fix/indent list
  • Loading branch information
zbeyens authored Oct 14, 2024
2 parents 3ef467d + 4f0ec4b commit 2fb3fea
Show file tree
Hide file tree
Showing 4 changed files with 84 additions and 10 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-seas-compare.md
Original file line number Diff line number Diff line change
@@ -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.
23 changes: 18 additions & 5 deletions packages/indent-list/src/lib/queries/getSiblingIndentList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<E>,
Expand All @@ -21,13 +24,14 @@ export interface GetSiblingIndentListOptions<
getPreviousEntry?: (
entry: TNodeEntry<ElementOrTextOf<E>>
) => TNodeEntry<N> | 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;
}

/**
Expand All @@ -42,6 +46,7 @@ export const getSiblingIndentList = <
[node, path]: ElementEntryOf<E>,
{
breakOnEqIndentNeqListStyleType = true,
breakOnListRestart = false,
breakOnLowerIndent = true,
breakQuery,
eqIndent = true,
Expand All @@ -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 &&
Expand All @@ -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;
Expand Down
20 changes: 15 additions & 5 deletions packages/indent-list/src/lib/transforms/toggleIndentList.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import {
type ElementEntryOf,
type ElementOf,
type SlateEditor,
type TElement,
getBlockAbove,
Expand All @@ -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 {
Expand All @@ -24,13 +27,17 @@ import { toggleIndentListSet } from './toggleIndentListSet';
import { toggleIndentListUnset } from './toggleIndentListUnset';

/** Toggle indent list. */
export const toggleIndentList = <E extends SlateEditor>(
export const toggleIndentList = <
N extends ElementOf<E>,
E extends SlateEditor = SlateEditor,
>(
editor: E,
options: IndentListOptions<E>
options: IndentListOptions<E>,
getSiblingIndentListOptions?: GetSiblingIndentListOptions<N, E>
) => {
const { listStyleType } = options;

const { getSiblingIndentListOptions } =
const { getSiblingIndentListOptions: _getSiblingIndentListOptions } =
editor.getOptions(BaseIndentListPlugin);

if (isCollapsed(editor.selection)) {
Expand All @@ -44,8 +51,11 @@ export const toggleIndentList = <E extends SlateEditor>(
return;
}

setIndentListSiblingNodes(editor, entry, {
getSiblingIndentListOptions,
setIndentListSiblingNodes(editor, entry as ElementEntryOf<E>, {
getSiblingIndentListOptions: {
..._getSiblingIndentListOptions,
...getSiblingIndentListOptions,
} as GetSiblingIndentListOptions<ElementOf<E>, E>,
listStyleType,
});

Expand Down
46 changes: 46 additions & 0 deletions packages/indent-list/src/lib/withIndentList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { BaseIndentPlugin } from '@udecode/plate-indent';
import {
type BaseIndentListConfig,
BaseIndentListPlugin,
INDENT_LIST_KEYS,
} from './BaseIndentListPlugin';
import {
shouldMergeNodesRemovePrevNodeIndentList,
Expand Down Expand Up @@ -110,8 +111,53 @@ export const withIndentList: ExtendEditor<BaseIndentListConfig> = ({
}
}

// 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<TElement>(editor, path);

if (node) {
const nextNodeEntryBefore = getNextIndentList<TElement>(
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<TElement>(editor, nextPath);

if (nextNode) {
normalizeIndentListStart<TElement>(
editor,
[nextNode, nextPath],
getSiblingIndentListOptions
);
}
}
}

if (operation.type === 'merge_node') {
const { properties } = operation;

Expand Down

0 comments on commit 2fb3fea

Please sign in to comment.