Skip to content

Commit

Permalink
Fixed: Store tabs data in both session and global storages
Browse files Browse the repository at this point in the history
(v5 prep)
  • Loading branch information
mbnuqw committed Apr 13, 2021
1 parent d3ff7e5 commit 8063976
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 93 deletions.
6 changes: 2 additions & 4 deletions src/sidebar/actions/panels.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,10 +95,8 @@ async function updatePanels(newPanels) {
}

// Save state
if (this.state.stateStorage === 'global') this.actions.saveTabsData()
if (this.state.stateStorage === 'session') {
this.state.tabs.forEach(t => this.actions.saveTabData(t))
}
this.actions.saveTabsData()
this.state.tabs.forEach(t => this.actions.saveTabData(t))
}

/**
Expand Down
16 changes: 0 additions & 16 deletions src/sidebar/actions/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ function updateSettings(settings) {
let toggleBookmarks = this.state.bookmarksPanel !== settings.bookmarksPanel
let theme = this.state.theme !== settings.theme
let highlightOpenBookmarks = this.state.highlightOpenBookmarks !== settings.highlightOpenBookmarks
let stateStorage = this.state.stateStorage !== settings.stateStorage
let bgNoise = this.state.bgNoise !== settings.bgNoise
let ctxMenuCtrIgnore = this.state.ctxMenuIgnoreContainers !== settings.ctxMenuIgnoreContainers

Expand Down Expand Up @@ -87,21 +86,6 @@ function updateSettings(settings) {
Actions.initTheme()
}

if (stateStorage) {
if (this.state.stateStorage === 'global') this.actions.saveTabsData()
if (this.state.stateStorage === 'session') {
for (let tab of this.state.tabs) {
browser.sessions.setTabValue(tab.id, 'data', {
id: tab.id,
panelId: tab.panelId,
parentId: tab.parentId,
folded: tab.folded,
})
}
this.actions.saveGroups()
}
}

if (bgNoise) {
if (this.state.bgNoise) this.actions.applyNoiseBg()
else this.actions.removeNoiseBg()
Expand Down
55 changes: 20 additions & 35 deletions src/sidebar/actions/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,6 @@ async function loadTabsFromSessionStorage() {
let target = Utils.findSuccessorTab(this.state, activeTab)
if (target) browser.tabs.moveInSuccession([activeTab.id], target.id)
}

this.state.tabs.forEach(t => this.actions.saveTabData(t))
}

/**
Expand Down Expand Up @@ -429,11 +427,9 @@ async function loadTabsFromInlineData(tabs, dataTabIndex) {
if (target) browser.tabs.moveInSuccession([activeTab.id], target.id)
}

if (this.state.stateStorage === 'global') this.actions.saveTabsData()
if (this.state.stateStorage === 'session') {
this.state.tabs.forEach(t => this.actions.saveTabData(t))
this.actions.saveGroups()
}
this.actions.saveTabsData()
this.state.tabs.forEach(t => this.actions.saveTabData(t))
this.actions.saveGroups()
}

/**
Expand Down Expand Up @@ -496,7 +492,6 @@ function saveTabData(tabOrId) {
panelId: tabOrId.panelId,
parentId: tabOrId.parentId,
folded: tabOrId.folded,
lvl: tabOrId.lvl,
})
}

Expand Down Expand Up @@ -628,11 +623,9 @@ function normalizeTabs(delay = 500) {
this.state.tabsNormalizing = false
this._normTabsMoving = false

if (this.state.stateStorage === 'global') this.actions.saveTabsData()
if (this.state.stateStorage === 'session') {
this.state.tabs.forEach(t => this.actions.saveTabData(t))
this.actions.saveGroups()
}
this.actions.saveTabsData()
this.state.tabs.forEach(t => this.actions.saveTabData(t))
this.actions.saveGroups()
}, delay)
}

Expand Down Expand Up @@ -1418,7 +1411,7 @@ async function moveTabsToNewWin(tabIds, incognito = false) {

await Promise.all(moving)

if (this.state.stateStorage === 'global') this.actions.saveTabsData()
this.actions.saveTabsData()
}

/**
Expand Down Expand Up @@ -1475,7 +1468,7 @@ async function moveTabsToWin(tabIds, windowOrConfig) {
)
}

if (this.state.stateStorage === 'global') this.actions.saveTabsData()
this.actions.saveTabsData()
}

/**
Expand Down Expand Up @@ -1606,10 +1599,8 @@ async function moveTabsToPanel(tabIds, panelId) {
this.actions.updateTabsTree(activePanel.startIndex, activePanel.endIndex + 1)
}
if (activeTab) this.state.panelIndex = targetPanel.index
if (this.state.stateStorage === 'global') this.actions.saveTabsData()
if (this.state.stateStorage === 'session') {
tabs.forEach(t => this.actions.saveTabData(t))
}
this.actions.saveTabsData()
tabs.forEach(t => this.actions.saveTabData(t))
}

/**
Expand Down Expand Up @@ -1697,8 +1688,8 @@ function foldTabsBranch(tabId) {
if (this.state.hideFoldedTabs && toHide.length) {
browser.tabs.hide(toHide)
}
if (this.state.stateStorage === 'global') this.actions.saveTabsData()
if (this.state.stateStorage === 'session') this.actions.saveTabData(tabId)
this.actions.saveTabsData()
this.actions.saveTabData(tabId)
}

/**
Expand Down Expand Up @@ -1758,8 +1749,8 @@ function expTabsBranch(tabId) {
if (this.state.hideFoldedTabs && toShow.length) {
browser.tabs.show(toShow)
}
if (this.state.stateStorage === 'global') this.actions.saveTabsData()
if (this.state.stateStorage === 'session') this.actions.saveTabData(tabId)
this.actions.saveTabsData()
this.actions.saveTabData(tabId)
}

/**
Expand Down Expand Up @@ -1980,10 +1971,8 @@ async function moveDroppedNodes(dropIndex, dropParent, nodes, pin, currentPanel)

// If there are no moving, just update tabs tree
this.actions.updateTabsTree(currentPanel.startIndex, currentPanel.endIndex + 1)
if (this.state.stateStorage === 'global') this.actions.saveTabsData()
if (this.state.stateStorage === 'session') {
tabs.forEach(t => this.actions.saveTabData(t))
}
this.actions.saveTabsData()
tabs.forEach(t => this.actions.saveTabData(t))
}

// Hide/Show tabs
Expand Down Expand Up @@ -2151,10 +2140,8 @@ function flattenTabs(tabIds) {
}

this.actions.updateTabsTree(ttf[0].index - 1, ttf[ttf.length - 1].index + 1)
if (this.state.stateStorage === 'global') this.actions.saveTabsData()
if (this.state.stateStorage === 'session') {
ttf.forEach(t => this.actions.saveTabData(t))
}
this.actions.saveTabsData()
ttf.forEach(t => this.actions.saveTabData(t))
}

/**
Expand Down Expand Up @@ -2228,10 +2215,8 @@ async function groupTabs(tabIds, conf = {}) {
}
}
this.actions.updateTabsTree(tabs[0].index - 2, tabs[tabs.length - 1].index + 1)
if (this.state.stateStorage === 'global') this.actions.saveTabsData()
if (this.state.stateStorage === 'session') {
tabs.forEach(t => this.actions.saveTabData(t))
}
this.actions.saveTabsData()
tabs.forEach(t => this.actions.saveTabData(t))
}

/**
Expand Down
24 changes: 8 additions & 16 deletions src/sidebar/handlers/keybindings.js
Original file line number Diff line number Diff line change
Expand Up @@ -551,10 +551,8 @@ function onKeyTabsIndent() {
align.forEach(([a, b]) => (a.parentId = b.parentId))

this.actions.updateTabsTree()
if (this.state.stateStorage === 'global') this.actions.saveTabsData()
if (this.state.stateStorage === 'session') {
selected.forEach(id => this.actions.saveTabData(id))
}
this.actions.saveTabsData()
selected.forEach(id => this.actions.saveTabData(id))
}

function onKeyTabsOutdent() {
Expand Down Expand Up @@ -589,10 +587,8 @@ function onKeyTabsOutdent() {
}

this.actions.updateTabsTree()
if (this.state.stateStorage === 'global') this.actions.saveTabsData()
if (this.state.stateStorage === 'session') {
selected.forEach(id => this.actions.saveTabData(id))
}
this.actions.saveTabsData()
selected.forEach(id => this.actions.saveTabData(id))
}

function onKeyMoveTabsUp() {
Expand Down Expand Up @@ -643,10 +639,8 @@ function onKeyMoveTabsUp() {
}

this.actions.updateTabsTree()
if (this.state.stateStorage === 'global') this.actions.saveTabsData()
if (this.state.stateStorage === 'session') {
toMove.forEach(t => this.actions.saveTabData(t))
}
this.actions.saveTabsData()
toMove.forEach(t => this.actions.saveTabData(t))

if (this._preserveSelectionTimeout) {
clearTimeout(this._preserveSelectionTimeout)
Expand Down Expand Up @@ -707,10 +701,8 @@ function onKeyMoveTabsDown() {
}

this.actions.updateTabsTree()
if (this.state.stateStorage === 'global') this.actions.saveTabsData()
if (this.state.stateStorage === 'session') {
toMove.forEach(t => this.actions.saveTabData(t))
}
this.actions.saveTabsData()
toMove.forEach(t => this.actions.saveTabData(t))

if (this._preserveSelectionTimeout) {
clearTimeout(this._preserveSelectionTimeout)
Expand Down
32 changes: 12 additions & 20 deletions src/sidebar/handlers/tabs.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ function onTabCreated(tab) {
}
}

if (this.state.stateStorage === 'global') this.actions.saveTabsData()
if (this.state.stateStorage === 'session') this.actions.saveTabData(tab)
this.actions.saveTabsData()
this.actions.saveTabData(tab)

const groupTab = this.actions.getGroupTab(tab)
if (groupTab && !groupTab.discarded) {
Expand Down Expand Up @@ -168,9 +168,7 @@ function onTabCreated(tab) {
}
}

if (this.state.stateStorage === 'session' && tab.url.startsWith(GROUP_URL)) {
this.actions.saveGroups()
}
if (tab.url.startsWith(GROUP_URL)) this.actions.saveGroups()

this.actions.recalcPanelScroll()

Expand Down Expand Up @@ -246,7 +244,7 @@ function onTabUpdated(tabId, change, tab) {

// Url
if (change.url !== undefined && change.url !== localTab.url) {
if (this.state.stateStorage === 'global') this.actions.saveTabsData()
this.actions.saveTabsData()
if (this.state.highlightOpenBookmarks && this.state.bookmarksUrlMap) {
if (this.state.bookmarksUrlMap[localTab.url]) {
for (let b of this.state.bookmarksUrlMap[localTab.url]) {
Expand All @@ -262,9 +260,7 @@ function onTabUpdated(tabId, change, tab) {
if (!change.url.startsWith(localTab.url.slice(0, 16))) {
localTab.favIconUrl = ''
}
if (this.state.stateStorage === 'session' && change.url.startsWith(GROUP_URL)) {
this.actions.saveGroups()
}
if (change.url.startsWith(GROUP_URL)) this.actions.saveGroups()
if (
this.state.urlRules &&
this.state.urlRules.length &&
Expand Down Expand Up @@ -353,7 +349,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)
this.actions.saveTabData(localTab)
}
}

Expand Down Expand Up @@ -496,8 +492,8 @@ function onTabRemoved(tabId, info, childfree) {
}

// Save new tabs state
if (this.state.stateStorage === 'global') this.actions.saveTabsData()
if (this.state.stateStorage === 'session') this.actions.saveGroups()
this.actions.saveTabsData()
this.actions.saveGroups()

// Update succession
if (this.state.activateAfterClosing !== 'none') {
Expand Down Expand Up @@ -561,10 +557,8 @@ function onTabMoved(id, info) {
if (info.toIndex > info.fromIndex) toIndex = toIndex - mvLen
let tabAtTargetPlace = this.state.tabs[toIndex]
if (tabAtTargetPlace && tabAtTargetPlace.id === id) {
if (this.state.stateStorage === 'global' && !this.state.movingTabs.length) {
this.actions.saveTabsData()
}
if (this.state.stateStorage === 'session') this.actions.saveTabData(id)
if (!this.state.movingTabs.length) this.actions.saveTabsData()
this.actions.saveTabData(id)
if (this.state.tabsMap[id]) this.state.tabsMap[id].destPanelId = undefined
return
}
Expand Down Expand Up @@ -640,10 +634,8 @@ function onTabMoved(id, info) {
this.actions.setPanel(this.state.panelsMap[movedTab.panelId].index)
}

if (this.state.stateStorage === 'global' && !this.state.movingTabs.length) {
this.actions.saveTabsData()
}
if (this.state.stateStorage === 'session') this.actions.saveTabData(movedTab)
if (!this.state.movingTabs.length) this.actions.saveTabsData()
this.actions.saveTabData(movedTab)

// Update succession
if (!this.state.movingTabs.length && this.state.activateAfterClosing !== 'none') {
Expand Down
5 changes: 3 additions & 2 deletions src/sidebar/sidebar.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,9 @@ export default new Vue({
Actions.loadFavicons()
Actions.loadPermissions(true)
Actions.updateTabsVisability()
if (State.stateStorage === 'global') Actions.saveTabsData()
if (State.stateStorage === 'session') Actions.saveGroups()
Actions.saveTabsData()
Actions.saveGroups()
State.tabs.forEach(t => Actions.saveTabData(t))

Actions.updateActiveGroupPage()
Actions.connectToBG()
Expand Down

0 comments on commit 8063976

Please sign in to comment.