Move the clean up of legacy messages into a separate module
authorAlexander Ebert <ebert@woltlab.com>
Thu, 4 May 2023 17:06:40 +0000 (19:06 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 8 May 2023 16:34:04 +0000 (18:34 +0200)
ts/WoltLabSuite/Core/Component/Ckeditor.ts
ts/WoltLabSuite/Core/Component/Ckeditor/Cleanup.ts [new file with mode: 0644]
wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor.js
wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor/Cleanup.js [new file with mode: 0644]

index 303115d95c27459beb22749e71e920892be4e644..ca168e4eee69e9875d700f53d50c1746192d73ac 100644 (file)
@@ -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 (file)
index 0000000..b5b0d5a
--- /dev/null
@@ -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 <textarea>.");
+  }
+
+  const div = document.createElement("div");
+  div.innerHTML = element.value;
+
+  stripLegacySpacerParagraphs(div);
+
+  element.value = div.innerHTML;
+}
index 271151fcd6b4763974e58f99ced22e9dadc5510b..bc41b4810e24c22c916c1e5bdb1318aecd815270 100644 (file)
@@ -11,7 +11,7 @@
  * @license GNU Lesser General Public License <http://opensource.org/licenses/lgpl-license.php>
  * @since 6.0
  */
-define(["require", "exports", "tslib", "./Ckeditor/Attachment", "./Ckeditor/Media", "./Ckeditor/Mention", "./Ckeditor/Quote", "./Ckeditor/Autosave", "./Ckeditor/Configuration", "./Ckeditor/Event", "./Ckeditor/SubmitOnEnter", "../Devtools", "ckeditor5-bundle"], function (require, exports, tslib_1, Attachment_1, Media_1, Mention_1, Quote_1, Autosave_1, Configuration_1, Event_1, SubmitOnEnter_1, Devtools_1) {
+define(["require", "exports", "tslib", "./Ckeditor/Attachment", "./Ckeditor/Media", "./Ckeditor/Mention", "./Ckeditor/Quote", "./Ckeditor/Autosave", "./Ckeditor/Configuration", "./Ckeditor/Event", "./Ckeditor/SubmitOnEnter", "./Ckeditor/Cleanup", "../Devtools", "ckeditor5-bundle"], function (require, exports, tslib_1, Attachment_1, Media_1, Mention_1, Quote_1, Autosave_1, Configuration_1, Event_1, SubmitOnEnter_1, Cleanup_1, Devtools_1) {
     "use strict";
     Object.defineProperty(exports, "__esModule", { value: true });
     exports.getCkeditorById = exports.getCkeditor = exports.setupCkeditor = void 0;
@@ -124,24 +124,6 @@ define(["require", "exports", "tslib", "./Ckeditor/Attachment", "./Ckeditor/Medi
         }
         return configuration;
     }
-    function stripLegacySpacerParagraphs(element) {
-        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];
-                if (child.tagName === "BR" && child.dataset.ckeFiller !== "true") {
-                    if (paragraph.textContent.trim() === "") {
-                        paragraph.remove();
-                    }
-                }
-            }
-        });
-        element.value = div.innerHTML;
-    }
     async function setupCkeditor(element, features, bbcodes) {
         if (instances.has(element)) {
             throw new TypeError(`Cannot initialize the editor for '${element.id}' twice.`);
@@ -158,7 +140,7 @@ define(["require", "exports", "tslib", "./Ckeditor/Attachment", "./Ckeditor/Medi
             (0, Quote_1.setup)(element);
         }
         const configuration = initializeConfiguration(element, features, bbcodes);
-        stripLegacySpacerParagraphs(element);
+        (0, Cleanup_1.normalizeLegacyMessage)(element);
         const cke = await window.CKEditor5.create(element, configuration);
         const ckeditor = new Ckeditor(cke, features);
         if (features.autosave) {
diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor/Cleanup.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor/Cleanup.js
new file mode 100644 (file)
index 0000000..31fa471
--- /dev/null
@@ -0,0 +1,27 @@
+define(["require", "exports"], function (require, exports) {
+    "use strict";
+    Object.defineProperty(exports, "__esModule", { value: true });
+    exports.normalizeLegacyMessage = void 0;
+    function stripLegacySpacerParagraphs(div) {
+        div.querySelectorAll("p").forEach((paragraph) => {
+            if (paragraph.childElementCount === 1) {
+                const child = paragraph.children[0];
+                if (child.tagName === "BR" && child.dataset.ckeFiller !== "true") {
+                    if (paragraph.textContent.trim() === "") {
+                        paragraph.remove();
+                    }
+                }
+            }
+        });
+    }
+    function normalizeLegacyMessage(element) {
+        if (!(element instanceof HTMLTextAreaElement)) {
+            throw new TypeError("Expected the element to be a <textarea>.");
+        }
+        const div = document.createElement("div");
+        div.innerHTML = element.value;
+        stripLegacySpacerParagraphs(div);
+        element.value = div.innerHTML;
+    }
+    exports.normalizeLegacyMessage = normalizeLegacyMessage;
+});