import {getAllModels} from "../layout/getAll";
import {setInlineStyle} from "../util/assets";
import {fetchPost} from "../util/fetch";
import {confirmDialog} from "../dialog/confirmDialog";
import {setPadding} from "../protyle/ui/initUI";
import {reloadProtyle} from "../protyle/util/reload";
export const editor = {
element: undefined as Element,
genHTML: () => {
let fontFamilyHTML = "";
fontFamilyHTML = '';
return `
${window.siyuan.languages.katexMacros}
${window.siyuan.languages.katexMacrosTip}
`;
},
bindEvent: () => {
const fontFamilyElement = editor.element.querySelector("#fontFamily") as HTMLSelectElement;
if (fontFamilyElement.tagName === "SELECT") {
let fontFamilyHTML = ``;
fetchPost("/api/system/getSysFonts", {}, (response) => {
response.data.forEach((item: string) => {
fontFamilyHTML += ``;
});
fontFamilyElement.innerHTML = fontFamilyHTML;
});
}
editor.element.querySelector("#clearHistory").addEventListener("click", () => {
confirmDialog(window.siyuan.languages.clearHistory, window.siyuan.languages.confirmClearHistory, () => {
fetchPost("/api/history/clearWorkspaceHistory", {});
});
});
const setEditor = () => {
fetchPost("/api/setting/setEditor", {
fullWidth: (editor.element.querySelector("#fullWidth") as HTMLInputElement).checked,
displayBookmarkIcon: (editor.element.querySelector("#displayBookmarkIcon") as HTMLInputElement).checked,
displayNetImgMark: (editor.element.querySelector("#displayNetImgMark") as HTMLInputElement).checked,
codeSyntaxHighlightLineNum: (editor.element.querySelector("#codeSyntaxHighlightLineNum") as HTMLInputElement).checked,
plantUMLServePath: (editor.element.querySelector("#plantUMLServePath") as HTMLInputElement).value,
katexMacros: (editor.element.querySelector("#katexMacros") as HTMLTextAreaElement).value,
codeLineWrap: (editor.element.querySelector("#codeLineWrap") as HTMLInputElement).checked,
virtualBlockRef: (editor.element.querySelector("#virtualBlockRef") as HTMLInputElement).checked,
virtualBlockRefExclude: (editor.element.querySelector("#virtualBlockRefExclude") as HTMLInputElement).value,
blockRefDynamicAnchorTextMaxLen: parseInt((editor.element.querySelector("#blockRefDynamicAnchorTextMaxLen") as HTMLInputElement).value),
codeLigatures: (editor.element.querySelector("#codeLigatures") as HTMLInputElement).checked,
codeTabSpaces: parseInt((editor.element.querySelector("#codeTabSpaces") as HTMLInputElement).value),
fontSize: parseInt((editor.element.querySelector("#fontSize") as HTMLInputElement).value),
generateHistoryInterval: parseInt((editor.element.querySelector("#generateHistoryInterval") as HTMLInputElement).value),
historyRetentionDays: parseInt((editor.element.querySelector("#historyRetentionDays") as HTMLInputElement).value),
fontFamily: fontFamilyElement.value,
emoji: window.siyuan.config.editor.emoji
}, response => {
editor.onSetEditor(response.data);
});
};
editor.element.querySelectorAll("input.b3-switch, select.b3-select, input.b3-slider").forEach((item) => {
item.addEventListener("change", () => {
setEditor();
});
});
editor.element.querySelectorAll("textarea.b3-text-field, input.b3-text-field, input.b3-slider").forEach((item) => {
item.addEventListener("blur", () => {
setEditor();
});
});
editor.element.querySelectorAll("input.b3-slider").forEach((item) => {
item.addEventListener("input", (event) => {
const target = event.target as HTMLInputElement;
target.parentElement.setAttribute("aria-label", target.value);
});
});
},
onSetEditor: (editor: IEditor) => {
window.siyuan.config.editor = editor;
getAllModels().editor.forEach((item) => {
reloadProtyle(item.editor.protyle)
setPadding(item.editor.protyle);
if (window.siyuan.config.editor.fullWidth) {
item.editor.protyle.contentElement.setAttribute("data-fullwidth", "true");
} else {
item.editor.protyle.contentElement.removeAttribute("data-fullwidth");
}
});
setInlineStyle();
}
};