-
Notifications
You must be signed in to change notification settings - Fork 0
/
rearrange.js
97 lines (81 loc) · 3.38 KB
/
rearrange.js
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
chrome.runtime.onInstalled.addListener(async ({ reason, previousVersion }) => {
let version;
try {
version = Number(previousVersion.split('.')[0]);
} catch(e) {
version = 3;
}
// Display only when updating from v2 to v3
if (reason == 'update' && version == 2) {
await chrome.tabs.create({ 'url': chrome.runtime.getURL('updated.html' )});
}
});
chrome.commands.onCommand.addListener(async (cmd) => {
const MOVE_LEFT = 'rtl';
const MOVE_RIGHT = 'rtr';
const MOVE_FRONT = 'rtf';
const MOVE_BACK = 'rtb';
let all = await chrome.tabs.query({ currentWindow: true });
let pinned = await chrome.tabs.query({ currentWindow: true, pinned: true });
let highlightedTabs = await chrome.tabs.query({ currentWindow: true, highlighted: true, pinned: false });
let highlightedPinnedTabs = await chrome.tabs.query({ currentWindow: true, highlighted: true, pinned: true });
// We attempt to move pinned tabs only if they're available
switch (cmd) {
case MOVE_LEFT:
if (highlightedPinnedTabs.length > 0) moveLeft(highlightedPinnedTabs, 0);
moveLeft(highlightedTabs, pinned.length);
break;
case MOVE_RIGHT:
if (highlightedPinnedTabs.length > 0) moveRight(highlightedPinnedTabs, pinned.length);
moveRight(highlightedTabs, all.length);
break;
case MOVE_BACK:
if (highlightedPinnedTabs.length > 0) moveBack(highlightedPinnedTabs, pinned.length);
moveBack(highlightedTabs, all.length);
break;
case MOVE_FRONT:
if (highlightedPinnedTabs.length > 0) moveFront(highlightedPinnedTabs, 0);
moveFront(highlightedTabs, pinned.length);
break;
default:
}
});
/**
* Each tab has its own range. This depends on the type, pinned
* or unpinned. It also depends on how many tabs are currently
* highlighted.
* pinned tabs: <position in the highlighted pinned tabs list> to <right most end of the pinned tabs>
* unpinned tabs: <position after pinned tabs + position in the highlighted unpinned tabs list> to <position in the highlighted unpinned tabs list from the right most end>
* When moving left, we update highlighted tabs from left to right, the natural order
* When moving right, we update highlighted tabs from right to left - this avoids a lot of UI bugs
*/
function moveLeft(highlightedTabs, offset) {
moveTabs(highlightedTabs.map((highlightedTab, i) => [highlightedTab.id, Math.max(offset + i, highlightedTab.index - 1)]));
}
function moveFront(highlightedTabs, offset) {
moveTabs(highlightedTabs.map((highlightedTab, i) => [highlightedTab.id, offset + i]));
}
function moveRight(highlightedTabs, lengthOfAllTabsOfCurrentType) {
let offset = lengthOfAllTabsOfCurrentType - highlightedTabs.length;
let tabs = Array(highlightedTabs.length);
for (let i = highlightedTabs.length - 1; i >= 0; i--) {
tabs[highlightedTabs.length - i - 1] = [highlightedTabs[i].id, Math.min(offset + i, highlightedTabs[i].index + 1)];
}
moveTabs(tabs);
}
function moveBack(highlightedTabs, lengthOfAllTabsOfCurrentType) {
let tabs = Array(highlightedTabs.length);
for (let i = highlightedTabs.length - 1; i >= 0; i--) {
let idx = highlightedTabs.length - i - 1;
tabs[idx] = [highlightedTabs[i].id, lengthOfAllTabsOfCurrentType - idx - 1];
}
moveTabs(tabs);
}
function moveTab(id, pos) {
chrome.tabs.move(id, { index: pos });
}
function moveTabs(tabs) {
for (let [id, pos] of tabs) {
moveTab(id, pos);
}
}