refactor: Add event listener for tab close in ZenWorkspaces

This commit adds an event listener for the "TabClose" event in the ZenWorkspaces module. When a tab is closed, the listener checks if it is the last tab in a workspace and creates a new tab if necessary. It also closes other tabs in the workspace. This enhancement improves the functionality and user experience of ZenWorkspaces.
This commit is contained in:
Mauro Balades 2024-08-21 20:48:13 +02:00
parent 7bf34b6f28
commit 2ef396ee87

View file

@ -56,6 +56,7 @@ var ZenWorkspaces = {
await IOUtils.writeJSON(this._storeFile, {});
}
if (this.workspaceEnabled) {
window.addEventListener("TabClose", this.handleTabClose.bind(this));
let workspaces = await this._workspaces();
if (workspaces.workspaces.length === 0) {
await this.createAndSaveWorkspace("Default Workspace", true);
@ -80,6 +81,20 @@ var ZenWorkspaces = {
}
},
handleTabClose(event) {
let tab = event.target;
let workspaceID = tab.getAttribute("zen-workspace-id");
// If the tab is the last one in the workspace, create a new tab
if (workspaceID) {
let tabs = gBrowser.tabs.filter(tab => tab.getAttribute("zen-workspace-id") === workspaceID);
if (tabs.length === 1) {
this._createNewTabForWorkspace({ uuid: workspaceID });
// We still need to close other tabs in the workspace
this.changeWorkspace({ uuid: workspaceID });
}
}
},
_initializeWorkspaceIcons() {
const kIcons = ["🏠", "📄", "💹", "💼", "📧", "✅", "👥"];
let container = document.getElementById("PanelUI-zen-workspaces-create-icons-container");