import {fetchPost} from "../util/fetch";
import {hasClosestByTag} from "../protyle/util/hasClosest";
import {isMobile} from "../util/functions";
export const publish = {
element: undefined as Element,
genHTML: () => {
const mobile = isMobile();
return `
${(()=>{
if (mobile) {
return `
${window.siyuan.languages.publishServicePort}
${window.siyuan.languages.publishServicePortTip}
`;
} else {
return `
`;
}
})()}
${window.siyuan.languages.publishServiceAddresses}
${window.siyuan.languages.publishServiceAddressesTip}
${(()=>{
if (mobile) {
return `
${window.siyuan.languages.publishServiceAuthAccounts}
${window.siyuan.languages.publishServiceAuthAccountsTip}
`;
} else {
return `
${window.siyuan.languages.publishServiceAuthAccounts}
${window.siyuan.languages.publishServiceAuthAccountsTip}
`;
}
})()}
`;
},
bindEvent: () => {
const publishAuthAccountAdd = publish.element.querySelector("#publishAuthAccountAdd");
// add account
publishAuthAccountAdd.addEventListener("click", () => {
window.siyuan.config.publish.auth.accounts.push({
username: "",
password: "",
memo: "",
});
publish._renderPublishAuthAccounts(publish.element);
});
// input change
publish.element.querySelectorAll("input").forEach(item => {
item.addEventListener("change", () => {
publish._savePublish();
});
});
publish._refreshPublish();
},
_refreshPublish: () => {
fetchPost("/api/setting/getPublish", {}, publish._updatePublishConfig.bind(null, true));
},
_savePublish: (reloadAccounts = true) => {
const publishEnable = publish.element.querySelector("#publishEnable");
const publishPort = publish.element.querySelector("#publishPort");
const publishAuthEnable = publish.element.querySelector("#publishAuthEnable");
fetchPost("/api/setting/setPublish", {
enable: publishEnable.checked,
port: publishPort.valueAsNumber,
auth: {
enable: publishAuthEnable.checked,
accounts: window.siyuan.config.publish.auth.accounts,
} as Config.IPublishAuth,
} as Config.IPublish, publish._updatePublishConfig.bind(null, reloadAccounts));
},
_updatePublishConfig: (
reloadAccounts: boolean,
response: IWebSocketData,
) => {
if (response.code === 0) {
window.siyuan.config.publish = response.data.publish;
if (reloadAccounts) {
publish._renderPublishAuthAccounts(publish.element);
}
publish._renderPublishAddressList(publish.element, response.data.port);
} else {
publish._renderPublishAddressList(publish.element, 0);
}
},
_renderPublishAuthAccounts: (
element: Element,
accounts: Config.IPublishAuthAccount[] = window.siyuan.config.publish.auth.accounts,
) => {
const mobile = isMobile();
const publishAuthAccounts = element.querySelector("#publishAuthAccounts");
publishAuthAccounts.innerHTML = ``;
// account field changed
publishAuthAccounts
.querySelectorAll("input")
.forEach(input => {
input.addEventListener("change", () => {
const li = hasClosestByTag(input, "LI");
if (li) {
const index = parseInt(li.dataset.index);
const name = input.dataset.name as keyof Config.IPublishAuthAccount;
if (name in window.siyuan.config.publish.auth.accounts[index]) {
window.siyuan.config.publish.auth.accounts[index][name] = input.value;
publish._savePublish(false);
}
}
});
});
// delete account
publishAuthAccounts
.querySelectorAll('[data-action="remove"]')
.forEach(remove => {
remove.addEventListener("click", () => {
const li = hasClosestByTag(remove, "LI");
if (li) {
const index = parseInt(li.dataset.index);
window.siyuan.config.publish.auth.accounts.splice(index, 1);
publish._savePublish();
}
});
});
// Toggle the password display status
publishAuthAccounts
.querySelectorAll('.b3-form__icona-icon[data-action="togglePassword"]')
.forEach(togglePassword => {
togglePassword.addEventListener("click", () => {
const isEye = togglePassword.firstElementChild.getAttribute("xlink:href") === "#iconEye";
togglePassword.firstElementChild.setAttribute("xlink:href", isEye ? "#iconEyeoff" : "#iconEye");
togglePassword.previousElementSibling.setAttribute("type", isEye ? "text" : "password");
});
});
},
_renderPublishAddressList: (
element: Element,
port: number,
) => {
const publishAddresses = element.querySelector("#publishAddresses");
if (port === 0) {
publishAddresses.innerText = window.siyuan.languages.publishServiceNotStarted;
} else {
publishAddresses.innerHTML = `${
window.siyuan.config.localIPs
.filter(ip => !(ip.startsWith("[") && ip.endsWith("]")))
.map(ip => `${ip}:${port} `)
.join("")
}${
window.siyuan.config.localIPs
.filter(ip => (ip.startsWith("[") && ip.endsWith("]")))
.map(ip => `${ip}:${port} `)
.join("")
}
`;
}
},
};