Skip to content

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

:bug: Apply field visibility rules in all database attribute panels https://github.com/siyuan-note/siyuan/issues/19993

Daniel5e5eefec17a1
diff --git a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy
index e555f780fc..8cab76b008 100644
--- a/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy
+++ b/app/guide/20210808180117-6v0mkxr/20200923234011-ieuun1p/20210808180303-xaduj2o/20240208172514-9dsv6na.sy
@@ -7,7 +7,7 @@
"id": "20240208172514-9dsv6na",
"title": "Database",
"type": "doc",
- "updated": "20260930175958"
+ "updated": "20260930212321"
},
"Children": [
{
@@ -59,12 +59,12 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20260930175958-sqtxjcl",
- "updated": "20260930175958"
+ "updated": "20260930212321"
},
"Children": [
{
"Type": "NodeText",
- "Data": "In the database attribute panel at the top of a document, click a field name and set Attribute panel field visibility to Always show, Hide when empty, or Always hide. Default follows the global setting for hiding empty fields. This rule applies to that field in the database's attribute panels and does not change its visibility in database views. Use the panel's edit button to temporarily reveal hidden fields, edit their values, or change their visibility rules."
+ "Data": "In a database attribute panel at the top of a document, on a database entry page, or in a block attribute dialog, click a field name to set Attribute panel field visibility to Always show, Hide when empty, or Always hide. Default preserves the panel's existing display settings; the panel at the top of a document follows the global setting for hiding empty fields. Rules apply to attribute panels of that database and do not change field visibility in its views. Click the panel's edit button to temporarily reveal hidden fields and edit their values or visibility rules."
}
]
},
diff --git a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy
index 94307e725a..eb1f5e3270 100644
--- a/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy
+++ b/app/guide/20210808180117-czj9bvb/20200812220555-lj3enxa/20210808180321-hbvl5c2/20240208113259-nykkvaq.sy
@@ -7,7 +7,7 @@
"id": "20240208113259-nykkvaq",
"title": "数据库",
"type": "doc",
- "updated": "20260930175958"
+ "updated": "20260930212321"
},
"Children": [
{
@@ -59,12 +59,12 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20260930175958-aw9rfh1",
- "updated": "20260930175958"
+ "updated": "20260930212321"
},
"Children": [
{
"Type": "NodeText",
- "Data": "在文档顶部的数据库属性面板中点击字段名,可以将“属性面板字段可见性”设置为“始终显示”“空白时隐藏”或“始终隐藏”。“默认”沿用全局隐藏空字段设置。规则对该数据库的属性面板生效,不影响数据库各视图的字段显隐。点击面板的编辑按钮可以临时展开隐藏字段,编辑字段值或修改显示规则。"
+ "Data": "在文档顶部、数据库条目页或块属性对话框的数据库属性面板中点击字段名,可以将“属性面板字段可见性”设置为“始终显示”“空白时隐藏”或“始终隐藏”。“默认”保留面板原有的显示设置,其中文档顶部面板沿用全局隐藏空字段设置。规则对该数据库的属性面板生效,不影响数据库各视图的字段显隐。点击面板的编辑按钮可以临时展开隐藏字段,编辑字段值或修改显示规则。"
}
]
},
diff --git a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy
index a9289b8e61..c5e609d4a9 100644
--- a/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy
+++ b/app/guide/20211226090932-5lcq56f/20211226115423-d5z1joq/20211226121203-rjjngpz/20240208171522-y7dxcno.sy
@@ -7,7 +7,7 @@
"id": "20240208171522-y7dxcno",
"title": "資料庫",
"type": "doc",
- "updated": "20260930175958"
+ "updated": "20260930212321"
},
"Children": [
{
@@ -59,12 +59,12 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20260930175958-m98nd8z",
- "updated": "20260930175958"
+ "updated": "20260930212321"
},
"Children": [
{
"Type": "NodeText",
- "Data": "在文件頂部的資料庫屬性面板中點擊欄位名稱,可以將「屬性面板欄位可見性」設定為「始終顯示」「空白時隱藏」或「始終隱藏」。「預設」沿用全域隱藏空欄位設定。規則對該資料庫的屬性面板生效,不影響資料庫各檢視的欄位顯隱。點擊面板的編輯按鈕可以暫時展開隱藏欄位,編輯欄位值或修改顯示規則。"
+ "Data": "在文件頂部、資料庫項目頁或區塊屬性對話方塊的資料庫屬性面板中點選欄位名稱,可以將「屬性面板欄位可見性」設為「始終顯示」「空白時隱藏」或「始終隱藏」。「預設」保留面板原有的顯示設定,其中文件頂部面板沿用全域隱藏空欄位設定。規則對該資料庫的屬性面板生效,不影響資料庫各檢視的欄位顯隱。點選面板的編輯按鈕可以暫時展開隱藏欄位,編輯欄位值或修改顯示規則。"
}
]
},
diff --git a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy
index 8d95b214cc..7df2cc6347 100644
--- a/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy
+++ b/app/guide/20240530133126-axarxgx/20240530101000-4qitucx/20240530101000-g3ugxml/20240530101000-3eaevtp.sy
@@ -8,7 +8,7 @@
"id": "20240530101000-3eaevtp",
"title": "データベース",
"type": "doc",
- "updated": "20260930175958"
+ "updated": "20260930212321"
},
"Children": [
{
@@ -60,12 +60,12 @@
"Type": "NodeParagraph",
"Properties": {
"id": "20260930175958-49g85qs",
- "updated": "20260930175958"
+ "updated": "20260930212321"
},
"Children": [
{
"Type": "NodeText",
- "Data": "ドキュメント上部のデータベース属性パネルでフィールド名をクリックすると、「属性パネルのフィールド表示」を「常に表示」「空の場合は非表示」「常に非表示」に設定できます。「デフォルト」は空のフィールドを非表示にする全体設定に従います。このルールは同じデータベースの属性パネルに適用され、各ビューのフィールド表示には影響しません。パネルの編集ボタンで非表示のフィールドを一時的に表示し、値の編集や表示ルールの変更ができます。"
+ "Data": "ドキュメント上部、データベース項目ページ、またはブロック属性ダイアログのデータベース属性パネルでフィールド名をクリックすると、「属性パネルのフィールド表示」を「常に表示」「空の場合は非表示」「常に非表示」に設定できます。「デフォルト」はパネルの従来の表示設定を維持し、ドキュメント上部のパネルでは空のフィールドを非表示にする全体設定に従います。ルールはそのデータベースの属性パネルに適用され、各ビューのフィールド表示には影響しません。パネルの編集ボタンをクリックすると、非表示のフィールドを一時的に展開して値や表示ルールを編集できます。"
}
]
},
diff --git a/app/src/assets/scss/business/_custom.scss b/app/src/assets/scss/business/_custom.scss
index 9bec23e99d..fabf736f5a 100644
--- a/app/src/assets/scss/business/_custom.scss
+++ b/app/src/assets/scss/business/_custom.scss
@@ -1,5 +1,10 @@
.custom-attr {
+ & > [data-av-id]:not([data-panel-show-all="true"]) .av__row[data-panel-visibility="hide"],
+ & > [data-av-id]:not([data-panel-show-all="true"]) .av__row[data-panel-visibility="hide-empty"][data-empty="true"] {
+ display: none;
+ }
+
&__avbacklinks {
border-bottom: 1px solid var(--b3-border-color);
padding: 4px 8px 8px 36px;
@@ -310,11 +315,6 @@
display: none;
}
- &:not(.protyle-db-attr--show-all) &__body > [data-av-id] .av__row[data-panel-visibility="hide"],
- &:not(.protyle-db-attr--show-all) &__body > [data-av-id] .av__row[data-panel-visibility="hide-empty"][data-empty="true"] {
- display: none;
- }
-
&__tabs {
.item {
background-color: transparent;
diff --git a/app/src/protyle/render/av/attributePanel.ts b/app/src/protyle/render/av/attributePanel.ts
index 066bdf5cb5..031ee1adc3 100644
--- a/app/src/protyle/render/av/attributePanel.ts
+++ b/app/src/protyle/render/av/attributePanel.ts
@@ -479,6 +479,9 @@ export class AVAttributePanel {
this.showEmptyFields = false;
}
this.element.classList.toggle("protyle-db-attr--show-all", this.showEmptyFields);
+ this.bodyElement.querySelectorAll<HTMLElement>(":scope > [data-av-id]").forEach(item => {
+ item.dataset.panelShowAll = String(this.showEmptyFields);
+ });
updateEmptyState(this.element, hideEmpty && !this.showEmptyFields);
const editElement = this.element.querySelector<HTMLElement>('[data-type="toggle-empty"]');
editElement?.classList.toggle("fn__none", (!hideEmpty && !hasHiddenFields) || this.collapsed);
diff --git a/app/src/protyle/render/av/attributePanelVisibility.test.ts b/app/src/protyle/render/av/attributePanelVisibility.test.ts
index f0745debe3..dbf860e4aa 100644
--- a/app/src/protyle/render/av/attributePanelVisibility.test.ts
+++ b/app/src/protyle/render/av/attributePanelVisibility.test.ts
@@ -8,7 +8,7 @@ import {promisify} from "node:util";
import {ScriptTarget, transpileModule} from "typescript";
import {compileString} from "sass";
-const browserCases = (source: string, css: string, columnSource: string) => {
+const browserCases = (source: string, css: string, columnSource: string, blockSource: string) => {
const check: typeof assert = require("node:assert/strict");
const config = {editor: {databaseAttrShow: true, databaseAttrHideEmpty: false, databaseAttrViewMode: 0, databaseAttrUseTabs: false}};
Object.assign(window, {siyuan: {config, languages: {
@@ -100,6 +100,63 @@ const browserCases = (source: string, css: string, columnSource: string) => {
check.equal(menuElement.querySelector('[data-type="attributePanelVisibility"]'), null);
menuElement.remove();
}
+ const tables = ["database-a", "database-b"].map(avID => ({
+ avID, avName: avID, blockIDs: ["document"],
+ keyValues: [
+ ["default", "", ""], ["always", "always", ""], ["empty", "hide-empty", ""],
+ ["hidden", "hide", "value"], ["filled", "hide-empty", "value"],
+ ].map(([id, visibility, content]) => ({
+ key: {id, name: id, type: "text", attributePanelVisibility: visibility},
+ values: [{type: "text", text: {content}, blockID: "item"}],
+ })),
+ }));
+ const renderAttributes = new Function("fetchPost", "createEmptyAVValue", "getColIconByType", "cellValueIsEmpty",
+ "genAVAttributeRowHTML", "preserveAVBindingRange", "renderAVRichTextElements", blockSource + "\nreturn renderAVAttribute;")(
+ (url: string, _data: unknown, callback: (response: {data: unknown}) => void) => {
+ callback({data: url === "/api/av/getAttributeViewKeys" ? tables : {total: 0}});
+ }, () => ({}), () => "iconText", (value: IAVCellValue) => !value.text.content,
+ (options: {keyID: string, attributePanelVisibility: string, empty: boolean}) =>
+ `<div class="av__row" data-col-id="${options.keyID}" data-panel-visibility="${options.attributePanelVisibility}" data-empty="${options.empty}"></div>`,
+ () => () => {}, () => {});
+ for (const className of ["custom-attr protyle-db-row__body", "custom-attr"]) {
+ const body = document.createElement("div");
+ body.className = className;
+ document.body.append(body);
+ const protyle = {disabled: false};
+ renderAttributes(body, "document", protyle);
+ const visible = (id: string, avID = "database-a") => getComputedStyle(body.querySelector(
+ `[data-av-id="${avID}"] .av__row[data-col-id="${id}"]`)).display !== "none";
+ check.equal(visible("default"), true, "existing dialog defaults remain visible");
+ check.equal(visible("always"), true);
+ check.equal(visible("empty"), false);
+ check.equal(visible("hidden"), false);
+ check.equal(visible("filled"), true);
+ const edit = () => body.querySelector<HTMLButtonElement>('[data-av-id="database-a"] [data-type="toggle-panel-visibility"]');
+ edit().querySelector("use").dispatchEvent(new MouseEvent("click", {bubbles: true}));
+ check.equal(visible("hidden"), true);
+ check.equal(visible("empty"), true);
+ check.equal(visible("hidden", "database-b"), false, "revealing one database keeps the others hidden");
+ renderAttributes(body, "document", protyle);
+ check.equal(edit().getAttribute("aria-pressed"), "true");
+ check.equal(visible("hidden"), true, "refresh preserves temporary reveal state");
+ protyle.disabled = true;
+ renderAttributes(body, "document", protyle);
+ edit().click();
+ check.equal(visible("hidden"), false, "readonly panels can restore visibility rules");
+ edit().click();
+ check.equal(visible("hidden"), true, "readonly panels can reveal hidden fields");
+ const previousRules = tables[0].keyValues.map(item => item.key.attributePanelVisibility);
+ tables[0].keyValues.forEach(item => item.key.attributePanelVisibility = "");
+ renderAttributes(body, "document", protyle);
+ check.equal(edit().classList.contains("fn__none"), true, "databases without hidden rules need no reveal button");
+ check.equal(edit().getAttribute("aria-pressed"), "false");
+ tables[0].keyValues.forEach((item, index) => item.key.attributePanelVisibility = previousRules[index]);
+ body.remove();
+ }
+ const topBody = document.createElement("div");
+ topBody.className = "custom-attr protyle-db-attr__body";
+ renderAttributes(topBody, "document", {disabled: false});
+ check.equal(topBody.querySelector('[data-type="toggle-panel-visibility"]'), null, "top panels reuse their existing edit button");
return "Attribute panel visibility cases passed";
};
@@ -114,9 +171,12 @@ test("attribute panel field rules preserve global defaults and reveal hidden fie
const columnSource = transpileModule(readFileSync(path.resolve(__dirname, "col.ts"), "utf8")
.replace(/^import [\s\S]*?;\r?\n/gm, "").replace(/^export /gm, ""),
{compilerOptions: {target: ScriptTarget.ES2021}}).outputText;
+ const blockSource = transpileModule(readFileSync(path.resolve(__dirname, "blockAttr.ts"), "utf8")
+ .replace(/^import [\s\S]*?;\r?\n/gm, "").replace(/^export /gm, ""),
+ {compilerOptions: {target: ScriptTarget.ES2021}}).outputText;
const temporary = mkdtempSync(path.join(tmpdir(), "siyuan-panel-visibility-test-"));
const script = path.join(temporary, "run.cjs");
- const code = "const __name = value => value; (" + browserCases.toString() + ")(" + JSON.stringify(source) + "," + JSON.stringify(css) + "," + JSON.stringify(columnSource) + ")";
+ const code = "const __name = value => value; (" + browserCases.toString() + ")(" + JSON.stringify(source) + "," + JSON.stringify(css) + "," + JSON.stringify(columnSource) + "," + JSON.stringify(blockSource) + ")";
writeFileSync(script, `const {app, BrowserWindow} = require("electron");
app.setPath("userData", ${JSON.stringify(path.join(temporary, "profile"))});
app.whenReady().then(async () => {
diff --git a/app/src/protyle/render/av/blockAttr.ts b/app/src/protyle/render/av/blockAttr.ts
index 67d0222f9b..7fc09c60f0 100644
--- a/app/src/protyle/render/av/blockAttr.ts
+++ b/app/src/protyle/render/av/blockAttr.ts
@@ -140,12 +140,15 @@ export const renderAVAttribute = (element: HTMLElement, id: string, protyle: IPr
// 条目 ID 仅用于读取行数据,编辑菜单和更新时间使用真实数据库载体块。
const blockID = row ? row.databaseBlockID : id;
const primaryValue = table.keyValues.find(item => item.key.type === "block")?.values?.[0] || table.keyValues[0]?.values?.[0];
+ const hasHiddenFields = table.keyValues.some(item => ["hide", "hide-empty"].includes(item.key.attributePanelVisibility));
+ const showAll = hasHiddenFields && element.querySelector<HTMLElement>(`[data-av-id="${table.avID}"]`)?.dataset.panelShowAll === "true";
let innerHTML = `<div class="custom-attr__avheader">
<div class="block__logo block__logo--icon popover__block" style="max-width:calc(100% - 40px)" data-id='${JSON.stringify(table.blockIDs)}'>
<svg class="block__logoicon"><use xlink:href="#iconDatabase"></use></svg>
<span class="fn__ellipsis">${table.avName || window.siyuan.languages.database}</span>
</div>
<div class="fn__flex-1"></div>
+ ${element.classList.contains("protyle-db-attr__body") ? "" : `<button type="button" data-type="toggle-panel-visibility" class="block__icon block__icon--show ariaLabel${hasHiddenFields ? "" : " fn__none"}" data-position="4west" aria-label="${window.siyuan.languages.edit}" aria-pressed="${showAll}"><svg><use xlink:href="#iconEdit"></use></svg></button>`}
<span data-type="remove" data-row-id="${primaryValue?.blockID || ""}" class="block__icon block__icon--warning block__icon--show b3-tooltips__w b3-tooltips" aria-label="${window.siyuan.languages.removeAV}"><svg><use xlink:href="#iconTrashcan"></use></svg></span>
</div>`;
table.keyValues?.forEach(item => {
@@ -173,7 +176,7 @@ export const renderAVAttribute = (element: HTMLElement, id: string, protyle: IPr
innerHTML += `<div class="fn__hr"></div>
<button data-type="addColumn" class="b3-button b3-button--cancel"><svg><use xlink:href="#iconAdd"></use></svg>${window.siyuan.languages.newCol}</button>
<div class="fn__hr--b"></div><div class="fn__hr--b"></div>`;
- const tableHTML = `<div data-av-id="${table.avID}" data-av-type="table" data-node-id="${blockID}" data-attribute-id="${id}" data-type="NodeAttributeView">${innerHTML}</div>`;
+ const tableHTML = `<div data-av-id="${table.avID}" data-panel-show-all="${showAll}" data-av-type="table" data-node-id="${blockID}" data-attribute-id="${id}" data-type="NodeAttributeView">${innerHTML}</div>`;
html += tableHTML;
if (element.innerHTML) {
@@ -181,6 +184,7 @@ export const renderAVAttribute = (element: HTMLElement, id: string, protyle: IPr
const blockElement = element.querySelector<HTMLElement>(`[data-attribute-id="${id}"][data-av-id="${table.avID}"]`);
if (blockElement) {
blockElement.dataset.nodeId = blockID;
+ blockElement.dataset.panelShowAll = String(showAll);
blockElement.innerHTML = innerHTML;
} else {
element.insertAdjacentHTML("beforeend", tableHTML);
@@ -360,6 +364,16 @@ export const renderAVAttribute = (element: HTMLElement, id: string, protyle: IPr
event.preventDefault();
});
element.addEventListener("click", (event) => {
+ const visibilityElement = (event.target as HTMLElement).closest<HTMLElement>('[data-type="toggle-panel-visibility"]');
+ if (visibilityElement) {
+ const databaseElement = visibilityElement.closest<HTMLElement>("[data-av-id]");
+ const showAll = databaseElement.dataset.panelShowAll !== "true";
+ databaseElement.dataset.panelShowAll = String(showAll);
+ visibilityElement.setAttribute("aria-pressed", String(showAll));
+ event.preventDefault();
+ event.stopPropagation();
+ return;
+ }
if (handleTemplateInteraction(protyle, event) || handleRichTextInteraction(protyle, event)) {
return;
}