2022-05-26 15:18:53 +08:00
|
|
|
import {genUUID} from "../util/genID";
|
2023-05-26 17:03:35 +08:00
|
|
|
/// #if !MOBILE
|
2023-05-22 11:33:06 +08:00
|
|
|
import {moveResize} from "./moveResize";
|
2023-05-22 22:19:38 +08:00
|
|
|
/// #endif
|
|
|
|
import {isMobile} from "../util/functions";
|
2023-11-12 12:28:20 +08:00
|
|
|
import {isNotCtrl} from "../protyle/util/compatibility";
|
2023-09-14 11:58:56 +08:00
|
|
|
import {Protyle} from "../protyle";
|
2023-12-22 13:09:44 +08:00
|
|
|
import {Constants} from "../constants";
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
export class Dialog {
|
2023-05-22 10:32:54 +08:00
|
|
|
private destroyCallback: (options?: IObject) => void;
|
2022-05-26 15:18:53 +08:00
|
|
|
public element: HTMLElement;
|
|
|
|
private id: string;
|
|
|
|
private disableClose: boolean;
|
2023-09-17 22:27:55 +08:00
|
|
|
public editor: Protyle;
|
2023-11-01 17:02:42 +08:00
|
|
|
public data: any;
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
constructor(options: {
|
2023-12-22 13:09:44 +08:00
|
|
|
positionId?: string,
|
2022-05-26 15:18:53 +08:00
|
|
|
title?: string,
|
2022-06-29 09:52:50 +08:00
|
|
|
transparent?: boolean,
|
2022-05-26 15:18:53 +08:00
|
|
|
content: string,
|
2023-09-14 09:56:13 +08:00
|
|
|
width?: string,
|
2022-05-26 15:18:53 +08:00
|
|
|
height?: string,
|
2023-09-14 09:56:13 +08:00
|
|
|
destroyCallback?: (options?: IObject) => void,
|
|
|
|
disableClose?: boolean,
|
2023-10-04 21:35:40 +08:00
|
|
|
hideCloseIcon?: boolean,
|
2023-09-14 09:56:13 +08:00
|
|
|
disableAnimation?: boolean,
|
|
|
|
resizeCallback?: (type: string) => void
|
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;
|
2023-12-25 21:31:32 +08:00
|
|
|
let left;
|
|
|
|
let top;
|
2023-12-22 13:09:44 +08:00
|
|
|
if (!isMobile() && options.positionId) {
|
|
|
|
const dialogPosition = window.siyuan.storage[Constants.LOCAL_DIALOGPOSITION][options.positionId];
|
|
|
|
if (dialogPosition) {
|
|
|
|
left = dialogPosition.left + "px";
|
|
|
|
top = dialogPosition.top + "px";
|
|
|
|
options.width = dialogPosition.width + "px";
|
|
|
|
options.height = dialogPosition.height + "px";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
this.element.innerHTML = `<div class="b3-dialog" style="z-index: ${++window.siyuan.zIndex};${typeof left === "string" ? "display:block" : ""}">
|
2022-06-29 09:52:50 +08:00
|
|
|
<div class="b3-dialog__scrim"${options.transparent ? 'style="background-color:transparent"' : ""}></div>
|
2023-12-22 13:09:44 +08:00
|
|
|
<div class="b3-dialog__container" style="width:${options.width || "auto"};height:${options.height || "auto"};left:${left};top:${top}">
|
2023-11-16 22:32:57 +08:00
|
|
|
<svg ${(isMobile() && options.title) ? 'style="top:0;right:0;"' : ""} class="b3-dialog__close${(this.disableClose || options.hideCloseIcon) ? " fn__none" : ""}"><use xlink:href="#iconCloseRound"></use></svg>
|
2023-05-22 11:33:06 +08:00
|
|
|
<div class="resize__move b3-dialog__header${options.title ? "" : " fn__none"}" onselectstart="return false;">${options.title || ""}</div>
|
2023-05-22 22:08:01 +08:00
|
|
|
<div class="b3-dialog__body">${options.content}</div>
|
2023-05-22 11:33:06 +08:00
|
|
|
<div class="resize__rd"></div><div class="resize__ld"></div><div class="resize__lt"></div><div class="resize__rt"></div><div class="resize__r"></div><div class="resize__d"></div><div class="resize__t"></div><div class="resize__l"></div>
|
2022-05-26 15:18:53 +08:00
|
|
|
</div></div>`;
|
|
|
|
|
2022-12-22 22:26:00 +08:00
|
|
|
this.element.querySelector(".b3-dialog__scrim").addEventListener("click", (event) => {
|
2022-06-29 09:52:50 +08:00
|
|
|
if (!this.disableClose) {
|
|
|
|
this.destroy();
|
|
|
|
}
|
2022-12-22 22:26:00 +08:00
|
|
|
event.preventDefault();
|
2022-05-26 15:18:53 +08:00
|
|
|
event.stopPropagation();
|
|
|
|
});
|
|
|
|
if (!this.disableClose) {
|
2022-12-22 22:26:00 +08:00
|
|
|
this.element.querySelector(".b3-dialog__close").addEventListener("click", (event) => {
|
2022-05-26 15:18:53 +08:00
|
|
|
this.destroy();
|
2022-12-22 22:26:00 +08:00
|
|
|
event.preventDefault();
|
2022-05-26 15:18:53 +08:00
|
|
|
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");
|
|
|
|
});
|
|
|
|
}
|
2023-05-26 17:03:35 +08:00
|
|
|
/// #if !MOBILE
|
2023-09-14 09:56:13 +08:00
|
|
|
moveResize(this.element.querySelector(".b3-dialog__container"), options.resizeCallback);
|
2023-05-22 10:32:54 +08:00
|
|
|
/// #endif
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
|
2023-05-22 10:32:54 +08:00
|
|
|
public destroy(options?: IObject) {
|
2023-10-04 21:02:49 +08:00
|
|
|
// av 修改列头emoji后点击关闭emoji图标
|
|
|
|
if ((this.element.querySelector(".b3-dialog") as HTMLElement).style.zIndex < window.siyuan.menus.menu.element.style.zIndex) {
|
|
|
|
// https://github.com/siyuan-note/siyuan/issues/6783
|
|
|
|
window.siyuan.menus.menu.remove();
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
this.element.remove();
|
|
|
|
if (this.destroyCallback) {
|
2023-04-28 22:08:57 +08:00
|
|
|
this.destroyCallback(options);
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
window.siyuan.dialogs.find((item, index) => {
|
|
|
|
if (item.id === this.id) {
|
|
|
|
window.siyuan.dialogs.splice(index, 1);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-11-16 22:32:57 +08:00
|
|
|
public bindInput(inputElement: HTMLInputElement | HTMLTextAreaElement, enterEvent?: () => void, bindEnter = true) {
|
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") {
|
2023-11-16 22:32:57 +08:00
|
|
|
this.destroy();
|
2022-05-26 15:18:53 +08:00
|
|
|
event.preventDefault();
|
|
|
|
event.stopPropagation();
|
|
|
|
return;
|
|
|
|
}
|
2023-11-16 22:32:57 +08:00
|
|
|
if (!event.shiftKey && isNotCtrl(event) && event.key === "Enter" && enterEvent && bindEnter) {
|
|
|
|
enterEvent();
|
2022-05-26 15:18:53 +08:00
|
|
|
event.preventDefault();
|
2023-08-16 13:31:37 +08:00
|
|
|
event.stopPropagation();
|
2022-05-26 15:18:53 +08:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|