Skip to content

Commit

Permalink
Show warning if Block to delete is not found (#1111)
Browse files Browse the repository at this point in the history
Resolves #1102
  • Loading branch information
gohabereg authored Apr 25, 2020
1 parent 2997ed0 commit 4a81699
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
2 changes: 1 addition & 1 deletion dist/editor.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions docs/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- `Improvements` - TSLint (deprecated) replaced with ESLint, old config changed to [CodeX ESLint Config](https://github.com/codex-team/eslint-config).
- `Improvements` - Fix many code-style issues, add missed annotations.
- `Improvements` - Adjusted GitHub action for ESLint.
- `Improvements` - API: If `blocks.delete` method is called, but no Block is selected, show warning instead of throwing an error [#1102](https://github.com/codex-team/editor.js/issues/1102)
- `Fix` - Editor's styles won't be appended to the `<head>` when another instance have already do that [#1079](https://github.com/codex-team/editor.js/issues/1079)
- `New` *I18n API* — Ability to provide internalization for Editor.js core and tools. [#751](https://github.com/codex-team/editor.js/issues/751)
- `Fix` - Fixed wrong toolbar icon centering in Firefox [#1120](https://github.com/codex-team/editor.js/pull/1120)
Expand Down
8 changes: 7 additions & 1 deletion src/components/modules/api/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,13 @@ export default class BlocksAPI extends Module {
* @param {number} blockIndex - index of Block to delete
*/
public delete(blockIndex?: number): void {
this.Editor.BlockManager.removeBlock(blockIndex);
try {
this.Editor.BlockManager.removeBlock(blockIndex);
} catch (e) {
_.logLabeled(e, 'warn');

return;
}

/**
* in case of last block deletion
Expand Down
11 changes: 8 additions & 3 deletions src/components/modules/blockManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,11 +348,16 @@ export default class BlockManager extends Module {
* Remove block with passed index or remove last
*
* @param {number|null} index - index of Block to remove
* @throws {Error} if Block to remove is not found
*/
public removeBlock(index?: number): void {
if (index === undefined) {
index = this.currentBlockIndex;
public removeBlock(index = this.currentBlockIndex): void {
/**
* If index is not passed and there is no block selected, show a warning
*/
if (!this.validateIndex(index)) {
throw new Error('Can\'t find a Block to remove');
}

this._blocks.remove(index);

if (this.currentBlockIndex >= index) {
Expand Down

0 comments on commit 4a81699

Please sign in to comment.