Sort tabs with less steps
This commit is contained in:
parent
0383836188
commit
33c6dfbf18
2 changed files with 31 additions and 11 deletions
|
@ -248,7 +248,8 @@ const backgroundLogic = {
|
||||||
|
|
||||||
async _sortTabsInternal(windowObj, pinnedTabs) {
|
async _sortTabsInternal(windowObj, pinnedTabs) {
|
||||||
const tabs = windowObj.tabs;
|
const tabs = windowObj.tabs;
|
||||||
let pos = 0;
|
const sortedTabs = [];
|
||||||
|
let offset = 0;
|
||||||
|
|
||||||
// Let's collect UCIs/tabs for this window.
|
// Let's collect UCIs/tabs for this window.
|
||||||
const map = new Map;
|
const map = new Map;
|
||||||
|
@ -260,7 +261,7 @@ const backgroundLogic = {
|
||||||
|
|
||||||
if (!pinnedTabs && tab.pinned) {
|
if (!pinnedTabs && tab.pinned) {
|
||||||
// pinned tabs must be consider as taken positions.
|
// pinned tabs must be consider as taken positions.
|
||||||
++pos;
|
++offset;
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -269,21 +270,39 @@ const backgroundLogic = {
|
||||||
map.set(userContextId, []);
|
map.set(userContextId, []);
|
||||||
}
|
}
|
||||||
map.get(userContextId).push(tab);
|
map.get(userContextId).push(tab);
|
||||||
|
sortedTabs.push(tab);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Let's sort the map.
|
// Let's sort the map.
|
||||||
const sortMap = new Map([...map.entries()].sort((a, b) => a[0] > b[0]));
|
const sortMap = new Map([...map.entries()].sort((a, b) => a[0] > b[0]));
|
||||||
|
|
||||||
// Let's move tabs.
|
// Let's move tabs.
|
||||||
sortMap.forEach(tabs => {
|
const afterIds = Array.from(sortMap.values()).reduce((acc, val) => acc.concat(val), []).map(tab => tab.id);
|
||||||
for (const tab of tabs) {
|
const beforeIds = sortedTabs.map(tab => tab.id);
|
||||||
++pos;
|
let sortedIds = beforeIds.slice(0);
|
||||||
browser.tabs.move(tab.id, {
|
for (const difference of Diff.diff(beforeIds, afterIds)) {
|
||||||
windowId: windowObj.id,
|
if (!difference.added)
|
||||||
index: pos
|
continue;
|
||||||
});
|
let movingIds = difference.value;
|
||||||
}
|
const nearestFollowingIndex = afterIds.indexOf(movingIds[movingIds.length - 1]) + 1;
|
||||||
});
|
let newIndex = nearestFollowingIndex < afterIds.length ? sortedIds.indexOf(afterIds[nearestFollowingIndex]) : -1;
|
||||||
|
if (newIndex < 0)
|
||||||
|
newIndex = beforeIds.length;
|
||||||
|
// Reject already moved tabs.
|
||||||
|
let oldIndices = movingIds.map(id => sortedIds.indexOf(id));
|
||||||
|
movingIds = movingIds.filter((id, index) => oldIndices[index] > -1);
|
||||||
|
if (movingIds.length == 0)
|
||||||
|
continue;
|
||||||
|
oldIndices = oldIndices.filter(index => index > -1);
|
||||||
|
if (oldIndices[0] < newIndex)
|
||||||
|
newIndex--;
|
||||||
|
browser.tabs.move(movingIds, {
|
||||||
|
windowId: windowObj.id,
|
||||||
|
index: newIndex + offset
|
||||||
|
});
|
||||||
|
sortedIds = sortedIds.filter(id => !movingIds.includes(id));
|
||||||
|
sortedIds.splice(newIndex, 0, ...movingIds);
|
||||||
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async hideTabs(options) {
|
async hideTabs(options) {
|
||||||
|
|
|
@ -13,6 +13,7 @@
|
||||||
"js/background/messageHandler.js",
|
"js/background/messageHandler.js",
|
||||||
]
|
]
|
||||||
-->
|
-->
|
||||||
|
<script type="text/javascript" src="diff.js"></script>
|
||||||
<script type="text/javascript" src="backgroundLogic.js"></script>
|
<script type="text/javascript" src="backgroundLogic.js"></script>
|
||||||
<script type="text/javascript" src="assignManager.js"></script>
|
<script type="text/javascript" src="assignManager.js"></script>
|
||||||
<script type="text/javascript" src="badge.js"></script>
|
<script type="text/javascript" src="badge.js"></script>
|
||||||
|
|
Loading…
Add table
Reference in a new issue