From 5e356439d5da2bebc42a364a79ffca4ef2e2de37 Mon Sep 17 00:00:00 2001 From: felixfeng Date: Mon, 25 Mar 2024 15:52:05 +0800 Subject: [PATCH] fix: deletebackword list --- packages/select/package.json | 3 ++ packages/select/src/queries/index.ts | 1 + .../select/src/queries/isIndentElement.ts | 5 ++ .../select/src/transforms/deleteWhenEmpty.ts | 48 +++++++++++++++++++ packages/select/src/transforms/index.ts | 1 + packages/select/src/withSelectOnBackspace.ts | 4 +- yarn.lock | 1 + 7 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 packages/select/src/queries/index.ts create mode 100644 packages/select/src/queries/isIndentElement.ts create mode 100644 packages/select/src/transforms/deleteWhenEmpty.ts create mode 100644 packages/select/src/transforms/index.ts diff --git a/packages/select/package.json b/packages/select/package.json index b95136d562..fb2e2e5490 100644 --- a/packages/select/package.json +++ b/packages/select/package.json @@ -38,6 +38,9 @@ "test:watch": "yarn p:test:watch", "typecheck": "yarn p:typecheck" }, + "dependencies": { + "@udecode/plate-indent": "31.0.0" + }, "devDependencies": { "@udecode/plate-common": "workspace:^" }, diff --git a/packages/select/src/queries/index.ts b/packages/select/src/queries/index.ts new file mode 100644 index 0000000000..4fdb709218 --- /dev/null +++ b/packages/select/src/queries/index.ts @@ -0,0 +1 @@ +export * from './isIndentElement'; diff --git a/packages/select/src/queries/isIndentElement.ts b/packages/select/src/queries/isIndentElement.ts new file mode 100644 index 0000000000..6cd3329c97 --- /dev/null +++ b/packages/select/src/queries/isIndentElement.ts @@ -0,0 +1,5 @@ +import { isDefined, isElement, TElement, Value } from '@udecode/plate-common'; + +export const isIndentElement = (element: TElement) => { + return isElement(element) && isDefined(element.listStyleType); +}; diff --git a/packages/select/src/transforms/deleteWhenEmpty.ts b/packages/select/src/transforms/deleteWhenEmpty.ts new file mode 100644 index 0000000000..39c9ed6852 --- /dev/null +++ b/packages/select/src/transforms/deleteWhenEmpty.ts @@ -0,0 +1,48 @@ +import { + ELEMENT_DEFAULT, + getAboveNode, + getNodeString, + getPreviousNode, + isDefined, + isElement, + PlateEditor, + removeNodes, + select, + TElement, + unsetNodes, + Value, +} from '@udecode/plate-common'; +import { outdent } from '@udecode/plate-indent'; +import { Point } from 'slate'; + +import { isIndentElement } from '../queries'; + +export const deleteWhenEmpty = ( + editor: PlateEditor, + pointBefore: Point +) => { + const aboveEntry = getAboveNode(editor, { + match: (n) => isElement(n) && n.type === ELEMENT_DEFAULT, + }); + + const prevEntry = getPreviousNode(editor, { + match: (n) => n.type === ELEMENT_DEFAULT || n.type === 'blockquote', + }); + + if (!prevEntry || !aboveEntry) return; + + const [prevCell] = prevEntry; + const [aboveCell] = aboveEntry; + + if (!getNodeString(prevCell) && !isDefined(aboveCell.listStyleType)) { + removeNodes(editor); + select(editor, pointBefore); + return true; + } + + if (!getNodeString(aboveCell) && isIndentElement(aboveCell as TElement)) { + outdent(editor); + unsetNodes(editor, ['listStyleType', 'checked']); + return true; + } +}; diff --git a/packages/select/src/transforms/index.ts b/packages/select/src/transforms/index.ts new file mode 100644 index 0000000000..3d1c79427c --- /dev/null +++ b/packages/select/src/transforms/index.ts @@ -0,0 +1 @@ +export * from './deleteWhenEmpty'; diff --git a/packages/select/src/withSelectOnBackspace.ts b/packages/select/src/withSelectOnBackspace.ts index 6a4628fc10..d90f5a6dca 100644 --- a/packages/select/src/withSelectOnBackspace.ts +++ b/packages/select/src/withSelectOnBackspace.ts @@ -15,6 +15,7 @@ import { import Slate from 'slate'; import { SelectOnBackspacePlugin } from './createSelectOnBackspacePlugin'; +import { deleteWhenEmpty } from './transforms'; /** * Set a list of element types to select on backspace @@ -58,7 +59,8 @@ export const withSelectOnBackspace = < // don't delete image, set selection there select(editor, pointBefore); } else { - deleteBackward(unit); + // 1. don't delete the indent list element when just a bullets numbering or checkbox should be turn to default paragraph + if (!deleteWhenEmpty(editor, pointBefore)) deleteBackward(unit); } } else { deleteBackward(unit); diff --git a/yarn.lock b/yarn.lock index bc60a90a92..3c720e222e 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6906,6 +6906,7 @@ __metadata: resolution: "@udecode/plate-select@workspace:packages/select" dependencies: "@udecode/plate-common": "workspace:^" + "@udecode/plate-indent": "npm:31.0.0" peerDependencies: "@udecode/plate-common": ">=31.0.0" react: ">=16.8.0"