electron-tabs/src/index.ts

577 lines
15 KiB
TypeScript
Raw Normal View History

2022-05-24 23:38:25 +02:00
import Sortable from "sortablejs";
// @ts-ignore
2022-05-24 23:38:25 +02:00
import styles from "bundle-text:./style.css";
2022-05-23 18:10:29 +02:00
if (!document) {
throw Error("electron-tabs module must be called in renderer process");
}
2022-05-24 23:38:25 +02:00
interface TabGroupOptions {
closeButtonText: string,
defaultTab: TabOptions | ((tabGroup: TabGroup) => TabOptions),
newTabButton: boolean,
newTabButtonText: string,
sortable: boolean,
sortableOptions?: Sortable.Options
tabClass: string,
viewClass: string,
visibilityThreshold: number,
}
interface TabOptions {
active?: boolean;
badge?: string;
closable?: boolean;
icon?: string;
iconURL?: string;
ready?: ((tab: Tab) => void);
src?: string;
title?: string;
visible?: boolean;
webviewAttributes?: { [key: string]: any };
}
function emit(emitter: TabGroup | Tab, type: string, args: any[]) {
if (type === "ready") {
emitter.isReady = true;
}
emitter.dispatchEvent(new CustomEvent(type, { detail: args }));
}
function on(emitter: TabGroup | Tab, type: string, fn: (detail: string) => void, options?: { [key: string]: any }) {
if (type === "ready" && emitter.isReady === true) {
fn.apply(emitter, [emitter]);
}
emitter.addEventListener(type, ((e: CustomEvent) => fn.apply(emitter, e.detail)) as EventListener, options);
}
2022-05-23 18:10:29 +02:00
class TabGroup extends HTMLElement {
2022-05-24 23:38:25 +02:00
buttonContainer: HTMLDivElement;
isReady: boolean;
newTabId: number;
options: TabGroupOptions;
shadow: ShadowRoot;
tabContainer: HTMLDivElement;
tabs: Array<Tab>;
viewContainer: HTMLDivElement;
constructor() {
2020-02-04 11:13:57 +01:00
super();
2022-05-23 18:10:29 +02:00
2022-05-24 23:38:25 +02:00
this.isReady = false;
2022-05-23 18:10:29 +02:00
// Options
this.options = {
closeButtonText: this.getAttribute("close-button-text") || "&#215;",
2022-05-24 23:38:25 +02:00
defaultTab: { title: "New Tab", active: true },
newTabButton: !!this.getAttribute("new-tab-button") === true || false,
2022-05-23 18:10:29 +02:00
newTabButtonText: this.getAttribute("new-tab-button-text") || "&#65291;",
2022-05-24 23:38:25 +02:00
sortable: !!this.getAttribute("sortable") === true || false,
2022-05-23 18:10:29 +02:00
tabClass: this.getAttribute("tab-class") || "etabs-tab",
2022-05-23 23:18:52 +02:00
viewClass: this.getAttribute("view-class") || "etabs-view",
2022-05-24 23:38:25 +02:00
visibilityThreshold: Number(this.getAttribute("visibility-threshold")) || 0
2020-02-04 11:13:57 +01:00
};
2022-05-23 18:10:29 +02:00
this.tabs = [];
this.newTabId = 0;
this.createComponent();
this.initVisibility();
if (this.options.sortable) {
this.initSortable();
}
this.emit("ready", this);
}
emit(type: string, ...args: any[]) {
return emit(this, type, args);
}
on(type: string, fn: (...detail: any[]) => void) {
return on(this, type, fn);
}
once(type: string, fn: (detail: string) => void) {
return on(this, type, fn, { once: true });
}
connectedCallback() {
// Support custom styles
const style = this.querySelector("style");
if (style) {
this.shadow.appendChild(style);
}
}
private createComponent() {
2022-05-23 18:10:29 +02:00
const shadow = this.attachShadow({mode: "open"});
2022-05-24 10:49:24 +02:00
this.shadow = shadow;
2022-05-23 18:10:29 +02:00
const wrapper = document.createElement("div");
wrapper.setAttribute("class", "etabs");
const tabgroup = document.createElement("div");
tabgroup.setAttribute("class", "etabs-tabgroup");
wrapper.appendChild(tabgroup);
const tabContainer = document.createElement("div");
tabContainer.setAttribute("class", "etabs-tabs");
tabgroup.appendChild(tabContainer);
this.tabContainer = tabContainer;
const buttonContainer = document.createElement("div");
buttonContainer.setAttribute("class", "etabs-buttons");
tabgroup.appendChild(buttonContainer);
this.buttonContainer = buttonContainer;
if (this.options.newTabButton) {
const button = this.buttonContainer.appendChild(document.createElement("button"));
button.classList.add(`${this.options.tabClass}-button-new`);
button.innerHTML = this.options.newTabButtonText;
button.addEventListener("click", this.addTab.bind(this, undefined), false);
}
2022-05-23 18:10:29 +02:00
const viewContainer = document.createElement("div");
viewContainer.setAttribute("class", "etabs-views");
wrapper.appendChild(viewContainer);
this.viewContainer = viewContainer;
const style = document.createElement("style");
2022-05-23 18:46:45 +02:00
style.textContent = styles;
2022-05-23 18:10:29 +02:00
shadow.appendChild(style);
shadow.appendChild(wrapper);
2022-05-24 23:38:25 +02:00
}
private initVisibility() {
function toggleTabsVisibility(tab: Tab, tabGroup: TabGroup) {
const visibilityThreshold = this.options.visibilityThreshold;
const el = tabGroup.tabContainer.parentElement;
if (this.tabs.length >= visibilityThreshold) {
el.classList.add("visible");
} else {
el.classList.remove("visible");
}
}
this.on("tab-added", toggleTabsVisibility);
this.on("tab-removed", toggleTabsVisibility);
}
initSortable() {
const createNewSortable = () => {
const options = Object.assign({
direction: "horizontal",
animation: 150,
swapThreshold: 0.20
}, this.options.sortableOptions);
new Sortable(this.tabContainer, options);
};
if (Sortable) {
createNewSortable();
} else {
document.addEventListener("DOMContentLoaded", createNewSortable);
}
}
2022-05-24 23:38:25 +02:00
setDefaultTab(tab: TabOptions) {
2022-05-23 23:18:52 +02:00
this.options.defaultTab = tab;
2022-05-23 19:17:47 +02:00
}
2022-05-24 23:38:25 +02:00
addTab(args = this.options.defaultTab) {
2020-02-04 11:13:57 +01:00
if (typeof args === "function") {
args = args(this);
}
2022-05-24 23:38:25 +02:00
const id = this.newTabId;
2020-02-04 11:13:57 +01:00
this.newTabId++;
2022-05-24 23:38:25 +02:00
const tab = new Tab(this, id, args);
2020-02-04 11:13:57 +01:00
this.tabs.push(tab);
// Don't call tab.activate() before a tab is referenced in this.tabs
if (args.active === true) {
tab.activate();
}
2020-02-04 11:13:57 +01:00
this.emit("tab-added", tab, this);
return tab;
}
2022-05-24 23:38:25 +02:00
getTab(id: number) {
2020-02-04 11:13:57 +01:00
for (let i in this.tabs) {
if (this.tabs[i].id === id) {
return this.tabs[i];
}
}
2020-02-04 11:13:57 +01:00
return null;
}
2022-05-24 23:38:25 +02:00
getTabByPosition(position: number) {
const fromRight = position < 0;
2020-02-04 11:13:57 +01:00
for (let i in this.tabs) {
if (this.tabs[i].getPosition(fromRight) === position) {
return this.tabs[i];
}
}
2020-02-04 11:13:57 +01:00
return null;
}
2022-05-24 23:38:25 +02:00
getTabByRelPosition(position: number) {
2020-02-04 11:13:57 +01:00
position = this.getActiveTab().getPosition() + position;
if (position <= 0) {
return null;
2016-11-01 14:40:03 +01:00
}
2020-02-04 11:13:57 +01:00
return this.getTabByPosition(position);
}
2022-05-24 23:38:25 +02:00
getNextTab() {
2020-02-04 11:13:57 +01:00
return this.getTabByRelPosition(1);
}
2022-05-24 23:38:25 +02:00
getPreviousTab() {
2020-02-04 11:13:57 +01:00
return this.getTabByRelPosition(-1);
}
2022-05-24 23:38:25 +02:00
getTabs() {
2020-02-04 11:13:57 +01:00
return this.tabs.slice();
}
2022-05-24 23:38:25 +02:00
eachTab(fn: (tab: Tab) => void) {
2020-02-04 11:13:57 +01:00
this.getTabs().forEach(fn);
}
2022-05-24 23:38:25 +02:00
getActiveTab() {
2020-02-04 11:13:57 +01:00
if (this.tabs.length === 0) return null;
return this.tabs[0];
}
2020-03-10 21:46:55 +01:00
2022-05-24 23:38:25 +02:00
setActiveTab(tab: Tab) {
this.removeTab(tab);
this.tabs.unshift(tab);
this.emit("tab-active", tab, this);
}
2020-03-10 21:46:55 +01:00
2022-05-24 23:38:25 +02:00
removeTab(tab: Tab, triggerEvent = false) {
const id = tab.id;
const index = this.tabs.findIndex((t: Tab) => t.id === id);
this.tabs.splice(index, 1);
2020-02-04 11:13:57 +01:00
if (triggerEvent) {
this.emit("tab-removed", tab, this);
}
2022-05-24 23:38:25 +02:00
}
2020-02-04 11:13:57 +01:00
2022-05-24 23:38:25 +02:00
activateRecentTab() {
2020-02-04 11:13:57 +01:00
if (this.tabs.length > 0) {
this.tabs[0].activate();
2016-10-31 15:53:50 +01:00
}
2020-02-04 11:13:57 +01:00
}
2022-05-24 23:38:25 +02:00
}
2016-10-31 15:53:50 +01:00
2022-05-23 18:10:29 +02:00
class Tab extends EventTarget {
2022-05-24 23:38:25 +02:00
badge: string;
closable: boolean;
icon: string;
iconURL: string;
id: number;
isClosed: boolean;
isReady: boolean;
tab: HTMLDivElement;
tabElements: { [key: string]: HTMLSpanElement };
tabGroup: TabGroup;
title: string;
webview: HTMLElement;
webviewAttributes: { [key: string]: any };
constructor(tabGroup: TabGroup, id: number, args: TabOptions) {
2020-02-04 11:13:57 +01:00
super();
this.badge = args.badge;
this.closable = args.closable === false ? false : true;
2022-05-24 23:38:25 +02:00
this.icon = args.icon;
this.iconURL = args.iconURL;
this.id = id;
this.isClosed = false;
this.isReady = false;
this.tabElements = {};
this.tabGroup = tabGroup;
this.title = args.title;
2020-02-04 11:13:57 +01:00
this.webviewAttributes = args.webviewAttributes || {};
this.webviewAttributes.src = args.src;
2022-05-24 23:38:25 +02:00
this.initTab();
this.initWebview();
2020-02-04 11:13:57 +01:00
if (args.visible !== false) {
this.show();
2016-10-31 15:53:50 +01:00
}
2020-02-04 11:13:57 +01:00
if (typeof args.ready === "function") {
args.ready(this);
2022-05-24 10:05:04 +02:00
} else {
this.emit("ready", this);
2016-10-31 15:53:50 +01:00
}
2020-02-04 11:13:57 +01:00
}
2022-05-24 23:38:25 +02:00
emit(type: string, ...args: any[]) {
return emit(this, type, args);
}
on(type: string, fn: (...detail: any[]) => void) {
return on(this, type, fn);
}
once(type: string, fn: (detail: string) => void) {
return on(this, type, fn, { once: true });
}
private initTab() {
const tabClass = this.tabGroup.options.tabClass;
// Create tab element
const tab = this.tab = document.createElement("div");
tab.classList.add(tabClass);
for (let el of ["icon", "title", "buttons", "badge"]) {
const span = tab.appendChild(document.createElement("span"));
span.classList.add(`${tabClass}-${el}`);
this.tabElements[el] = span;
}
this.setTitle(this.title);
this.setBadge(this.badge);
this.setIcon(this.iconURL, this.icon);
this.initTabButtons();
this.initTabClickHandler();
this.tabGroup.tabContainer.appendChild(this.tab);
}
private initTabButtons() {
const container = this.tabElements.buttons;
const tabClass = this.tabGroup.options.tabClass;
if (this.closable) {
const button = container.appendChild(document.createElement("button"));
button.classList.add(`${tabClass}-button-close`);
button.innerHTML = this.tabGroup.options.closeButtonText;
button.addEventListener("click", this.close.bind(this, false), false);
}
}
private initTabClickHandler() {
// Mouse up
const tabClickHandler = function(e: KeyboardEvent) {
if (this.isClosed) return;
if (e.which === 2) {
this.close();
}
};
this.tab.addEventListener("mouseup", tabClickHandler.bind(this), false);
// Mouse down
const tabMouseDownHandler = function(e: KeyboardEvent) {
if (this.isClosed) return;
if (e.which === 1) {
if ((e.target as HTMLElement).matches("button")) return;
this.activate();
}
};
this.tab.addEventListener("mousedown", tabMouseDownHandler.bind(this), false);
}
initWebview() {
const webview = this.webview = document.createElement("webview");
const tabWebviewDidFinishLoadHandler = function(e: Event) {
this.emit("webview-ready", this);
};
this.webview.addEventListener("did-finish-load", tabWebviewDidFinishLoadHandler.bind(this), false);
const tabWebviewDomReadyHandler = function(e: Event) {
// Remove this once https://github.com/electron/electron/issues/14474 is fixed
webview.blur();
webview.focus();
this.emit("webview-dom-ready", this);
};
this.webview.addEventListener("dom-ready", tabWebviewDomReadyHandler.bind(this), false);
this.webview.classList.add(this.tabGroup.options.viewClass);
if (this.webviewAttributes) {
const attrs = this.webviewAttributes;
for (let key in attrs) {
const attr = attrs[key];
if (attr === false) continue;
this.webview.setAttribute(key, attr);
}
}
this.tabGroup.viewContainer.appendChild(this.webview);
}
setTitle(title: string) {
2020-02-04 11:13:57 +01:00
if (this.isClosed) return;
2022-05-24 23:38:25 +02:00
const span = this.tabElements.title;
2020-02-04 11:13:57 +01:00
span.innerHTML = title;
span.title = title;
this.title = title;
this.emit("title-changed", title, this);
return this;
}
2022-05-24 23:38:25 +02:00
getTitle() {
2020-02-04 11:13:57 +01:00
if (this.isClosed) return;
return this.title;
}
2022-05-24 23:38:25 +02:00
setBadge(badge: string) {
2020-02-04 11:13:57 +01:00
if (this.isClosed) return;
2022-05-24 23:38:25 +02:00
const span = this.tabElements.badge;
2020-02-04 11:13:57 +01:00
this.badge = badge;
if (badge) {
span.innerHTML = badge;
2022-05-23 19:00:39 +02:00
span.classList.remove("hidden");
2020-02-04 11:13:57 +01:00
} else {
2022-05-23 19:00:39 +02:00
span.classList.add("hidden");
2016-10-31 15:53:50 +01:00
}
2020-02-04 11:13:57 +01:00
this.emit("badge-changed", badge, this);
}
2022-05-24 23:38:25 +02:00
getBadge() {
2020-02-04 11:13:57 +01:00
if (this.isClosed) return;
return this.badge;
}
2022-05-24 23:38:25 +02:00
setIcon(iconURL: string, icon: string) {
2020-02-04 11:13:57 +01:00
if (this.isClosed) return;
this.iconURL = iconURL;
this.icon = icon;
2022-05-24 23:38:25 +02:00
const span = this.tabElements.icon;
2020-02-04 11:13:57 +01:00
if (iconURL) {
span.innerHTML = `<img src="${iconURL}" />`;
this.emit("icon-changed", iconURL, this);
} else if (icon) {
span.innerHTML = `<i class="${icon}"></i>`;
this.emit("icon-changed", icon, this);
2017-06-03 15:01:10 +02:00
}
2020-02-04 11:13:57 +01:00
return this;
}
2022-05-24 23:38:25 +02:00
getIcon() {
2020-02-04 11:13:57 +01:00
if (this.isClosed) return;
if (this.iconURL) return this.iconURL;
return this.icon;
}
2022-05-24 23:38:25 +02:00
setPosition(newPosition: number) {
const tabContainer = this.tabGroup.tabContainer;
const length = tabContainer.childElementCount;
const thisPosition = this.getPosition();
const tabs = Array.from(tabContainer.children)
tabs.splice(thisPosition, 1);
2020-02-04 11:13:57 +01:00
if (newPosition < 0) {
newPosition += length;
2020-02-04 11:13:57 +01:00
if (newPosition < 0) {
newPosition = 0;
}
2016-10-31 17:24:50 +01:00
}
if (newPosition < length) {
tabContainer.insertBefore(this.tab, tabs[newPosition]);
} else {
tabContainer.appendChild(this.tab);
2016-10-31 15:53:50 +01:00
}
2016-10-27 22:38:20 +02:00
2020-02-04 11:13:57 +01:00
return this;
}
2017-10-26 20:55:12 -05:00
2022-05-24 23:38:25 +02:00
getPosition(fromRight = false) {
2020-02-04 11:13:57 +01:00
let position = 0;
let tab = this.tab;
2022-05-24 23:38:25 +02:00
while ((tab = tab.previousSibling as HTMLDivElement) != null) position++;
2017-10-26 20:55:12 -05:00
2020-02-04 11:13:57 +01:00
if (fromRight === true) {
position -= this.tabGroup.tabContainer.childElementCount;
2017-10-26 20:55:12 -05:00
}
2020-02-04 11:13:57 +01:00
return position;
}
2017-10-26 20:55:12 -05:00
2022-05-24 23:38:25 +02:00
activate() {
2020-02-04 11:13:57 +01:00
if (this.isClosed) return;
2022-05-24 23:38:25 +02:00
const activeTab = this.tabGroup.getActiveTab();
2020-02-04 11:13:57 +01:00
if (activeTab) {
activeTab.tab.classList.remove("active");
activeTab.webview.classList.remove("visible");
activeTab.emit("inactive", activeTab);
2017-10-26 20:55:12 -05:00
}
2022-05-24 23:38:25 +02:00
this.tabGroup.setActiveTab(this);
2020-02-04 11:13:57 +01:00
this.tab.classList.add("active");
this.webview.classList.add("visible");
this.webview.focus();
this.emit("active", this);
return this;
}
2022-05-24 23:38:25 +02:00
show(flag = true) {
2020-02-04 11:13:57 +01:00
if (this.isClosed) return;
2022-05-24 23:38:25 +02:00
if (flag) {
2020-02-04 11:13:57 +01:00
this.tab.classList.add("visible");
this.emit("visible", this);
} else {
this.tab.classList.remove("visible");
this.emit("hidden", this);
2016-10-31 15:53:50 +01:00
}
2020-02-04 11:13:57 +01:00
return this;
}
2022-05-24 23:38:25 +02:00
hide() {
2020-02-04 11:13:57 +01:00
return this.show(false);
}
2022-05-24 23:38:25 +02:00
flash(flag = true) {
2020-02-04 11:13:57 +01:00
if (this.isClosed) return;
if (flag !== false) {
this.tab.classList.add("flash");
this.emit("flash", this);
} else {
this.tab.classList.remove("flash");
this.emit("unflash", this);
2016-11-01 11:13:00 +01:00
}
2020-02-04 11:13:57 +01:00
return this;
}
2022-05-24 23:38:25 +02:00
unflash() {
2020-02-04 11:13:57 +01:00
return this.flash(false);
}
2022-05-24 23:38:25 +02:00
hasClass(classname: string) {
2020-03-12 21:56:23 +01:00
return this.tab.classList.contains(classname);
}
2022-05-24 23:38:25 +02:00
close(force: boolean) {
2020-03-10 22:38:20 +01:00
const abortController = new AbortController();
const abort = () => abortController.abort();
this.emit("closing", this, abort);
const abortSignal = abortController.signal;
if (this.isClosed || (!this.closable && !force) || abortSignal.aborted) return;
2020-02-04 11:13:57 +01:00
this.isClosed = true;
2022-05-24 23:38:25 +02:00
const tabGroup = this.tabGroup;
2020-02-04 11:13:57 +01:00
tabGroup.tabContainer.removeChild(this.tab);
tabGroup.viewContainer.removeChild(this.webview);
2022-05-24 23:38:25 +02:00
const activeTab = this.tabGroup.getActiveTab();
tabGroup.removeTab(this, true);
2020-03-10 22:38:20 +01:00
2020-02-04 11:13:57 +01:00
this.emit("close", this);
if (activeTab.id === this.id) {
2022-05-24 23:38:25 +02:00
tabGroup.activateRecentTab();
2016-11-01 11:13:00 +01:00
}
2020-02-04 11:13:57 +01:00
}
}
2016-11-01 11:13:00 +01:00
2022-05-23 18:10:29 +02:00
customElements.define("tab-group", TabGroup);
2022-05-25 09:11:49 +02:00
export type { TabGroup, Tab };