diff background.js @ 28:8279a650da6b

Preserve active and highlighted properties of moved tabs This is consistent with the equivalent built-in drag-and-drop operations.
author Guido Berhoerster <guido+tab-mover@berhoerster.name>
date Sat, 27 Feb 2021 09:14:34 +0100
parents f418a6305f17
children aaed574396b8
line wrap: on
line diff
--- a/background.js	Wed Dec 23 10:53:28 2020 +0100
+++ b/background.js	Sat Feb 27 09:14:34 2021 +0100
@@ -32,8 +32,18 @@
         highlighted: true,
         windowId: tab.windowId
     }) : [tab];
-    browser.tabs.move(selectedTabs.map(selectedTab => selectedTab.id),
+    let activeTab = selectedTabs.find(tab => tab.active);
+    await browser.tabs.move(selectedTabs.map(selectedTab => selectedTab.id),
             {windowId: targetWindowId, index: -1});
+
+    // mark the previously active tab active again before highlighting other
+    // tabs since this resets the selected tabs
+    await browser.tabs.update(activeTab.id, {active: true});
+    for (let tab of selectedTabs) {
+        if (tab.id !== activeTab.id) {
+            browser.tabs.update(tab.id, {active: false, highlighted: true});
+        }
+    }
 }
 
 async function reopenTabs(tab, targetWindowId) {
@@ -49,11 +59,27 @@
     if (selectedTabs.length === 0) {
         return;
     }
+    let activeTab = selectedTabs.find(tab => tab.active);
+    // the actually active tab may have been filtered out above, fall back to
+    // the first highlighted one
+    if (typeof activeTab === 'undefined') {
+        activeTab = selectedTabs[0];
+        activeTab.active = true;
+    }
 
-    await Promise.all(selectedTabs.map(selectedTab => browser.tabs.create({
-        url: selectedTab.url,
-        windowId: targetWindowId
-    })));
+    let newTabs = await Promise.all(selectedTabs.map(selectedTab => {
+        return browser.tabs.create({
+            url: selectedTab.url,
+            windowId: targetWindowId,
+            active: selectedTab.active
+        });
+    }));
+    // tabs can only be highlighted after they have been created
+    for (let tab of newTabs) {
+        if (!tab.active) {
+            browser.tabs.update(tab.id, {active: false, highlighted: true});
+        }
+    }
     browser.tabs.remove(selectedTabs.map(selectedTab => selectedTab.id));
 }