Skip to content

Commit

Permalink
fix: refetch li node after removing selection to retrieve the current…
Browse files Browse the repository at this point in the history
… path
  • Loading branch information
alex-vladut committed Dec 8, 2021
1 parent 2cb28c7 commit bdb3f53
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions docs/docs/plugins/list.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ npm install @udecode/plate-list-ui
createExitBreakPlugin(CONFIG.exitBreak),
createListPlugin(),
createTodoListPlugin(),
createDeserializeAstPlugin(),
], {
components: CONFIG.components,
});
Expand Down
17 changes: 12 additions & 5 deletions packages/elements/list/src/insertFragmentList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
TDescendant,
TElement,
} from '@udecode/plate-core';
import { Editor, Element, Node, NodeEntry, Path, Transforms } from 'slate';
import { Element, Node, NodeEntry, Path, Transforms } from 'slate';
import { ELEMENT_LI } from './createListPlugin';
import {
getListItemContentType,
Expand Down Expand Up @@ -112,7 +112,7 @@ export const insertFragmentList = (editor: PlateEditor) => {
) => {
const [, liPath] = liEntry;
const [licNode, licPath] = licEntry;
const isEmptyNode = Editor.isEmpty(editor, licNode as TElement);
const isEmptyNode = !Node.string(licNode);
const [first, ...rest] = fragment
.flatMap(trimList)
.map(wrapNodeIntoListItem);
Expand Down Expand Up @@ -165,7 +165,7 @@ export const insertFragmentList = (editor: PlateEditor) => {
};

return (fragment: TDescendant[]) => {
const liEntry = findNode(editor, {
let liEntry = findNode(editor, {
match: { type: listItemType },
mode: 'lowest',
});
Expand All @@ -179,6 +179,12 @@ export const insertFragmentList = (editor: PlateEditor) => {
// delete selection (if necessary) so that it can check if needs to insert into an empty block
Transforms.insertFragment(editor, [{ text: '' }]);

// refetch to find the currently selected LI after the deletion above is performed
liEntry = findNode(editor, {
match: { type: listItemType },
mode: 'lowest',
});

const licEntry = findNode(editor, {
match: { type: listItemContentType },
mode: 'lowest',
Expand All @@ -191,13 +197,14 @@ export const insertFragmentList = (editor: PlateEditor) => {

const { textNode, listItemNodes } = getTextAndListItemNodes(
fragment,
liEntry,
liEntry!,
licEntry
);

Transforms.insertFragment(editor, [textNode]); // insert text if needed

const [, liPath] = liEntry;
const [, liPath] = liEntry!;

return Transforms.insertNodes(editor, listItemNodes, {
at: Path.next(liPath),
select: true,
Expand Down

0 comments on commit bdb3f53

Please sign in to comment.