Gracefully handle a missing CKEditor instance
authorAlexander Ebert <ebert@woltlab.com>
Thu, 27 Apr 2023 16:29:09 +0000 (18:29 +0200)
committerAlexander Ebert <ebert@woltlab.com>
Thu, 27 Apr 2023 16:29:09 +0000 (18:29 +0200)
Fixes #5441

ts/WoltLabSuite/Core/Component/Ckeditor.ts
ts/WoltLabSuite/Core/Ui/Message/InlineEditor.ts
wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor.js
wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Message/InlineEditor.js

index 784d109ceed6d2f7465ab8a1b7100986fe8d43b2..8bd445a887558d9266facf6cb530c0c5cdc5c333 100644 (file)
@@ -233,10 +233,14 @@ export function getCkeditor(element: HTMLElement): CKEditor | undefined {
   return instances.get(element);
 }
 
-export function getCkeditorById(id: string): Ckeditor | undefined {
+export function getCkeditorById(id: string, throwIfNotExists = true): Ckeditor | undefined {
   const element = document.getElementById(id);
   if (element === null) {
-    throw new Error(`Unable to find an element with the id '${id}'.`);
+    if (throwIfNotExists) {
+      throw new Error(`Unable to find an element with the id '${id}'.`);
+    } else {
+      return undefined;
+    }
   }
 
   return getCkeditor(element);
index db6cef880f9e45a8f8b406daf7314c1ba6c8c0cd..063a9b02fc41668d1d303d76df908c15e4fa024e 100644 (file)
@@ -624,7 +624,7 @@ class UiMessageInlineEditor implements AjaxCallbackObject {
    * Destroys the editor instance.
    */
   protected _destroyEditor(): void {
-    void getCkeditorById(this._getEditorId())!.destroy();
+    void getCkeditorById(this._getEditorId(), false)?.destroy();
   }
 
   /**
index 836fee2c0c6624734b4204ee98d010aa1358821e..9bbfdb336fd9abf30da83f744d22811f4d632850 100644 (file)
@@ -168,10 +168,15 @@ define(["require", "exports", "tslib", "./Ckeditor/Attachment", "./Ckeditor/Medi
         return instances.get(element);
     }
     exports.getCkeditor = getCkeditor;
-    function getCkeditorById(id) {
+    function getCkeditorById(id, throwIfNotExists = true) {
         const element = document.getElementById(id);
         if (element === null) {
-            throw new Error(`Unable to find an element with the id '${id}'.`);
+            if (throwIfNotExists) {
+                throw new Error(`Unable to find an element with the id '${id}'.`);
+            }
+            else {
+                return undefined;
+            }
         }
         return getCkeditor(element);
     }
index 473121e68aa3fe1108d0df70ecdffec3a64e8828..afac8a09c9d80a2831ccd7b1ca54198f379d55e2 100644 (file)
@@ -489,7 +489,7 @@ define(["require", "exports", "tslib", "WoltLabSuite/Core/Component/Ckeditor/Eve
          * Destroys the editor instance.
          */
         _destroyEditor() {
-            void (0, Ckeditor_1.getCkeditorById)(this._getEditorId()).destroy();
+            void (0, Ckeditor_1.getCkeditorById)(this._getEditorId(), false)?.destroy();
         }
         /**
          * Returns the hash added to the url after successfully editing a message.