Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Use a submenu for code block Tab spacing https://github.com/siyuan-note/siyuan/issues/14722

Daniel64dbd3c3c6d6
diff --git a/app/src/config/entryVisibility/catalog.test.ts b/app/src/config/entryVisibility/catalog.test.ts
index 0830fa5699..926b89114f 100644
--- a/app/src/config/entryVisibility/catalog.test.ts
+++ b/app/src/config/entryVisibility/catalog.test.ts
@@ -121,6 +121,14 @@ test("code block actions follow the code block menu order", () => {
"md27",
"saveCodeBlockAsFile",
]);
+ assert.deepEqual(getEntryCatalogChildren("gutter.single.code.md29").map((item) => item.key), [
+ "default",
+ "tabSpaces0",
+ "tabSpaces2",
+ "tabSpaces4",
+ "tabSpaces6",
+ "tabSpaces8",
+ ]);
});
test("multiple block heading transform follows the regular transform menu", () => {
diff --git a/app/src/config/entryVisibility/catalog.ts b/app/src/config/entryVisibility/catalog.ts
index 8f639a6b13..7f3519c657 100644
--- a/app/src/config/entryVisibility/catalog.ts
+++ b/app/src/config/entryVisibility/catalog.ts
@@ -1,3 +1,5 @@
+import {CODE_TAB_SPACE_VALUES} from "../../protyle/wysiwyg/codeBlockUtil";
+
export interface IEntryCatalogNode {
key: string;
label: () => string;
@@ -30,6 +32,11 @@ const separator = (key: string): IEntryCatalogNode => ({
type: "separator",
});
+const codeTabSpacesChildren = () => [
+ node("default", () => `${window.siyuan.languages.default} (${window.siyuan.config.editor.codeTabSpaces})`),
+ ...CODE_TAB_SPACE_VALUES.map((value) => node(`tabSpaces${value}`, literal(value.toString()))),
+];
+
const copyChildren = () => [
node("copyBlockRef", lang("copyBlockRef")),
node("copyBlockEmbed", lang("copyBlockEmbed")),
@@ -312,7 +319,7 @@ const gutterSingle = () => [
]),
separator("separator_code"),
node("code", lang("code"), true, [
- node("md29", lang("md29")),
+ node("md29", lang("md29"), true, codeTabSpacesChildren()),
node("md31", lang("md31")),
node("md2", lang("md2")),
node("md27", lang("md27")),
diff --git a/app/src/protyle/gutter/index.ts b/app/src/protyle/gutter/index.ts
index 60002eada4..1d95ffeed4 100644
--- a/app/src/protyle/gutter/index.ts
+++ b/app/src/protyle/gutter/index.ts
@@ -2072,9 +2072,17 @@ export class Gutter {
const ligatures = nodeElement.getAttribute("ligatures");
const linenumber = nodeElement.getAttribute("linenumber");
const codeTabSpaces = nodeElement.getAttribute(Constants.CUSTOM_SY_CODE_TAB_SPACES);
- const codeTabSpacesOptions = [`<option value=""${codeTabSpaces === null ? " selected" : ""}>${window.siyuan.languages.default} (${window.siyuan.config.editor.codeTabSpaces})</option>`]
- .concat(CODE_TAB_SPACE_VALUES.map((value) => `<option value="${value}"${codeTabSpaces === value.toString() ? " selected" : ""}>${value}</option>`))
- .join("");
+ const setCodeTabSpaces = (value: string) => {
+ if (value === "") {
+ nodeElement.removeAttribute(Constants.CUSTOM_SY_CODE_TAB_SPACES);
+ } else {
+ nodeElement.setAttribute(Constants.CUSTOM_SY_CODE_TAB_SPACES, value);
+ }
+ fetchPost("/api/attr/setBlockAttrs", {
+ id,
+ attrs: {[Constants.CUSTOM_SY_CODE_TAB_SPACES]: value}
+ });
+ };
window.siyuan.menus.menu.append(new MenuItem({
id: "code",
@@ -2085,25 +2093,25 @@ export class Gutter {
id: "md29",
iconHTML: "",
ignore: protyle.disabled,
- label: `<div class="fn__flex"><span>${window.siyuan.languages.md29}</span><span class="fn__space fn__flex-1"></span>
-<select class="b3-select">${codeTabSpacesOptions}</select></div>`,
- bind(element) {
- const selectElement = element.querySelector("select") as HTMLSelectElement;
- selectElement.addEventListener("click", (event) => event.stopPropagation());
- selectElement.addEventListener("change", () => {
- const value = selectElement.value;
- if (value === "") {
- nodeElement.removeAttribute(Constants.CUSTOM_SY_CODE_TAB_SPACES);
- } else {
- nodeElement.setAttribute(Constants.CUSTOM_SY_CODE_TAB_SPACES, value);
- }
- fetchPost("/api/attr/setBlockAttrs", {
- id,
- attrs: {[Constants.CUSTOM_SY_CODE_TAB_SPACES]: value}
- });
- window.siyuan.menus.menu.remove();
- });
- }
+ label: window.siyuan.languages.md29,
+ type: "submenu",
+ submenu: [{
+ id: "default",
+ iconHTML: "",
+ label: `${window.siyuan.languages.default} (${window.siyuan.config.editor.codeTabSpaces})`,
+ checked: codeTabSpaces === null,
+ click() {
+ setCodeTabSpaces("");
+ }
+ }, ...CODE_TAB_SPACE_VALUES.map((value) => ({
+ id: `tabSpaces${value}`,
+ iconHTML: "",
+ label: value.toString(),
+ checked: codeTabSpaces === value.toString(),
+ click() {
+ setCodeTabSpaces(value.toString());
+ }
+ }))]
}, {
id: "md31",
iconHTML: "",