Skip to content

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

:art: Use standard rows for dependent parent document click settings https://github.com/siyuan-note/siyuan/issues/20169

Daniel393ed5d78316
diff --git a/app/src/assets/scss/business/_config.scss b/app/src/assets/scss/business/_config.scss
index 3140e58f73..cbb0fe5fda 100644
--- a/app/src/assets/scss/business/_config.scss
+++ b/app/src/assets/scss/business/_config.scss
@@ -1,7 +1,3 @@
-.config-filetree-click__child {
- margin-inline-start: 24px;
-}
-
.config-chatgpt__actions,
.config-chatgpt__account {
gap: 8px;
diff --git a/app/src/config/render/fragments.ts b/app/src/config/render/fragments.ts
index adaaa4b3d1..b0ae4a3116 100644
--- a/app/src/config/render/fragments.ts
+++ b/app/src/config/render/fragments.ts
@@ -9,15 +9,14 @@ export const genConfigItemMainHtml = (title: string, desc?: string): string =>
${desc ? `<div class="b3-label__text">${desc}</div>` : ""}
</div>`;
-const genSwitchInputHtml = (id: string, checked: boolean, disabled = false): string =>
- `<input class="b3-switch fn__flex-center" id="${id}" type="checkbox"${checked ? " checked" : ""}${disabled ? " disabled" : ""}/>`;
+const genSwitchInputHtml = (id: string, checked: boolean): string =>
+ `<input class="b3-switch fn__flex-center" id="${id}" type="checkbox"${checked ? " checked" : ""}/>`;
-export const genSwitchRow = (id: string, title: string, desc: string | undefined, checked: boolean,
- options: {disabled?: boolean, inner?: boolean} = {}): string =>
- `<label class="fn__flex b3-label ${options.inner ? "b3-label--inner" : "config-item"}">
+export const genSwitchRow = (id: string, title: string, desc: string | undefined, checked: boolean): string =>
+ `<label class="fn__flex b3-label config-item">
${genConfigItemMainHtml(title, desc)}
<span class="fn__space"></span>
- ${genSwitchInputHtml(id, checked, options.disabled)}
+ ${genSwitchInputHtml(id, checked)}
</label>`;
/** 列表项开关(`b3-list-item` 布局) */
diff --git a/app/src/config/setting/builder.ts b/app/src/config/setting/builder.ts
index 72a86b69bb..86b7c2b94f 100644
--- a/app/src/config/setting/builder.ts
+++ b/app/src/config/setting/builder.ts
@@ -46,6 +46,8 @@ interface PanelSettingTabOptions<TId extends string = string> extends SettingTab
type ControlSpecBase = {
title: string;
desc?: string;
+ /** 补充检索词,用于同时展示有依赖关系的设置项 */
+ keywords?: string[];
save?: SaveFn;
afterMount?: (root: HTMLElement) => void | Promise<void>;
/** 搜索时动态判断条目是否可用 */
@@ -255,6 +257,7 @@ class SettingGroupBuilder<TId extends string> {
kind: "full",
rowParts,
searchTitle: spec.title,
+ searchTexts: spec.keywords ? () => spec.keywords : undefined,
searchAvailability: spec.searchAvailability,
readValue: (el) => control.readValue(el),
save: spec.save ?? this.tab.defaultSave?.bind(null, id),
diff --git a/app/src/config/setting/item.ts b/app/src/config/setting/item.ts
index 48ba4bf1dc..0dbb9f6536 100644
--- a/app/src/config/setting/item.ts
+++ b/app/src/config/setting/item.ts
@@ -28,6 +28,7 @@ export type SettingItemSearchAvailability = {
type FullSettingItem = SettingItemBase & {
kind: "full";
rowParts: RowPart[];
+ searchTexts?: () => string[];
};
/** 自定义 HTML 块:参与 mount、搜索 */
diff --git a/app/src/config/tabs/fileTab.test.ts b/app/src/config/tabs/fileTab.test.ts
index b20189afcf..06a87617a8 100644
--- a/app/src/config/tabs/fileTab.test.ts
+++ b/app/src/config/tabs/fileTab.test.ts
@@ -5,65 +5,117 @@ import {join} from "node:path";
import {runInNewContext} from "node:vm";
import * as ts from "typescript";
import {controlBoolean} from "../setting/control";
-import {genSwitchRow} from "../render/fragments";
+import * as fragments from "../render/fragments";
+import {isSettingControl} from "../render/parts";
+import {buildItemSearchIndex} from "../search/normalize";
+import type {RegisterSettingItem} from "../setting/item";
+import type {SettingTabBuilder, SettingTabSearchResult} from "../setting/builder";
+
+const compile = (file: string, extra = "") => ts.transpileModule(
+ readFileSync(join(__dirname, file), "utf8") + extra,
+ {compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020}},
+).outputText;
const loadBehaviorSettings = (parentEnabled: boolean, childEnabled?: boolean) => {
const fileTree = {parentDocClickExpand: parentEnabled, parentDocDoubleClickOpen: childEnabled};
const languages = {parentDocClickExpand: "Expand parent title", parentDocClickExpandTip: "Toggle child documents",
parentDocDoubleClickOpen: "Double-click to open", parentDocDoubleClickOpenTip: "Wait about 300 ms"};
const saved: unknown[][] = [];
- let item: {keywords: string[], html(): string, afterMount(root: unknown): void,
- controls: {control: ReturnType<typeof controlBoolean>, save?: (value: boolean) => void}[]};
- const source = readFileSync(join(__dirname, "fileTab.ts"), "utf8") + "\nexport {registerFileTreeBehaviorGroup};";
- const code = ts.transpileModule(source, {
- compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020},
- }).outputText;
- const exports: {registerFileTreeBehaviorGroup?: (tab: unknown) => void} = {};
- runInNewContext(code, {
- exports, CSS: {escape: (text: string) => text}, window: {siyuan: {config: {fileTree}, languages}},
+ const registered: RegisterSettingItem[] = [];
+ const windowContext = {siyuan: {config: {fileTree}, languages}};
+ const builderExports: {SettingTabBuilder?: typeof SettingTabBuilder} = {};
+ runInNewContext(compile("../setting/builder.ts"), {
+ exports: builderExports, window: windowContext,
+ require: (name: string) => {
+ if (name === "./control") return {controlBoolean: (id: string, options?: {readConfig?: () => boolean}) =>
+ controlBoolean(id, {readConfig: options?.readConfig ??
+ (() => Boolean(fileTree[id.split(".")[1] as keyof typeof fileTree]))})};
+ if (name === "./group") return {registerSettingGroup() {}};
+ if (name === "./item") return {registerSettingItem: (item: RegisterSettingItem) => registered.push(item)};
+ return {};
+ },
+ });
+ const exports: {registerFileTreeBehaviorGroup?: (tab: SettingTabBuilder) => void} = {};
+ runInNewContext(compile("fileTab.ts", "\nexport {registerFileTreeBehaviorGroup};"), {
+ exports, CSS: {escape: (text: string) => text}, window: windowContext,
+ require: (name: string) => name.endsWith("/fileRuntime")
+ ? {fileConfigApi: {patch: (...args: unknown[]) => saved.push(args)}} : {},
+ });
+ exports.registerFileTreeBehaviorGroup(new builderExports.SettingTabBuilder({
+ id: "file", icon: "iconFile", title: () => "Documents",
+ }));
+ const parent = registered.find(item => item.id === "fileTree.parentDocClickExpand");
+ const child = registered.find(item => item.id === "fileTree.parentDocDoubleClickOpen");
+ assert.ok(parent?.kind === "full" && child?.kind === "full");
+ const entries = () => [{group: {id: "behavior", title: "Behavior", searchTitle: "behavior"},
+ items: registered.filter(item => item.kind === "full").map(item => ({...item, searchIndex: buildItemSearchIndex(item)}))}];
+ const renderExports: {buildGroupedItemsView?: (id: string) => {html: string}} = {};
+ runInNewContext(compile("../render/render.ts"), {
+ exports: renderExports, window: windowContext,
require: (name: string) => {
- if (name.endsWith("/control")) return {controlBoolean};
- if (name.endsWith("/fragments")) return {genSwitchRow};
- if (name.endsWith("/fileRuntime")) return {fileConfigApi: {patch: (...args: unknown[]) => saved.push(args)}};
+ if (name.endsWith("/item")) return {getTabGroupEntries: entries};
+ if (name === "./parts") return {isSettingControl};
+ if (name === "./fragments") return fragments;
+ if (name.endsWith("/escape")) return {escapeAttr: (text: string) => text};
return {};
},
});
- exports.registerFileTreeBehaviorGroup({group: () => ({switch() {}, composite: (spec: typeof item) => { item = spec; }})});
- return {item, languages, saved};
+ const scanExports: {scanSettingTabSearch?: (id: string, title: string, query: string) => SettingTabSearchResult} = {};
+ runInNewContext(compile("../search/scan.ts"), {exports: scanExports,
+ require: () => ({getTabGroupEntries: entries})});
+ return {parent, child, saved, fileTree,
+ html: () => renderExports.buildGroupedItemsView("file").html,
+ search: (query: string) => scanExports.scanSettingTabSearch("file", "documents", query)};
};
-test("double-click opening is a searchable child control and missing preferences inherit enabled", () => {
- const {item, languages} = loadBehaviorSettings(false);
- assert.equal(item.controls[0].control.id, "fileTree.parentDocClickExpand");
- assert.equal(item.controls[1].control.id, "fileTree.parentDocDoubleClickOpen");
- assert.equal(item.controls[1].control.readConfig(), true);
- const html = item.html();
- assert.equal(html.match(/b3-label--inner/g)?.length, 2);
- assert.match(html, /config-filetree-click__child/);
- assert.match(html, /id="fileTree.parentDocDoubleClickOpen" type="checkbox" checked disabled/);
- assert.ok(item.keywords.includes(languages.parentDocDoubleClickOpen));
- assert.ok(item.keywords.includes(languages.parentDocDoubleClickOpenTip));
+test("parent and child use separate standard setting rows and inherit the enabled default", () => {
+ const h = loadBehaviorSettings(false);
+ const control = h.child.rowParts.find(isSettingControl);
+ assert.equal(control.id, "fileTree.parentDocDoubleClickOpen");
+ assert.equal(control.readConfig(), true);
+ const html = h.html();
+ assert.equal(html.includes("b3-label--inner"), false);
+ assert.match(html, /<label class="fn__flex b3-label config-item" data-config-item-id="fileTree.parentDocClickExpand">/);
+ assert.match(html, /<label class="fn__flex b3-label config-item" data-config-item-id="fileTree.parentDocDoubleClickOpen">/);
});
-test("parent toggling enables its child without clearing the child's saved choice", () => {
+test("searching either setting retains parent and child context", () => {
+ const h = loadBehaviorSettings(false);
+ for (const query of ["double-click", "expand parent title", "300 ms"]) {
+ const result = h.search(query);
+ assert.equal(result.matches, true);
+ assert.equal(result.visibleItemIds.has(h.parent.id), true);
+ assert.equal(result.visibleItemIds.has(h.child.id), true);
+ }
+});
+
+test("parent toggling hides its child without changing or saving the child's preference", async () => {
for (const checked of [false, true]) {
- const {item, saved} = loadBehaviorSettings(false, checked);
- const parent = {checked: false, addEventListener: (_name: string, listener: () => void) => { change = listener; }};
- const child = {checked, disabled: false};
+ const h = loadBehaviorSettings(false, checked);
let change: () => void;
- item.afterMount({querySelector: (selector: string) => selector.includes("parentDocClickExpand") ? parent : child});
- assert.equal(child.disabled, true);
+ const parent = {checked: false, addEventListener: (_name: string, listener: () => void) => { change = listener; }};
+ const classes = new Set<string>();
+ const childRow = {classList: {
+ add: (name: string) => classes.add(name),
+ toggle: (name: string, enabled: boolean) => {
+ if (enabled) { classes.add(name); } else { classes.delete(name); }
+ },
+ }};
+ const child = {closest: () => childRow};
+ const root = {querySelector: (selector: string) => selector.includes("parentDocClickExpand") ? parent : child};
+ await h.parent.afterMount(root as unknown as HTMLElement);
+ assert.equal(classes.has("fn__none"), true);
+ assert.equal(classes.has("config-filetree-click__child"), true);
parent.checked = true;
change();
- assert.equal(child.disabled, false);
- assert.equal(child.checked, checked);
+ assert.equal(classes.has("fn__none"), false);
parent.checked = false;
change();
- assert.equal(child.disabled, true);
- assert.equal(child.checked, checked);
- assert.equal(saved.length, 0);
- item.controls[0].save(true);
- assert.equal(saved[0][0], "parentDocClickExpand");
- assert.equal(saved[0][1], true);
+ assert.equal(classes.has("fn__none"), true);
+ assert.equal(h.fileTree.parentDocDoubleClickOpen, checked);
+ assert.equal(h.saved.length, 0);
+ h.parent.save(true);
+ assert.equal(h.saved[0][0], "parentDocClickExpand");
+ assert.equal(h.saved[0][1], true);
}
});
diff --git a/app/src/config/tabs/fileTab.ts b/app/src/config/tabs/fileTab.ts
index 62968ebb18..259d725fef 100644
--- a/app/src/config/tabs/fileTab.ts
+++ b/app/src/config/tabs/fileTab.ts
@@ -4,8 +4,8 @@ import {fetchPost} from "../../util/fetch";
import {editorConfigApi} from "./editorRuntime";
import {fileConfigApi} from "./fileRuntime";
import type {SettingTabBuilder} from "../setting/builder";
-import {controlBoolean, controlNumber, controlSelect, controlString} from "../setting/control";
-import {genConfigItemName, genSwitchRow} from "../render/fragments";
+import {controlNumber, controlSelect, controlString} from "../setting/control";
+import {genConfigItemName} from "../render/fragments";
import {genButtonHtml, genNumberInputHtml} from "../render/render";
import {refreshDefaultFileTreeIcons} from "../../emoji/fileTreeIcon";
/// #if !MOBILE
@@ -15,6 +15,18 @@ import {getAllModels} from "../../layout/getAll";
const isMobileKernelContainer = () =>
["android", "ios", "harmony"].includes(window.siyuan.config.system.container);
+const bindParentDocClickSettingsVisibility = (root: HTMLElement) => {
+ const parent = root.querySelector<HTMLInputElement>(`#${CSS.escape("fileTree.parentDocClickExpand")}`);
+ const childRow = root.querySelector(`#${CSS.escape("fileTree.parentDocDoubleClickOpen")}`)?.closest(".config-item");
+ if (!parent || !childRow) {
+ return;
+ }
+ childRow.classList.add("config-filetree-click__child");
+ const updateVisibility = () => childRow.classList.toggle("fn__none", !parent.checked);
+ parent.addEventListener("change", updateVisibility);
+ updateVisibility();
+};
+
const genNotebookSavePathHtml = (
title: string,
desc: string,
@@ -52,42 +64,24 @@ const registerFileTreeBehaviorGroup = (tab: SettingTabBuilder) => {
}),
});
/// #endif
- group.composite({
- key: "parentDocClick",
- keywords: [window.siyuan.languages.parentDocClickExpand, window.siyuan.languages.parentDocClickExpandTip,
- window.siyuan.languages.parentDocDoubleClickOpen, window.siyuan.languages.parentDocDoubleClickOpenTip],
- html: () => `<div class="b3-label config-item">
- ${genSwitchRow("fileTree.parentDocClickExpand", window.siyuan.languages.parentDocClickExpand,
- window.siyuan.languages.parentDocClickExpandTip, window.siyuan.config.fileTree.parentDocClickExpand, {inner: true})}
- <div class="config-filetree-click__child">
- ${genSwitchRow("fileTree.parentDocDoubleClickOpen", window.siyuan.languages.parentDocDoubleClickOpen,
- window.siyuan.languages.parentDocDoubleClickOpenTip,
- window.siyuan.config.fileTree.parentDocDoubleClickOpen !== false,
- {inner: true, disabled: !window.siyuan.config.fileTree.parentDocClickExpand})}
- </div>
-</div>`,
- afterMount: (root) => {
- const parent = root.querySelector<HTMLInputElement>(`#${CSS.escape("fileTree.parentDocClickExpand")}`);
- const child = root.querySelector<HTMLInputElement>(`#${CSS.escape("fileTree.parentDocDoubleClickOpen")}`);
- const updateDisabled = () => { child.disabled = !parent.checked; };
- updateDisabled();
- parent.addEventListener("change", updateDisabled);
- },
- controls: [
- {
- control: controlBoolean("fileTree.parentDocClickExpand"),
- save: (value) => fileConfigApi.patch("parentDocClickExpand", value, () => {
- /// #if !MOBILE
- getAllModels().files.forEach((files) => files.updateDocActions());
- /// #endif
- }),
- },
- {
- control: controlBoolean("fileTree.parentDocDoubleClickOpen", {
- readConfig: () => window.siyuan.config.fileTree.parentDocDoubleClickOpen !== false,
- }),
- },
- ],
+ const parentDocClickKeywords = [window.siyuan.languages.parentDocClickExpand, window.siyuan.languages.parentDocClickExpandTip,
+ window.siyuan.languages.parentDocDoubleClickOpen, window.siyuan.languages.parentDocDoubleClickOpenTip];
+ group.switch("fileTree.parentDocClickExpand", {
+ title: window.siyuan.languages.parentDocClickExpand,
+ desc: window.siyuan.languages.parentDocClickExpandTip,
+ keywords: parentDocClickKeywords,
+ afterMount: bindParentDocClickSettingsVisibility,
+ save: (value) => fileConfigApi.patch("parentDocClickExpand", value, () => {
+ /// #if !MOBILE
+ getAllModels().files.forEach((files) => files.updateDocActions());
+ /// #endif
+ }),
+ });
+ group.switch("fileTree.parentDocDoubleClickOpen", {
+ title: window.siyuan.languages.parentDocDoubleClickOpen,
+ desc: window.siyuan.languages.parentDocDoubleClickOpenTip,
+ keywords: parentDocClickKeywords,
+ readConfig: () => window.siyuan.config.fileTree.parentDocDoubleClickOpen !== false,
});
group.switch("fileTree.alwaysSelectOpenedFile", {
title: window.siyuan.languages.selectOpen,