diff --git a/src/App.tsx b/src/App.tsx index 1147100..2196701 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -7,6 +7,11 @@ class EditorJSWrapper { private holder: HTMLDivElement | null = null; private onChange!: (data: OutputData) => void; private static instance: EditorJSWrapper | null = null; + private currentData: OutputData = { + time: Date.now(), + blocks: [], + version: '2.28.2' + }; constructor(onChange: (data: OutputData) => void) { if (EditorJSWrapper.instance) { @@ -29,13 +34,9 @@ class EditorJSWrapper { this.editor = new EditorJS({ holder: holder, placeholder: 'Start writing...', + data: this.currentData, onChange: async () => { - try { - const content = await this.editor!.save(); - this.onChange(content); - } catch (err) { - console.error('Error saving editor content:', err); - } + await this.syncState(); } }); } catch (error) { @@ -43,13 +44,22 @@ class EditorJSWrapper { } } + private async syncState() { + if (!this.editor) return; + try { + this.currentData = await this.editor.save(); + this.onChange(this.currentData); + } catch (err) { + console.error('Error syncing state:', err); + } + } + async addBlock(text: string) { if (!this.editor) return; try { await this.editor.blocks.insert('paragraph', { text }); - const updatedData = await this.editor.save(); - this.onChange(updatedData); + await this.syncState(); } catch (err) { console.error('Error adding block:', err); } @@ -59,14 +69,19 @@ class EditorJSWrapper { if (!this.editor) return; try { await this.editor.clear(); + this.currentData = { + time: Date.now(), + blocks: [], + version: '2.28.2' + }; + this.onChange(this.currentData); } catch (err) { console.error('Error clearing editor:', err); } } async getData(): Promise { - if (!this.editor) throw new Error('Editor not initialized'); - return this.editor.save(); + return this.currentData; } async destroy() { @@ -75,6 +90,11 @@ class EditorJSWrapper { await this.editor.destroy(); this.editor = null; this.holder = null; + this.currentData = { + time: Date.now(), + blocks: [], + version: '2.28.2' + }; EditorJSWrapper.instance = null; } } catch (error: unknown) {