import {needLogin, needSubscribe} from "../util/needSubscribe";
import {fetchPost} from "../util/fetch";
import {showMessage} from "../dialog/message";
import {bindSyncCloudListEvent, getSyncCloudList} from "../sync/syncGuide";
import {hasClosestByClassName} from "../protyle/util/hasClosest";
import {processSync} from "../dialog/processSystem";
import {getCloudURL} from "./util/about";
const renderProvider = (provider: number) => {
if (provider === 0) {
if (needSubscribe("")) {
return `
${window.siyuan.config.system.container === "ios" ? window.siyuan.languages._kernel[122] : window.siyuan.languages._kernel[29].replace("${url}", getCloudURL("subscribe/siyuan"))}
${window.siyuan.languages.cloudIntro1}
- ${window.siyuan.languages.cloudIntro2}
- ${window.siyuan.languages.cloudIntro3}
- ${window.siyuan.languages.cloudIntro4}
- ${window.siyuan.languages.cloudIntro5}
- ${window.siyuan.languages.cloudIntro6}
- ${window.siyuan.languages.cloudIntro7}
- ${window.siyuan.languages.cloudIntro8}
${window.siyuan.languages.cloudIntro9}
- ${window.siyuan.languages.cloudIntro10}
- ${window.siyuan.languages.cloudIntro11}
`;
}
return `
${window.siyuan.languages.syncOfficialProviderIntro}
`;
}
if (needLogin("")) {
return `${window.siyuan.languages.needLogin}
`;
}
if (provider === 2) {
return `
${window.siyuan.languages.syncThirdPartyProviderS3Intro}
${window.siyuan.languages.featureBetaStage}
${window.siyuan.languages.syncThirdPartyProviderTip}
`;
} else if (provider === 3) {
return `
${window.siyuan.languages.syncThirdPartyProviderWebDAVIntro}
${window.siyuan.languages.featureBetaStage}
${window.siyuan.languages.syncThirdPartyProviderTip}
`;
}
return "";
};
const bindProviderEvent = () => {
const reposDataElement = repos.element.querySelector("#reposData");
const loadingElement = repos.element.querySelector("#reposLoading");
if (window.siyuan.config.sync.provider === 0) {
if (needSubscribe("")) {
loadingElement.classList.add("fn__none");
let nextElement = reposDataElement;
while (nextElement) {
nextElement.classList.add("fn__none");
nextElement = nextElement.nextElementSibling;
}
return;
}
fetchPost("/api/cloud/getCloudSpace", {}, (response) => {
loadingElement.classList.add("fn__none");
if (response.code === 1) {
reposDataElement.innerHTML = response.msg;
return;
} else {
reposDataElement.innerHTML = `
${window.siyuan.languages.cloudStorage}
- ${window.siyuan.languages.sync}${response.data.sync ? response.data.sync.hSize : "0B"}
- ${window.siyuan.languages.backup}${response.data.backup ? response.data.backup.hSize : "0B"}
- ${window.siyuan.languages.cdn}${response.data.hAssetSize}
- ${window.siyuan.languages.total}${response.data.hSize}
- ${window.siyuan.languages.sizeLimit}${response.data.hTotalSize}
- ${window.siyuan.languages.pointExchangeSize}${response.data.hExchangeSize}
${window.siyuan.languages.trafficStat}
- ${window.siyuan.languages.upload}${response.data.hTrafficUploadSize}
- ${window.siyuan.languages.download}${response.data.hTrafficDownloadSize}
- API GET${response.data.hTrafficAPIGet}
- API PUT${response.data.hTrafficAPIPut}
`;
}
});
reposDataElement.classList.remove("fn__none");
return;
}
loadingElement.classList.add("fn__none");
let nextElement = reposDataElement.nextElementSibling;
while (nextElement) {
if (!needLogin("")) {
nextElement.classList.remove("fn__none");
} else {
nextElement.classList.add("fn__none");
}
nextElement = nextElement.nextElementSibling;
}
reposDataElement.classList.add("fn__none");
const providerPanelElement = repos.element.querySelector("#syncProviderPanel");
providerPanelElement.querySelectorAll(".b3-text-field, .b3-select").forEach(item => {
item.addEventListener("blur", () => {
if (window.siyuan.config.sync.provider === 2) {
let timeout = parseInt((providerPanelElement.querySelector("#timeout") as HTMLInputElement).value, 10);
if (7 > timeout) {
if (1 > timeout) {
timeout = 30;
} else {
timeout = 7;
}
}
if (300 < timeout) {
timeout = 300;
}
(providerPanelElement.querySelector("#timeout") as HTMLInputElement).value = timeout.toString();
const s3 = {
endpoint: (providerPanelElement.querySelector("#endpoint") as HTMLInputElement).value,
accessKey: (providerPanelElement.querySelector("#accessKey") as HTMLInputElement).value,
secretKey: (providerPanelElement.querySelector("#secretKey") as HTMLInputElement).value,
bucket: (providerPanelElement.querySelector("#bucket") as HTMLInputElement).value,
pathStyle: (providerPanelElement.querySelector("#pathStyle") as HTMLInputElement).value === "true",
region: (providerPanelElement.querySelector("#region") as HTMLInputElement).value,
skipTlsVerify: (providerPanelElement.querySelector("#s3SkipTlsVerify") as HTMLInputElement).value === "true",
timeout: timeout,
};
fetchPost("/api/sync/setSyncProviderS3", {s3}, () => {
window.siyuan.config.sync.s3 = s3;
});
} else if (window.siyuan.config.sync.provider === 3) {
let timeout = parseInt((providerPanelElement.querySelector("#timeout") as HTMLInputElement).value, 10);
if (7 > timeout) {
timeout = 7;
}
if (300 < timeout) {
timeout = 300;
}
(providerPanelElement.querySelector("#timeout") as HTMLInputElement).value = timeout.toString();
const webdav = {
endpoint: (providerPanelElement.querySelector("#endpoint") as HTMLInputElement).value,
username: (providerPanelElement.querySelector("#username") as HTMLInputElement).value,
password: (providerPanelElement.querySelector("#password") as HTMLInputElement).value,
skipTlsVerify: (providerPanelElement.querySelector("#webdavSkipTlsVerify") as HTMLInputElement).value === "true",
timeout: timeout,
};
fetchPost("/api/sync/setSyncProviderWebDAV", {webdav}, () => {
window.siyuan.config.sync.webdav = webdav;
});
}
});
});
};
export const repos = {
element: undefined as Element,
genHTML: () => {
return `
${renderProvider(window.siyuan.config.sync.provider)}
${window.siyuan.languages.cloudStorage}
${window.siyuan.languages.trafficStat}
${window.siyuan.languages.cloudBackup}
${window.siyuan.languages.cloudBackupTip}
`;
},
bindEvent: () => {
bindProviderEvent();
const switchElement = repos.element.querySelector("#reposCloudSyncSwitch") as HTMLInputElement;
switchElement.addEventListener("change", () => {
if (switchElement.checked && window.siyuan.config.sync.cloudName === "") {
switchElement.checked = false;
showMessage(window.siyuan.languages._kernel[123]);
return;
}
fetchPost("/api/sync/setSyncEnable", {enabled: switchElement.checked}, () => {
window.siyuan.config.sync.enabled = switchElement.checked;
processSync();
});
});
const syncPerceptionElement = repos.element.querySelector("#syncPerception") as HTMLInputElement;
syncPerceptionElement.addEventListener("change", () => {
fetchPost("/api/sync/setSyncPerception", {enabled: syncPerceptionElement.checked}, () => {
window.siyuan.config.sync.perception = syncPerceptionElement.checked;
processSync();
});
});
const switchConflictElement = repos.element.querySelector("#generateConflictDoc") as HTMLInputElement;
switchConflictElement.addEventListener("change", () => {
fetchPost("/api/sync/setSyncGenerateConflictDoc", {enabled: switchConflictElement.checked}, () => {
window.siyuan.config.sync.generateConflictDoc = switchConflictElement.checked;
});
});
const syncModeElement = repos.element.querySelector("#syncMode") as HTMLSelectElement;
syncModeElement.addEventListener("change", () => {
fetchPost("/api/sync/setSyncMode", {mode: parseInt(syncModeElement.value, 10)}, () => {
if (syncModeElement.value === "1" && window.siyuan.config.sync.provider === 0 && window.siyuan.config.system.container !== "docker") {
syncPerceptionElement.parentElement.classList.remove("fn__none");
} else {
syncPerceptionElement.parentElement.classList.add("fn__none");
}
window.siyuan.config.sync.mode = parseInt(syncModeElement.value, 10);
});
});
const syncConfigElement = repos.element.querySelector("#reposCloudSyncList");
const syncProviderElement = repos.element.querySelector("#syncProvider") as HTMLSelectElement;
syncProviderElement.addEventListener("change", () => {
fetchPost("/api/sync/setSyncProvider", {provider: parseInt(syncProviderElement.value, 10)}, (response) => {
if (response.code === 1) {
showMessage(response.msg);
syncProviderElement.value = "0";
window.siyuan.config.sync.provider = 0;
} else {
window.siyuan.config.sync.provider = parseInt(syncProviderElement.value, 10);
}
repos.element.querySelector("#syncProviderPanel").innerHTML = renderProvider(window.siyuan.config.sync.provider);
bindProviderEvent();
syncConfigElement.innerHTML = "";
syncConfigElement.classList.add("fn__none");
if (window.siyuan.config.sync.mode !== 1 || window.siyuan.config.system.container === "docker" || window.siyuan.config.sync.provider !== 0) {
syncPerceptionElement.parentElement.classList.add("fn__none");
} else {
syncPerceptionElement.parentElement.classList.remove("fn__none");
}
});
});
const loadingElement = repos.element.querySelector("#reposLoading") as HTMLElement;
loadingElement.style.width = repos.element.clientWidth + "px";
loadingElement.style.height = repos.element.clientHeight + "px";
bindSyncCloudListEvent(syncConfigElement);
repos.element.firstElementChild.addEventListener("click", (event) => {
const target = event.target as HTMLElement;
if (target.getAttribute("data-type") === "config") {
if (syncConfigElement.classList.contains("fn__none")) {
getSyncCloudList(syncConfigElement, true);
syncConfigElement.classList.remove("fn__none");
} else {
syncConfigElement.classList.add("fn__none");
}
return;
}
const eyeElement = hasClosestByClassName(target, "b3-form__icona-icon");
if (eyeElement) {
const isEye = eyeElement.firstElementChild.getAttribute("xlink:href") === "#iconEye";
eyeElement.firstElementChild.setAttribute("xlink:href", isEye ? "#iconEyeoff" : "#iconEye");
eyeElement.previousElementSibling.setAttribute("type", isEye ? "text" : "password");
}
});
},
};