diff --git a/packages/model/src/EditorJSModel.ts b/packages/model/src/EditorJSModel.ts index ab23644e..eca41d02 100644 --- a/packages/model/src/EditorJSModel.ts +++ b/packages/model/src/EditorJSModel.ts @@ -196,17 +196,7 @@ export class EditorJSModel extends EventBus { * @param data - data to insert */ public insertData(index: Index, data: unknown): void { - switch (true) { - case index.isTextIndex: - this.#document.insertText(index.blockIndex!, index.dataKey!, data as string, index.textRange![0]); - break; - - case index.isBlockIndex: - // eslint-disable-next-line @typescript-eslint/no-magic-numbers - this.#document.addBlock(data as Parameters[0], index.blockIndex); - default: - throw new Error('Unsupported index'); - } + this.#document.insertData(index, data); } /** @@ -215,16 +205,7 @@ export class EditorJSModel extends EventBus { * @param index - index to remove data from */ public removeData(index: Index): void { - switch (true) { - case (index.blockIndex !== undefined && index.dataKey !== undefined && index.textRange !== undefined): - this.#document.removeText(index.blockIndex, index.dataKey, index.textRange[0], index.textRange[1]); - break; - - case (index.blockIndex !== undefined): - this.#document.removeBlock(index.blockIndex); - default: - throw new Error('Unsupported index'); - } + this.#document.removeData(index); } /** diff --git a/packages/model/src/entities/EditorDocument/index.ts b/packages/model/src/entities/EditorDocument/index.ts index 9e16d177..c582ea75 100644 --- a/packages/model/src/entities/EditorDocument/index.ts +++ b/packages/model/src/entities/EditorDocument/index.ts @@ -22,6 +22,7 @@ import { } from '../../EventBus/events/index.js'; import type { Constructor } from '../../utils/types.js'; import { BaseDocumentEvent } from '../../EventBus/events/BaseEvent.js'; +import { Index } from '../Index'; /** * EditorDocument class represents the top-level container for a tree-like structure of BlockNodes in an editor document. @@ -323,6 +324,44 @@ export class EditorDocument extends EventBus { return this.#children[blockIndex].getFragments(dataKey, start, end, tool); } + /** + * Inserts data to the specified index + * + * @param index - index to insert data + * @param data - data to insert + */ + public insertData(index: Index, data: unknown): void { + switch (true) { + case index.isTextIndex: + this.insertText(index.blockIndex!, index.dataKey!, data as string, index.textRange![0]); + break; + + case index.isBlockIndex: + // eslint-disable-next-line @typescript-eslint/no-magic-numbers + this.addBlock(data as Parameters[0], index.blockIndex); + default: + throw new Error('Unsupported index'); + } + } + + /** + * Removes data from the specified index + * + * @param index - index to remove data from + */ + public removeData(index: Index): void { + switch (true) { + case (index.blockIndex !== undefined && index.dataKey !== undefined && index.textRange !== undefined): + this.removeText(index.blockIndex, index.dataKey, index.textRange[0], index.textRange[1]); + break; + + case (index.blockIndex !== undefined): + this.removeBlock(index.blockIndex); + default: + throw new Error('Unsupported index'); + } + } + /** * Listens to BlockNode events and bubbles them to the EditorDocument *