2022-05-26 15:18:53 +08:00
|
|
|
import {getEventName} from "../protyle/util/compatibility";
|
|
|
|
import {genUUID} from "../util/genID";
|
|
|
|
|
|
|
|
export class Dialog {
|
|
|
|
private destroyCallback: () => void;
|
|
|
|
public element: HTMLElement;
|
|
|
|
private id: string;
|
|
|
|
private disableClose: boolean;
|
|
|
|
|
|
|
|
constructor(options: {
|
|
|
|
title?: string,
|
2022-06-29 09:52:50 +08:00
|
|
|
transparent?: boolean,
|
2022-05-26 15:18:53 +08:00
|
|
|
content: string,
|
|
|
|
width?: string
|
|
|
|
height?: string,
|
|
|
|
destroyCallback?: () => void
|
|
|
|
disableClose?: boolean
|
2022-06-29 09:52:50 +08:00
|
|
|
disableAnimation?: boolean
|
2022-05-26 15:18:53 +08:00
|
|
|
}) {
|
|
|
|
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 = `<div class="b3-dialog">
|
2022-06-29 09:52:50 +08:00
|
|
|
<div class="b3-dialog__scrim"${options.transparent ? 'style="background-color:transparent"' : ""}></div>
|
2022-05-26 15:18:53 +08:00
|
|
|
<div class="b3-dialog__container" style="width:${options.width || "auto"}">
|
|
|
|
<svg class="b3-dialog__close fn__a${this.disableClose ? " fn__none" : ""}"><use xlink:href="#iconClose"></use></svg>
|
|
|
|
<div class="b3-dialog__header${options.title ? "" : " fn__none"}" onselectstart="return false;">${options.title || ""}</div>
|
|
|
|
<div style="height:${options.height || "auto"}">${options.content}</div>
|
|
|
|
</div></div>`;
|
|
|
|
|
|
|
|
this.element.querySelector(".b3-dialog__scrim").addEventListener(getEventName(), (event) => {
|
2022-06-29 09:52:50 +08:00
|
|
|
if (!this.disableClose) {
|
|
|
|
this.destroy();
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
event.stopPropagation();
|
2022-07-18 23:09:58 +08:00
|
|
|
// https://ld246.com/article/1657969292700/comment/1658147006669#comments
|
|
|
|
window.siyuan.menus.menu.remove();
|
2022-05-26 15:18:53 +08:00
|
|
|
});
|
|
|
|
if (!this.disableClose) {
|
|
|
|
this.element.querySelector(".b3-dialog__close").addEventListener(getEventName(), (event) => {
|
|
|
|
this.destroy();
|
|
|
|
event.stopPropagation();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
document.body.append(this.element);
|
2022-06-29 09:52:50 +08:00
|
|
|
if (options.disableAnimation) {
|
2022-05-26 15:18:53 +08:00
|
|
|
this.element.classList.add("b3-dialog--open");
|
2022-06-29 09:52:50 +08:00
|
|
|
} else {
|
|
|
|
setTimeout(() => {
|
|
|
|
this.element.classList.add("b3-dialog--open");
|
|
|
|
});
|
|
|
|
}
|
2022-12-05 12:51:00 +08:00
|
|
|
// https://github.com/siyuan-note/siyuan/issues/6783
|
|
|
|
window.siyuan.menus.menu.remove();
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public destroy() {
|
|
|
|
this.element.remove();
|
2022-12-05 12:58:59 +08:00
|
|
|
// https://github.com/siyuan-note/siyuan/issues/6783
|
|
|
|
window.siyuan.menus.menu.remove();
|
2022-05-26 15:18:53 +08:00
|
|
|
if (this.destroyCallback) {
|
|
|
|
this.destroyCallback();
|
|
|
|
}
|
|
|
|
window.siyuan.dialogs.find((item, index) => {
|
|
|
|
if (item.id === this.id) {
|
|
|
|
window.siyuan.dialogs.splice(index, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2022-07-17 11:06:05 +08:00
|
|
|
public bindInput(inputElement: HTMLInputElement | HTMLTextAreaElement, enterEvent?: () => void) {
|
2022-05-26 15:18:53 +08:00
|
|
|
inputElement.focus();
|
2022-07-17 11:06:05 +08:00
|
|
|
inputElement.addEventListener("keydown", (event: KeyboardEvent) => {
|
2022-05-26 15:18:53 +08:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|