From 1ff6a46cfa0a433d9429008ab080d01cc01246ba Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Fri, 28 Apr 2023 16:03:27 +0200 Subject: [PATCH] =?utf8?q?Inject=20poll=20data=20into=20the=20editor?= =?utf8?q?=E2=80=99s=20meta=20data?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- ts/WoltLabSuite/Core/Ui/Poll/Editor.ts | 45 +++++++++++++++++++ .../js/WoltLabSuite/Core/Ui/Poll/Editor.js | 27 ++++++++++- 2 files changed, 71 insertions(+), 1 deletion(-) diff --git a/ts/WoltLabSuite/Core/Ui/Poll/Editor.ts b/ts/WoltLabSuite/Core/Ui/Poll/Editor.ts index bd0f3328c2..700227bb99 100644 --- a/ts/WoltLabSuite/Core/Ui/Poll/Editor.ts +++ b/ts/WoltLabSuite/Core/Ui/Poll/Editor.ts @@ -13,6 +13,7 @@ import UiSortableList from "../Sortable/List"; import * as EventHandler from "../../Event/Handler"; import * as DatePicker from "../../Date/Picker"; import { DatabaseObjectActionResponse } from "../../Ajax/Data"; +import { listenToCkeditor } from "../../Component/Ckeditor/Event"; interface UiPollEditorOptions { isAjax: boolean; @@ -42,6 +43,17 @@ interface ValidationData { valid: boolean; } +type PollData = { + pollEndTime: string; + pollIsChangeable?: boolean; + pollIsPublic?: boolean; + pollMaxVotes: number; + pollOptions: string[]; + pollQuestion: string; + pollResultsRequireVote?: true; + pollSortByVotes?: boolean; +}; + class UiPollEditor { private readonly container: HTMLElement; private readonly endTimeField: HTMLInputElement; @@ -120,6 +132,10 @@ class UiPollEditor { this.reset(); }); + listenToCkeditor(element).collectMetaData((payload) => { + payload.metaData.poll = this.#getPollData(); + }); + ["handleError", "submit", "validate"].forEach((event) => { EventHandler.add("com.woltlab.wcf.ckeditor5", event + "_" + this.wysiwygId, (...args: unknown[]) => this[event](...args), @@ -322,6 +338,35 @@ class UiPollEditor { } } + #getPollData(): PollData { + const data: PollData = { + pollEndTime: DatePicker.getValue(this.endTimeField), + pollMaxVotes: parseInt(this.maxVotesField.value) || 0, + pollQuestion: this.questionField.value, + pollOptions: [], + }; + + if (this.isChangeableYesField.checked) { + data.pollIsChangeable = true; + } + + if (this.resultsRequireVoteYesField.checked) { + data.pollResultsRequireVote = true; + } + + if (this.sortByVotesYesField.checked) { + data.pollSortByVotes = true; + } + + if (this.isPublicYesField?.checked) { + data.pollIsPublic = true; + } + + data.pollOptions = this.getOptions(); + + return data; + } + /** * Validates the poll data. */ diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Poll/Editor.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Poll/Editor.js index 36d559dc46..1fd8e86757 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Poll/Editor.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Poll/Editor.js @@ -6,7 +6,7 @@ * @license GNU Lesser General Public License * @woltlabExcludeBundle all */ -define(["require", "exports", "tslib", "../../Core", "../../Language", "../Sortable/List", "../../Event/Handler", "../../Date/Picker"], function (require, exports, tslib_1, Core, Language, List_1, EventHandler, DatePicker) { +define(["require", "exports", "tslib", "../../Core", "../../Language", "../Sortable/List", "../../Event/Handler", "../../Date/Picker", "../../Component/Ckeditor/Event"], function (require, exports, tslib_1, Core, Language, List_1, EventHandler, DatePicker, Event_1) { "use strict"; Core = tslib_1.__importStar(Core); Language = tslib_1.__importStar(Language); @@ -73,6 +73,9 @@ define(["require", "exports", "tslib", "../../Core", "../../Language", "../Sorta element.addEventListener("reset", () => { this.reset(); }); + (0, Event_1.listenToCkeditor)(element).collectMetaData((payload) => { + payload.metaData.poll = this.#getPollData(); + }); ["handleError", "submit", "validate"].forEach((event) => { EventHandler.add("com.woltlab.wcf.ckeditor5", event + "_" + this.wysiwygId, (...args) => this[event](...args)); }); @@ -246,6 +249,28 @@ define(["require", "exports", "tslib", "../../Core", "../../Language", "../Sorta }); } } + #getPollData() { + const data = { + pollEndTime: DatePicker.getValue(this.endTimeField), + pollMaxVotes: parseInt(this.maxVotesField.value) || 0, + pollQuestion: this.questionField.value, + pollOptions: [], + }; + if (this.isChangeableYesField.checked) { + data.pollIsChangeable = true; + } + if (this.resultsRequireVoteYesField.checked) { + data.pollResultsRequireVote = true; + } + if (this.sortByVotesYesField.checked) { + data.pollSortByVotes = true; + } + if (this.isPublicYesField?.checked) { + data.pollIsPublic = true; + } + data.pollOptions = this.getOptions(); + return data; + } /** * Validates the poll data. */ -- 2.20.1