From a6709444df572dae187a9022394051417b092176 Mon Sep 17 00:00:00 2001 From: Peter Savchenko Date: Fri, 30 Aug 2024 23:38:57 +0300 Subject: [PATCH 1/6] playground uses editor --- packages/core/src/index.ts | 8 +- .../core/src/utils/composeDataFromVersion2.ts | 10 +- .../src/BlockToolAdapter/index.ts | 3 +- packages/model/src/EditorJSModel.spec.ts | 1 + packages/model/src/EditorJSModel.ts | 9 ++ packages/playground/src/App.vue | 138 +++++++----------- packages/playground/src/style.css | 3 +- 7 files changed, 82 insertions(+), 90 deletions(-) diff --git a/packages/core/src/index.ts b/packages/core/src/index.ts index 6b05ef52..e3ff1cd1 100644 --- a/packages/core/src/index.ts +++ b/packages/core/src/index.ts @@ -1,4 +1,4 @@ -import { EditorJSModel } from '@editorjs/model'; +import { EditorJSModel, EventType } from '@editorjs/model'; import type { ContainerInstance } from 'typedi'; import { Container } from 'typedi'; import { composeDataFromVersion2 } from './utils/composeDataFromVersion2.js'; @@ -94,6 +94,12 @@ export default class Core { this.#iocContainer.get(BlocksManager); + if (config.onModelUpdate !== undefined) { + this.#model.addEventListener(EventType.Changed, () => { + config.onModelUpdate?.(this.#model); + }); + } + this.#model.initializeDocument({ blocks }); } diff --git a/packages/core/src/utils/composeDataFromVersion2.ts b/packages/core/src/utils/composeDataFromVersion2.ts index 3249c2ce..972a8f1c 100644 --- a/packages/core/src/utils/composeDataFromVersion2.ts +++ b/packages/core/src/utils/composeDataFromVersion2.ts @@ -2,6 +2,14 @@ import type { OutputData } from '@editorjs/editorjs'; import type { InlineFragment } from '@editorjs/model'; import { createInlineToolData, createInlineToolName, TextNode, ValueNode, type BlockNodeSerialized } from '@editorjs/model'; +/** + * Removes HTML tags from the input string + * @param input - any string with HTML tags like 'bold link' + */ +function stripTags(input: string): string { + return input.replace(/<\/?[^>]+(>|$)/g, ''); +} + /** * Extracts inline fragments from the HTML string * @param html - any html string like 'bold link' @@ -97,7 +105,7 @@ export function composeDataFromVersion2(data: OutputData): { if (typeof value === 'string') { const fragments = extractFragments(value); const textNode = new TextNode({ - value, + value: stripTags(value), fragments, }); diff --git a/packages/dom-adapters/src/BlockToolAdapter/index.ts b/packages/dom-adapters/src/BlockToolAdapter/index.ts index 68f3c5b5..362d6441 100644 --- a/packages/dom-adapters/src/BlockToolAdapter/index.ts +++ b/packages/dom-adapters/src/BlockToolAdapter/index.ts @@ -91,8 +91,7 @@ export class BlockToolAdapter implements BlockToolAdapterInterface { const fragments = this.#model.getFragments(this.#blockIndex, key); fragments.forEach(fragment => { - console.log('fragment', fragment); - // this.#formattingAdapter.formatElementContent(input, fragment); + this.#formattingAdapter.formatElementContent(input, fragment); }); } diff --git a/packages/model/src/EditorJSModel.spec.ts b/packages/model/src/EditorJSModel.spec.ts index 7bd1af8b..6ac730a9 100644 --- a/packages/model/src/EditorJSModel.spec.ts +++ b/packages/model/src/EditorJSModel.spec.ts @@ -23,6 +23,7 @@ describe('EditorJSModel', () => { 'createCaret', 'updateCaret', 'removeCaret', + 'devModeGetDocument', ]; const ownProperties = Object.getOwnPropertyNames(EditorJSModel.prototype); diff --git a/packages/model/src/EditorJSModel.ts b/packages/model/src/EditorJSModel.ts index f90ab397..6a8bd13f 100644 --- a/packages/model/src/EditorJSModel.ts +++ b/packages/model/src/EditorJSModel.ts @@ -283,6 +283,15 @@ export class EditorJSModel extends EventBus { return this.#document.getFragments(...parameters); } + /** + * Exposing document for dev-tools + * + * USE ONLY FOR DEV PURPOSES + */ + public devModeGetDocument(): EditorDocument { + return this.#document; + } + /** * Listens to BlockNode events and bubbles re-emits them from the EditorJSModel instance * diff --git a/packages/playground/src/App.vue b/packages/playground/src/App.vue index 0c8d0ccc..b6343a7b 100644 --- a/packages/playground/src/App.vue +++ b/packages/playground/src/App.vue @@ -1,61 +1,21 @@