Skip to content

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

:recycle: Use a data attribute for compact UI density https://github.com/siyuan-note/siyuan/issues/20503

Danielb1b2ed8d8985
diff --git a/AGENTS.md b/AGENTS.md
index 7808817562..558ec6ec7d 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -158,7 +158,9 @@ Unambiguous mechanical edits with no behavioral or compatibility impact need no
- When reviewing control-style changes, check consistency with shared controls, theme overrides through shared classes, light/dark themes, narrow layouts, and large editor fonts. Preserve necessary layout and touch-target constraints; do not remove all feature styles indiscriminately
7. **CSS positioning and scrolling:** When changing `position`, `transform`, `contain`, or `overflow` on a shared container, check the effects on descendant positioning reference frames, overlay coverage, and clipping. Prefer a dedicated container when a local control needs a positioning reference. For settings dialog changes, verify detail overlays, the top drag area, and scrollbar placement at different window widths
-8. **Built-in custom attributes:** Use the `custom-sy-` prefix for custom attributes owned by built-in features
+8. **Custom attributes:**
+ - HTML custom attributes used only for DOM/UI state must use the `data-` prefix without an extra `sy` namespace, for example `data-ui-density`
+ - Custom attributes owned by built-in features and persisted in notebook, document, or block data must use the `custom-sy-` prefix. Preserve existing persisted attribute names and their compatibility; DOM/UI naming changes must not rename these data attributes
- Define custom attribute name constants in `app/src/constants.ts`, alongside similar constants in `Constants`, rather than in individual feature modules
9. **Frontend preference storage:** Do not use browser `localStorage` directly, including `window.localStorage` and `globalThis.localStorage`
diff --git a/app/src/assets/scss/util/_density.scss b/app/src/assets/scss/util/_density.scss
index bd92830726..f7a94a3bb5 100644
--- a/app/src/assets/scss/util/_density.scss
+++ b/app/src/assets/scss/util/_density.scss
@@ -31,7 +31,7 @@ $compact-sizes: (
"config-nav-margin": 6px
);
-:where(:root[custom-sy-ui-density="compact"]) {
+:where(:root[data-ui-density="compact"]) {
@each $name, $value in $compact-sizes {
--b3-ui-#{$name}: #{$value};
}
diff --git a/app/src/config/tabs/compactMode.test.ts b/app/src/config/tabs/compactMode.test.ts
index abf103b1c3..1d87da53ac 100644
--- a/app/src/config/tabs/compactMode.test.ts
+++ b/app/src/config/tabs/compactMode.test.ts
@@ -30,7 +30,7 @@ const fixture = (frontend = "desktop", mobileApp = false) => {
querySelectorAll: () => [input],
},
require: () => ({
- Constants: {LOCAL_COMPACT_MODE: "compact", CUSTOM_SY_UI_DENSITY: "density"},
+ Constants: {LOCAL_COMPACT_MODE: "compact", ATTRIBUTE_UI_DENSITY: "data-ui-density"},
getFrontend: () => frontend,
isInMobileApp: () => mobileApp,
setStorageVal: (...args: unknown[]) => saved.push(args),
@@ -45,7 +45,7 @@ test("compact mode defaults to off and only accepts an explicit boolean preferen
for (const value of [undefined, null, false, "true", 1]) {
f.storage.compact = value;
f.api.applyCompactMode();
- assert.equal(f.attributes.has("density"), false);
+ assert.equal(f.attributes.has("data-ui-density"), false);
assert.equal(f.input.checked, false);
}
assert.deepEqual(f.events, []);
@@ -56,13 +56,13 @@ test("compact mode persists through the workspace preference store and updates o
f.api.setCompactModeEnabled(true);
assert.deepEqual(f.saved, [["compact", true]]);
assert.equal(f.storage.compact, true);
- assert.equal(f.attributes.get("density"), "compact");
+ assert.equal(f.attributes.get("data-ui-density"), "compact");
assert.equal(f.input.checked, true);
f.api.applyCompactMode();
assert.deepEqual(f.events, ["resize"]);
delete f.storage.compact;
f.api.onCompactModeStorageChanged("compact");
- assert.equal(f.attributes.has("density"), false);
+ assert.equal(f.attributes.has("data-ui-density"), false);
assert.equal(f.input.checked, false);
assert.deepEqual(f.events, ["resize", "resize"]);
f.api.onCompactModeStorageChanged("unrelated");
@@ -76,7 +76,7 @@ test("mobile frontends and mobile apps retain their density and do not persist d
f.api.applyCompactMode();
f.api.setCompactModeEnabled(false);
await f.api.refreshCompactMode();
- assert.equal(f.attributes.has("density"), false);
+ assert.equal(f.attributes.has("data-ui-density"), false);
assert.equal(f.storage.compact, true);
assert.equal(f.saved.length, 0);
assert.equal(f.requests.length, 0);
@@ -102,11 +102,11 @@ test("reconnection restores changes made while offline and removes a missing pre
let refresh = f.api.refreshCompactMode();
f.requests.shift()({code: 0, data: {compact: true}});
await refresh;
- assert.equal(f.attributes.get("density"), "compact");
+ assert.equal(f.attributes.get("data-ui-density"), "compact");
refresh = f.api.refreshCompactMode();
f.requests.shift()({code: 0, data: {}});
await refresh;
- assert.equal(f.attributes.has("density"), false);
+ assert.equal(f.attributes.has("data-ui-density"), false);
assert.equal(f.saved.length, 0);
});
diff --git a/app/src/config/tabs/compactMode.ts b/app/src/config/tabs/compactMode.ts
index b9d0fdb568..8a52446876 100644
--- a/app/src/config/tabs/compactMode.ts
+++ b/app/src/config/tabs/compactMode.ts
@@ -14,11 +14,11 @@ export const isCompactModeEnabled = () =>
export const applyCompactMode = () => {
const root = document.documentElement;
const enabled = isCompactModeEnabled();
- const changed = root.hasAttribute(Constants.CUSTOM_SY_UI_DENSITY) !== enabled;
+ const changed = root.hasAttribute(Constants.ATTRIBUTE_UI_DENSITY) !== enabled;
if (enabled) {
- root.setAttribute(Constants.CUSTOM_SY_UI_DENSITY, "compact");
+ root.setAttribute(Constants.ATTRIBUTE_UI_DENSITY, "compact");
} else {
- root.removeAttribute(Constants.CUSTOM_SY_UI_DENSITY);
+ root.removeAttribute(Constants.ATTRIBUTE_UI_DENSITY);
}
document.querySelectorAll<HTMLInputElement>('input[id="compactMode"]').forEach(input => {
input.checked = enabled;
diff --git a/app/src/constants.ts b/app/src/constants.ts
index f5689533e7..6df45b17f0 100644
--- a/app/src/constants.ts
+++ b/app/src/constants.ts
@@ -96,14 +96,13 @@ export abstract class Constants {
public static readonly CUSTOM_SY_TABLE_HEADER_COLUMN: string = "custom-sy-table-header-column";
public static readonly CUSTOM_SY_AV_STATIC_TEXT: string = "custom-sy-av-s-text-";
- // 界面密度标记仅用于根元素,不写入笔记内容。
- public static readonly CUSTOM_SY_UI_DENSITY = "custom-sy-ui-density";
-
// 临时标记 DOM 属性以辅助完成其功能
public static readonly ATTRIBUTE_EDITING = "data-editing";
public static readonly ATTRIBUTE_V_SCROLL = "data-v-scroll";
public static readonly ATTRIBUTE_DOCK_WIDTH = "data-dock-width";
public static readonly ATTRIBUTE_MENU_KEYMAP = "data-menu-keymap";
+ // 界面密度标记仅用于根元素,不写入笔记内容。
+ public static readonly ATTRIBUTE_UI_DENSITY = "data-ui-density";
// size
public static readonly SIZE_DATABASE_MAZ_SIZE: number = 102400;
diff --git a/app/tests/compactModeLayout.test.js b/app/tests/compactModeLayout.test.js
index d5d0192e02..ec1a9a719b 100644
--- a/app/tests/compactModeLayout.test.js
+++ b/app/tests/compactModeLayout.test.js
@@ -62,7 +62,7 @@ const browserCases = ({css, desktop, community}) => {
near(normal[key].height, expected, `${key} retains the default size`);
}
}
- document.documentElement.setAttribute("custom-sy-ui-density", "compact");
+ document.documentElement.setAttribute("data-ui-density", "compact");
settle();
const compact = snapshot();
if (!desktop) {
@@ -116,7 +116,7 @@ const browserCases = ({css, desktop, community}) => {
find("#navigation").style.removeProperty("font-size");
find(".b3-menu").style.removeProperty("font-size");
}
- document.documentElement.removeAttribute("custom-sy-ui-density");
+ document.documentElement.removeAttribute("data-ui-density");
settle();
check.deepEqual(snapshot(), normal, "disabling restores the theme's original component dimensions");
if (!community) {
@@ -124,7 +124,7 @@ const browserCases = ({css, desktop, community}) => {
const theme = document.createElement("style");
theme.textContent = ":root{--b3-ui-list-height:36px}.b3-menu__item{line-height:38px}";
document.head.prepend(theme);
- document.documentElement.setAttribute("custom-sy-ui-density", "compact");
+ document.documentElement.setAttribute("data-ui-density", "compact");
settle();
near(height("#navigation .b3-list-item"), 36, "theme root variable wins");
near(height(".b3-menu__item"), 26, "same-specificity early component style follows source order");