From aa5a3d05f3cd7adc3ab7f1aa719113d31716b362 Mon Sep 17 00:00:00 2001 From: Tomoyuki Hata <7702653+hata6502@users.noreply.github.com> Date: Fri, 9 Apr 2021 03:38:08 +0900 Subject: [PATCH] Create a new block when clicked at the bottom (#1605) * Create a new block when clicked at the bottom * Fix for 2.20.0 Co-authored-by: George Berezhnoy --- docs/CHANGELOG.md | 1 + src/components/modules/ui.ts | 22 ++++++++++++++++++++-- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/docs/CHANGELOG.md b/docs/CHANGELOG.md index 32363be5..ceeb9684 100644 --- a/docs/CHANGELOG.md +++ b/docs/CHANGELOG.md @@ -2,6 +2,7 @@ ### 2.20.1 +- `Fix` - Create a new block when clicked at the bottom [#1588](https://github.com/codex-team/editor.js/issues/1588). - `Fix` — Fix sanitisation problem with Inline Tools [#1631](https://github.com/codex-team/editor.js/issues/1631) - `Refactoring` - The Sanitizer module is util now. - `Refactoring` - Tooltip module is util now. diff --git a/src/components/modules/ui.ts b/src/components/modules/ui.ts index bc81516c..8719d6a9 100644 --- a/src/components/modules/ui.ts +++ b/src/components/modules/ui.ts @@ -691,10 +691,28 @@ export default class UI extends Module { return; } - if (!this.Editor.BlockManager.currentBlock) { + const isClickedBottom = event.target instanceof Element && event.target.isEqualNode(this.nodes.redactor); + + if (isClickedBottom) { stopPropagation(); - this.Editor.BlockManager.insert(); + const { BlockManager, Caret, Toolbar } = this.Editor; + + /** + * Insert a default-block at the bottom if: + * - last-block is not a default-block (Text) + * to prevent unnecessary tree-walking on Tools with many nodes (for ex. Table) + * - Or, default-block is not empty + */ + if (!BlockManager.lastBlock.tool.isDefault || !BlockManager.lastBlock.isEmpty) { + BlockManager.insertAtEnd(); + } + + /** + * Set the caret and toolbar to empty Block + */ + Caret.setToTheLastBlock(); + Toolbar.move(); } /**