From: Alexander Ebert Date: Thu, 4 May 2023 17:06:40 +0000 (+0200) Subject: Move the clean up of legacy messages into a separate module X-Git-Tag: 6.0.0_Alpha_1~136^2~14 X-Git-Url: https://git.stricted.de/?a=commitdiff_plain;h=f61b829fc50c85ca667a4bd632967bbe538c04a6;p=GitHub%2FWoltLab%2FWCF.git Move the clean up of legacy messages into a separate module --- 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