From: Alexander Ebert Date: Mon, 22 Jan 2024 12:15:15 +0000 (+0100) Subject: Add an event when the editor data has changed X-Git-Tag: 6.0.7_dev_1~9 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=bdecc09b7508a52f9aa3956e3c446c0cca1c7e04;p=GitHub%2FWoltLab%2FWCF.git Add an event when the editor data has changed --- diff --git a/ts/WoltLabSuite/Core/Component/Ckeditor.ts b/ts/WoltLabSuite/Core/Component/Ckeditor.ts index 45a55d3fbd..d48ebf4246 100644 --- a/ts/WoltLabSuite/Core/Component/Ckeditor.ts +++ b/ts/WoltLabSuite/Core/Component/Ckeditor.ts @@ -259,6 +259,12 @@ function hasToolbarButton(items: CKEditor5.Core.ToolbarConfigItem[], name: strin return false; } +function notifyOfDataChanges(editor: CKEditor5.ClassicEditor.ClassicEditor, element: HTMLElement): void { + editor.model.document.on("change:data", () => { + dispatchToCkeditor(element).changeData(); + }); +} + export async function setupCkeditor( element: HTMLElement, features: Features, @@ -320,6 +326,7 @@ export async function setupCkeditor( } setupSubmitShortcut(ckeditor); + notifyOfDataChanges(cke, element); const enableDebug = window.ENABLE_DEBUG_MODE && window.ENABLE_DEVELOPER_TOOLS; if (enableDebug && Devtools._internal_.editorInspector()) { diff --git a/ts/WoltLabSuite/Core/Component/Ckeditor/Event.ts b/ts/WoltLabSuite/Core/Component/Ckeditor/Event.ts index 8761e8321e..fd31fa3b15 100644 --- a/ts/WoltLabSuite/Core/Component/Ckeditor/Event.ts +++ b/ts/WoltLabSuite/Core/Component/Ckeditor/Event.ts @@ -19,6 +19,7 @@ import type { CKEditor5 } from "@woltlab/editor"; const enum EventNames { Autosave = "ckeditor5:autosave", Bbcode = "ckeditor5:bbcode", + ChangeData = "ckeditor5:change-data", CollectMetaData = "ckeditor5:collect-meta-data", Destroy = "ckeditor5:destroy", DiscardRecoveredData = "ckeditor5:discard-recovered-data", @@ -73,6 +74,10 @@ class EventDispatcher { ); } + changeData(): void { + this.#element.dispatchEvent(new CustomEvent(EventNames.ChangeData)); + } + destroy(): void { this.#element.dispatchEvent(new CustomEvent(EventNames.Destroy)); } @@ -192,6 +197,14 @@ class EventListener { return this; } + changeData(callback: () => void): this { + this.#element.addEventListener(EventNames.ChangeData, () => { + callback(); + }); + + return this; + } + collectMetaData(callback: (payload: CollectMetaDataEventPayload) => void): this { this.#element.addEventListener(EventNames.CollectMetaData, (event: CustomEvent) => { callback(event.detail);