Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/indent list #3632

Merged
merged 5 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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