Skip to content

Commit d0df19e

Browse files
committed
feat: move tab to an existing window by orientation
1 parent 385ee8e commit d0df19e

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

background_scripts/all_commands.js

+32
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,38 @@ const allCommands = [
471471
background: true,
472472
},
473473

474+
{
475+
name: "mergeTabToExistingWindowOnLeft",
476+
desc: "Move tab to an existing window on left, if exists",
477+
group: "tabs",
478+
advanced: true,
479+
background: true,
480+
},
481+
482+
{
483+
name: "mergeTabToExistingWindowOnRight",
484+
desc: "Move tab to an existing window on right, if exists",
485+
group: "tabs",
486+
advanced: true,
487+
background: true,
488+
},
489+
490+
{
491+
name: "mergeTabToExistingWindowAbove",
492+
desc: "Move tab to an existing window above, if exists",
493+
group: "tabs",
494+
advanced: true,
495+
background: true,
496+
},
497+
498+
{
499+
name: "mergeTabToExistingWindowBelow",
500+
desc: "Move tab to an existing window below, if exists",
501+
group: "tabs",
502+
advanced: true,
503+
background: true,
504+
},
505+
474506
{
475507
name: "closeTabsOnLeft",
476508
desc: "Close tabs on the left",

background_scripts/commands.js

+4
Original file line numberDiff line numberDiff line change
@@ -345,6 +345,10 @@ const defaultKeyMappings = {
345345
"g0": "firstTab",
346346
"g$": "lastTab",
347347
"W": "moveTabToNewWindow",
348+
"wh": "mergeTabToExistingWindowOnLeft",
349+
"wl": "mergeTabToExistingWindowOnRight",
350+
"wk": "mergeTabToExistingWindowAbove",
351+
"wj": "mergeTabToExistingWindowBelow",
348352
"t": "createTab",
349353
"yt": "duplicateTab",
350354
"x": "removeTab",

background_scripts/main.js

+42
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,35 @@ function nextZoomLevel(currentZoom, steps) {
232232
}
233233
}
234234

235+
const moveTabToExistingWindow = function(orientation, currentTab) {
236+
chrome.windows.getCurrent({}, currentWindow => {
237+
chrome.windows.getAll({populate: true}, windows => {
238+
const filteredWindows = windows.filter(window => {
239+
if (window.id !== currentWindow.id) {
240+
if (orientation === 'left') {
241+
return window.left < currentWindow.left;
242+
} else if (orientation === 'right') {
243+
return window.left > currentWindow.left;
244+
} else if (orientation === 'top') {
245+
return window.top < currentWindow.top;
246+
} else if (orientation === 'bottom') {
247+
return window.top > currentWindow.top;
248+
}
249+
}
250+
});
251+
if (filteredWindows.length > 0) {
252+
const destinationWindow = filteredWindows[0];
253+
chrome.tabs.move(currentTab.id, { windowId: destinationWindow.id, index: -1 }).then(() => {
254+
chrome.windows.get(destinationWindow.id, {populate: true}, newWindow => {
255+
const newTab = newWindow.tabs.slice(-1)[0];
256+
selectSpecificTab({ id: newTab.id });
257+
});
258+
});
259+
}
260+
});
261+
});
262+
};
263+
235264
// These are commands which are bound to keystrokes which must be handled by the background page.
236265
// They are mapped in commands.js.
237266
const BackgroundCommands = {
@@ -313,6 +342,19 @@ const BackgroundCommands = {
313342
});
314343
},
315344

345+
mergeTabToExistingWindowOnLeft(request) {
346+
moveTabToExistingWindow("left", request.tab);
347+
},
348+
mergeTabToExistingWindowOnRight(request) {
349+
moveTabToExistingWindow("right", request.tab);
350+
},
351+
mergeTabToExistingWindowAbove(request) {
352+
moveTabToExistingWindow("top", request.tab);
353+
},
354+
mergeTabToExistingWindowBelow(request) {
355+
moveTabToExistingWindow("bottom", request.tab);
356+
},
357+
316358
nextTab(request) {
317359
return selectTab("next", request);
318360
},

0 commit comments

Comments
 (0)