From 52fa52faf667a5508742e03c37f201aa6344f356 Mon Sep 17 00:00:00 2001 From: Peter Savchenko Date: Sat, 31 Aug 2024 04:59:56 +0300 Subject: [PATCH] unhardcode paragraph --- packages/core/src/components/BlockManager.ts | 10 +++++++++- .../dom-adapters/src/BlockToolAdapter/index.ts | 18 +++++++++++++++--- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/packages/core/src/components/BlockManager.ts b/packages/core/src/components/BlockManager.ts index cc668dc..260972a 100644 --- a/packages/core/src/components/BlockManager.ts +++ b/packages/core/src/components/BlockManager.ts @@ -159,7 +159,15 @@ export class BlocksManager { throw new Error('[BlockManager] Block index should be defined. Probably something wrong with the Editor Model. Please, report this issue'); } - const blockToolAdapter = new BlockToolAdapter(this.#model, this.#caretAdapter, index.blockIndex, this.#formattingAdapter); + const toolName = event.detail.data.name; + + const blockToolAdapter = new BlockToolAdapter( + this.#model, + this.#caretAdapter, + index.blockIndex, + this.#formattingAdapter, + toolName + ); const tool = this.#toolsManager.blockTools.get(data.name); diff --git a/packages/dom-adapters/src/BlockToolAdapter/index.ts b/packages/dom-adapters/src/BlockToolAdapter/index.ts index 621fed7..0f4be31 100644 --- a/packages/dom-adapters/src/BlockToolAdapter/index.ts +++ b/packages/dom-adapters/src/BlockToolAdapter/index.ts @@ -49,6 +49,11 @@ export class BlockToolAdapter implements BlockToolAdapterInterface { */ #formattingAdapter: FormattingAdapter; + /** + * Name of the tool that this adapter is connected to + */ + #toolName: string; + /** * BlockToolAdapter constructor * @@ -56,12 +61,14 @@ export class BlockToolAdapter implements BlockToolAdapterInterface { * @param caretAdapter - CaretAdapter instance * @param blockIndex - index of the block that this adapter is connected to * @param formattingAdapter - needed to render formatted text + * @param toolName - tool name of the block */ - constructor(model: EditorJSModel, caretAdapter: CaretAdapter, blockIndex: number, formattingAdapter: FormattingAdapter) { + constructor(model: EditorJSModel, caretAdapter: CaretAdapter, blockIndex: number, formattingAdapter: FormattingAdapter, toolName: string) { this.#model = model; this.#blockIndex = blockIndex; this.#caretAdapter = caretAdapter; this.#formattingAdapter = formattingAdapter; + this.#toolName = toolName; } /** @@ -311,7 +318,7 @@ export class BlockToolAdapter implements BlockToolAdapterInterface { case InputType.InsertParagraph: this.#handleSplit(key, start, end); - + break; case InputType.InsertLineBreak: /** * @todo Think if we need to keep that or not @@ -319,6 +326,7 @@ export class BlockToolAdapter implements BlockToolAdapterInterface { if (isInputNative === true) { this.#model.insertText(this.#blockIndex, key, '\n', start); } + break; default: } }; @@ -338,7 +346,7 @@ export class BlockToolAdapter implements BlockToolAdapterInterface { this.#model.removeText(this.#blockIndex, key, start, currentValue.length); this.#model.addBlock({ - name: 'paragraph', + name: this.#toolName, data : { [key]: { $t: 't', @@ -347,6 +355,10 @@ export class BlockToolAdapter implements BlockToolAdapterInterface { } } }, this.#blockIndex + 1); + + /** + * Raf is needed to ensure that the new block is added so caret can be moved to it + */ requestAnimationFrame(() => { this.#caretAdapter.updateIndex( new IndexBuilder()