From f61b829fc50c85ca667a4bd632967bbe538c04a6 Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Thu, 4 May 2023 19:06:40 +0200 Subject: [PATCH] Move the clean up of legacy messages into a separate module --- ts/WoltLabSuite/Core/Component/Ckeditor.ts | 25 ++--------------- .../Core/Component/Ckeditor/Cleanup.ts | 25 +++++++++++++++++ .../WoltLabSuite/Core/Component/Ckeditor.js | 22 ++------------- .../Core/Component/Ckeditor/Cleanup.js | 27 +++++++++++++++++++ 4 files changed, 56 insertions(+), 43 deletions(-) create mode 100644 ts/WoltLabSuite/Core/Component/Ckeditor/Cleanup.ts create mode 100644 wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor/Cleanup.js diff --git a/ts/WoltLabSuite/Core/Component/Ckeditor.ts b/ts/WoltLabSuite/Core/Component/Ckeditor.ts index 303115d95c..ca168e4eee 100644 --- a/ts/WoltLabSuite/Core/Component/Ckeditor.ts +++ b/ts/WoltLabSuite/Core/Component/Ckeditor.ts @@ -20,6 +20,7 @@ import { deleteDraft, initializeAutosave, setupRestoreDraft } from "./Ckeditor/A import { createConfigurationFor, Features } from "./Ckeditor/Configuration"; import { dispatchToCkeditor } from "./Ckeditor/Event"; import { setup as setupSubmitOnEnter } from "./Ckeditor/SubmitOnEnter"; +import { normalizeLegacyMessage } from "./Ckeditor/Cleanup"; import Devtools from "../Devtools"; import { ClassicEditor, EditorConfig, Element as CkeElement } from "./Ckeditor/Types"; @@ -174,28 +175,6 @@ function initializeConfiguration(element: HTMLElement, features: Features, bbcod return configuration; } -function stripLegacySpacerParagraphs(element: HTMLElement): void { - if (!(element instanceof HTMLTextAreaElement)) { - return; - } - - const div = document.createElement("div"); - div.innerHTML = element.value; - - div.querySelectorAll("p").forEach((paragraph) => { - if (paragraph.childElementCount === 1) { - const child = paragraph.children[0] as HTMLElement; - if (child.tagName === "BR" && child.dataset.ckeFiller !== "true") { - if (paragraph.textContent!.trim() === "") { - paragraph.remove(); - } - } - } - }); - - element.value = div.innerHTML; -} - export async function setupCkeditor( element: HTMLElement, features: Features, @@ -220,7 +199,7 @@ export async function setupCkeditor( const configuration = initializeConfiguration(element, features, bbcodes); - stripLegacySpacerParagraphs(element); + normalizeLegacyMessage(element); const cke = await window.CKEditor5.create(element, configuration); const ckeditor = new Ckeditor(cke, features); diff --git a/ts/WoltLabSuite/Core/Component/Ckeditor/Cleanup.ts b/ts/WoltLabSuite/Core/Component/Ckeditor/Cleanup.ts new file mode 100644 index 0000000000..b5b0d5adbb --- /dev/null +++ b/ts/WoltLabSuite/Core/Component/Ckeditor/Cleanup.ts @@ -0,0 +1,25 @@ +function stripLegacySpacerParagraphs(div: HTMLElement): void { + div.querySelectorAll("p").forEach((paragraph) => { + if (paragraph.childElementCount === 1) { + const child = paragraph.children[0] as HTMLElement; + if (child.tagName === "BR" && child.dataset.ckeFiller !== "true") { + if (paragraph.textContent!.trim() === "") { + paragraph.remove(); + } + } + } + }); +} + +export function normalizeLegacyMessage(element: HTMLElement): void { + if (!(element instanceof HTMLTextAreaElement)) { + throw new TypeError("Expected the element to be a