From 6ca6817465ba739fbc958cc4dbe8314d01fefd4b Mon Sep 17 00:00:00 2001 From: Vanessa Date: Mon, 15 May 2023 15:43:16 +0800 Subject: [PATCH] :art: https://github.com/siyuan-note/siyuan/issues/8220 --- app/src/config/about.ts | 37 +++------------------- app/src/mobile/settings/about.ts | 37 +++------------------- app/src/sync/syncGuide.ts | 53 ++++++++++++++++++++++++-------- 3 files changed, 49 insertions(+), 78 deletions(-) diff --git a/app/src/config/about.ts b/app/src/config/about.ts index a43143926..1cb9f4262 100644 --- a/app/src/config/about.ts +++ b/app/src/config/about.ts @@ -12,6 +12,7 @@ import {showMessage} from "../dialog/message"; import {Dialog} from "../dialog"; import {confirmDialog} from "../dialog/confirmDialog"; import {setProxy} from "./util/setProxy"; +import {setKey} from "../sync/syncGuide"; export const about = { element: undefined as Element, @@ -265,39 +266,9 @@ export const about = { }); }); about.element.querySelector("#initKeyByPW").addEventListener("click", () => { - const initDialog = new Dialog({ - title: "🔑 " + window.siyuan.languages.genKeyByPW, - content: `
- -
-
-
- -
`, - 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"); - }); - }); + setKey(false, () => { + importKeyElement.parentElement.classList.add("fn__none"); + importKeyElement.parentElement.nextElementSibling.classList.remove("fn__none"); }); }); about.element.querySelector("#copyKey").addEventListener("click", () => { diff --git a/app/src/mobile/settings/about.ts b/app/src/mobile/settings/about.ts index 812e343ae..3fe4421ee 100644 --- a/app/src/mobile/settings/about.ts +++ b/app/src/mobile/settings/about.ts @@ -8,6 +8,7 @@ import {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"; export const initAbout = () => { if (!window.siyuan.config.localIPs || window.siyuan.config.localIPs.length === 0 || @@ -195,39 +196,9 @@ export const initAbout = () => { event.stopPropagation(); break; } else if (target.id === "initKeyByPW") { - const initDialog = new Dialog({ - title: "🔑 " + window.siyuan.languages.genKeyByPW, - content: `
- -
-
-
- -
`, - width: "92vw", - }); - 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"); - }); - }); + setKey(false, () => { + importKeyElement.parentElement.classList.add("fn__none"); + importKeyElement.parentElement.nextElementSibling.classList.remove("fn__none"); }); event.preventDefault(); event.stopPropagation(); diff --git a/app/src/sync/syncGuide.ts b/app/src/sync/syncGuide.ts index 1f8e7358e..d51459ffc 100644 --- a/app/src/sync/syncGuide.ts +++ b/app/src/sync/syncGuide.ts @@ -166,7 +166,7 @@ export const syncGuide = (app?: App) => { } /// #endif if (!window.siyuan.config.repo.key) { - setKey(); + setKey(true); return; } if (!window.siyuan.config.sync.enabled) { @@ -276,39 +276,68 @@ const setSync = (key?: string, dialog?: Dialog) => { } }; -const setKey = () => { +export const setKey = (isSync:boolean, cb?:() => void) => { const dialog = new Dialog({ title: window.siyuan.languages.syncConfGuide1, content: `
${window.siyuan.languages.syncConfGuide2}
-
+
- +
+ + + +
`, width: isMobile() ? "92vw" : "520px", }); dialog.element.querySelector(".b3-button--cancel").addEventListener("click", () => { dialog.destroy(); }); - const inputElement = dialog.element.querySelector(".b3-text-field") as HTMLInputElement; - dialog.element.querySelector("#initKeyByPW").addEventListener("click", () => { - if (!inputElement.value) { + const genBtnElement = dialog.element.querySelector("#initKeyByPW") + dialog.element.querySelector(".b3-switch").addEventListener("change", function () { + if (this.checked) { + genBtnElement.removeAttribute("disabled"); + } else { + genBtnElement.setAttribute("disabled", "disabled"); + } + }); + const inputElements = dialog.element.querySelectorAll(".b3-text-field") as NodeListOf; + genBtnElement.addEventListener("click", () => { + if (!inputElements[0].value || !inputElements[1].value) { showMessage(window.siyuan.languages._kernel[142]); return; } + if (inputElements[0].value !== inputElements[1].value) { + showMessage(window.siyuan.languages.passwordNoMatch); + return; + } confirmDialog("🔑 " + window.siyuan.languages.genKeyByPW, window.siyuan.languages.initRepoKeyTip, () => { - fetchPost("/api/repo/initRepoKeyFromPassphrase", {pass: inputElement.value}, (response) => { - setSync(response.data.key, dialog); + if (!isSync) { + dialog.destroy(); + } + fetchPost("/api/repo/initRepoKeyFromPassphrase", {pass: inputElements[0].value}, (response) => { + window.siyuan.config.repo.key = response.data.key; + if (cb) { + cb() + } + if (isSync) { + setSync(response.data.key, dialog); + } }); }); }); - inputElement.focus(); + inputElements[0].focus(); };