Mercurial > addons > firefox-addons > tab-mover
comparison 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 |
comparison
equal
deleted
inserted
replaced
27:a58b1c0373b5 | 28:8279a650da6b |
---|---|
30 // group | 30 // group |
31 let selectedTabs = (tab.highlighted) ? await browser.tabs.query({ | 31 let selectedTabs = (tab.highlighted) ? await browser.tabs.query({ |
32 highlighted: true, | 32 highlighted: true, |
33 windowId: tab.windowId | 33 windowId: tab.windowId |
34 }) : [tab]; | 34 }) : [tab]; |
35 browser.tabs.move(selectedTabs.map(selectedTab => selectedTab.id), | 35 let activeTab = selectedTabs.find(tab => tab.active); |
36 await browser.tabs.move(selectedTabs.map(selectedTab => selectedTab.id), | |
36 {windowId: targetWindowId, index: -1}); | 37 {windowId: targetWindowId, index: -1}); |
38 | |
39 // mark the previously active tab active again before highlighting other | |
40 // tabs since this resets the selected tabs | |
41 await browser.tabs.update(activeTab.id, {active: true}); | |
42 for (let tab of selectedTabs) { | |
43 if (tab.id !== activeTab.id) { | |
44 browser.tabs.update(tab.id, {active: false, highlighted: true}); | |
45 } | |
46 } | |
37 } | 47 } |
38 | 48 |
39 async function reopenTabs(tab, targetWindowId) { | 49 async function reopenTabs(tab, targetWindowId) { |
40 // if the current tab is part of a highlighted group then reopen the whole | 50 // if the current tab is part of a highlighted group then reopen the whole |
41 // group | 51 // group |
47 selectedTabs = selectedTabs.filter(selectedTab => | 57 selectedTabs = selectedTabs.filter(selectedTab => |
48 ALLOWED_PROTOCOLS.has(new URL(selectedTab.url).protocol)); | 58 ALLOWED_PROTOCOLS.has(new URL(selectedTab.url).protocol)); |
49 if (selectedTabs.length === 0) { | 59 if (selectedTabs.length === 0) { |
50 return; | 60 return; |
51 } | 61 } |
62 let activeTab = selectedTabs.find(tab => tab.active); | |
63 // the actually active tab may have been filtered out above, fall back to | |
64 // the first highlighted one | |
65 if (typeof activeTab === 'undefined') { | |
66 activeTab = selectedTabs[0]; | |
67 activeTab.active = true; | |
68 } | |
52 | 69 |
53 await Promise.all(selectedTabs.map(selectedTab => browser.tabs.create({ | 70 let newTabs = await Promise.all(selectedTabs.map(selectedTab => { |
54 url: selectedTab.url, | 71 return browser.tabs.create({ |
55 windowId: targetWindowId | 72 url: selectedTab.url, |
56 }))); | 73 windowId: targetWindowId, |
74 active: selectedTab.active | |
75 }); | |
76 })); | |
77 // tabs can only be highlighted after they have been created | |
78 for (let tab of newTabs) { | |
79 if (!tab.active) { | |
80 browser.tabs.update(tab.id, {active: false, highlighted: true}); | |
81 } | |
82 } | |
57 browser.tabs.remove(selectedTabs.map(selectedTab => selectedTab.id)); | 83 browser.tabs.remove(selectedTabs.map(selectedTab => selectedTab.id)); |
58 } | 84 } |
59 | 85 |
60 async function onMenuShown(info, tab) { | 86 async function onMenuShown(info, tab) { |
61 let menuInstanceId = nextMenuInstanceId++; | 87 let menuInstanceId = nextMenuInstanceId++; |