import {Constants} from "../constants";
/// #if !BROWSER
import {shell} from "electron";
import {dialog} from "@electron/remote";
/// #endif
import {isBrowser} from "../util/functions";
import {fetchPost} from "../util/fetch";
import {setAccessAuthCode} from "./util/setAccessAuthCode";
import {exportLayout} from "../layout/util";
import {exitSiYuan} from "../dialog/processSystem";
import {openByMobile, writeText} from "../protyle/util/compatibility";
import {showMessage} from "../dialog/message";
import {Dialog} from "../dialog";
import {confirmDialog} from "../dialog/confirmDialog";
export const about = {
element: undefined as Element,
genHTML: () => {
return `
${window.siyuan.languages.autoDownloadUpdatePkg}
${window.siyuan.languages.autoDownloadUpdatePkgTip}
${window.siyuan.languages.about9}
${window.siyuan.languages.about10}
${window.siyuan.languages.about11}
${window.siyuan.languages.about12}
${window.siyuan.languages.useFixedPort}
${window.siyuan.languages.useFixedPortTip}
${window.siyuan.languages.about2}
${window.siyuan.languages.about3.replace("${port}", location.port)}
${window.siyuan.config.localIPs.join(" ")}
${window.siyuan.languages.about4}
${window.siyuan.languages.about5}
${window.siyuan.languages.about6}
${window.siyuan.languages.config}
${window.siyuan.languages.dataRepoKey}
${window.siyuan.languages.dataRepoKeyTip1}
${window.siyuan.languages.dataRepoKeyTip2}
${window.siyuan.languages.importKey}
${window.siyuan.languages.genKey}
${window.siyuan.languages.genKeyByPW}
${window.siyuan.languages.copyKey}
${window.siyuan.languages.resetRepo}
${window.siyuan.languages.systemLog}
${window.siyuan.languages.systemLogTip}
${window.siyuan.languages.export}
${window.siyuan.languages.currentVer} v${Constants.SIYUAN_VERSION}
${window.siyuan.languages.visitAnnouncements}
${window.siyuan.languages.checkUpdate}
${window.siyuan.languages.about8}
${window.siyuan.languages.about13}
${window.siyuan.languages.about14}
${window.siyuan.languages.siyuanNote}
${window.siyuan.languages.slogan}
会泽百家 至公天下
${window.siyuan.languages.about1}
`;
},
bindEvent: () => {
if (window.siyuan.config.system.isInsider) {
about.element.querySelector("#isInsider").innerHTML = "Insider Preview ";
}
const tokenElement = about.element.querySelector("#token") as HTMLInputElement;
tokenElement.addEventListener("click", () => {
tokenElement.select();
});
about.element.querySelector("#exportLog").addEventListener("click", () => {
fetchPost("/api/system/exportLog", {}, (response) => {
openByMobile(response.data.zip);
});
});
about.element.querySelector("#menuSafeQuit").addEventListener("click", () => {
exitSiYuan();
});
const updateElement = about.element.querySelector("#checkUpdateBtn");
updateElement.addEventListener("click", () => {
if (updateElement.firstElementChild.classList.contains("fn__rotate")) {
return;
}
updateElement.innerHTML = ` ${window.siyuan.languages.checkUpdate}`;
fetchPost("/api/system/checkUpdate", {showMsg: true}, () => {
updateElement.innerHTML = ` ${window.siyuan.languages.checkUpdate}`;
});
});
/// #if !BROWSER
about.element.querySelectorAll('[data-type="open"]').forEach(item => {
item.addEventListener("click", () => {
const url = item.getAttribute("data-url");
if (url.startsWith("http")) {
shell.openExternal(url);
} else {
shell.openPath(url);
}
});
});
const workspaceDirElement = about.element.querySelector("#workspaceDir") as HTMLInputElement;
workspaceDirElement.addEventListener("change", async () => {
let workspace = workspaceDirElement.value;
if (workspaceDirElement.value === "0") {
const localPath = await dialog.showOpenDialog({
defaultPath: window.siyuan.config.system.homeDir,
properties: ["openDirectory", "createDirectory"],
});
if (localPath.filePaths.length === 0) {
workspaceDirElement.value = window.siyuan.config.system.workspaceDir;
return;
}
workspace = localPath.filePaths[0];
}
fetchPost("/api/system/setWorkspaceDir", {
path: workspace
}, () => {
const searchData = JSON.parse(localStorage.getItem(Constants.LOCAL_SEARCHEDATA) || "{}");
if (searchData.hPath) {
searchData.hPath = "";
searchData.idPath = "";
localStorage.setItem(Constants.LOCAL_SEARCHEDATA, JSON.stringify(searchData));
}
localStorage.removeItem(Constants.LOCAL_DAILYNOTEID);
localStorage.removeItem(Constants.LOCAL_DOCINFO);
localStorage.removeItem(Constants.LOCAL_HISTORYNOTEID);
localStorage.removeItem("pdfjs.history");
exportLayout(false, () => {
exitSiYuan();
});
});
});
fetchPost("/api/system/listWorkspaceDirs", {}, (response) => {
let optionsHTML = "";
response.data.forEach((item: string) => {
optionsHTML += `${item} `;
});
workspaceDirElement.innerHTML = optionsHTML + `${window.siyuan.languages.updatePath} `;
workspaceDirElement.value = window.siyuan.config.system.workspaceDir;
});
/// #endif
about.element.querySelector("#authCode").addEventListener("click", () => {
setAccessAuthCode();
});
const importKeyElement = about.element.querySelector("#importKey");
importKeyElement.addEventListener("click", () => {
const passwordDialog = new Dialog({
title: "🔑 " + window.siyuan.languages.key,
content: `
${window.siyuan.languages.cancel}
${window.siyuan.languages.confirm}
`,
width: "520px",
});
const textAreaElement = passwordDialog.element.querySelector("textarea");
textAreaElement.focus();
const btnsElement = passwordDialog.element.querySelectorAll(".b3-button");
btnsElement[0].addEventListener("click", () => {
passwordDialog.destroy();
});
btnsElement[1].addEventListener("click", () => {
fetchPost("/api/repo/importRepoKey", {key: textAreaElement.value}, () => {
window.siyuan.config.repo.key = textAreaElement.value;
importKeyElement.parentElement.classList.add("fn__none");
importKeyElement.parentElement.nextElementSibling.classList.remove("fn__none");
passwordDialog.destroy();
});
});
});
about.element.querySelector("#initKey").addEventListener("click", () => {
confirmDialog("🔑 " + window.siyuan.languages.genKey, window.siyuan.languages.initRepoKeyTip, () => {
fetchPost("/api/repo/initRepoKey", {}, (response) => {
window.siyuan.config.repo.key = response.data.key;
importKeyElement.parentElement.classList.add("fn__none");
importKeyElement.parentElement.nextElementSibling.classList.remove("fn__none");
});
});
});
about.element.querySelector("#initKeyByPW").addEventListener("click", () => {
const initDialog = new Dialog({
title: "🔑 " + window.siyuan.languages.genKeyByPW,
content: `
${window.siyuan.languages.cancel}
${window.siyuan.languages.confirm}
`,
width: "520px",
});
const inputElement = initDialog.element.querySelector(".b3-text-field") as HTMLInputElement;
inputElement.focus();
const btnsElement = initDialog.element.querySelectorAll(".b3-button");
initDialog.bindInput(inputElement, () => {
(btnsElement[1] as HTMLButtonElement).click();
});
btnsElement[0].addEventListener("click", () => {
initDialog.destroy();
});
btnsElement[1].addEventListener("click", () => {
if (!inputElement.value) {
showMessage(window.siyuan.languages._kernel[142]);
return;
}
confirmDialog("🔑 " + window.siyuan.languages.genKeyByPW, window.siyuan.languages.initRepoKeyTip, () => {
initDialog.destroy();
fetchPost("/api/repo/InitRepoKeyFromPassphrase", {pass: inputElement.value}, (response) => {
window.siyuan.config.repo.key = response.data.key;
importKeyElement.parentElement.classList.add("fn__none");
importKeyElement.parentElement.nextElementSibling.classList.remove("fn__none");
});
});
});
});
about.element.querySelector("#copyKey").addEventListener("click", () => {
showMessage(window.siyuan.languages.copied);
writeText(window.siyuan.config.repo.key);
});
about.element.querySelector("#resetRepo").addEventListener("click", () => {
confirmDialog("⚠️ " + window.siyuan.languages.resetRepo, window.siyuan.languages.resetRepoTip, () => {
fetchPost("/api/repo/resetRepo", {}, () => {
window.siyuan.config.repo.key = "";
window.siyuan.config.sync.enabled = false;
importKeyElement.parentElement.classList.remove("fn__none");
importKeyElement.parentElement.nextElementSibling.classList.add("fn__none");
});
});
});
const networkServeElement = about.element.querySelector("#networkServe") as HTMLInputElement;
networkServeElement.addEventListener("change", () => {
fetchPost("/api/system/setNetworkServe", {networkServe: networkServeElement.checked}, () => {
exportLayout(false, () => {
exitSiYuan();
});
});
});
const fixedPortElement = about.element.querySelector("#fixedPort") as HTMLInputElement;
fixedPortElement.addEventListener("change", () => {
fetchPost("/api/system/setFixedPort", {fixedPort: fixedPortElement.checked}, () => {
exportLayout(false, () => {
exitSiYuan();
});
});
});
const uploadErrLogElement = about.element.querySelector("#uploadErrLog") as HTMLInputElement;
uploadErrLogElement.addEventListener("change", () => {
fetchPost("/api/system/setUploadErrLog", {uploadErrLog: uploadErrLogElement.checked}, () => {
exportLayout(false, () => {
exitSiYuan();
});
});
});
const downloadInstallPkgElement = about.element.querySelector("#downloadInstallPkg") as HTMLInputElement;
downloadInstallPkgElement.addEventListener("change", () => {
fetchPost("/api/system/setDownloadInstallPkg", {downloadInstallPkg: downloadInstallPkgElement.checked}, () => {
window.siyuan.config.system.downloadInstallPkg = downloadInstallPkgElement.checked;
});
});
about.element.querySelector("#aboutConfim").addEventListener("click", () => {
fetchPost("/api/system/setNetworkProxy", {
scheme: (about.element.querySelector("#aboutScheme") as HTMLInputElement).value,
host: (about.element.querySelector("#aboutHost") as HTMLInputElement).value,
port: (about.element.querySelector("#aboutPort") as HTMLInputElement).value
}, () => {
exportLayout(false, () => {
exitSiYuan();
});
});
});
}
};