diff --git a/addon/actions/msg.js b/addon/actions/msg.js index 75964ba2c..9ecf05369 100644 --- a/addon/actions/msg.js +++ b/addon/actions/msg.js @@ -34,7 +34,6 @@ function initMessaging() { // Setup message handling let info = JSON.parse(port.name) if (info.instanceType === 'sidebar') { - if (!this.windows[info.windowId]) return connectedSidebars[info.windowId] = port port.onMessage.addListener(onSidebarMsg) @@ -71,31 +70,6 @@ function onFirstSidebarInit(handler) { firstSidebarInitHandlers.push(handler) } -/** - * Wait for connecting sidebery instance of - * target window. - */ -async function waitForSidebarConnect(winId, limit = 1000) { - let waitingId = String(Math.random()) - let waiting = new Promise(res => { - if (connectedSidebars[winId]) return res(true) - - connectPending.push({ - id: waitingId, - winId, - resolve: res, - }) - - setTimeout(() => { - let index = connectPending.findIndex(w => w.id === waitingId) - if (index > -1) connectPending.splice(index, 1) - res(false) - }, limit) - }) - - return waiting -} - /** * Handle message from sidebar */ @@ -113,5 +87,4 @@ export default { initMessaging, onSidebarMsg, onFirstSidebarInit, - waitForSidebarConnect, } diff --git a/addon/actions/tabs.js b/addon/actions/tabs.js index 261fd6a1a..ad4687bb7 100644 --- a/addon/actions/tabs.js +++ b/addon/actions/tabs.js @@ -216,42 +216,6 @@ async function updateTabsTree() { } } -/** - * Move provided tabs to window - */ -async function moveTabsToWin(winId, tabs, fromPrivate, rmTabId) { - let sidebarIsOpen = await browser.sidebarAction.isOpen({ windowId: winId }) - let isConnected = false - - if (sidebarIsOpen) { - isConnected = await this.actions.waitForSidebarConnect(winId, 7000) - if (isConnected) { - await browser.runtime.sendMessage({ - instanceType: 'sidebar', - windowId: winId, - action: 'moveTabsToThisWin', - args: [tabs, fromPrivate], - }) - } - } - - if (!isConnected) { - let win = this.windows[winId] - if (win.incognito === fromPrivate) { - for (let tab of tabs) { - await browser.tabs.move(tab.id, { windowId: winId, index: -1 }) - } - } else { - for (let tab of tabs) { - await browser.tabs.create({ url: tab.url, windowId: winId }) - browser.tabs.remove(tab.id) - } - } - } - - if (rmTabId > -1) await browser.tabs.remove(rmTabId) -} - function setupTabsListeners() { browser.tabs.onCreated.addListener(this.actions.onTabCreated) browser.tabs.onRemoved.addListener(this.actions.onTabRemoved) @@ -282,7 +246,6 @@ export default { updateTabsTree, backupTabsData, saveTabsData, - moveTabsToWin, setupTabsListeners, } diff --git a/addon/background.js b/addon/background.js index ca98105fa..0eefc74c2 100644 --- a/addon/background.js +++ b/addon/background.js @@ -13,14 +13,14 @@ void (async function main() { Actions.initGlobalMessaging() Actions.initMessaging() - // Load containers - Actions.setupContainersListeners() - await Actions.loadContainers() - // Load windows state.windows = await Actions.getWindows() Actions.setupWindowsListeners() + // Load containers + Actions.setupContainersListeners() + await Actions.loadContainers() + // Load settings let { settings } = await browser.storage.local.get({ settings: null }) state.settings = settings ? settings : {} diff --git a/addon/manifest.json b/addon/manifest.json index 1579d23f2..80af8d713 100644 --- a/addon/manifest.json +++ b/addon/manifest.json @@ -8,7 +8,7 @@ }, "author": "mbnuqw", "name": "__MSG_ExtName__", - "version": "4.0.4", + "version": "4.0.5", "default_locale": "en", "description": "__MSG_ExtDesc__", "homepage_url": "https://github.com/mbnuqw/sidebery", diff --git a/addon/utils.js b/addon/utils.js index 4c7316c6b..a9a9f5a1f 100644 --- a/addon/utils.js +++ b/addon/utils.js @@ -526,15 +526,14 @@ function findDataForTabs(tabs, data) { // Saved tab is a group and its missing if (isGroupUrl(tabData.url) && tabData.url !== tab.url) { - tabData.index = k + gOffset tabData.isMissedGroup = true k-- - gOffset++ continue } // Match - if (tabData.url === tab.url || (tab.active && tab.url === 'about:blank')) { + let blindspot = tab.status === 'loading' && tab.url === 'about:blank' + if (tabData.url === tab.url || blindspot) { tabData.index = k + gOffset equalityCounter++ } diff --git a/package.json b/package.json index 38e667992..8e4e10115 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "sidebery", - "version": "4.0.4", + "version": "4.0.5", "description": "Manage your tabs and bookmarks in sidebar", "main": "index.js", "scripts": { @@ -91,6 +91,7 @@ "./test/env-setup.js" ], "testMatch": [ + "**/addon/**/__tests__/*.test.js", "**/src/**/__tests__/*.test.js" ] } diff --git a/src/sidebar/actions/bookmarks.js b/src/sidebar/actions/bookmarks.js index 2891641d5..5c6689616 100644 --- a/src/sidebar/actions/bookmarks.js +++ b/src/sidebar/actions/bookmarks.js @@ -620,6 +620,15 @@ async function sortBookmarks(type, nodeIds) { } } +function highlightBookmarks(url) { + if (!this.state.highlightOpenBookmarks || !this.state.bookmarksUrlMap) return + if (!this.state.bookmarksUrlMap[url]) return + + for (let b of this.state.bookmarksUrlMap[url]) { + b.isOpen = true + } +} + export default { loadBookmarks, saveBookmarksTree, @@ -634,4 +643,5 @@ export default { collapseAllBookmarks, updateBookmarksCounter, sortBookmarks, + highlightBookmarks, } diff --git a/src/sidebar/actions/settings.js b/src/sidebar/actions/settings.js index c6e31d176..df52292e0 100644 --- a/src/sidebar/actions/settings.js +++ b/src/sidebar/actions/settings.js @@ -96,12 +96,6 @@ function updateSettings(settings) { parentId: tab.parentId, folded: tab.folded, }) - if (Utils.isGroupUrl(tab.url)) { - if (!this.state.groupTabs) this.state.groupTabs = {} - this.state.groupTabs[tab.id] = true - } else if (this.state.groupTabs[tab.id]) { - this.state.groupTabs[tab.id] = false - } } this.actions.saveGroups() } diff --git a/src/sidebar/actions/tabs.js b/src/sidebar/actions/tabs.js index a45077b4b..a4fc7eb10 100644 --- a/src/sidebar/actions/tabs.js +++ b/src/sidebar/actions/tabs.js @@ -24,6 +24,7 @@ async function loadTabsFromGlobalStorage() { if (old.tabsTrees) return await this.actions._old_loadTabs(tabs) } + // Check if there is blank tab with inlined info about tabs let dataTabIndex = tabs.findIndex(t => t.url.startsWith('about:blank#tabsdata')) if (dataTabIndex !== -1) { return await this.actions.loadTabsFromInlineData(tabs, dataTabIndex) @@ -31,86 +32,84 @@ async function loadTabsFromGlobalStorage() { let activePanel = this.state.panels[this.state.panelIndex] || this.state.panels[1] let tabsData = storage.tabsData_v4 ? storage.tabsData_v4 : [] + let lastPanel = this.state.panels.find(p => p.tabs) + let idsMap = {} let activeTab - // Set tabs initial props and update state - this.state.tabsMap = [] - for (let t of tabs) { - Utils.normalizeTab(t, null) - if ( - this.state.highlightOpenBookmarks && - this.state.bookmarksUrlMap && - this.state.bookmarksUrlMap[t.url] - ) { - for (let b of this.state.bookmarksUrlMap[t.url]) { - b.isOpen = true - } - } - this.state.tabsMap[t.id] = t - if (t.active) activeTab = t - if (t.active) this.state.activeTabId = t.id - } - + // Find most appropriate data-set to restoring prev tabs state tabsData = Utils.findDataForTabs(tabs, tabsData) if (!tabsData.length) { let storage = await browser.storage.local.get({ prevTabsData_v4: [] }) tabsData = Utils.findDataForTabs(tabs, storage.prevTabsData_v4) } - let idsMap = {} - let prevTab - let pIndex = 0 - for (let tab, tabData, i = 0; i < tabsData.length; i++) { - tabData = tabsData[i] - if (!tabData || tabData.index === undefined) continue - - if (tabData.parentId === undefined) tabData.parentId = -1 - if (tabData.panelId === undefined) tabData.panelId = DEFAULT_CTX_ID - if (tabData.folded === undefined) tabData.folded = false - if (tabData.ctx === undefined) tabData.ctx = DEFAULT_CTX_ID - - // Recreate group tab (e.g. after addon was disabled-enabled) - if (tabData.isMissedGroup) { - let groupId = Utils.getGroupId(tabData.url) - let url = browser.runtime.getURL('group/group.html') + `#${groupId}` - let restoredTab = await browser.tabs.create({ - windowId: this.state.windowId, - index: tabData.index, - url, - cookieStoreId: tabData.ctx, - active: false, - }) - restoredTab.url = url - Utils.normalizeTab(restoredTab, DEFAULT_CTX_ID) - tabs.splice(tabData.index, 0, restoredTab) - this.state.tabsMap[restoredTab.id] = restoredTab - for (let j = tabData.index; j < tabs.length; j++) { - tabs[j].index = j - } - } + // Select missed group tabs + let groups = {} + for (let tabData of tabsData) { + if (tabData.isMissedGroup) groups[tabData.id] = tabData + } - tab = tabs[tabData.index] - if (!tab) break + // Zip tabs with sidebery data + let tabsWithData = tabs.map(t => [t, tabsData.find(d => d.index === t.index)]) - let panel = this.state.panelsMap[tabData.panelId] - if (panel && (tab.pinned || panel.index >= pIndex)) { - tab.panelId = tabData.panelId - if (!tab.pinned) pIndex = panel.index - } else { - if (prevTab) { - tab.panelId = prevTab.panelId + // Go through tabs and restore sidebery props + this.state.tabsMap = [] + for (let [tab, data] of tabsWithData) { + // Normalize tab + let defaultPanel = tab.pinned ? DEFAULT_CTX_ID : null + Utils.normalizeTab(tab, defaultPanel) + + // Highlight bookmarks with that url + this.actions.highlightBookmarks(tab.url) + + if (data) { + // If parent tab is missed try to find it in groups + if (data.parentId > -1 && idsMap[data.parentId] === undefined) { + let group = groups[data.parentId] + + if (group) { + let toRestore = [group] + + while (group.parentId > -1 && idsMap[group.parentId] === undefined) { + group = groups[group.parentId] + if (!group) break + toRestore.unshift(group) + } + + await this.actions.recreateParentGroups(tabs, toRestore, idsMap, tab.index) + for (let k = tab.index + 1; k < tabs.length; k++) { + tabs[k].index = k + } + } + } + + // Restore props + tab.panelId = data.panelId || lastPanel.id + if (idsMap[data.parentId] >= 0) tab.parentId = idsMap[data.parentId] + tab.folded = !!data.folded + idsMap[data.id] = tab.id + + // Normalize panelId + let panel = this.state.panelsMap[tab.panelId] + if (!panel) { + if (tab.pinned) tab.panelId = DEFAULT_CTX_ID + else tab.panelId = lastPanel.id } else { - let firstTabsPanel = this.state.panels.find(p => p.tabs) - if (firstTabsPanel) tab.panelId = firstTabsPanel.id - else tab.panelId = DEFAULT_CTX_ID + if (!tab.pinned) { + // Check order of panels + if (panel.index < lastPanel.index) tab.panelId = lastPanel.id + else lastPanel = panel + } } } - if (idsMap[tabData.parentId] >= 0) tab.parentId = idsMap[tabData.parentId] - tab.folded = tabData.folded - idsMap[tabData.id] = tab.id - prevTab = tab - if (tab.pinned && !this.state.panelsMap[tab.panelId]) tab.panelId = DEFAULT_CTX_ID + // Use openerTabId as fallback for parentId + if (tab.parentId === -1 && this.state.tabsMap[tab.openerTabId]) tab.parentId = tab.openerTabId + + this.state.tabsMap[tab.id] = tab + + // Find active tab + if (tab.active) activeTab = tab } this.state.tabs = tabs @@ -128,6 +127,9 @@ async function loadTabsFromGlobalStorage() { } } + // Set active tab id + this.state.activeTabId = activeTab.id + // Update succession if (this.state.activateAfterClosing !== 'none' && activeTab) { let target = Utils.findSuccessorTab(this.state, activeTab) @@ -144,103 +146,81 @@ async function loadTabsFromSessionStorage() { browser.tabs.query({ windowId }), browser.sessions.getWindowValue(this.state.windowId, 'groups'), ]) + if (!groups) groups = {} + // Check if there is blank tab with inlined info about tabs let dataTabIndex = tabs.findIndex(t => t.url.startsWith('about:blank#tabsdata')) if (dataTabIndex !== -1) { return await this.actions.loadTabsFromInlineData(tabs, dataTabIndex) } - if (!groups) groups = {} - let tabsData = await Promise.all( - tabs.map(t => { - return browser.sessions.getTabValue(t.id, 'data') - }) - ) + // Get previuos state of tabs + let tabsData = await Promise.all(tabs.map(t => browser.sessions.getTabValue(t.id, 'data'))) let activePanel = this.state.panels[this.state.panelIndex] || this.state.panels[1] + let lastPanel = this.state.panels.find(p => p.tabs).id + let offset = 0 let activeTab + let idsMap = {} // Set tabs initial props and update state this.state.tabsMap = [] - let oldNewMap = {} - let panel - let panelIndex = this.state.panels.findIndex(p => p.tabs) - for (let dt, t, i = 0; i < tabs.length; i++) { - t = tabs[i] - dt = tabsData[i] + for (let data, tab, i = 0; i < tabs.length; i++) { + tab = tabs[i] + data = tabsData[i - offset] - Utils.normalizeTab(t, null) - if ( - this.state.highlightOpenBookmarks && - this.state.bookmarksUrlMap && - this.state.bookmarksUrlMap[t.url] - ) { - for (let b of this.state.bookmarksUrlMap[t.url]) { - b.isOpen = true - } - } + let defaultPanel = tab.pinned ? DEFAULT_CTX_ID : null + Utils.normalizeTab(tab, defaultPanel) - if (dt) { - panel = this.state.panelsMap[dt.panelId] + // Highlight bookmarks with that url + this.actions.highlightBookmarks(tab.url) - if (dt.parentId > -1 && oldNewMap[dt.parentId] === undefined && groups[dt.parentId]) { - let group = groups[dt.parentId] + if (data) { + // Check if parent tab is missing and it group page + if (data.parentId > -1 && idsMap[data.parentId] === undefined && groups[data.parentId]) { + let group = groups[data.parentId] let toRestore = [group] - while (oldNewMap[group.parentId] === undefined && groups[group.parentId]) { + while (idsMap[group.parentId] === undefined && groups[group.parentId]) { group = groups[group.parentId] toRestore.unshift(group) } - for (let group, j = 0; j < toRestore.length; j++) { - group = toRestore[j] - let url = browser.runtime.getURL('group/group.html') + `#${group.groupId}` - let groupTab = await browser.tabs.create({ - windowId: this.state.windowId, - index: i + j, - url, - cookieStoreId: group.ctx, - active: false, - }) - groupTab.url = url - - tabs.splice(i + j, 0, groupTab) - tabsData.splice(i + j, 0, group) - for (let k = i + j + 1; k < tabs.length; k++) { - tabs[k].index = k - } - oldNewMap[group.id] = groupTab.id + await this.actions.recreateParentGroups(tabs, toRestore, idsMap, tab.index) + i += toRestore.length + offset += toRestore.length + for (let k = tab.index + 1; k < tabs.length; k++) { + tabs[k].index = k } - - i-- - continue - } - - if (Utils.isGroupUrl(t.url)) { - if (!this.state.groupTabs) this.state.groupTabs = {} - this.state.groupTabs[t.id] = true } - if (panel && panel.index >= panelIndex) { - panelIndex = panel.index - t.panelId = dt.panelId - } else if (this.state.panels[panelIndex]) { - t.panelId = this.state.panels[panelIndex].id - } - if (oldNewMap[dt.parentId] !== undefined) { - t.parentId = oldNewMap[dt.parentId] + // Restore props + tab.panelId = data.panelId || lastPanel.id + if (idsMap[data.parentId] >= 0) tab.parentId = idsMap[data.parentId] + tab.folded = !!data.folded + idsMap[data.id] = tab.id + + // Normalize panelId + let panel = this.state.panelsMap[tab.panelId] + if (!panel) { + if (tab.pinned) tab.panelId = DEFAULT_CTX_ID + else tab.panelId = lastPanel.id + } else { + if (!tab.pinned) { + // Check order of panels + if (panel.index < lastPanel.index) tab.panelId = lastPanel.id + else lastPanel = panel + } } - t.folded = dt.folded - oldNewMap[dt.id] = t.id } - this.state.tabsMap[t.id] = t - if (t.active) { - activeTab = t - this.state.activeTabId = t.id - } + // Use openerTabId as fallback for parentId + if (tab.parentId === -1 && this.state.tabsMap[tab.openerTabId]) tab.parentId = tab.openerTabId - if (t.pinned && !this.state.panelsMap[t.panelId]) t.panelId = DEFAULT_CTX_ID + this.state.tabsMap[tab.id] = tab + + // Find active tab + if (tab.active) activeTab = tab } this.state.tabs = tabs @@ -258,6 +238,9 @@ async function loadTabsFromSessionStorage() { } } + // Set active tab id + this.state.activeTabId = activeTab.id + // Update succession if (this.state.activateAfterClosing !== 'none' && activeTab) { let target = Utils.findSuccessorTab(this.state, activeTab) @@ -267,6 +250,36 @@ async function loadTabsFromSessionStorage() { this.state.tabs.forEach(t => this.actions.saveTabData(t)) } +/** + * Recreate group tabs + */ +async function recreateParentGroups(tabs, groups, idsMap, index) { + for (let group, j = 0; j < groups.length; j++) { + group = groups[j] + let groupId = Utils.getGroupId(group.url) + let url = browser.runtime.getURL('group/group.html') + `#${groupId}` + let groupTab = await browser.tabs.create({ + windowId: this.state.windowId, + index: index + j, + url, + cookieStoreId: group.ctx, + active: false, + }) + groupTab.url = url + + Utils.normalizeTab(groupTab, null) + + tabs.splice(index + j, 0, groupTab) + groupTab.panelId = group.panelId || DEFAULT_CTX_ID + if (idsMap[group.parentId] >= 0) groupTab.parentId = idsMap[group.parentId] + groupTab.folded = !!group.folded + idsMap[group.id] = groupTab.id + + this.state.tabsMap[groupTab.id] = groupTab + this.actions.saveTabData(groupTab) + } +} + /** * Load tabs using info from the first tab (e.g. for snapshots) */ @@ -423,35 +436,32 @@ function saveTabData(tabOrId) { panelId: tabOrId.panelId, parentId: tabOrId.parentId, folded: tabOrId.folded, + lvl: tabOrId.lvl, }) } function saveGroups(delay = 300) { - if (!this.state.groupTabs) return if (!this._saveGroupsTimeout) clearTimeout(this._saveGroupsTimeout) this._saveGroupsTimeout = setTimeout(() => { this._saveGroupsTimeout = null let groups = {} - for (let id of Object.keys(this.state.groupTabs)) { - if (!this.state.groupTabs[id]) continue - let groupTab = this.state.tabsMap[id] - if (!groupTab) continue - let prevTab = this.state.tabs[groupTab.index - 1] - let nextTab = this.state.tabs[groupTab.index + 1] + for (let tab of this.state.tabs) { + if (!Utils.isGroupUrl(tab.url)) continue + let prevTab = this.state.tabs[tab.index - 1] + let nextTab = this.state.tabs[tab.index + 1] let groupInfo = { - id, - index: groupTab.index, - ctx: groupTab.cookieStoreId, - groupId: Utils.getGroupId(groupTab.url), - panelId: groupTab.panelId, - parentId: groupTab.parentId, - folded: groupTab.folded, - url: groupTab.url, + id: tab.id, + index: tab.index, + ctx: tab.cookieStoreId, + panelId: tab.panelId, + parentId: tab.parentId, + folded: tab.folded, + url: tab.url, } if (prevTab) groupInfo.prevTab = prevTab.id if (nextTab) groupInfo.nextTab = nextTab.id - groups[id] = groupInfo + groups[tab.id] = groupInfo } browser.sessions.setWindowValue(this.state.windowId, 'groups', groups) @@ -668,10 +678,10 @@ function switchTab(globaly, cycle, step, pinned) { let activePanel = this.state.panels[this.state.panelIndex] if (!activePanel || !activePanel.tabs) return - let tab, - index = activeTab.index, - t = true, - cycled = false + let tab + let index = activeTab.index + let t = true + let cycled = false if ( (!pinned && !globaly && activeTab.panelId !== activePanel.id) || @@ -2059,7 +2069,9 @@ function resetUpdateGroupTabTimeout() { } function updateActiveGroupPage() { - let activeTab = this.state.tabs.find(t => t.active) + let activeTab = this.state.tabsMap[this.state.activeTabId] + if (!activeTab) activeTab = this.state.tabs.find(t => t.active) + if (!activeTab) return if (Utils.isGroupUrl(activeTab.url)) { this.actions.updateGroupTab(activeTab) } @@ -2279,6 +2291,18 @@ function handleReopening(tabId, newCtx) { return index } +/** + * Update indexes of tabs + */ +function updateTabsIndexes(fromIndex = 0, toIndex = -1) { + let tabs = this.state.tabs + if (toIndex === -1) toIndex = tabs.length + for (let t, i = fromIndex; i < toIndex; i++) { + t = tabs[i] + if (t && t.index !== i) t.index = i + } +} + // ----------------- // --- Old stuff --- // @@ -2451,6 +2475,7 @@ async function _old_restoreTabsTree() { // --------------------- export default { + recreateParentGroups, loadTabsFromGlobalStorage, loadTabsFromSessionStorage, loadTabsFromInlineData, @@ -2520,6 +2545,7 @@ export default { checkUrlRules, updateHighlightedTabs, handleReopening, + updateTabsIndexes, _old_loadTabs, _old_restoreGroupTab, diff --git a/src/sidebar/handlers/tabs.js b/src/sidebar/handlers/tabs.js index d444b2f6a..5f455581f 100644 --- a/src/sidebar/handlers/tabs.js +++ b/src/sidebar/handlers/tabs.js @@ -173,7 +173,6 @@ function onTabCreated(tab) { } if (this.state.stateStorage === 'session' && tab.url.startsWith(GROUP_URL)) { - this.state.groupTabs[tab.id] = true this.actions.saveGroups() } @@ -258,13 +257,8 @@ function onTabUpdated(tabId, change, tab) { if (!change.url.startsWith(localTab.url.slice(0, 16))) { localTab.favIconUrl = '' } - if (this.state.stateStorage === 'session') { - if (change.url.startsWith(GROUP_URL)) { - this.state.groupTabs[localTab.id] = true - this.actions.saveGroups() - } else if (this.state.groupTabs[localTab.id]) { - this.state.groupTabs[localTab.id] = false - } + if (this.state.stateStorage === 'session' && change.url.startsWith(GROUP_URL)) { + this.actions.saveGroups() } if ( this.state.urlRules && @@ -318,9 +312,7 @@ function onTabUpdated(tabId, change, tab) { this.state.tabs.splice(localTab.index, 1) this.state.tabs.splice(panel.startIndex - 1, 0, localTab) panel.tabs.splice(0, 0, localTab) - for (let i = panel.startIndex - 1; i < this.state.tabs.length; i++) { - this.state.tabs[i].index = i - } + this.actions.updateTabsIndexes() if (panel && panel.tabs) browser.tabs.move(tabId, { index: panel.startIndex - 1 }) this.actions.updatePanelsRanges() } @@ -339,6 +331,7 @@ function onTabUpdated(tabId, change, tab) { if (localTab.moveTime + 1000 > Date.now()) { localTab.panelId = localTab.prevPanelId panel = this.state.panelsMap[localTab.panelId] + if (this.state.stateStorage === 'session') this.actions.saveTabData(localTab) } } @@ -359,6 +352,7 @@ function onTabUpdated(tabId, change, tab) { */ function onTabRemoved(tabId, info, childfree) { if (info.windowId !== this.state.windowId) return + if (info.isWindowClosing) return if (!this.state.removingTabs) this.state.removingTabs = [] else this.state.removingTabs.splice(this.state.removingTabs.indexOf(tabId), 1) @@ -424,10 +418,6 @@ function onTabRemoved(tabId, info, childfree) { } this.state.tabsMap[tabId] = undefined this.state.tabs.splice(tab.index, 1) - if (this.state.stateStorage === 'session' && this.state.groupTabs[tabId]) { - this.state.groupTabs[tabId] = false - this.actions.saveGroups() - } // Remove tab from panel if (panel && panel.tabs) { @@ -448,25 +438,28 @@ function onTabRemoved(tabId, info, childfree) { panel.updated.splice(i, 1) } - if (!this.state.removingTabs.length) this.actions.recalcPanelScroll() + // On removing the last tab + if (!this.state.removingTabs.length) { + this.actions.recalcPanelScroll() - // Update tree - if (this.state.tabsTree && !this.state.removingTabs.length) { - const startIndex = panel ? panel.startIndex : 0 - const endIndex = panel ? panel.endIndex + 1 : -1 - this.actions.updateTabsTree(startIndex, endIndex) - } + // Update tree + if (this.state.tabsTree) { + const startIndex = panel ? panel.startIndex : 0 + const endIndex = panel ? panel.endIndex + 1 : -1 + this.actions.updateTabsTree(startIndex, endIndex) + } - if (!this.state.removingTabs.length) { + // Save new tabs state if (this.state.stateStorage === 'global') this.actions.saveTabsData() - } - - // Update succession - if (!this.state.removingTabs.length && this.state.activateAfterClosing !== 'none') { - const activeTab = this.state.tabsMap[this.state.activeTabId] - if (activeTab && activeTab.active) { - const target = Utils.findSuccessorTab(this.state, activeTab) - if (target) browser.tabs.moveInSuccession([activeTab.id], target.id) + if (this.state.stateStorage === 'session') this.actions.saveGroups() + + // Update succession + if (this.state.activateAfterClosing !== 'none') { + const activeTab = this.state.tabsMap[this.state.activeTabId] + if (activeTab && activeTab.active) { + const target = Utils.findSuccessorTab(this.state, activeTab) + if (target) browser.tabs.moveInSuccession([activeTab.id], target.id) + } } } @@ -476,11 +469,11 @@ function onTabRemoved(tabId, info, childfree) { this.state.bookmarksUrlMap && this.state.bookmarksUrlMap[tab.url] ) { - for (let t of this.state.tabs) { - if (t.url === tab.url) return - } - for (let b of this.state.bookmarksUrlMap[tab.url]) { - b.isOpen = false + let tabWithSameUrl = this.state.tabs.find(t => t.url === tab.url) + if (!tabWithSameUrl) { + for (let b of this.state.bookmarksUrlMap[tab.url]) { + b.isOpen = false + } } } @@ -539,9 +532,7 @@ function onTabMoved(id, info) { // Update tabs indexes. const minIndex = Math.min(info.fromIndex, info.toIndex) const maxIndex = Math.max(info.fromIndex, info.toIndex) - for (let i = minIndex; i <= maxIndex; i++) { - if (this.state.tabs[i]) this.state.tabs[i].index = i - } + this.actions.updateTabsIndexes(minIndex, maxIndex + 1) // Move tab in panel let srcPanel diff --git a/src/sidebar/sidebar.js b/src/sidebar/sidebar.js index 21968ae3a..08157e2a1 100644 --- a/src/sidebar/sidebar.js +++ b/src/sidebar/sidebar.js @@ -85,8 +85,8 @@ export default new Vue({ if (State.stateStorage === 'global') Actions.saveTabsData() if (State.stateStorage === 'session') Actions.saveGroups() - Actions.connectToBG() Actions.updateActiveGroupPage() + Actions.connectToBG() }, mounted() {