Skip to content

Commit fa6ffaf

Browse files
committed
fix(LocalTabs): Poperly distinguish between window IDs and group IDs
(important for mappings and tree index) Signed-off-by: Marcel Klehr <mklehr@gmx.net>
1 parent 71229e1 commit fa6ffaf

File tree

1 file changed

+38
-22
lines changed

1 file changed

+38
-22
lines changed

src/lib/LocalTabs.ts

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,22 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
1515
this.queue = new PQueue({ concurrency: 10 })
1616
}
1717

18+
private getWindowIdFromFolderId(folderId:string|number):number {
19+
return parseInt(String(folderId).slice('window-'.length))
20+
}
21+
22+
private getTabGroupIdFromFolderId(folderId:string|number):number {
23+
return parseInt(String(folderId).slice('group-'.length))
24+
}
25+
26+
private getFolderIdFromWindowId(windowId:number):string {
27+
return 'window-' + windowId
28+
}
29+
30+
private getFolderIdFromTabGroupId(tabGroupId:number):string {
31+
return 'group-' + tabGroupId
32+
}
33+
1834
async getBookmarksTree():Promise<Folder<typeof ItemLocation.LOCAL>> {
1935
let tabs = await browser.tabs.query({
2036
windowType: 'normal' // no devtools or panels or popups
@@ -46,7 +62,7 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
4662
id: t.id,
4763
title: t.title,
4864
url: t.url,
49-
parentId: windowId,
65+
parentId: this.getFolderIdFromWindowId(windowId),
5066
location: ItemLocation.LOCAL,
5167
}))
5268

@@ -63,7 +79,7 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
6379
id: t.id,
6480
title: t.title,
6581
url: t.url,
66-
parentId: group.id,
82+
parentId: this.getFolderIdFromTabGroupId(group.id),
6783
location: ItemLocation.LOCAL,
6884
}))
6985

