Skip to content

Commit

Permalink
Merge pull request #91 from editor-js/support-max-level
Browse files Browse the repository at this point in the history
Feat(properties): support maxLevel property for list
  • Loading branch information
e11sy authored Oct 28, 2024
2 parents 307277c + 20cb12d commit bb6781e
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 0 deletions.
1 change: 1 addition & 0 deletions index.html
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
data: {
style: 'ordered',
start: 2,
maxLevel: 3,
items: [
{
content: "Canon",
Expand Down
42 changes: 42 additions & 0 deletions src/ListTabulator/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,34 @@ export default class ListTabulator<Renderer extends ListRenderer> {
return currentNode.closest(`.${DefaultListCssClasses.item}`);
}

/**
* Method that returns nesting level of the current item, null if there is no selection
*/
private get currentItemLevel(): number | null {
const currentItem = this.currentItem;

if (currentItem === null) {
return null;
}

let parentNode = currentItem.parentNode;

let levelCounter = 0;

while (parentNode !== null && parentNode !== this.listWrapper) {
if (isHtmlElement(parentNode) && parentNode.classList.contains(DefaultListCssClasses.item)) {
levelCounter += 1;
}

parentNode = parentNode.parentNode;
}

/**
* Level counter is number of the parent element, so it should be increased by one
*/
return levelCounter + 1;
}

/**
* Assign all passed params and renderer to relevant class properties
* @param params - tool constructor options
Expand Down Expand Up @@ -869,6 +897,20 @@ export default class ListTabulator<Renderer extends ListRenderer> {
return;
}

/**
* Check that maxLevel specified in config
*/
if (this.data.maxLevel !== undefined) {
const currentItemLevel = this.currentItemLevel;

/**
* Check that current item is not in the maximum nesting level
*/
if (currentItemLevel !== null && currentItemLevel === this.data.maxLevel) {
return;
}
}

/**
* Check that the item has potential parent
* Previous sibling is potential parent in case of adding tab
Expand Down
5 changes: 5 additions & 0 deletions src/types/ListParams.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ export interface ListData {
* Start property used only in ordered list
*/
start?: number;
/**
* Max level of the nesting in list
* If nesting is not needed, it could be set to 1
*/
maxLevel?: number;
}

/**
Expand Down

0 comments on commit bb6781e

Please sign in to comment.