Skip to content

Commit

Permalink
fix: deletebackword list
Browse files Browse the repository at this point in the history
  • Loading branch information
felixfeng33 committed Mar 25, 2024
1 parent 424dd4e commit 5e35643
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 1 deletion.
3 changes: 3 additions & 0 deletions packages/select/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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:^"
},
Expand Down
1 change: 1 addition & 0 deletions packages/select/src/queries/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './isIndentElement';
5 changes: 5 additions & 0 deletions packages/select/src/queries/isIndentElement.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import { isDefined, isElement, TElement, Value } from '@udecode/plate-common';

export const isIndentElement = <V extends Value>(element: TElement) => {
return isElement(element) && isDefined(element.listStyleType);
};
48 changes: 48 additions & 0 deletions packages/select/src/transforms/deleteWhenEmpty.ts
Original file line number Diff line number Diff line change
@@ -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 = <V extends Value>(
editor: PlateEditor<V>,
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;
}
};
1 change: 1 addition & 0 deletions packages/select/src/transforms/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './deleteWhenEmpty';
4 changes: 3 additions & 1 deletion packages/select/src/withSelectOnBackspace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down

0 comments on commit 5e35643

Please sign in to comment.