${window.siyuan.languages.appearance14}
@@ -251,6 +263,7 @@ export const appearance = {
}
});
},
+ _snippet: [] as ISnippet[],
bindEvent: () => {
if (window.siyuan.config.appearance.customCSS) {
fetchPost("/api/setting/getCustomCSS", {
@@ -259,6 +272,58 @@ export const appearance = {
appearance.onGetcustomcss(response.data);
});
}
+ const codeSnippetPanelElement = appearance.element.querySelector("#codeSnippetPanel");
+ const codeSnippetElement = appearance.element.querySelector("#codeSnippet");
+ codeSnippetElement.addEventListener("click", () => {
+ if (codeSnippetPanelElement.innerHTML) {
+ codeSnippetPanelElement.innerHTML = "";
+ return;
+ }
+ fetchPost("/api/snippet/getSnippet", {type: "all", enabled: 2}, (response) => {
+ appearance._snippet = response.data._snippet;
+ let html = `
+
+
+
+
+
+
`;
+ response.data.snippets.forEach((item: ISnippet) => {
+ html += `
+
+
${item.type}
+
+
${item.name}
+
+
+
+
+
+
`
+ });
+ codeSnippetPanelElement.innerHTML = html;
+ });
+ });
+ codeSnippetPanelElement.addEventListener("click", (event) => {
+ const target = event.target as HTMLElement;
+ if (target.id === "addCodeSnippetCSS" || target.id === "addCodeSnippetJS") {
+ target.parentElement.insertAdjacentHTML("afterend", `
+
+
${target.id === "addCodeSnippetCSS" ? "css" : "js"}
+
+
+
+
+
+
+
+
`)
+ }
+ })
const appearanceCustomElement = appearance.element.querySelector("#appearanceCustom");
appearanceCustomElement.addEventListener("click", () => {
if (window.siyuan.config.appearance.customCSS) {
diff --git a/app/src/types/index.d.ts b/app/src/types/index.d.ts
index b9e8e3ae5..935bff846 100644
--- a/app/src/types/index.d.ts
+++ b/app/src/types/index.d.ts
@@ -49,6 +49,13 @@ interface ITextOption {
type: string
}
+interface ISnippet {
+ name: string
+ type: string
+ enabled: boolean
+ content: string
+}
+
interface IInbox {
oId: string
shorthandContent: string
diff --git a/app/src/util/assets.ts b/app/src/util/assets.ts
index 56319d4ad..bfad33f50 100644
--- a/app/src/util/assets.ts
+++ b/app/src/util/assets.ts
@@ -87,20 +87,30 @@ export const loadAssets = (data: IAppearance) => {
};
export const renderSnippet = () => {
- fetchPost("/api/snippet/getSnippet", {type: "all", enabled: 1}, (response) => {
- response.data.snippets.forEach((item: {
- "name": string
- "type": string
- "content": string
- }) => {
+ fetchPost("/api/snippet/getSnippet", {type: "all"}, (response) => {
+ response.data.snippets.forEach((item: ISnippet) => {
+ const id = `snippet${item.type === "css" ? "CSS" : "JS"}${item.name}`;
+ let exitElement = document.getElementById(id) as HTMLScriptElement;
+ if (!item.enabled) {
+ if (exitElement) {
+ exitElement.remove();
+ }
+ return;
+ }
+ if (exitElement) {
+ if (exitElement.innerHTML === item.content) {
+ return;
+ }
+ exitElement.remove();
+ }
if (item.type === "css") {
- document.head.insertAdjacentHTML("beforeend", ``);
+ document.head.insertAdjacentHTML("beforeend", ``);
} else if (item.type === "js") {
- const scriptElement = document.createElement("script");
- scriptElement.type = "text/javascript";
- scriptElement.text = item.content;
- scriptElement.id = `snippetJS${item.name}`;
- document.head.appendChild(scriptElement);
+ exitElement = document.createElement("script");
+ exitElement.type = "text/javascript";
+ exitElement.text = item.content;
+ exitElement.id = id;
+ document.head.appendChild(exitElement);
}
});
});