An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
import {Menu as SiyuanMenu} from "../menus/Menu";
import {toggleMenu} from "../menus/menuToggle";
const independentMenus = new WeakMap<HTMLElement, Menu>();
export class Menu {
// 在创建共享菜单前按触发元素切换,保留构造器及独立菜单的既有行为。
static toggle(options: {
target?: Element;
toggle?: boolean;
id?: string;
closeCB?: () => void;
build: (menu: Menu) => void;
show: (menu: Menu) => void;
}) {
let menu: Menu;
toggleMenu({
target: options.target,
toggle: options.toggle,
build: () => {
menu = new Menu(options.id, options.closeCB);
options.build(menu);
},
show: () => options.show(menu),
});
}
private menu: SiyuanMenu;
private parentMenu?: Menu;
private childMenus = new Set<Menu>();
public isOpen: boolean;
public element: HTMLElement;
constructor(id?: string, closeCB?: () => void, independent = false) {
if (independent) {
const element = window.siyuan.menus.menu.element.cloneNode(true) as HTMLElement;
element.removeAttribute("id");
element.removeAttribute("data-name");
element.removeAttribute("data-from");
element.removeAttribute("style");
element.setAttribute("data-menu", "true");
element.classList.add("fn__none");
element.classList.remove("b3-menu--list", "b3-menu--fullscreen");
element.firstElementChild.classList.add("fn__none");
element.lastElementChild.innerHTML = "";
document.body.append(element);
this.menu = new SiyuanMenu(element);
} else {
this.menu = window.siyuan.menus.menu;
}
this.isOpen = false;
this.element = this.menu.element;
if (id && !independent) {
const dataName = this.menu.element.getAttribute("data-name");
if (dataName && dataName === id) {
this.isOpen = true;
}
}
this.menu.remove();
if (!this.isOpen) {
if (id) {
this.menu.element.setAttribute("data-name", id);
}
if (independent) {
independentMenus.set(this.element, this);
const closeEvent = (event: MouseEvent) => {
if (!this.contains(event.target as Node)) {
this.close();
}
};
const keydownEvent = (event: KeyboardEvent) => {
event.stopPropagation();
if (event.key === "Escape") {
event.preventDefault();
this.close();
}
};
window.addEventListener("click", closeEvent, true);
this.element.addEventListener("keydown", keydownEvent);
this.menu.removeCB = () => {
window.removeEventListener("click", closeEvent, true);
this.element.removeEventListener("keydown", keydownEvent);
this.childMenus.forEach(menu => menu.close());
this.childMenus.clear();
this.parentMenu?.childMenus.delete(this);
this.parentMenu = undefined;
independentMenus.delete(this.element);
closeCB?.();
this.element.remove();
};
} else {
this.menu.removeCB = closeCB;
}
}
}
showSubMenu(subMenuElement: HTMLElement) {
this.menu.showSubMenu(subMenuElement);
}
addItem(option: IMenu) {
if (this.isOpen) {
return;
}
return this.menu.addItem(option);
}
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;
}
if (ignore || this.isOpen) {
return;
}
return this.menu.addItem({id, type: "separator", index});
}
open(options: IPosition) {
if (this.isOpen) {
return;
}
// 按触发控件记录菜单归属,点击派生菜单时保留父表单,父菜单关闭时清理子菜单。
if (independentMenus.has(this.element)) {
this.parentMenu?.childMenus.delete(this);
this.parentMenu = undefined;
const parentElement = options.target?.closest<HTMLElement>(".b3-menu");
const parentMenu = parentElement && independentMenus.get(parentElement);
if (parentMenu && parentMenu !== this && !this.contains(parentMenu.element)) {
this.parentMenu = parentMenu;
parentMenu.childMenus.add(this);
}
}
this.menu.popup(options);
}
fullscreen(position: "bottom" | "all" = "all") {
if (this.isOpen) {
return;
}
this.menu.fullscreen(position);
}
close() {
this.menu.remove();
}
private contains(target: Node): boolean {
return this.element.contains(target) || Array.from(this.childMenus).some(menu => menu.contains(target));
}
}