From a4875bf5f6c344650faad280b0aaecd893b81ec7 Mon Sep 17 00:00:00 2001 From: GogoVega <92022724+GogoVega@users.noreply.github.com> Date: Thu, 30 Oct 2025 17:25:16 +0100 Subject: [PATCH] Ensure the order matches the flow file order --- .../@node-red/editor-client/src/js/red.js | 50 ++++++++++++++----- 1 file changed, 38 insertions(+), 12 deletions(-) diff --git a/packages/node_modules/@node-red/editor-client/src/js/red.js b/packages/node_modules/@node-red/editor-client/src/js/red.js index 7a4dcc674..ea9d7fbd6 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/red.js +++ b/packages/node_modules/@node-red/editor-client/src/js/red.js @@ -233,6 +233,35 @@ var RED = (function() { }); } + /** + * The result of [A,C,B,D,E] and [B,C] is [A,B,C,D,E]. + * + * @param {string[]} storedOrder + * @param {string[]} currentOrder + * @returns {string[]} + */ + function mergeTabOrder(storedOrder, currentOrder) { + const result = [...storedOrder]; + + for (let i = 0; i < currentOrder.length; i++) { + const current = currentOrder[i]; + const currentIndex = result.indexOf(current); + + for (let j = i + 1; j < currentOrder.length; j++) { + const next = currentOrder[j]; + const nextIndex = result.indexOf(next); + + if (nextIndex < currentIndex) { + result.splice(nextIndex, 1); + const newIndex = result.indexOf(current) + 1; + result.splice(newIndex, 0, next); + } + } + } + + return result; + } + function loadFlows(done) { loader.reportProgress(RED._("event.loadFlows"),80 ) $.ajax({ @@ -288,20 +317,17 @@ var RED = (function() { const tabOrder = JSON.parse(RED.settings.getLocal("tabOrder") || "[]"); if (tabOrder.length) { - // Checks that the last session order contains all current flows - // Otherwise, ignore the reorder const currentOrder = RED.nodes.getWorkspaceOrder(); - const orderNotUpToDate = currentOrder.some((t) => !tabOrder.includes(t)); - if (!orderNotUpToDate) { - for (const tab of tabOrder) { - // Add missing tabs - RED.workspaces.show(tab, true); - } - // Restore the order from the last session - RED.workspaces.order(tabOrder); - // Select the active workspace again because adding missing tab has changed it - RED.workspaces.show(activeWorkspace, true); + // Ensure the order from last session matches the current order + const newOrder = mergeTabOrder(tabOrder, currentOrder); + for (const tab of newOrder) { + // Add missing tabs + RED.workspaces.show(tab, true); } + // Restore the order from the last session + RED.workspaces.order(newOrder); + // Select the active workspace again because adding missing tab has changed it + RED.workspaces.show(activeWorkspace, true); } const hiddenTabs = JSON.parse(RED.settings.getLocal("hiddenTabs")||"{}");