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: deletebackword list #3064

Closed
wants to merge 3 commits into from
Closed
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/lazy-files-lie.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@udecode/plate-indent-list": patch
---

fix the deletebackward behavior
Original file line number Diff line number Diff line change
@@ -1,36 +1,75 @@
import {
ELEMENT_DEFAULT,
getAboveNode,
getNodeString,
getPointBefore,
getPreviousNode,
isCollapsed,
isDefined,
isElement,
PlateEditor,
removeNodes,
select,
TElement,
unsetNodes,
Value,
} from '@udecode/plate-common';
import { TextUnit } from 'slate';
import { outdent } from '@udecode/plate-indent';
import Slate, { TextUnit, TextUnitAdjustment } from 'slate';

import { KEY_LIST_STYLE_TYPE } from '../createIndentListPlugin';
import { outdentList } from '../transforms';
import { isIndentElement } from '../queries';

export const deleteBackwardIndentList = <V extends Value>(
editor: PlateEditor<V>
) => {
const { deleteBackward } = editor;

return function (unit: TextUnit) {
deleteBackwardHelper(editor);
deleteBackward(unit);
// don't delete the indent list element when just a bullets numbering or checkbox should be turn to default paragraph
if (!deleteWhenEmpty(editor, unit)) {
return deleteBackward(unit);
}
};
};

function deleteBackwardHelper<V extends Value>(editor: PlateEditor<V>) {
if (isCollapsed(editor.selection)) {
const str = getNodeString(editor);
if (str) return;
const entry = getAboveNode(editor);
if (!entry) return;
const node = entry[0];
if (isDefined(node[KEY_LIST_STYLE_TYPE])) {
outdentList(editor);
const deleteWhenEmpty = <V extends Value>(
editor: PlateEditor<V>,
unit: TextUnitAdjustment
) => {
const { selection } = editor;

if (unit !== 'character' || !isCollapsed(selection)) return;

const pointBefore = getPointBefore(
editor,
editor.selection as Slate.Location,
{
unit,
}
);
if (!pointBefore) return;
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/indent-list/src/queries/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ export * from './getNextIndentList';
export * from './getPreviousIndentList';
export * from './getSiblingIndentList';
export * from './getSiblingListStyleType';
export * from './isIndentElement';
5 changes: 5 additions & 0 deletions packages/indent-list/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);
};
5 changes: 4 additions & 1 deletion packages/indent-list/src/withIndentList.spec.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ describe('normalizeIndentList', () => {
<hp indent={1} listStyleType="decimal">
1
</hp>
<hp indent={1} listStyleType="decimal" listStart={2}>
<hp>
<cursor />
</hp>
<hp indent={1} listStyleType="decimal">
1
</hp>
</editor>
Expand Down