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 985595200..fde831481 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({ @@ -250,10 +279,12 @@ var RED = (function() { RED.nodes.import(nodes.flows); RED.nodes.dirty(false); RED.view.redraw(true); + let activeWorkspace = 0; if (/^#(flow|node|group)\/.+$/.test(currentHash)) { const hashParts = currentHash.split('/') const showEditDialog = hashParts.length > 2 && hashParts[2] === 'edit' if (hashParts[0] === '#flow') { + activeWorkspace = hashParts[1]; RED.workspaces.show(hashParts[1], true); if (showEditDialog) { RED.workspaces.edit() @@ -283,6 +314,22 @@ var RED = (function() { } } if (RED.workspaces.count() > 0) { + const tabOrder = JSON.parse(RED.settings.getLocal("tabOrder") || "[]"); + + if (tabOrder.length) { + const currentOrder = RED.nodes.getWorkspaceOrder(); + // 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")||"{}"); const workspaces = RED.nodes.getWorkspaceOrder(); if (RED.workspaces.active() === 0) { diff --git a/packages/node_modules/@node-red/editor-client/src/js/ui/workspaces.js b/packages/node_modules/@node-red/editor-client/src/js/ui/workspaces.js index 03a15d767..aa41b43fa 100644 --- a/packages/node_modules/@node-red/editor-client/src/js/ui/workspaces.js +++ b/packages/node_modules/@node-red/editor-client/src/js/ui/workspaces.js @@ -413,6 +413,13 @@ RED.workspaces = (function() { if (workspaceTabCount === 1) { showWorkspace(); } + + // Save the new state of tab order in the local storage + const tabOrder = JSON.parse(RED.settings.getLocal("tabOrder") || "[]"); + if (!tabOrder.includes(tab.id)) { + tabOrder.push(tab.id); + RED.settings.setLocal("tabOrder", JSON.stringify(tabOrder)); + } }, onremove: function(tab) { if (tab.type === "tab") { @@ -425,6 +432,14 @@ RED.workspaces = (function() { if (workspaceTabCount === 0) { hideWorkspace(); } + + // Save the new state of tab order in the local storage + const tabOrder = JSON.parse(RED.settings.getLocal("tabOrder") || "[]"); + const index = tabOrder.indexOf(tab.id); + if (index > -1) { + tabOrder.splice(index, 1); + RED.settings.setLocal("tabOrder", JSON.stringify(tabOrder)); + } }, onreorder: function(oldOrder, newOrder) { RED.history.push({ @@ -443,6 +458,9 @@ RED.workspaces = (function() { RED.nodes.dirty(true); setWorkspaceOrder(newOrder); } + + // Save the new state of tab order in the local storage + RED.settings.setLocal("tabOrder", JSON.stringify(newOrder)); }, onselect: function(selectedTabs) { RED.view.select(false) @@ -949,6 +967,8 @@ RED.workspaces = (function() { RED.events.emit("flows:reorder",newOrder); } workspace_tabs.order(order); + // Save the new state of tab order in the local storage + RED.settings.setLocal("tabOrder", JSON.stringify(order)); return newOrder }