Skip to content

Commit

Permalink
Fix setCurrentBlockByChildNode for EditorJS nesting
Browse files Browse the repository at this point in the history
  • Loading branch information
t.hata committed Mar 10, 2020
1 parent 68f6b87 commit 03975f5
Showing 1 changed file with 13 additions and 3 deletions.
16 changes: 13 additions & 3 deletions src/components/modules/blockManager.ts
Expand Up @@ -515,14 +515,24 @@ export default class BlockManager extends Module {
childNode = childNode.parentNode;
}

const parentFirstLevelBlock = (childNode as HTMLElement).closest(`.${Block.CSS.wrapper}`);
/**
* Search parent Block
*/
let parentBlock: HTMLElement | null = childNode as HTMLElement;
let currentBlockIndex: number;

do {
parentBlock = parentBlock.closest(`.${Block.CSS.wrapper}`);
currentBlockIndex = this._blocks.nodes.indexOf(parentBlock);
parentBlock = parentBlock.parentElement;
}while (parentBlock !== null && currentBlockIndex === -1);

if (parentFirstLevelBlock) {
if (currentBlockIndex !== -1) {
/**
* Update current Block's index
* @type {number}
*/
this.currentBlockIndex = this._blocks.nodes.indexOf(parentFirstLevelBlock as HTMLElement);
this.currentBlockIndex = currentBlockIndex;
return this.currentBlock;
} else {
throw new Error('Can not find a Block from this child Node');
Expand Down

0 comments on commit 03975f5

Please sign in to comment.