import {genUUID} from "../util/genID"; export class Dialog { private destroyCallback: () => void; public element: HTMLElement; private id: string; private disableClose: boolean; constructor(options: { title?: string, transparent?: boolean, content: string, width?: string height?: string, destroyCallback?: () => void disableClose?: boolean disableAnimation?: boolean }) { this.disableClose = options.disableClose; this.id = genUUID(); window.siyuan.dialogs.push(this); this.destroyCallback = options.destroyCallback; this.element = document.createElement("div") as HTMLElement; this.element.innerHTML = `
${options.title || ""}
${options.content}
`; this.element.querySelector(".b3-dialog__scrim").addEventListener("click", (event) => { if (!this.disableClose) { this.destroy(); } event.preventDefault(); event.stopPropagation(); // https://ld246.com/article/1657969292700/comment/1658147006669#comments window.siyuan.menus.menu.remove(); }); if (!this.disableClose) { this.element.querySelector(".b3-dialog__close").addEventListener("click", (event) => { this.destroy(); event.preventDefault(); event.stopPropagation(); }); } document.body.append(this.element); if (options.disableAnimation) { this.element.classList.add("b3-dialog--open"); } else { setTimeout(() => { this.element.classList.add("b3-dialog--open"); }); } // https://github.com/siyuan-note/siyuan/issues/6783 window.siyuan.menus.menu.remove(); } public destroy() { this.element.remove(); // https://github.com/siyuan-note/siyuan/issues/6783 window.siyuan.menus.menu.remove(); if (this.destroyCallback) { this.destroyCallback(); } window.siyuan.dialogs.find((item, index) => { if (item.id === this.id) { window.siyuan.dialogs.splice(index, 1); return true; } }); } public bindInput(inputElement: HTMLInputElement | HTMLTextAreaElement, enterEvent?: () => void) { inputElement.focus(); inputElement.addEventListener("keydown", (event: KeyboardEvent) => { if (event.isComposing) { event.preventDefault(); return; } if (event.key === "Escape") { this.destroy(); event.preventDefault(); event.stopPropagation(); return; } if (event.key === "Enter" && enterEvent) { enterEvent(); event.preventDefault(); } }); } }