From 789b8a4a9313c26eebb9e2f8a954c47c571bd883 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 db83ddf172c..d3779093277 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 d60f7c9ce7e..2357c096fd8 100644 --- a/src/editor.js +++ b/src/editor.js @@ -52,6 +52,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() @@ -149,6 +156,7 @@ window.OCA.Text.createEditor = async function({ props: null, }, + onCreate = ({ markdown }) => {}, onLoaded = () => {}, onUpdate = ({ markdown }) => {}, onOutlineToggle = (visible) => {}, @@ -228,6 +236,7 @@ window.OCA.Text.createEditor = async function({ store, }) return new TextEditorEmbed(vm, data) + .onCreate(onCreate) .onLoaded(onLoaded) .onUpdate(onUpdate) .onOutlineToggle(onOutlineToggle)