From 05ccc1a151d54b2522d77eeb60448d0cdbb022b2 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Thu, 6 Apr 2023 14:00:59 +0200 Subject: [PATCH] Improve the collection of message meta data Fixes #5393 --- .../Core/Component/Ckeditor/Event.ts | 21 ++++++++++++++++++- .../Core/Ui/Message/InlineEditor.ts | 3 ++- ts/WoltLabSuite/Core/Ui/Message/Reply.ts | 3 ++- wcfsetup/install/files/js/WCF.Attachment.js | 5 +++++ .../Core/Component/Ckeditor/Event.js | 11 ++++++++++ .../Core/Ui/Message/InlineEditor.js | 4 ++-- .../js/WoltLabSuite/Core/Ui/Message/Reply.js | 4 ++-- 7 files changed, 44 insertions(+), 7 deletions(-) diff --git a/ts/WoltLabSuite/Core/Component/Ckeditor/Event.ts b/ts/WoltLabSuite/Core/Component/Ckeditor/Event.ts index e92ebb69a1..9663087b3e 100644 --- a/ts/WoltLabSuite/Core/Component/Ckeditor/Event.ts +++ b/ts/WoltLabSuite/Core/Component/Ckeditor/Event.ts @@ -16,6 +16,7 @@ import type { UploadMediaEventPayload } from "./Media"; import type { InsertQuoteEventPayload } from "./Quote"; const enum EventNames { + CollectMetaData = "ckeditor5:collect-meta-data", Destroy = "ckeditor5:destroy", DiscardRecoveredData = "ckeditor5:discard-recovered-data", InsertAttachment = "ckeditor5:insert-attachment", @@ -29,7 +30,9 @@ const enum EventNames { UploadAttachment = "ckeditor5:upload-attachment", UploadMedia = "ckeditor5:upload-media", } - +type CollectMetaDataEventPayload = { + metaData: Record; +}; type ReadyEventPayload = { ckeditor: CKEditor; }; @@ -55,6 +58,14 @@ class EventDispatcher { this.#element = element; } + collectMetaData(payload: CollectMetaDataEventPayload): void { + this.#element.dispatchEvent( + new CustomEvent(EventNames.CollectMetaData, { + detail: payload, + }), + ); + } + destroy(): void { this.#element.dispatchEvent(new CustomEvent(EventNames.Destroy)); } @@ -151,6 +162,14 @@ class EventListener { this.#element = element; } + collectMetaData(callback: (payload: CollectMetaDataEventPayload) => void): this { + this.#element.addEventListener(EventNames.CollectMetaData, (event: CustomEvent) => { + callback(event.detail); + }); + + return this; + } + destroy(callback: () => void): this { this.#element.addEventListener(EventNames.Destroy, () => { callback(); diff --git a/ts/WoltLabSuite/Core/Ui/Message/InlineEditor.ts b/ts/WoltLabSuite/Core/Ui/Message/InlineEditor.ts index 3e33fc6202..2e7626372a 100644 --- a/ts/WoltLabSuite/Core/Ui/Message/InlineEditor.ts +++ b/ts/WoltLabSuite/Core/Ui/Message/InlineEditor.ts @@ -6,6 +6,7 @@ * @license GNU Lesser General Public License */ +import { dispatchToCkeditor } from "WoltLabSuite/Core/Component/Ckeditor/Event"; import * as Ajax from "../../Ajax"; import { AjaxCallbackObject, AjaxCallbackSetup, ResponseData } from "../../Ajax/Data"; import { getCkeditorById } from "../../Component/Ckeditor"; @@ -486,7 +487,7 @@ class UiMessageInlineEditor implements AjaxCallbackObject { (validateResult as Promise).then( () => { - EventHandler.fire("com.woltlab.wcf.ckeditor5", `submit_${id}`, parameters); + dispatchToCkeditor(ckeditor.sourceElement).collectMetaData({ metaData: parameters }); Ajax.api(this, { actionName: "save", diff --git a/ts/WoltLabSuite/Core/Ui/Message/Reply.ts b/ts/WoltLabSuite/Core/Ui/Message/Reply.ts index ae1c70d701..f195f438cb 100644 --- a/ts/WoltLabSuite/Core/Ui/Message/Reply.ts +++ b/ts/WoltLabSuite/Core/Ui/Message/Reply.ts @@ -21,6 +21,7 @@ import ControllerCaptcha from "../../Controller/Captcha"; import * as UiScroll from "../Scroll"; import { CKEditor, getCkeditor } from "../../Component/Ckeditor"; +import { dispatchToCkeditor } from "WoltLabSuite/Core/Component/Ckeditor/Event"; interface MessageReplyOptions { ajax: { @@ -188,7 +189,7 @@ class UiMessageReply { }); } - EventHandler.fire("com.woltlab.wcf.ckeditor5", "submit_text", parameters.data as any); + dispatchToCkeditor(this._textarea).collectMetaData({ metaData: parameters.data as Record }); if (!User.userId && !additionalParameters) { parameters.requireGuestDialog = true; diff --git a/wcfsetup/install/files/js/WCF.Attachment.js b/wcfsetup/install/files/js/WCF.Attachment.js index 95104dfdfc..45895d0355 100644 --- a/wcfsetup/install/files/js/WCF.Attachment.js +++ b/wcfsetup/install/files/js/WCF.Attachment.js @@ -138,6 +138,11 @@ WCF.Attachment.Upload = WCF.Upload.extend({ }) .discardRecoveredData(() => { discardAllAttachments(); + }) + .collectMetaData((payload) => { + if (this._tmpHash) { + payload.metaData.tmpHash = this._tmpHash; + } }); const ckeditor = getCkeditor(this._sourceElement); diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor/Event.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor/Event.js index f689552a65..46527dbc92 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor/Event.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor/Event.js @@ -16,6 +16,11 @@ define(["require", "exports"], function (require, exports) { constructor(element) { this.#element = element; } + collectMetaData(payload) { + this.#element.dispatchEvent(new CustomEvent("ckeditor5:collect-meta-data" /* EventNames.CollectMetaData */, { + detail: payload, + })); + } destroy() { this.#element.dispatchEvent(new CustomEvent("ckeditor5:destroy" /* EventNames.Destroy */)); } @@ -78,6 +83,12 @@ define(["require", "exports"], function (require, exports) { constructor(element) { this.#element = element; } + collectMetaData(callback) { + this.#element.addEventListener("ckeditor5:collect-meta-data" /* EventNames.CollectMetaData */, (event) => { + callback(event.detail); + }); + return this; + } destroy(callback) { this.#element.addEventListener("ckeditor5:destroy" /* EventNames.Destroy */, () => { callback(); diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Message/InlineEditor.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Message/InlineEditor.js index eb2d0c0cbf..9ae1fc64d3 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Message/InlineEditor.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Message/InlineEditor.js @@ -5,7 +5,7 @@ * @copyright 2001-2021 WoltLab GmbH * @license GNU Lesser General Public License */ -define(["require", "exports", "tslib", "../../Ajax", "../../Component/Ckeditor", "../../Core", "../../Dom/Change/Listener", "../../Dom/Util", "../../Event/Handler", "../../Language", "../Dropdown/Reusable", "../Notification", "../Screen", "../Scroll"], function (require, exports, tslib_1, Ajax, Ckeditor_1, Core, Listener_1, Util_1, EventHandler, Language, UiDropdownReusable, UiNotification, UiScreen, UiScroll) { +define(["require", "exports", "tslib", "WoltLabSuite/Core/Component/Ckeditor/Event", "../../Ajax", "../../Component/Ckeditor", "../../Core", "../../Dom/Change/Listener", "../../Dom/Util", "../../Event/Handler", "../../Language", "../Dropdown/Reusable", "../Notification", "../Screen", "../Scroll"], function (require, exports, tslib_1, Event_1, Ajax, Ckeditor_1, Core, Listener_1, Util_1, EventHandler, Language, UiDropdownReusable, UiNotification, UiScreen, UiScroll) { "use strict"; Ajax = tslib_1.__importStar(Ajax); Core = tslib_1.__importStar(Core); @@ -380,7 +380,7 @@ define(["require", "exports", "tslib", "../../Ajax", "../../Component/Ckeditor", } } validateResult.then(() => { - EventHandler.fire("com.woltlab.wcf.ckeditor5", `submit_${id}`, parameters); + (0, Event_1.dispatchToCkeditor)(ckeditor.sourceElement).collectMetaData({ metaData: parameters }); Ajax.api(this, { actionName: "save", parameters: parameters, diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Message/Reply.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Message/Reply.js index 8c49fb37d1..eb80e1f046 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Message/Reply.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Message/Reply.js @@ -6,7 +6,7 @@ * @license GNU Lesser General Public License * @woltlabExcludeBundle tiny */ -define(["require", "exports", "tslib", "../../Ajax", "../../Core", "../../Event/Handler", "../../Language", "../../Dom/Change/Listener", "../../Dom/Util", "../Dialog", "../Notification", "../../User", "../../Controller/Captcha", "../Scroll", "../../Component/Ckeditor"], function (require, exports, tslib_1, Ajax, Core, EventHandler, Language, Listener_1, Util_1, Dialog_1, UiNotification, User_1, Captcha_1, UiScroll, Ckeditor_1) { +define(["require", "exports", "tslib", "../../Ajax", "../../Core", "../../Event/Handler", "../../Language", "../../Dom/Change/Listener", "../../Dom/Util", "../Dialog", "../Notification", "../../User", "../../Controller/Captcha", "../Scroll", "../../Component/Ckeditor", "WoltLabSuite/Core/Component/Ckeditor/Event"], function (require, exports, tslib_1, Ajax, Core, EventHandler, Language, Listener_1, Util_1, Dialog_1, UiNotification, User_1, Captcha_1, UiScroll, Ckeditor_1, Event_1) { "use strict"; Ajax = tslib_1.__importStar(Ajax); Core = tslib_1.__importStar(Core); @@ -145,7 +145,7 @@ define(["require", "exports", "tslib", "../../Ajax", "../../Core", "../../Event/ parameters[name] = element.value.trim(); }); } - EventHandler.fire("com.woltlab.wcf.ckeditor5", "submit_text", parameters.data); + (0, Event_1.dispatchToCkeditor)(this._textarea).collectMetaData({ metaData: parameters.data }); if (!User_1.default.userId && !additionalParameters) { parameters.requireGuestDialog = true; } -- 2.20.1