Skip to content

Commit

Permalink
Fixed: Converting bookmarks panel to tabs panel
Browse files Browse the repository at this point in the history
  • Loading branch information
mbnuqw committed Apr 6, 2023
1 parent 870a039 commit 64231d5
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 8 deletions.
26 changes: 25 additions & 1 deletion src/services/bookmarks.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ export async function open(

// Set url for parent node
const prev = toOpen[toOpen.length - 1]
if (prev && prev.id === info.parentId && prev.title === info.title && info.url) {
if (prev && prev.id === info.parentId && bookmarkIsParentTab(node, prev.title)) {
prev.url = info.url
continue
}
Expand Down Expand Up @@ -1409,3 +1409,27 @@ export function getTargetTabsPanelId(): ID {
}
return panelId
}

export function isFolderWithURL(folder: Bookmark): boolean {
if (!folder.children) return false

const firstChild = folder.children[0]
if (!firstChild?.url) return false

const title = folder.title
const childTitle = firstChild.title

if (childTitle === title) return true

return childTitle.startsWith(title) && childTitle[title.length + 1] === '['
}

function bookmarkIsParentTab(node: Bookmark, parentTitle?: string): boolean {
if (!node.url || !parentTitle) return false

const childTitle = node.title

if (childTitle === parentTitle) return true

return childTitle.startsWith(parentTitle) && childTitle[parentTitle.length + 1] === '['
}
19 changes: 13 additions & 6 deletions src/services/sidebar.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1787,12 +1787,11 @@ export async function restoreFromBookmarks(panel: TabsPanel, silent?: boolean):

// Set url for parent
if (!node.url && node.children) {
const firstChild = node.children[0]

// Use first child for parent tab
if (firstChild && firstChild.url && firstChild.title === node.title) {
const firstChild = node.children[0]
if (Bookmarks.isFolderWithURL(node)) {
rawUrl = firstChild.url
info.url = Utils.normalizeUrl(firstChild.url, firstChild.title)
info.url = Utils.normalizeUrl(firstChild.url, node.title)
usedAsParent[firstChild.id] = true
}

Expand Down Expand Up @@ -1828,7 +1827,11 @@ export async function restoreFromBookmarks(panel: TabsPanel, silent?: boolean):
active: false,
cookieStoreId: info.container,
}
Tabs.setNewTabPosition(indexPinned, NOID, panel.id)
Tabs.setNewTabPosition(indexPinned, NOID, panel.id, false)
if (info.url) {
const containerId = Containers.getContainerFor(info.url)
if (containerId) conf.cookieStoreId = containerId
}
const newTab = await browser.tabs.create(conf)
idsMap[info.id] = newTab.id
indexPinned++
Expand Down Expand Up @@ -1877,13 +1880,17 @@ export async function restoreFromBookmarks(panel: TabsPanel, silent?: boolean):
active: false,
cookieStoreId: info.container,
}
if (info.url) {
const containerId = Containers.getContainerFor(info.url)
if (containerId) conf.cookieStoreId = containerId
}
const isDefaultContainer = !conf.cookieStoreId || conf.cookieStoreId === CONTAINER_ID
const parentId = idsMap[info.parentId ?? NOID] ?? NOID
if (conf.url && !conf.url.startsWith('about') && isDefaultContainer) {
conf.discarded = true
conf.title = info.title
}
Tabs.setNewTabPosition(index, parentId, panel.id)
Tabs.setNewTabPosition(index, parentId, panel.id, false)
const newTab = await browser.tabs.create(conf)
idsMap[info.id] = newTab.id
}
Expand Down
8 changes: 7 additions & 1 deletion src/services/tabs.fg.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3153,10 +3153,16 @@ export function updateTabsIndexes(fromIndex = 0, toIndex = -1): void {
/**
* Set expected position (parent/panel) of new tab by its index
*/
export function setNewTabPosition(index: number, parentId: ID, panelId: ID): void {
export function setNewTabPosition(
index: number,
parentId: ID,
panelId: ID,
unread?: boolean
): void {
Tabs.newTabsPosition[index] = {
parent: parentId,
panel: panelId,
unread: unread,
}
}

Expand Down
1 change: 1 addition & 0 deletions src/services/tabs.fg.handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ function onTabCreated(tab: Tab, attached?: boolean): void {
}
index = tab.index
tab.openerTabId = position.parent
if (position.unread !== undefined) tab.unread = position.unread
delete Tabs.newTabsPosition[tab.index]

// Handle tab reopening
Expand Down
1 change: 1 addition & 0 deletions src/types/tabs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export interface ActiveTabsHistory {
export interface NewTabPosition {
parent: ID
panel: ID
unread?: boolean
}

export interface GroupedTabInfo {
Expand Down

0 comments on commit 64231d5

Please sign in to comment.