`setHtml()` should replace the model to preserve the history
authorAlexander Ebert <ebert@woltlab.com>
Mon, 9 Oct 2023 16:39:25 +0000 (18:39 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Mon, 9 Oct 2023 16:39:25 +0000 (18:39 +0200)
ts/WoltLabSuite/Core/Component/Ckeditor.ts
wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor.js

index 0eaa6b303c03e13f371b8eff6974dc771e5434cc..45a55d3fbdf676ae57dafab66cf550e2b67931d7 100644 (file)
@@ -105,7 +105,19 @@ class Ckeditor {
   }
 
   setHtml(html: string): void {
-    this.#editor.data.set(html);
+    html = normalizeLegacyHtml(html);
+
+    this.#editor.model.change((writer) => {
+      let range = this.#editor.model.createRangeIn(this.#editor.model.document.getRoot()!);
+
+      const viewFragment = this.#editor.data.processor.toView(html);
+      const modelFragment = this.#editor.data.toModel(viewFragment);
+
+      range = this.#editor.model.insertContent(modelFragment, range);
+
+      writer.setSelection(range.end);
+      this.focus();
+    });
   }
 
   removeAll(model: string, attributes: Record<string, string | number | boolean>): void {
index 5b968abdd82b3e93c78112cc1360a44db5bd9e21..abdd6c17b596a78c4be90ae553f95d7362e3f870 100644 (file)
@@ -78,7 +78,15 @@ define(["require", "exports", "tslib", "./Ckeditor/Attachment", "./Ckeditor/Medi
             return this.#editor.ui.element.clientWidth !== 0;
         }
         setHtml(html) {
-            this.#editor.data.set(html);
+            html = (0, Normalizer_1.normalizeLegacyHtml)(html);
+            this.#editor.model.change((writer) => {
+                let range = this.#editor.model.createRangeIn(this.#editor.model.document.getRoot());
+                const viewFragment = this.#editor.data.processor.toView(html);
+                const modelFragment = this.#editor.data.toModel(viewFragment);
+                range = this.#editor.model.insertContent(modelFragment, range);
+                writer.setSelection(range.end);
+                this.focus();
+            });
         }
         removeAll(model, attributes) {
             this.#editor.model.change((writer) => {