-
Notifications
You must be signed in to change notification settings - Fork 0
/
tabtree.ts
133 lines (128 loc) · 3.7 KB
/
tabtree.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/**
* Chrome does not manage "openerTabId" rigidly,
* so we need to manage tab tree by ourselves.
*/
export class TabTree {
constructor(tabs: chrome.tabs.Tab[], windowNumberMap: WindowNumberMap) {
this.tree = new Map()
tabs
.map((tab) => convertTabToTabTreeNode(tab, windowNumberMap))
.forEach((tabTreeNode) => {
this.tree.set(tabTreeNode.id, tabTreeNode)
})
}
tree: Map<chrome.tabs.Tab['id'], TabTreeNode>
addTabToTree(
tab: chrome.tabs.Tab,
parentId: chrome.tabs.Tab['id'] | null,
windowNumberMap: WindowNumberMap,
) {
const newTabTreeNode: TabTreeNode = convertTabToTabTreeNode(
tab,
windowNumberMap,
)
newTabTreeNode.parentId = parentId
this.tree.set(tab.id, newTabTreeNode)
if (parentId) {
const parentOfNewTab = this.tree.get(parentId)
if (parentOfNewTab) {
parentOfNewTab.childrenIds.push(tab.id)
} else {
console.error('parent of new tab does not exist')
}
}
}
/**
* It removes the tab from the tree,
* and reassigns children of the removed tab to its parent.
*/
removeTabFromTree(removedTabId: chrome.tabs.Tab['id']) {
const parentId = this.tree.get(removedTabId).parentId
const childrenIds = this.tree.get(removedTabId).childrenIds
if (parentId) {
const parentNode = this.tree.get(parentId)
if (parentNode) {
parentNode.childrenIds.splice(
parentNode.childrenIds.indexOf(removedTabId),
1,
)
parentNode.childrenIds.push(...childrenIds)
} else {
console.error('parent of removed tab does not exist')
}
}
childrenIds.forEach((childId) => {
const childNode = this.tree.get(childId)
if (childNode) {
childNode.parentId = parentId
} else {
console.error('child of removed tab does not exist:', childId)
}
})
this.tree.delete(removedTabId)
}
changeWindowOfTabInTree(
tabId: chrome.tabs.Tab['id'],
newWindowId: chrome.tabs.Tab['windowId'],
windowNumberMap: WindowNumberMap,
) {
this.tree.get(tabId).windowNumber = calcWindowNumber(
newWindowId,
windowNumberMap,
)
}
updateTabRefs(tabs: chrome.tabs.Tab[]) {
Array.from(this.tree.keys()).forEach((tabId) => {
const tab = tabs.find((tab) => tab.id === tabId)
if (tab) {
this.tree.get(tabId).tabRef = tab
} else {
console.error('tab does not exist:', tabId)
}
})
}
}
/**
* This type is used to send tab tree to side panel,
* because it's not possible to send Map
* by using chrome.runtime.sendMessage.
*/
export type TabTreeMessage = {
name: 'tabTree'
data: [TabTreeNode['id'], TabTreeNode][]
}
/**
* Any other info than parent-children relationship
* can be obtained from chrome.tabs.Tab.
*/
export type TabTreeNode = {
id: chrome.tabs.Tab['id']
childrenIds: chrome.tabs.Tab['id'][]
parentId: chrome.tabs.Tab['id'] | null
windowNumber: number // it's not tab's windowId. It's a number managed by this extension to distinguish windows.
tabRef: chrome.tabs.Tab // to get title, favicon, etc.
}
export type WindowNumberMap = Map<chrome.tabs.Tab['windowId'], number>
export function convertTabToTabTreeNode(
tab: chrome.tabs.Tab,
windowNumberMap: WindowNumberMap,
): TabTreeNode {
return {
id: tab.id,
childrenIds: [],
parentId: null,
windowNumber: calcWindowNumber(tab.windowId, windowNumberMap),
tabRef: tab,
}
}
export function calcWindowNumber(
windowId: chrome.tabs.Tab['windowId'],
windowNumberMap: WindowNumberMap,
): number {
if (windowNumberMap.get(windowId)) {
return windowNumberMap.get(windowId)
} else {
console.error('windowNumberMap does not have windowId')
return 0
}
}