From 0d3166c5c72988dc1d568315b2b2be9101db8dd8 Mon Sep 17 00:00:00 2001 From: Jonas Date: Wed, 28 Aug 2024 15:08:41 +0200 Subject: [PATCH] fix(editorApi): Add support for a onCreate callback that gets content Required for Collectives to update the reader content with editor content on initial load (i.e. before first editor update happens). Signed-off-by: Jonas --- src/components/Editor.vue | 4 ++++ src/editor.js | 9 +++++++++ 2 files changed, 13 insertions(+) diff --git a/src/components/Editor.vue b/src/components/Editor.vue index 21ef52c8f60..b9847f8ab00 100644 --- a/src/components/Editor.vue +++ b/src/components/Editor.vue @@ -514,6 +514,10 @@ export default { session, onCreate: ({ editor }) => { this.$syncService.startSync() + const proseMirrorMarkdown = this.$syncService.serialize(editor.state.doc) + this.emit('create:content', { + markdown: proseMirrorMarkdown, + }) }, onUpdate: ({ editor }) => { // this.debugContent(editor) diff --git a/src/editor.js b/src/editor.js index 19e06b47619..bf699d2702f 100644 --- a/src/editor.js +++ b/src/editor.js @@ -36,6 +36,13 @@ class TextEditorEmbed { return this.#vm.$children[0] } + onCreate(onCreateCallback = () => {}) { + this.#vm.$on('create:content', (content) => { + onCreateCallback(content) + }) + return this + } + onLoaded(onLoadedCallback = () => {}) { this.#vm.$on('ready', () => { onLoadedCallback() @@ -153,6 +160,7 @@ window.OCA.Text.createEditor = async function({ props: null, }, + onCreate = ({ markdown }) => {}, onLoaded = () => {}, onUpdate = ({ markdown }) => {}, onOutlineToggle = (visible) => {}, @@ -231,6 +239,7 @@ window.OCA.Text.createEditor = async function({ store, }) return new TextEditorEmbed(vm, data) + .onCreate(onCreate) .onLoaded(onLoaded) .onUpdate(onUpdate) .onOutlineToggle(onOutlineToggle)