2023-06-11 23:17:51 +08:00
|
|
|
import {Menu as SiyuanMenu} from "../menus/Menu";
|
|
|
|
|
|
|
|
|
|
export class Menu {
|
|
|
|
|
private menu: SiyuanMenu;
|
|
|
|
|
public isOpen: boolean;
|
2023-09-19 10:12:01 +08:00
|
|
|
public element: HTMLElement;
|
2023-06-11 23:17:51 +08:00
|
|
|
|
|
|
|
|
constructor(id?: string, closeCB?: () => void) {
|
|
|
|
|
this.menu = window.siyuan.menus.menu;
|
|
|
|
|
this.isOpen = false;
|
2023-09-19 10:12:01 +08:00
|
|
|
this.element = this.menu.element;
|
|
|
|
|
|
2023-06-11 23:17:51 +08:00
|
|
|
if (id) {
|
|
|
|
|
const dataName = this.menu.element.getAttribute("data-name");
|
|
|
|
|
if (dataName && dataName === id) {
|
|
|
|
|
this.isOpen = true;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
this.menu.remove();
|
|
|
|
|
if (!this.isOpen) {
|
2024-01-02 12:34:10 +08:00
|
|
|
this.menu.element.setAttribute("data-name", id || "");
|
2023-06-11 23:17:51 +08:00
|
|
|
this.menu.removeCB = closeCB;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
showSubMenu(subMenuElement: HTMLElement) {
|
|
|
|
|
this.menu.showSubMenu(subMenuElement);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
addItem(option: IMenu) {
|
|
|
|
|
if (this.isOpen) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
return this.menu.addItem(option);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-25 23:48:53 +08:00
|
|
|
addSeparator(options?: number | {
|
|
|
|
|
index?: number,
|
|
|
|
|
id?: string,
|
|
|
|
|
ignore?: boolean
|
|
|
|
|
}, ignoreParam = false) {
|
|
|
|
|
// 兼容 3.1.24 之前的版本 addSeparator(index?: number, ignore?: boolean): HTMLElement;
|
|
|
|
|
let id: string;
|
|
|
|
|
let index: number;
|
|
|
|
|
let ignore = false;
|
|
|
|
|
if (typeof options === "object") {
|
|
|
|
|
ignore = options.ignore || false;
|
|
|
|
|
index = options.index;
|
|
|
|
|
id = options.id;
|
|
|
|
|
} else if (typeof options === "number") {
|
|
|
|
|
index = options;
|
|
|
|
|
ignore = ignoreParam;
|
2024-07-05 21:00:21 +08:00
|
|
|
}
|
2025-02-25 23:48:53 +08:00
|
|
|
if (ignore || this.isOpen) {
|
2023-06-11 23:17:51 +08:00
|
|
|
return;
|
|
|
|
|
}
|
2025-02-25 23:48:53 +08:00
|
|
|
return this.menu.addItem({id, type: "separator", index});
|
2023-06-11 23:17:51 +08:00
|
|
|
}
|
|
|
|
|
|
2023-12-23 23:50:04 +08:00
|
|
|
open(options: IPosition) {
|
2023-06-11 23:17:51 +08:00
|
|
|
if (this.isOpen) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2023-10-04 20:11:33 +08:00
|
|
|
this.menu.popup(options);
|
2023-06-11 23:17:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fullscreen(position: "bottom" | "all" = "all") {
|
|
|
|
|
if (this.isOpen) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
this.menu.fullscreen(position);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
close() {
|
|
|
|
|
this.menu.remove();
|
|
|
|
|
}
|
|
|
|
|
}
|