Skip to content

Commit 5fe4def

Browse files
committed
Create a new session from tab context menu
1 parent 9d62261 commit 5fe4def

File tree

1 file changed

+77
-62
lines changed

1 file changed

+77
-62
lines changed

src/ts/background/BrowserTabContextMenu.ts

Lines changed: 77 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -102,76 +102,91 @@ async function addToSessionMenu(
102102
activeSessions:Set<SessionId>,
103103
tabs:Tab[]
104104
) {
105-
console.assert(tabs.length > 0);
105+
dynamicMenus.push(
106+
browser.menus.create({
107+
parentId: "parent",
108+
id: "add",
109+
title: browser.i18n.getMessage(tabs.length > 1 ?
110+
"tab_contextmenu_add_multiple" :
111+
"tab_contextmenu_add"
112+
),
113+
icons: {
114+
"16": "img/browserMenu/add.svg",
115+
"32": "img/browserMenu/add.svg"
116+
}
117+
})
118+
);
119+
120+
// create new session
121+
browser.menus.create({
122+
parentId: "add",
123+
title: "&create new session",
124+
icons: {
125+
"16": "img/browserMenu/add.svg",
126+
"32": "img/browserMenu/add.svg"
127+
},
128+
onclick: () => SessionManager.createSessionFromTabs(tabs, false)
129+
});
106130

107131
if(sessions.length > 0) {
108-
dynamicMenus.push(
109-
browser.menus.create({
110-
parentId: "parent",
111-
id: "add",
112-
title: browser.i18n.getMessage(tabs.length > 1 ?
113-
"tab_contextmenu_add_multiple" :
114-
"tab_contextmenu_add"
115-
),
116-
icons: {
117-
"16": "img/browserMenu/add.svg",
118-
"32": "img/browserMenu/add.svg"
119-
}
120-
})
121-
);
122-
123-
sessions.forEach(session => browser.menus.create({
132+
browser.menus.create({
124133
parentId: "add",
125-
title: "&" + session.title.replace(/&/ig, "&&").trim(),
126-
icons: activeSessions.has(session.id) ? {
127-
"16": "img/browserMenu/active.svg",
128-
"32": "img/browserMenu/active.svg"
129-
} : undefined,
130-
enabled: !currentSessionIds.has(session.id),
131-
onclick: async (info) => {
132-
let added = false;
133-
134-
// move tabs to active session
135-
if(activeSessions.has(session.id)) {
136-
let as = ActiveSessionManager.getActiveSession(session.id);
137-
console.assert(as);
138-
139-
// only if the target session has its own window
140-
if(as.getWindowId() !== null) {
141-
// move or copy tabs to new session
142-
if(currentSessionIds.size === 0) {
143-
for(let tab of tabs) {
144-
await browser.tabs.move(tab.id, {
145-
windowId: as.getWindowId(),
146-
index: tab.pinned ? 0 : -1
147-
});
148-
}
149-
} else {
150-
// duplicate tabs
151-
for(let tab of tabs) {
152-
let details = TabData.createFromTab(tab).getTabCreateProperties(true);
153-
details.windowId = as.getWindowId();
154-
await createTab(details);
155-
}
134+
type: "separator"
135+
})
136+
}
137+
138+
// add to existing session
139+
sessions.forEach(session => browser.menus.create({
140+
parentId: "add",
141+
title: "&" + session.title.replace(/&/ig, "&&").trim(),
142+
icons: activeSessions.has(session.id) ? {
143+
"16": "img/browserMenu/active.svg",
144+
"32": "img/browserMenu/active.svg"
145+
} : undefined,
146+
enabled: !currentSessionIds.has(session.id),
147+
onclick: async (info) => {
148+
let added = false;
149+
150+
// move tabs to active session
151+
if(activeSessions.has(session.id)) {
152+
let as = ActiveSessionManager.getActiveSession(session.id);
153+
console.assert(as);
154+
155+
// only if the target session has its own window
156+
if(as.getWindowId() !== null) {
157+
// move or copy tabs to new session
158+
if(currentSessionIds.size === 0) {
159+
for(let tab of tabs) {
160+
await browser.tabs.move(tab.id, {
161+
windowId: as.getWindowId(),
162+
index: tab.pinned ? 0 : -1
163+
});
164+
}
165+
} else {
166+
// duplicate tabs
167+
for(let tab of tabs) {
168+
let details = TabData.createFromTab(tab).getTabCreateProperties(true);
169+
details.windowId = as.getWindowId();
170+
await createTab(details);
156171
}
157-
added = true;
158172
}
173+
added = true;
159174
}
160-
161-
// otherwise just create the bookmark
162-
if(!added) {
163-
for(let tab of tabs) {
164-
await browser.bookmarks.create(
165-
TabData.createFromTab(tab).getBookmarkCreateDetails(session.id)
166-
);
167-
}
175+
}
176+
177+
// otherwise just create the bookmark
178+
if(!added) {
179+
for(let tab of tabs) {
180+
await browser.bookmarks.create(
181+
TabData.createFromTab(tab).getBookmarkCreateDetails(session.id)
182+
);
168183
}
169-
170-
// update sidebar
171-
SessionContentUpdate.send(session.id);
172184
}
173-
}));
174-
}
185+
186+
// update sidebar
187+
SessionContentUpdate.send(session.id);
188+
}
189+
}));
175190
}
176191

177192
async function addAndSetAsideMenu(

0 commit comments

Comments
 (0)