An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:sparkles: Refine built-in entry presets and mobile sidebar defaults
Daniel7525a4d6bd7a
diff --git a/app/src/config/entryVisibility/catalog.test.ts b/app/src/config/entryVisibility/catalog.test.tsindex e7976f5c43..d8c0f7e70f 100644--- a/app/src/config/entryVisibility/catalog.test.ts+++ b/app/src/config/entryVisibility/catalog.test.ts@@ -244,16 +244,16 @@ test("top bar markup stays aligned with its configurable built-in catalog", () =assert.doesNotMatch(source, /id="drag"[^>]*data-topbar-entry/);});-test("daily note and flashcard top bar entries default to visible only in the Simple profile", () => {+test("desktop daily note and flashcard top bar entries are visible in both built-in profiles", () => {for (const key of ["barDailyNote", "barRiffCard"]) {const path = `${TOP_BAR_ROOT_PATH}.${key}`;const entry = getEntryCatalogNode(path)!;const defaultVisible = getEntryCatalogDefaultVisibility(path);- assert.equal(defaultVisible, false);+ assert.equal(defaultVisible, true);assert.equal(entry.simpleDefaultVisible, true);assert.equal(getEntryCatalogCustomDefaultVisibility(path), false);assert.equal(getBuiltinProfileEntryVisibility("full", entry.simple, defaultVisible,- entry.simpleDefaultVisible), false);+ entry.simpleDefaultVisible), true);assert.equal(getBuiltinProfileEntryVisibility("simple", entry.simple, defaultVisible,entry.simpleDefaultVisible), true);}@@ -380,7 +380,8 @@ test("toolbar catalog follows the default toolbar declaration", () => {const familyIndex = children.findIndex(item => item.key === "font-family");assert.deepEqual(children.slice(familyIndex, familyIndex + 3).map(item => item.key),["font-family", "font-size", "text"]);- assert.deepEqual(children.filter((item) => !item.simple).map(item => item.key), ["font-family", "font-size"]);+ assert.deepEqual(children.filter((item) => !item.simple).map(item => item.key),+ ["font-family", "font-size", "sup", "sub", "kbd", "inline-math"]);});test("inline text paste entries follow the menu order", () => {diff --git a/app/src/config/entryVisibility/catalog.ts b/app/src/config/entryVisibility/catalog.tsindex d95cd04fde..3ee781030f 100644--- a/app/src/config/entryVisibility/catalog.ts+++ b/app/src/config/entryVisibility/catalog.ts@@ -514,7 +514,8 @@ const toolbarBuiltinChildren = [return separator(item.key);}const fontControl = ["font-family", "font-size"].includes(item.key);- return node(item.key, lang(item.lang), !fontControl, undefined, undefined,+ const advancedFormat = ["sup", "sub", "kbd", "inline-math"].includes(item.key);+ return node(item.key, lang(item.lang), !fontControl && !advancedFormat, undefined, undefined,fontControl ? {defaultVisible: () => typeof window === "undefined" || !window.siyuan.mobile,customDefaultVisible: false,@@ -644,12 +645,12 @@ const statusBarCatalogSection: IEntryCatalogSection = {const topBarBuiltinChildren = [node("barSync", lang("syncNow")),node("barDailyNote", lang("dailyNote"), true, undefined, undefined, {- defaultVisible: () => false,+ defaultVisible: () => typeof window === "undefined" || !window.siyuan.mobile,simpleDefaultVisible: true,customDefaultVisible: false,}),node("barRiffCard", lang("riffCard"), true, undefined, undefined, {- defaultVisible: () => false,+ defaultVisible: () => typeof window === "undefined" || !window.siyuan.mobile,simpleDefaultVisible: true,customDefaultVisible: false,}),diff --git a/app/src/config/entryVisibility/runtime.test.ts b/app/src/config/entryVisibility/runtime.test.tsindex d0b45fcab0..2a5a2a9421 100644--- a/app/src/config/entryVisibility/runtime.test.ts+++ b/app/src/config/entryVisibility/runtime.test.ts@@ -6,6 +6,9 @@ import {runInNewContext} from "node:vm";import {ModuleKind, transpileModule} from "typescript";import {resolveEntryOrder} from "./order";import {resetEntryProfileOrder} from "./profile";+import * as catalog from "./catalog";+import * as profileVisibility from "./profile";+import {TOOLBAR_ENTRY_ROOT_PATH} from "../../protyle/toolbar/defaults";import {DOCK_ORDER_SCOPES,getDefaultDockEntryOrderSnapshot,@@ -13,6 +16,151 @@ import {mergeDockEntryOrderSnapshot,} from "./dockOrder";+test("daily note and flashcard presets preserve custom choices and mobile defaults", () => {+ const source = readFileSync(resolve(process.cwd(), "src/config/entryVisibility/runtime.ts"), "utf8");+ const compiled = transpileModule(source + "\nexports.writable = getWritableEntryProfile;", {+ compilerOptions: {module: ModuleKind.CommonJS},+ }).outputText;+ const descriptor = Object.getOwnPropertyDescriptor(globalThis, "window");+ const paths = ["topBar.barDailyNote", "topBar.barRiffCard"];+ try {+ for (const mobile of [false, true]) {+ const config: Config.IEntryVisibility = {version: 6, active: "full", profiles: []};+ const window = {siyuan: {+ config: {appearance: {entryVisibility: config}},+ languages: {entryCustomProfile: "Custom"},+ ...(mobile ? {mobile: {}} : {}),+ }};+ Object.defineProperty(globalThis, "window", {configurable: true, value: window});+ const runtime = {} as typeof import("./runtime") & {+ writable: (config: Config.IEntryVisibility) => Config.IEntryVisibilityProfile;+ };+ runInNewContext(compiled, {exports: runtime, window, require: () => ({+ ...catalog, ...profileVisibility, TOOLBAR_ENTRY_ROOT_PATH, genUUID: () => "custom",+ })});+ for (const template of ["full", "simple"] as const) {+ config.active = template;+ const expected = template === "simple" || !mobile;+ const snapshot = runtime.createEntryProfileSnapshot(template);+ for (const path of paths) {+ assert.equal(runtime.getConfiguredEntryVisibility(path), expected);+ assert.equal(snapshot[path], expected);+ }+ }+ config.active = "full";+ const custom = runtime.writable(config);+ for (const path of paths) {+ assert.equal(custom.entries[path], !mobile);+ for (const visible of [false, true]) {+ custom.entries[path] = visible;+ assert.equal(runtime.getConfiguredEntryVisibility(path), visible);+ }+ delete custom.entries[path];+ assert.equal(runtime.getConfiguredEntryVisibility(path), false);+ }+ // 使用完整模板重置时采用当前默认值,不改写其他已保存方案。+ custom.entries = runtime.createEntryProfileSnapshot("full");+ for (const path of paths) {+ assert.equal(runtime.getConfiguredEntryVisibility(path), !mobile);+ custom.entries[path] = false;+ }+ const saved = JSON.stringify(custom);+ config.active = "full";+ for (const path of paths) {+ assert.equal(runtime.getConfiguredEntryVisibility(path), !mobile);+ }+ config.active = custom.id;+ for (const path of paths) {+ assert.equal(runtime.getConfiguredEntryVisibility(path), false);+ }+ assert.equal(JSON.stringify(custom), saved);+ }+ } finally {+ if (descriptor) {+ Object.defineProperty(globalThis, "window", descriptor);+ } else {+ Reflect.deleteProperty(globalThis, "window");+ }+ }+});++test("simple formatting defaults preserve color and full and custom toolbar choices", () => {+ const source = readFileSync(resolve(process.cwd(), "src/config/entryVisibility/runtime.ts"), "utf8");+ const compiled = transpileModule(source + "\nexports.writable = getWritableEntryProfile;", {+ compilerOptions: {module: ModuleKind.CommonJS},+ }).outputText;+ const descriptor = Object.getOwnPropertyDescriptor(globalThis, "window");+ const advanced = ["sup", "sub", "kbd", "inline-math"];+ try {+ for (const mobile of [false, true]) {+ const root = mobile ? "editor.toolbar.mobile-selection" : "editor.toolbar";+ const config: Config.IEntryVisibility = {version: 6, active: "simple", profiles: []};+ const window = {siyuan: {+ config: {appearance: {entryVisibility: config}},+ languages: {entryCustomProfile: "Custom"},+ ...(mobile ? {mobile: {}} : {}),+ }};+ Object.defineProperty(globalThis, "window", {configurable: true, value: window});+ const runtime = {} as typeof import("./runtime") & {+ writable: (config: Config.IEntryVisibility) => Config.IEntryVisibilityProfile;+ };+ runInNewContext(compiled, {exports: runtime, window, require: () => ({+ ...catalog, ...profileVisibility, TOOLBAR_ENTRY_ROOT_PATH, genUUID: () => "custom",+ })});+ for (const template of ["simple", "full"] as const) {+ config.active = template;+ const snapshot = runtime.createEntryProfileSnapshot(template);+ for (const key of advanced) {+ assert.equal(runtime.isEntryVisible(`${root}.${key}`), template === "full");+ assert.equal(snapshot[`${root}.${key}`], template === "full");+ }+ for (const key of ["text", "strong", "em", "u", "s", "mark", "code", "tag", "inline-memo"]) {+ assert.equal(runtime.isEntryVisible(`${root}.${key}`), true, `${template}: ${key}`);+ assert.equal(snapshot[`${root}.${key}`], true);+ }+ }+ config.active = "simple";+ const custom = runtime.writable(config);+ for (const key of [...advanced, "text"]) {+ const path = `${root}.${key}`;+ assert.equal(custom.entries[path], key === "text");+ for (const visible of [false, true]) {+ custom.entries[path] = visible;+ assert.equal(runtime.isEntryVisible(path), visible);+ }+ delete custom.entries[path];+ if (mobile) {+ const legacy = `editor.toolbar.${key}`;+ for (const visible of [false, true]) {+ custom.entries[legacy] = visible;+ assert.equal(runtime.isEntryVisible(path), visible);+ }+ delete custom.entries[legacy];+ }+ assert.equal(runtime.isEntryVisible(path), true);+ custom.entries[path] = false;+ }+ const saved = JSON.stringify(custom);+ for (const template of ["simple", "full"] as const) {+ config.active = template;+ assert.equal(runtime.isEntryVisible(`${root}.text`), true);+ }+ config.active = custom.id;+ assert.equal(JSON.stringify(custom), saved);+ for (const key of [...advanced, "text"]) {+ assert.equal(runtime.isEntryVisible(`${root}.${key}`), false);+ assert.equal(catalog.getEntryCatalogNode(`editor.toolbar.mobile-input.${key}`), undefined);+ }+ }+ } finally {+ if (descriptor) {+ Object.defineProperty(globalThis, "window", descriptor);+ } else {+ Reflect.deleteProperty(globalThis, "window");+ }+ }+});+test("visibility edits on a built-in profile leave untouched orders following catalog updates", () => {const source = readFileSync(resolve(process.cwd(), "src/config/entryVisibility/runtime.ts"), "utf8");const compiled = transpileModule(source + "\nexports.writable = getWritableEntryProfile;", {diff --git a/app/src/mobile/util/mobileBarsConfig.test.ts b/app/src/mobile/util/mobileBarsConfig.test.tsindex 0b9b25fab5..e197ba3084 100644--- a/app/src/mobile/util/mobileBarsConfig.test.ts+++ b/app/src/mobile/util/mobileBarsConfig.test.ts@@ -1,9 +1,12 @@import {test} from "node:test";import * as assert from "node:assert/strict";-import {resolveMobileSidebarConfig} from "./mobileBarsConfig";+import {createDefaultMobileBarsConfig, resolveMobileSidebarConfig} from "./mobileBarsConfig";test("sidebar access defaults and stored configurations always retain an entry", () => {- assert.deepEqual(resolveMobileSidebarConfig(), {sidebarSwipe: true, sidebarButtons: false});+ assert.deepEqual(resolveMobileSidebarConfig(), {sidebarSwipe: true, sidebarButtons: true});+ assert.deepEqual(resolveMobileSidebarConfig(createDefaultMobileBarsConfig()), {sidebarSwipe: true, sidebarButtons: true});+ assert.deepEqual(resolveMobileSidebarConfig({}), {sidebarSwipe: true, sidebarButtons: false});+ assert.deepEqual(resolveMobileSidebarConfig(null), {sidebarSwipe: true, sidebarButtons: false});assert.deepEqual(resolveMobileSidebarConfig({sidebarButtons: true}), {sidebarSwipe: true, sidebarButtons: true});assert.deepEqual(resolveMobileSidebarConfig({sidebarSwipe: false}), {sidebarSwipe: false, sidebarButtons: true});assert.deepEqual(resolveMobileSidebarConfig({sidebarButtons: false}), {sidebarSwipe: true, sidebarButtons: false});diff --git a/app/src/mobile/util/mobileBarsConfig.ts b/app/src/mobile/util/mobileBarsConfig.tsindex 30f8c0b184..ef2f79fa3e 100644--- a/app/src/mobile/util/mobileBarsConfig.ts+++ b/app/src/mobile/util/mobileBarsConfig.ts@@ -3,9 +3,16 @@ export const MOBILE_BARS_CONFIG_KEY = "local-mobile-bars";export const isMobileBarsAutoHide = () =>window.siyuan.storage[MOBILE_BARS_CONFIG_KEY]?.autoHide !== false;-export const resolveMobileSidebarConfig = (config: {sidebarSwipe?: boolean, sidebarButtons?: boolean} = {}) => ({- sidebarSwipe: config.sidebarSwipe !== false,- sidebarButtons: config.sidebarButtons === true || config.sidebarSwipe === false,+export const createDefaultMobileBarsConfig = () => ({+ autoHide: true,+ sidebarSwipe: true,+ sidebarButtons: true,+});++export const resolveMobileSidebarConfig = (config: {sidebarSwipe?: boolean, sidebarButtons?: boolean} | null = createDefaultMobileBarsConfig()) => ({+ sidebarSwipe: config?.sidebarSwipe !== false,+ // 保留已存储的侧栏访问方式,并确保至少有一个入口。+ sidebarButtons: config?.sidebarButtons === true || config?.sidebarSwipe === false,});export const getMobileSidebarConfig = () =>diff --git a/app/src/protyle/util/compatibility.ts b/app/src/protyle/util/compatibility.tsindex 75481f3d3f..46f000a99b 100644--- a/app/src/protyle/util/compatibility.ts+++ b/app/src/protyle/util/compatibility.ts@@ -17,6 +17,7 @@ import {prepareExternalClipboardHTML} from "./richClipboard";import {isIOSPlatform, isIPadOSPlatform} from "./browserCompatibility";import {canOpenExternalURL, getHostCapabilities} from "../../util/hostCapabilities";import {createDefaultMobileBottomBarConfig} from "../../mobile/util/mobileBottomBarConfig";+import {createDefaultMobileBarsConfig, MOBILE_BARS_CONFIG_KEY} from "../../mobile/util/mobileBarsConfig";export {encodeBase64, getTextSiyuanFromTextHTML} from "./clipboardData";@@ -786,7 +787,9 @@ export const getLocalStorage = (cb: () => void) => {version: 1,tabs: [],};- defaultStorage["local-mobile-bars"] = {autoHide: true};+ // 仅未保存过移动端偏好时启用默认侧栏按钮。+ defaultStorage[MOBILE_BARS_CONFIG_KEY] = typeof response.data[MOBILE_BARS_CONFIG_KEY] === "undefined" ?+ createDefaultMobileBarsConfig() : {autoHide: true};defaultStorage[Constants.LOCAL_MOBILE_BOTTOM_BAR] = createDefaultMobileBottomBarConfig();defaultStorage[Constants.LOCAL_MOBILE_SIDE_PANEL] = {version: 1,@@ -827,7 +830,7 @@ export const getLocalStorage = (cb: () => void) => {[Constants.LOCAL_EXPORTIMG, Constants.LOCAL_EXPORTPATH, Constants.LOCAL_SEARCHKEYS, Constants.LOCAL_PDFTHEME, Constants.LOCAL_BAZAAR,Constants.LOCAL_EXPORTWORD, Constants.LOCAL_EXPORTPDF, Constants.LOCAL_DOCINFO, Constants.LOCAL_MOBILE_TABS,- Constants.LOCAL_MOBILE_BOTTOM_BAR, Constants.LOCAL_MOBILE_SIDE_PANEL, "local-mobile-bars",+ Constants.LOCAL_MOBILE_BOTTOM_BAR, Constants.LOCAL_MOBILE_SIDE_PANEL, MOBILE_BARS_CONFIG_KEY,Constants.LOCAL_FONTSTYLES,Constants.LOCAL_SEARCHDATA, Constants.LOCAL_ZOOM, Constants.LOCAL_LAYOUTS,Constants.LOCAL_PLUGINTOPUNPIN, Constants.LOCAL_SEARCHASSET, Constants.LOCAL_FLASHCARD,diff --git a/app/src/protyle/util/storageStartup.test.ts b/app/src/protyle/util/storageStartup.test.tsindex 115dad71b8..a3dd64a0db 100644--- a/app/src/protyle/util/storageStartup.test.ts+++ b/app/src/protyle/util/storageStartup.test.ts@@ -6,6 +6,7 @@ import {runInNewContext} from "node:vm";import {ModuleKind, transpileModule} from "typescript";import type {Constants as AppConstants} from "../../constants";import {createDefaultMobileBottomBarConfig, normalizeMobileBottomBarConfig} from "../../mobile/util/mobileBottomBarConfig";+import {createDefaultMobileBarsConfig, MOBILE_BARS_CONFIG_KEY, resolveMobileSidebarConfig} from "../../mobile/util/mobileBarsConfig";const constantsExports: {Constants?: typeof AppConstants} = {};runInNewContext(transpileModule(readFileSync(resolve("src/constants.ts"), "utf8"), {@@ -34,6 +35,8 @@ const loadStorage = (values: Record<string, unknown>) => {getDefaultType: () => ({}),getDefaultSubType: () => ({}),createDefaultMobileBottomBarConfig,+ createDefaultMobileBarsConfig,+ MOBILE_BARS_CONFIG_KEY,normalizeSearchTypes: (types: unknown) => types,sanitizeClosedTabs: (tabs: unknown[]) => tabs,setStorageVal: () => assert.fail("Loading storage must not overwrite persisted data"),@@ -53,6 +56,36 @@ const loadZoom = (value: unknown) => {return zoom;};+test("fresh or reset mobile preferences enable sidebar buttons alongside swipe", () => {+ const first = loadStorage({})[MOBILE_BARS_CONFIG_KEY];+ const second = loadStorage({})[MOBILE_BARS_CONFIG_KEY];+ assert.deepEqual(first, createDefaultMobileBarsConfig());+ assert.deepEqual(resolveMobileSidebarConfig(first as ReturnType<typeof createDefaultMobileBarsConfig>), {sidebarSwipe: true, sidebarButtons: true});+ assert.notEqual(first, second);+});++test("startup preserves legacy and explicit sidebar choices in object and serialized storage", () => {+ for (const persisted of [+ {autoHide: false},+ {autoHide: false, sidebarSwipe: true, sidebarButtons: false},+ {autoHide: false, sidebarSwipe: true, sidebarButtons: true},+ {autoHide: false, sidebarSwipe: false, sidebarButtons: true},+ ]) {+ for (const value of [persisted, JSON.stringify(persisted)]) {+ const loaded = loadStorage({[MOBILE_BARS_CONFIG_KEY]: value})[MOBILE_BARS_CONFIG_KEY];+ assert.equal(JSON.stringify(loaded), JSON.stringify(persisted));+ assert.deepEqual(resolveMobileSidebarConfig(loaded as typeof persisted), resolveMobileSidebarConfig(persisted));+ }+ }+});++test("malformed legacy mobile preferences retain safe swipe access", () => {+ for (const value of [null, "null", "invalid", "false", "{}", "[]"]) {+ const loaded = loadStorage({[MOBILE_BARS_CONFIG_KEY]: value})[MOBILE_BARS_CONFIG_KEY];+ assert.deepEqual(resolveMobileSidebarConfig(loaded as Parameters<typeof resolveMobileSidebarConfig>[0]), {sidebarSwipe: true, sidebarButtons: false});+ }+});+test("startup storage uses the current mobile bottom bar default without sharing mutable slots", () => {const first = loadStorage({})[Constants.LOCAL_MOBILE_BOTTOM_BAR];const second = loadStorage({})[Constants.LOCAL_MOBILE_BOTTOM_BAR];