import {Constants} from "../constants";
import {fetchPost} from "../util/fetch";
/// #if !MOBILE
import {exportLayout} from "../layout/util";
/// #endif
/// #if !BROWSER
import {ipcRenderer} from "electron";
import {getCurrentWindow} from "@electron/remote";
/// #endif
import {hideMessage, showMessage} from "./message";
import {Dialog} from "./index";
import {isMobile} from "../util/functions";
import {confirmDialog} from "./confirmDialog";
import {escapeHtml} from "../util/escape";
import {getWorkspaceName} from "../util/noRelyPCFunction";
import {needSubscribe} from "../util/needSubscribe";
import {redirectToCheckAuth} from "../util/pathName";
export const lockScreen = () => {
if (window.siyuan.config.readonly) {
return;
}
/// #if BROWSER
fetchPost("/api/system/logoutAuth", {}, () => {
redirectToCheckAuth();
});
/// #else
ipcRenderer.send(Constants.SIYUAN_SEND_WINDOWS, {cmd: "lockscreen"});
/// #endif
};
export const kernelError = () => {
let iosReStart = "";
if (window.siyuan.config.system.container === "ios" && window.webkit?.messageHandlers) {
iosReStart = `
${window.siyuan.languages.kernelFault1}
${window.siyuan.languages.kernelFault2}
${iosReStart}
`
});
const restartElement = dialog.element.querySelector(".b3-button");
if (restartElement) {
restartElement.addEventListener("click", () => {
dialog.destroy();
window.webkit.messageHandlers.startKernelFast.postMessage("startKernelFast");
});
}
};
export const exitSiYuan = () => {
fetchPost("/api/system/exit", {force: false}, (response) => {
if (response.code === 1) { // 同步执行失败
const msgId = showMessage(response.msg, response.data.closeTimeout, "error");
const buttonElement = document.querySelector(`#message [data-id="${msgId}"] button`);
if (buttonElement) {
buttonElement.addEventListener("click", () => {
fetchPost("/api/system/exit", {force: true}, () => {
/// #if !BROWSER
ipcRenderer.send(Constants.SIYUAN_QUIT, location.port);
/// #else
if (["ios", "android"].includes(window.siyuan.config.system.container) && (window.webkit?.messageHandlers || window.JSAndroid)) {
window.location.href = "siyuan://api/system/exit";
}
/// #endif
});
});
}
} else if (response.code === 2) { // 提示新安装包
hideMessage();
confirmDialog(window.siyuan.languages.tip, response.msg, () => {
fetchPost("/api/system/exit", {
force: true,
execInstallPkg: 2 // 0:默认检查新版本,1:不执行新版本安装,2:执行新版本安装
}, () => {
/// #if !BROWSER
// 桌面端退出拉起更新安装时有时需要重启两次 https://github.com/siyuan-note/siyuan/issues/6544
// 这里先将主界面隐藏
setTimeout(() => {
getCurrentWindow().hide();
}, 2000);
// 然后等待一段时间后再退出,避免界面主进程退出以后内核子进程被杀死
setTimeout(() => {
ipcRenderer.send(Constants.SIYUAN_QUIT, location.port);
}, 4000);
/// #endif
});
}, () => {
fetchPost("/api/system/exit", {
force: true,
execInstallPkg: 1 // 0:默认检查新版本,1:不执行新版本安装,2:执行新版本安装
}, () => {
/// #if !BROWSER
ipcRenderer.send(Constants.SIYUAN_QUIT, location.port);
/// #endif
});
});
} else { // 正常退出
/// #if !BROWSER
ipcRenderer.send(Constants.SIYUAN_QUIT, location.port);
/// #else
if (["ios", "android"].includes(window.siyuan.config.system.container) && (window.webkit?.messageHandlers || window.JSAndroid)) {
window.location.href = "siyuan://api/system/exit";
}
/// #endif
}
});
};
export const transactionError = () => {
if (document.getElementById("transactionError")) {
return;
}
const dialog = new Dialog({
disableClose: true,
title: `${window.siyuan.languages.stateExcepted} v${Constants.SIYUAN_VERSION}`,
content: `