@@ -77,8 +93,8 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
7793
return {
7894
folder: new Folder({
7995
title: group.title || `Group ${group.id}`,
80-
id: group.id,
81-
parentId: windowId,
96+
id: this.getFolderIdFromTabGroupId(group.id),
97+
parentId: this.getFolderIdFromWindowId(windowId),
8298
location: ItemLocation.LOCAL,
8399
children: groupTabs
84100
}),
@@ -110,7 +126,7 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
110126

111127
return new Folder({
112128
title: 'Window ' + i,
113-
id: windowId,
129+
id: this.getFolderIdFromWindowId(windowId),
114130
parentId: 'tabs',
115131
location: ItemLocation.LOCAL,
116132
children: sortedItems
@@ -138,7 +154,7 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
138154
// Try to query the tab group to see if it exists
139155
if (typeof browser.tabGroups !== 'undefined') {
140156
const tabGroup = await this.queue.add(() =>
141-
browser.tabGroups.get(bookmark.parentId)
157+
browser.tabGroups.get(this.getTabGroupIdFromFolderId(bookmark.parentId))
142158
)
143159
if (tabGroup) {
144160
isTabGroup = true
@@ -169,7 +185,7 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
169185
await this.queue.add(() =>
170186
browser.tabs.group({
171187
tabIds: [node.id],
172-
groupId: bookmark.parentId
188+
groupId: this.getTabGroupIdFromFolderId(bookmark.parentId)
173189
})
174190
)
175191
}
@@ -209,7 +225,7 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
209225
// Try to query the tab group to see if it exists
210226
if (typeof browser.tabGroups !== 'undefined') {
211227
const tabGroup = await this.queue.add(() =>
212-
browser.tabGroups.get(bookmark.parentId)
228+
browser.tabGroups.get(this.getTabGroupIdFromFolderId(bookmark.parentId))
213229
)
214230
isTabGroup = !!tabGroup
215231
}
@@ -225,7 +241,7 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
225241
await this.queue.add(() =>
226242
browser.tabs.group({
227243
tabIds: [bookmark.id],
228-
groupId: bookmark.parentId
244+
groupId: this.getTabGroupIdFromFolderId(bookmark.parentId)
229245
})
230246
)
231247
} else {
@@ -240,7 +256,7 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
240256
Logger.log('Moving tab to window', bookmark.parentId)
241257
await this.queue.add(() =>
242258
browser.tabs.move(bookmark.id, {
243-
windowId: bookmark.parentId,
259+
windowId: this.getWindowIdFromFolderId(bookmark.parentId),
244260
index: -1, // last
245261
})
246262
)
@@ -268,22 +284,22 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
268284
await awaitTabsUpdated()
269285
}
270286

271-
async createFolder(folder:Folder<typeof ItemLocation.LOCAL>): Promise<number> {
287+
async createFolder(folder:Folder<typeof ItemLocation.LOCAL>): Promise<string> {
272288
Logger.log('(tabs)CREATEFOLDER', folder)
273289

274290
// If parentId is 'tabs', create a window
275291
if (folder.parentId === 'tabs') {
276292
const node = await this.queue.add(() =>
277293
browser.windows.create()
278294
)
279-
return node.id
295+
return this.getFolderIdFromWindowId(node.id)
280296
} else {
281297
// Otherwise, create a tab group
282298
try {
283299
const groupId = await this.queue.add(async() => {
284300
// Create a dummy tab in the parent window to hold the group
285301
const dummyTab = await browser.tabs.create({
286-
windowId: folder.parentId,
302+
windowId: this.getWindowIdFromFolderId(folder.parentId),
287303
url: 'about:blank',
288304
active: false
289305
})
@@ -292,8 +308,8 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
292308
const groupId = await browser.tabs.group({
293309
tabIds: [dummyTab.id],
294310
createProperties: {
295-
windowId: folder.parentId
296-
}
311+
windowId: this.getWindowIdFromFolderId(folder.parentId),
312+
},
297313
})
298314

299315
// Update the tab group title
@@ -318,7 +334,7 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
318334
})
319335

320336
await awaitTabsUpdated()
321-
return groupId
337+
return this.getFolderIdFromTabGroupId(groupId)
322338
} catch (e) {
323339
Logger.log('Failed to create tab group', e)
324340
throw e
@@ -339,7 +355,7 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
339355
if (tabGroupsSupported && id !== 'tabs') {
340356
try {
341357
// Try to get the tab group to see if it exists
342-
const tabs = await this.queue.add(() => browser.tabs.query({groupId: id}))
358+
const tabs = await this.queue.add(() => browser.tabs.query({groupId: this.getTabGroupIdFromFolderId(id)}))
343359
if (tabs.length) {
344360
isTabGroup = true
345361
// Get the tab group's current index
@@ -382,14 +398,14 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
382398
try {
383399
if (tabGroupsSupported) {
384400
await this.queue.add(() =>
385-
browser.tabGroups.move(folder.id, { index: currentIndex })
401+
browser.tabGroups.move(this.getTabGroupIdFromFolderId(folder.id), { index: currentIndex })
386402
)
387403

388404
// Get the size of the folder (number of tabs in the group)
389405
const folderTabs = await this.queue.add(() =>
390406
browser.tabs.query({
391407
windowType: 'normal',
392-
groupId: folder.id
408+
groupId: this.getTabGroupIdFromFolderId(folder.id)
393409
})
394410
)
395411

@@ -431,7 +447,7 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
431447
try {
432448
// Update the tab group title
433449
await this.queue.add(() =>
434-
browser.tabGroups.update(folder.id, {
450+
browser.tabGroups.update(this.getTabGroupIdFromFolderId(folder.id), {
435451
title: folder.title
436452
})
437453
)
@@ -453,7 +469,7 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
453469
// If parentId is 'tabs', it's a window
454470
if (folder.parentId === 'tabs') {
455471
try {
456-
await this.queue.add(() => browser.windows.remove(id))
472+
await this.queue.add(() => browser.windows.remove(this.getWindowIdFromFolderId(id)))
457473
} catch (e) {
458474
Logger.log('Failed to remove window', e)
459475
// Don't throw error if the window doesn't exist anymore
@@ -467,7 +483,7 @@ export default class LocalTabs implements OrderFolderResource<typeof ItemLocatio
467483
// Get all tabs in the group
468484
const tabs = await this.queue.add(() =>
469485
browser.tabs.query({
470-
groupId: id
486+
groupId: this.getTabGroupIdFromFolderId(id)
471487
})
472488
)
473489

0 commit comments

Comments
 (0)