Skip to content

Commit 086b95a

Browse files
committed
feat: move tab to an existing window by orientation
1 parent 3c4e6da commit 086b95a

File tree

3 files changed

+78
-0
lines changed

3 files changed

+78
-0
lines changed

background_scripts/all_commands.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -519,6 +519,38 @@ const allCommands = [
519519
background: true,
520520
},
521521

522+
{
523+
name: "mergeTabToExistingWindowOnLeft",
524+
desc: "Move tab to an existing window on left, if exists",
525+
group: "tabs",
526+
advanced: true,
527+
background: true,
528+
},
529+
530+
{
531+
name: "mergeTabToExistingWindowOnRight",
532+
desc: "Move tab to an existing window on right, if exists",
533+
group: "tabs",
534+
advanced: true,
535+
background: true,
536+
},
537+
538+
{
539+
name: "mergeTabToExistingWindowAbove",
540+
desc: "Move tab to an existing window above, if exists",
541+
group: "tabs",
542+
advanced: true,
543+
background: true,
544+
},
545+
546+
{
547+
name: "mergeTabToExistingWindowBelow",
548+
desc: "Move tab to an existing window below, if exists",
549+
group: "tabs",
550+
advanced: true,
551+
background: true,
552+
},
553+
522554
{
523555
name: "closeTabsOnLeft",
524556
desc: "Close tabs on the left",

background_scripts/commands.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -469,6 +469,10 @@ const defaultKeyMappings = {
469469
"g0": "firstTab",
470470
"g$": "lastTab",
471471
"W": "moveTabToNewWindow",
472+
"wh": "mergeTabToExistingWindowOnLeft",
473+
"wl": "mergeTabToExistingWindowOnRight",
474+
"wk": "mergeTabToExistingWindowAbove",
475+
"wj": "mergeTabToExistingWindowBelow",
472476
"t": "createTab",
473477
"yt": "duplicateTab",
474478
"x": "removeTab",

background_scripts/main.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,35 @@ function nextZoomLevel(currentZoom, steps) {
235235
}
236236
}
237237

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

341+
mergeTabToExistingWindowOnLeft(request) {
342+
moveTabToExistingWindow("left", request.tab);
343+
},
344+
mergeTabToExistingWindowOnRight(request) {
345+
moveTabToExistingWindow("right", request.tab);
346+
},
347+
mergeTabToExistingWindowAbove(request) {
348+
moveTabToExistingWindow("top", request.tab);
349+
},
350+
mergeTabToExistingWindowBelow(request) {
351+
moveTabToExistingWindow("bottom", request.tab);
352+
},
353+
312354
nextTab(request) {
313355
return selectTab("next", request);
314356
},

0 commit comments

Comments
 (0)