X-Git-Url: http://source.bookstackapp.com/bookstack/blobdiff_plain/b80992ca59a4803fe81d577add6a0611e976c83b..refs/pull/5721/head:/resources/js/wysiwyg/index.ts diff --git a/resources/js/wysiwyg/index.ts b/resources/js/wysiwyg/index.ts index 8f6c41c1a..f572f9de5 100644 --- a/resources/js/wysiwyg/index.ts +++ b/resources/js/wysiwyg/index.ts @@ -123,6 +123,8 @@ export function createBasicEditorInstance(container: HTMLElement, htmlContent: s export class SimpleWysiwygEditorInterface { protected context: EditorUiContext; + protected onChangeListeners: (() => void)[] = []; + protected editorListenerTeardown: (() => void)|null = null; constructor(context: EditorUiContext) { this.context = context; @@ -132,12 +134,32 @@ export class SimpleWysiwygEditorInterface { return await getEditorContentAsHtml(this.context.editor); } + onChange(listener: () => void) { + this.onChangeListeners.push(listener); + this.startListeningToChanges(); + } + focus(): void { focusEditor(this.context.editor); } remove() { - this.context.editorDOM.remove(); this.context.manager.teardown(); + this.context.containerDOM.remove(); + if (this.editorListenerTeardown) { + this.editorListenerTeardown(); + } + } + + protected startListeningToChanges(): void { + if (this.editorListenerTeardown) { + return; + } + + this.editorListenerTeardown = this.context.editor.registerUpdateListener(() => { + for (const listener of this.onChangeListeners) { + listener(); + } + }); } } \ No newline at end of file