import {Constants} from "../../constants"; import {setAccessAuthCode} from "../../config/util/about"; import {Dialog} from "../../dialog"; import {fetchPost} from "../../util/fetch"; import {confirmDialog} from "../../dialog/confirmDialog"; import {showMessage} from "../../dialog/message"; import {isInAndroid, isInHarmony, isInIOS, isIPad, openByMobile, writeText} from "../../protyle/util/compatibility"; import {exitSiYuan, processSync} from "../../dialog/processSystem"; import {pathPosix} from "../../util/pathName"; import {openModel} from "../menu/model"; import {setKey} from "../../sync/syncGuide"; import {isBrowser} from "../../util/functions"; export const initAbout = () => { openModel({ title: window.siyuan.languages.about, icon: "iconInfo", html: `
${window.siyuan.languages.exportCACert}
${window.siyuan.languages.exportCACertTip}
${window.siyuan.languages.exportCABundle}
${window.siyuan.languages.exportCABundleTip}
${window.siyuan.languages.importCABundle}
${window.siyuan.languages.importCABundleTip}
${window.siyuan.languages.about2}
${window.siyuan.languages.about4}
${window.siyuan.languages.about3.replace("${port}", location.port)}
${(() => { const serverAddrs: string[] = []; for (const serverAddr of window.siyuan.config.serverAddrs) { if (!serverAddr.trim()) { break; } serverAddrs.push(`${serverAddr}`); } return `
${serverAddrs.join(" ")}
`; })()}
${window.siyuan.languages.about18}
${window.siyuan.languages.about5}
${window.siyuan.languages.about6}
${window.siyuan.languages.dataRepoKey}
${window.siyuan.languages.dataRepoKeyTip1}
${window.siyuan.languages.dataRepoKeyTip2}
${window.siyuan.languages.dataRepoPurge}
${window.siyuan.languages.dataRepoPurgeTip}
${window.siyuan.languages.dataRepoAutoPurgeIndexRetentionDays}
${window.siyuan.languages.dataRepoAutoPurgeRetentionIndexesDaily}
${window.siyuan.languages.vacuumDataIndex}
${window.siyuan.languages.vacuumDataIndexTip}
${window.siyuan.languages.rebuildDataIndex}
${window.siyuan.languages.rebuildDataIndexTip}
${window.siyuan.languages.clearTempFiles}
${window.siyuan.languages.clearTempFilesTip}
${window.siyuan.languages.systemLog}
${window.siyuan.languages.systemLogTip}
${window.siyuan.languages.export} Data
${window.siyuan.languages.exportDataTip}
${window.siyuan.languages.import} Data
${window.siyuan.languages.importDataTip}
${window.siyuan.languages.exportConf}
${window.siyuan.languages.exportConfTip}
${window.siyuan.languages.importConf}
${window.siyuan.languages.importConfTip}
${window.siyuan.languages.workspaceList}
${window.siyuan.languages.about13}
${window.siyuan.languages.about14.replace("${token}", window.siyuan.config.api.token)}
会泽百家 至公天下
${window.siyuan.languages.about1} ${"harmony" === window.siyuan.config.system.container ? " • " + window.siyuan.languages.feedback + " 845765@qq.com" : ""}
`, bindEvent(modelMainElement: HTMLElement) { const workspaceDirElement = modelMainElement.querySelector("#workspaceDir"); genWorkspace(workspaceDirElement); const importKeyElement = modelMainElement.querySelector("#importKey"); modelMainElement.firstElementChild.addEventListener("click", (event) => { let target = event.target as HTMLElement; while (target && (target !== modelMainElement)) { if (target.id === "authCode") { setAccessAuthCode(); event.preventDefault(); event.stopPropagation(); break; } else if (target.id === "importKey") { const passwordDialog = new Dialog({ title: "🔑 " + window.siyuan.languages.key, content: `
`, width: "92vw", }); passwordDialog.element.setAttribute("data-key", Constants.DIALOG_PASSWORD); 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}, (response) => { window.siyuan.config.repo.key = response.data.key; importKeyElement.parentElement.classList.add("fn__none"); importKeyElement.parentElement.nextElementSibling.classList.remove("fn__none"); passwordDialog.destroy(); }); }); event.preventDefault(); event.stopPropagation(); break; } else if (target.id === "initKey") { 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"); }); }); event.preventDefault(); event.stopPropagation(); break; } else if (target.id === "initKeyByPW") { setKey(false, () => { importKeyElement.parentElement.classList.add("fn__none"); importKeyElement.parentElement.nextElementSibling.classList.remove("fn__none"); }); event.preventDefault(); event.stopPropagation(); break; } else if (target.id === "copyKey") { showMessage(window.siyuan.languages.copied); writeText(window.siyuan.config.repo.key); event.preventDefault(); event.stopPropagation(); break; } else if (target.id === "removeKey") { confirmDialog("⚠️ " + window.siyuan.languages.resetRepo, window.siyuan.languages.resetRepoTip, () => { fetchPost("/api/repo/resetRepo", {}, () => { window.siyuan.config.repo.key = ""; window.siyuan.config.sync.enabled = false; processSync(); importKeyElement.parentElement.classList.remove("fn__none"); importKeyElement.parentElement.nextElementSibling.classList.add("fn__none"); }); }); event.preventDefault(); event.stopPropagation(); break; } else if (target.id === "purgeRepo") { confirmDialog("♻️ " + window.siyuan.languages.dataRepoPurge, window.siyuan.languages.dataRepoPurgeConfirm, () => { fetchPost("/api/repo/purgeRepo"); }); event.preventDefault(); event.stopPropagation(); break; } else if (target.id === "tokenCopy") { showMessage(window.siyuan.languages.copied); writeText(window.siyuan.config.api.token); event.preventDefault(); event.stopPropagation(); break; } else if (target.id === "exportData") { fetchPost("/api/export/exportData", {}, response => { openByMobile(response.data.zip); }); event.preventDefault(); event.stopPropagation(); break; } else if (target.id === "exportConf") { fetchPost("/api/system/exportConf", {}, response => { openByMobile(response.data.zip); }); event.preventDefault(); event.stopPropagation(); break; } else if (target.id === "vacuumDataIndex") { fetchPost("/api/system/vacuumDataIndex", {}, () => { }); event.preventDefault(); event.stopPropagation(); break; } else if (target.id === "rebuildDataIndex") { fetchPost("/api/system/rebuildDataIndex", {}, () => { }); event.preventDefault(); event.stopPropagation(); break; } else if (target.id === "clearTempFiles") { fetchPost("/api/system/clearTempFiles", {}, () => { }); event.preventDefault(); event.stopPropagation(); break; } else if (target.id === "exportLog") { fetchPost("/api/system/exportLog", {}, (response) => { openByMobile(response.data.zip); }); event.preventDefault(); event.stopPropagation(); break; } else if (target.id === "openWorkspace") { fetchPost("/api/system/getMobileWorkspaces", {}, (response) => { let selectHTML = ""; response.data.forEach((item: string, index: number) => { selectHTML += ``; }); const openWorkspaceDialog = new Dialog({ title: window.siyuan.languages.openBy, content: `
`, width: "92vw", }); openWorkspaceDialog.element.setAttribute("data-key", Constants.SIYUAN_OPEN_WORKSPACE); const btnsElement = openWorkspaceDialog.element.querySelectorAll(".b3-button"); btnsElement[0].addEventListener("click", () => { openWorkspaceDialog.destroy(); }); btnsElement[1].addEventListener("click", () => { const openPath = openWorkspaceDialog.element.querySelector("select").value; if (openPath === window.siyuan.config.system.workspaceDir) { openWorkspaceDialog.destroy(); return; } confirmDialog(window.siyuan.languages.confirm, `${pathPosix().basename(window.siyuan.config.system.workspaceDir)} -> ${pathPosix().basename(openPath)}?`, () => { fetchPost("/api/system/setWorkspaceDir", { path: openPath }, () => { exitSiYuan(false); }); }); }); }); event.preventDefault(); event.stopPropagation(); break; } else if (target.id === "creatWorkspace") { const createWorkspaceDialog = new Dialog({ title: window.siyuan.languages.new, content: `
`, width: "92vw", }); createWorkspaceDialog.element.setAttribute("data-key", Constants.DIALOG_CREATEWORKSPACE); const inputElement = createWorkspaceDialog.element.querySelector("input"); inputElement.focus(); const btnsElement = createWorkspaceDialog.element.querySelectorAll(".b3-button"); btnsElement[0].addEventListener("click", () => { createWorkspaceDialog.destroy(); }); btnsElement[1].addEventListener("click", () => { fetchPost("/api/system/createWorkspaceDir", { path: pathPosix().join(pathPosix().dirname(window.siyuan.config.system.workspaceDir), inputElement.value) }, () => { genWorkspace(workspaceDirElement); createWorkspaceDialog.destroy(); }); }); event.preventDefault(); event.stopPropagation(); break; } else if (target.getAttribute("data-type") === "remove") { const removePath = target.parentElement.getAttribute("data-path"); fetchPost("/api/system/removeWorkspaceDir", {path: removePath}, () => { genWorkspace(workspaceDirElement); confirmDialog(window.siyuan.languages.deleteOpConfirm, window.siyuan.languages.removeWorkspacePhysically.replace("${x}", removePath), () => { fetchPost("/api/system/removeWorkspaceDirPhysically", {path: removePath}); }, undefined, true); }); event.preventDefault(); event.stopPropagation(); break; } else if (target.classList.contains("b3-list-item") && !target.classList.contains("b3-list-item--focus")) { confirmDialog(window.siyuan.languages.confirm, `${pathPosix().basename(window.siyuan.config.system.workspaceDir)} -> ${pathPosix().basename(target.getAttribute("data-path"))}?`, () => { fetchPost("/api/system/setWorkspaceDir", { path: target.getAttribute("data-path") }, () => { exitSiYuan(false); }); }); event.preventDefault(); event.stopPropagation(); break; } target = target.parentElement; } }); modelMainElement.querySelector("#importData").addEventListener("change", (event: InputEvent & { target: HTMLInputElement }) => { const formData = new FormData(); formData.append("file", event.target.files[0]); fetchPost("/api/import/importData", formData); }); modelMainElement.querySelector("#importConf").addEventListener("change", (event: InputEvent & { target: HTMLInputElement }) => { const formData = new FormData(); formData.append("file", event.target.files[0]); fetchPost("/api/system/importConf", formData, (response) => { if (response.code !== 0) { showMessage(response.msg); return; } exitSiYuan(); }); }); const networkServeElement = modelMainElement.querySelector("#networkServe") as HTMLInputElement; const networkServeTLSElement = modelMainElement.querySelector("#networkServeTLS") as HTMLInputElement; networkServeElement.addEventListener("change", () => { networkServeTLSElement.disabled = !networkServeElement.checked; if (!networkServeElement.checked) { networkServeTLSElement.checked = false; } fetchPost("/api/system/setNetworkServe", {networkServe: networkServeElement.checked}, () => { exitSiYuan(); }); }); networkServeTLSElement.addEventListener("change", () => { const exportCACertSection = modelMainElement.querySelector("#exportCACertSection"); const exportCABundleSection = modelMainElement.querySelector("#exportCABundleSection"); const importCABundleSection = modelMainElement.querySelector("#importCABundleSection"); if (exportCACertSection && exportCABundleSection && importCABundleSection) { if (networkServeTLSElement.checked) { exportCACertSection.classList.remove("fn__none"); exportCABundleSection.classList.remove("fn__none"); importCABundleSection.classList.remove("fn__none"); } else { exportCACertSection.classList.add("fn__none"); exportCABundleSection.classList.add("fn__none"); importCABundleSection.classList.add("fn__none"); } } fetchPost("/api/system/setNetworkServeTLS", {networkServeTLS: networkServeTLSElement.checked}, () => { exitSiYuan(); }); }); modelMainElement.querySelector("#exportCACert")?.addEventListener("click", () => { fetchPost("/api/system/exportTLSCACert", {}, (response) => { openByMobile(response.data.path); }); }); modelMainElement.querySelector("#exportCABundle")?.addEventListener("click", () => { fetchPost("/api/system/exportTLSCABundle", {}, (response) => { openByMobile(response.data.path); }); }); modelMainElement.querySelector("#importCABundle")?.addEventListener("click", () => { const input = document.createElement("input"); input.type = "file"; input.accept = ".zip"; input.onchange = () => { if (input.files && input.files[0]) { const formData = new FormData(); formData.append("file", input.files[0]); fetch("/api/system/importTLSCABundle", { method: "POST", body: formData, }).then(res => res.json()).then((response) => { if (response.code === 0) { showMessage(window.siyuan.languages.importCABundleSuccess); } else { showMessage(response.msg, 6000, "error"); } }); } }; input.click(); }); const tokenElement = modelMainElement.querySelector("#token") as HTMLInputElement; tokenElement.addEventListener("change", () => { fetchPost("/api/system/setAPIToken", {token: tokenElement.value}, () => { window.siyuan.config.api.token = tokenElement.value; modelMainElement.querySelector("#tokenTip").innerHTML = window.siyuan.languages.about14.replace("${token}", window.siyuan.config.api.token); }); }); const indexRetentionDaysElement = modelMainElement.querySelector("#indexRetentionDays") as HTMLInputElement; indexRetentionDaysElement.addEventListener("change", () => { fetchPost("/api/repo/setRepoIndexRetentionDays", {days: parseInt(indexRetentionDaysElement.value)}, () => { window.siyuan.config.repo.indexRetentionDays = parseInt(indexRetentionDaysElement.value); }); }); const retentionIndexesDailyElement = modelMainElement.querySelector("#retentionIndexesDaily") as HTMLInputElement; retentionIndexesDailyElement.addEventListener("change", () => { fetchPost("/api/repo/setRetentionIndexesDaily", {indexes: parseInt(retentionIndexesDailyElement.value)}, () => { window.siyuan.config.repo.retentionIndexesDaily = parseInt(retentionIndexesDailyElement.value); }); }); } }); }; const genWorkspace = (workspaceDirElement: Element) => { fetchPost("/api/system/getWorkspaces", {}, (response) => { let html = ""; response.data.forEach((item: IWorkspace) => { html += `
  • ${pathPosix().basename(item.path)}
  • `; }); workspaceDirElement.innerHTML = html; }); };