MDL-75078 editor_tiny: Fix undestroyed editors

Part of MDL-75966
This commit is contained in:
Andrew Nicols 2022-08-18 10:04:50 +08:00
parent 417dee3a65
commit 41952a03b6
3 changed files with 22 additions and 2 deletions

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -268,6 +268,26 @@ export const setupForTarget = async(target, options = {}) => {
]);
const {pluginNames, pluginConfig} = pluginValues;
// TinyMCE uses the element ID as a map key internally, even if the target has changed.
// In the case where we have an editor in a modal form which has been detached from the DOM, but the editor not removed,
// we need to manually destroy the editor.
// We could theoretically do this with a Mutation Observer, but in some cases the Node may be moved,
// or added back elsewhere in the DOM.
const existingEditor = tinyMCE.EditorManager.get(target.id);
if (existingEditor) {
if (existingEditor.targetElm.closest('body')) {
if (existingEditor.targetElm === target) {
pendingPromise.resolve();
return Promise.resolve(existingEditor);
} else {
pendingPromise.resolve();
throw new Error('TinyMCE instance already exists for different target with same ID');
}
} else {
existingEditor.destroy();
}
}
// Allow plugins to modify the configuration.
const instanceConfig = getStandardConfig(target, tinyMCE, options, pluginNames);
pluginConfig.forEach((pluginConfig) => {