Fix `onremove` is not called on a pure editor plugin

pull/5284/head
GogoVega 2025-09-29 10:23:48 +02:00
parent 0c92332033
commit 79bfe6a353
No known key found for this signature in database
GPG Key ID: E1E048B63AC5AC2B
1 changed files with 22 additions and 12 deletions

View File

@ -1612,22 +1612,32 @@ RED.palette.editor = (function() {
refreshUpdateStatus();
}
const pluginList = [];
Object.entries(entry.pluginSet).forEach(([key, set]) => {
if (set.plugins && set.plugins.length) {
// Adds ID of plugins that exist in the runtime
pluginList.push(...set.plugins.map((plugin) => plugin.id));
} else {
// Adds ID of plugins that only exist in the editor
pluginList.push(key);
}
});
// We assume that a plugin that implements onremove
// cleans the editor accordingly of its left-overs.
let found_onremove = true;
let keys = Object.keys(entry.pluginSet);
keys.forEach((key) => {
let set = entry.pluginSet[key];
for (let i=0; i<set.plugins?.length; i++) {
let plgn = RED.plugins.getPlugin(set.plugins[i].id);
if (plgn && plgn.onremove && typeof plgn.onremove === 'function') {
plgn.onremove();
} else {
if (plgn && plgn.onadd && typeof plgn.onadd === 'function') {
// if there's no 'onadd', there shouldn't be any left-overs
found_onremove = false;
pluginList.forEach((id) => {
const plugin = RED.plugins.getPlugin(id);
if (plugin) {
if (plugin.onremove && typeof plugin.onremove === 'function') {
try {
plugin.onremove();
} catch (error) {
console.warn(`Error while calling 'onremove' for plugin '${id}': `, error);
}
} else if (plugin.onadd && typeof plugin.onadd === 'function') {
// if there's no 'onadd', there shouldn't be any left-overs
found_onremove = false;
}
}
});