From 8e483f55f147e68df971bcc4eea945181e4663ec Mon Sep 17 00:00:00 2001 From: Alexander Ebert Date: Thu, 27 Apr 2023 18:29:09 +0200 Subject: [PATCH] Gracefully handle a missing CKEditor instance Fixes #5441 --- ts/WoltLabSuite/Core/Component/Ckeditor.ts | 8 ++++++-- ts/WoltLabSuite/Core/Ui/Message/InlineEditor.ts | 2 +- .../files/js/WoltLabSuite/Core/Component/Ckeditor.js | 9 +++++++-- .../js/WoltLabSuite/Core/Ui/Message/InlineEditor.js | 2 +- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/ts/WoltLabSuite/Core/Component/Ckeditor.ts b/ts/WoltLabSuite/Core/Component/Ckeditor.ts index 784d109cee..8bd445a887 100644 --- a/ts/WoltLabSuite/Core/Component/Ckeditor.ts +++ b/ts/WoltLabSuite/Core/Component/Ckeditor.ts @@ -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); diff --git a/ts/WoltLabSuite/Core/Ui/Message/InlineEditor.ts b/ts/WoltLabSuite/Core/Ui/Message/InlineEditor.ts index db6cef880f..063a9b02fc 100644 --- a/ts/WoltLabSuite/Core/Ui/Message/InlineEditor.ts +++ b/ts/WoltLabSuite/Core/Ui/Message/InlineEditor.ts @@ -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(); } /** diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor.js index 836fee2c0c..9bbfdb336f 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Component/Ckeditor.js @@ -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); } diff --git a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Message/InlineEditor.js b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Message/InlineEditor.js index 473121e68a..afac8a09c9 100644 --- a/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Message/InlineEditor.js +++ b/wcfsetup/install/files/js/WoltLabSuite/Core/Ui/Message/InlineEditor.js @@ -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. -- 2.20.1