|
| 1 | +/** |
| 2 | + * Provides previews for mulitple CKEditor 5 message fields. |
| 3 | + * |
| 4 | + * @author Marcel Werk |
| 5 | + * @copyright 2001-2024 WoltLab GmbH |
| 6 | + * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php> |
| 7 | + * @since 6.2 |
| 8 | + */ |
| 9 | + |
| 10 | +import { promiseMutex } from "WoltLabSuite/Core/Helper/PromiseMutex"; |
| 11 | +import { dboAction } from "WoltLabSuite/Core/Ajax"; |
| 12 | +import { listenToCkeditor } from "WoltLabSuite/Core/Component/Ckeditor/Event"; |
| 13 | +import { dialogFactory } from "WoltLabSuite/Core/Component/Dialog"; |
| 14 | +import { getPhrase } from "WoltLabSuite/Core/Language"; |
| 15 | +import DomUtil from "WoltLabSuite/Core/Dom/Util"; |
| 16 | +import { CKEditor } from "WoltLabSuite/Core/Component/Ckeditor"; |
| 17 | + |
| 18 | +type ResponseGetMessagePreview = { |
| 19 | + message: string; |
| 20 | + raw: string; |
| 21 | +}; |
| 22 | + |
| 23 | +async function loadPreview(message: string, objectType: string, objectId: number): Promise<void> { |
| 24 | + const response = (await dboAction("getMessagePreview", "wcf\\data\\bbcode\\MessagePreviewAction") |
| 25 | + .payload({ |
| 26 | + data: { |
| 27 | + message, |
| 28 | + }, |
| 29 | + messageObjectType: objectType, |
| 30 | + messageObjectID: objectId, |
| 31 | + }) |
| 32 | + .dispatch()) as ResponseGetMessagePreview; |
| 33 | + |
| 34 | + const dialog = dialogFactory() |
| 35 | + .fromHtml('<div class="htmlContent">' + response.message + "</div>") |
| 36 | + .withoutControls(); |
| 37 | + dialog.show(getPhrase("wcf.global.preview")); |
| 38 | +} |
| 39 | + |
| 40 | +function getEditorMap(messageFieldIds: string[]): Map<string, CKEditor> { |
| 41 | + const map = new Map<string, CKEditor>(); |
| 42 | + messageFieldIds.forEach((messageFieldId) => { |
| 43 | + listenToCkeditor(document.getElementById(messageFieldId)!).ready(({ ckeditor }) => { |
| 44 | + map.set(messageFieldId, ckeditor); |
| 45 | + }); |
| 46 | + }); |
| 47 | + |
| 48 | + return map; |
| 49 | +} |
| 50 | + |
| 51 | +function getActiveEditor(map: Map<string, CKEditor>): CKEditor | undefined { |
| 52 | + let activeEditor: CKEditor | undefined = undefined; |
| 53 | + map.forEach((editor) => { |
| 54 | + if (editor.isVisible()) { |
| 55 | + activeEditor = editor; |
| 56 | + } |
| 57 | + }); |
| 58 | + |
| 59 | + return activeEditor; |
| 60 | +} |
| 61 | + |
| 62 | +export function setup(messageFieldIds: string[], previewButtonId: string, objectType: string, objectId: number): void { |
| 63 | + const map = getEditorMap(messageFieldIds); |
| 64 | + |
| 65 | + document.getElementById(previewButtonId)?.addEventListener( |
| 66 | + "click", |
| 67 | + promiseMutex(() => { |
| 68 | + const activeEditor = getActiveEditor(map); |
| 69 | + if (activeEditor === undefined) { |
| 70 | + return Promise.resolve(); |
| 71 | + } |
| 72 | + |
| 73 | + if (activeEditor.getHtml() === "") { |
| 74 | + DomUtil.innerError(activeEditor.element, getPhrase("wcf.global.form.error.empty")); |
| 75 | + return Promise.resolve(); |
| 76 | + } else { |
| 77 | + DomUtil.innerError(activeEditor.element, false); |
| 78 | + } |
| 79 | + |
| 80 | + return loadPreview(activeEditor.getHtml(), objectType, objectId); |
| 81 | + }), |
| 82 | + ); |
| 83 | +} |
0 commit comments