Skip to content
This repository has been archived by the owner on Oct 25, 2022. It is now read-only.

Commit

Permalink
Improved logic for: selection is on the last list item in a list
Browse files Browse the repository at this point in the history
  • Loading branch information
silviubogan committed Jul 3, 2020
1 parent db1934c commit 7e60509
Showing 1 changed file with 15 additions and 13 deletions.
28 changes: 15 additions & 13 deletions src/TextBlock/decorators/withList.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,29 @@ const isBlockTextEmpty = (node) => {
return Text.isText(lastChild) && !lastChild.text.length;
};

const thereIsNoListItemBelowSelection = (editor) => {
/**
* @param {Editor} editor
* @returns true if the cursor is on a list item after which there are no more list items
*/
const thereIsNoListItemBelowSelection = (editor, listItemPath) => {
let sel = editor.selection;
if (Range.isExpanded(sel)) {
Transforms.collapse(editor, { edge: 'start' });
}
// path of paragraph (TODO: what if there is no paragraph, but a nested list?)
let pg = Path.parent(sel.anchor.path);
// Path of list-item
let p = Path.parent(pg);
// Path of numbered/bulleted list
let pp = Path.parent(p);

// path of numbered/bulleted list that should be taken into account
let pp = Path.parent(listItemPath);

let indexInList = listItemPath[listItemPath.length - 1];

let listItems = Node.children(editor, pp);

for (let [, path] of listItems) {
if (Path.isAfter(path, p)) {
return false;
}
if (indexInList === Array.from(listItems).length - 1) {
// the list item with selection is the last in the list
return true;
}

return true;
return false;
};

const insertNewListItem = (
Expand Down Expand Up @@ -92,7 +94,7 @@ const handleBreakInListItem = (
*/
if (isStart) {
if (isBlockTextEmpty(paragraphNode)) {
if (thereIsNoListItemBelowSelection(editor)) {
if (thereIsNoListItemBelowSelection(editor, listItemPath)) {
simulateBackspaceAtEndOfEditor(editor);
const bottomBlockValue = settings.slate.defaultValue();
createAndSelectNewBlockAfter(bottomBlockValue);
Expand Down

0 comments on commit 7e60509

Please sign in to comment.