Skip to content

Commit

Permalink
split list feature
Browse files Browse the repository at this point in the history
resolves #30
  • Loading branch information
talyguryn committed Jan 21, 2022
1 parent 8375ae1 commit 73c71ae
Showing 1 changed file with 101 additions and 6 deletions.
107 changes: 101 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,15 +412,48 @@ export default class NestedList {
const isEmpty = this.getItemContent(currentItem).trim().length === 0;
const isFirstLevelItem = currentItem.parentNode === this.nodes.wrapper;
const isLastItem = currentItem.nextElementSibling === null;
const haveNoChilds = !currentItem.querySelector(`.${this.CSS.itemChildren}`);

if (isFirstLevelItem && isLastItem && isEmpty) {
this.getOutOfList();
if (isEmpty && haveNoChilds) {
/**
* Get out of list
*
* 1. first item 1. first item
* 2. second item => 2. second item
* 3. | |
*/
if (isFirstLevelItem && isLastItem) {
this.getOutOfList();

return;
} else if (isLastItem && isEmpty) {
this.unshiftItem();
return;
}

return;

/**
* Split list into two lists
*
* 1. first item 1. first item
* 2. | => |
* 3. third item 1. third item
*/
if (isFirstLevelItem) {
this.splitList();

return;
}

/**
* Unshift item
*
* 1. first item 1. first item
* 2. second item => 2. second item
* 2.1. |child item 3. child item
*/
if (isLastItem) {
this.unshiftItem();

return;
}
}

/**
Expand Down Expand Up @@ -530,6 +563,68 @@ export default class NestedList {
this.api.caret.setToBlock(this.api.blocks.getCurrentBlockIndex());
}

/**
* Split list by Enter on the empty first-level non-last item
*
* @returns {void}
*/
splitList() {
/**
* Get current item
* @type {Element}
*/
const currentItem = this.currentItem;

/**
* Get first-level list parent item wrapper
*/
const parentItem = currentItem.parentNode.closest(`.${this.CSS.wrapper}`);

/**
* Get first-leveled items
*/
const parentChildrenList = Array.from(parentItem.querySelectorAll(`:scope > .${this.CSS.item}`));

/**
* Detect currentItem position
*/
const currentItemPosition = parentChildrenList.indexOf(currentItem);

/**
* Get list of items to be moved to the new block
*/
const childsToBeMoved = parentChildrenList.slice(currentItemPosition + 1);

/**
* Get raw data for this list
* @type {ListData}
*/
const rawData = this.save();

/**
* Remove first items and current item from data
* @type {ListItem[]}
*/
rawData.items = rawData.items.slice(currentItemPosition + 1);

/**
* Remove childs from current list
*/
childsToBeMoved.forEach(item => {
item.remove();
})

/**
* Remove current item, create an empty paragraph and focus it
*/
this.getOutOfList();

/**
* Create a new list with saved data
*/
this.api.blocks.insert('nestedList', rawData); // @todo: get tool name from variable
}

/**
* Handle backspace
*
Expand Down

0 comments on commit 73c71ae

Please sign in to comment.