* Remove passed Block * * @param block - Block to remove * @param addLastBlock - if true, adds new default block at the end. @todo remove this logic and use event-bus instead
(block: Block, addLastBlock = true)
| 523 | * @param addLastBlock - if true, adds new default block at the end. @todo remove this logic and use event-bus instead |
| 524 | */ |
| 525 | public removeBlock(block: Block, addLastBlock = true): Promise<void> { |
| 526 | return new Promise((resolve) => { |
| 527 | const index = this._blocks.indexOf(block); |
| 528 | |
| 529 | /** |
| 530 | * If index is not passed and there is no block selected, show a warning |
| 531 | */ |
| 532 | if (!this.validateIndex(index)) { |
| 533 | throw new Error('Can\'t find a Block to remove'); |
| 534 | } |
| 535 | |
| 536 | this._blocks.remove(index); |
| 537 | block.destroy(); |
| 538 | |
| 539 | /** |
| 540 | * Force call of didMutated event on Block removal |
| 541 | */ |
| 542 | this.blockDidMutated(BlockRemovedMutationType, block, { |
| 543 | index, |
| 544 | }); |
| 545 | |
| 546 | if (this.currentBlockIndex >= index) { |
| 547 | this.currentBlockIndex--; |
| 548 | } |
| 549 | |
| 550 | /** |
| 551 | * If first Block was removed, insert new Initial Block and set focus on it`s first input |
| 552 | */ |
| 553 | if (!this.blocks.length) { |
| 554 | this.unsetCurrentBlock(); |
| 555 | |
| 556 | if (addLastBlock) { |
| 557 | this.insert(); |
| 558 | } |
| 559 | } else if (index === 0) { |
| 560 | this.currentBlockIndex = 0; |
| 561 | } |
| 562 | |
| 563 | resolve(); |
| 564 | }); |
| 565 | } |
| 566 | |
| 567 | /** |
| 568 | * Remove only selected Blocks |
no test coverage detected