An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作
TypeScript agentic-aiai-agentknowledge-baselocal-firstmarkdownmcpmindmapnote-takingself-hostedsiyuanworkspace
:bug: Preserve database carrier context when editing row details
Daniel0e10de11aec0
diff --git a/app/scripts/avAttributeCarrier.test.js b/app/scripts/avAttributeCarrier.test.jsnew file mode 100644index 0000000000..6f7df1890a--- /dev/null+++ b/app/scripts/avAttributeCarrier.test.js@@ -0,0 +1,165 @@+const assert = require("node:assert/strict");+const {readFileSync, mkdtempSync, rmSync} = require("node:fs");+const path = require("node:path");+const os = require("node:os");++const runCases = async () => {+ const assert = require("node:assert/strict");+ const requests = [];+ const transactions = [];+ let tables;+ Object.assign(window, {+ siyuan: {languages: {database: "Database", removeAV: "Remove", newCol: "New column"}},+ attributeViewRenderID: 0,+ attributeTableData: new WeakMap(),+ fetchPost: (url, data, callback) => {+ requests.push({url, data});+ if (url.endsWith("getAttributeViewKeys")) {+ callback({data: tables});+ } else if (url.endsWith("getAttributeViewBacklinks")) {+ callback({data: {total: 0, items: []}});+ }+ },+ renderAVRichTextElements: () => {},+ getColIconByType: () => "iconDatabase",+ genAVAttributeRowHTML: ({value}) => `<div data-row-id="${value.blockID}" data-col-id="${value.keyID}"></div>`,+ getPageSize: () => ({unGroupPageSize: 20, groupPageSize: {}}),+ getAVBatchEditMode: () => "replace",+ transformCellValue: (_type, value) => value,+ objEquals: (a, b) => JSON.stringify(a) === JSON.stringify(b),+ getCellValueText: () => "",+ dayjs: () => ({format: () => "20260909120000"}),+ transaction: (_protyle, doOperations, undoOperations) => transactions.push({doOperations, undoOperations}),+ });+ const protyle = {};+ for (const detached of [true, false]) {+ const itemID = detached ? "detached-item" : "bound-item";+ const row = {avID: "database", itemID, valueID: "primary-value", databaseBlockID: "carrier-b"};+ const primary = {id: row.valueID, keyID: "primary", blockID: itemID, type: "block",+ isDetached: detached, block: {content: "Title", ...(detached ? {} : {id: "bound-block"})}};+ tables = [{avID: row.avID, blockIDs: ["carrier-a", "carrier-b"], keyValues: [+ {key: {id: "primary", type: "block"}, values: [primary]},+ ]}];+ const element = document.createElement("div");+ element.className = "custom-attr";+ document.body.replaceChildren(element);+ let callbackCount = 0;+ const render = () => window.renderAVAttribute(element, itemID, protyle, () => callbackCount++, row);+ render();+ let block = element.querySelector('[data-type="NodeAttributeView"]');+ assert.equal(block.dataset.nodeId, "carrier-b");+ assert.equal(block.dataset.attributeId, itemID);+ assert.equal(block.querySelector("[data-row-id]").dataset.rowId, itemID);+ assert.equal(window.attributeTableData.get(block), tables[0]);+ for (const type of ["select", "relation", "date", "edit"]) {+ window.openMenuPanel({protyle, blockElement: block, type});+ assert.equal(requests.at(-1).url, "/api/av/renderAttributeView");+ assert.equal(requests.at(-1).data.blockID, "carrier-b");+ }+ for (const type of ["select", "mSelect", "relation"]) {+ const oldValue = {type, ...(type === "relation" ? {relation: {blockIDs: [], contents: []}} : {mSelect: []})};+ const newValue = {type, ...(type === "relation" ?+ {relation: {blockIDs: ["related-item"], contents: []}} : {mSelect: [{content: "Option", color: "1"}]})};+ const cells = [{rowID: itemID, colID: "field", cell: {id: "value", value: oldValue}, column: {type}}];+ await window.updateCellsValue(protyle, block, newValue, undefined, undefined, undefined,+ false, false, false, cells, false);+ const {doOperations, undoOperations} = transactions.at(-1);+ for (const operations of [doOperations, undoOperations]) {+ assert.equal(operations[0].action, "updateAttrViewCell");+ assert.equal(operations[0].avID, row.avID);+ assert.equal(operations[0].rowID, itemID);+ assert.equal(operations[0].blockID, "carrier-b");+ assert.equal(operations[1].action, "doUpdateUpdated");+ assert.equal(operations[1].id, "carrier-b");+ }+ assert.deepEqual(doOperations[0].data[type === "relation" ? "relation" : "mSelect"],+ newValue[type === "relation" ? "relation" : "mSelect"]);+ assert.deepEqual(undoOperations[0].data, oldValue);+ }+ row.databaseBlockID = "carrier-a";+ render();+ assert.equal(element.querySelectorAll('[data-type="NodeAttributeView"]').length, 1);+ assert.equal(element.firstElementChild, block);+ assert.equal(block.dataset.nodeId, "carrier-a");+ assert.equal(callbackCount, 2);+ assert.equal(requests.filter(request => request.url.endsWith("getAttributeViewKeys")).at(-1).data.itemID, itemID);+ }+ // 普通块属性仍以真实块为上下文,不被所在数据库的其他镜像替换。+ const element = document.createElement("div");+ document.body.replaceChildren(element);+ window.renderAVAttribute(element, "document-block", protyle);+ assert.equal(element.firstElementChild.dataset.nodeId, "document-block");+ assert.equal(element.firstElementChild.dataset.attributeId, "document-block");+ assert.deepEqual(requests.filter(request => request.url.endsWith("getAttributeViewKeys")).at(-1).data,+ {id: "document-block"});+};++const runElectron = async () => {+ const {app, BrowserWindow} = require("electron");+ app.setPath("userData", process.argv[2]);+ app.commandLine.appendSwitch("disable-gpu");+ await app.whenReady();+ const win = new BrowserWindow({show: false, webPreferences: {nodeIntegration: true, contextIsolation: false}});+ let exitCode = 0;+ try {+ const ts = require("typescript");+ const root = path.join(__dirname, "../src/protyle/render/av");+ const extract = (name, names) => {+ const text = readFileSync(path.join(root, name + ".ts"), "utf8");+ if (!names) {+ return text;+ }+ const source = ts.createSourceFile(name, text, ts.ScriptTarget.Latest, true);+ const statements = source.statements.filter(statement => ts.isVariableStatement(statement) &&+ statement.declarationList.declarations.some(declaration => names.includes(declaration.name.getText(source))));+ assert.equal(statements.length, names.length);+ return statements.map(statement => "export " + statement.getText(source).replace(/^export /, "")).join("\n");+ };+ // 执行实际渲染、菜单请求和事务组装函数,仅隔离网络、富文本显示与编辑器外壳。+ const sources = [extract("cellValue"), extract("dragFillValue", ["rebindAVCellValue"]),+ extract("blockAttr", ["renderAVAttribute", "renderAttributeViewBacklinks"]),+ extract("openMenuPanel", ["openMenuPanel"]), extract("cell", ["updateCellsValue"])].map(source =>+ ts.transpileModule(source, {compilerOptions: {module: ts.ModuleKind.CommonJS, target: ts.ScriptTarget.ES2020}}).outputText);+ await win.loadURL("data:text/html,<html><body></body></html>");+ await win.webContents.executeJavaScript(`(() => {+ for (const source of ${JSON.stringify(sources)}) {+ const exports = {};+ new Function("exports", source)(exports);+ Object.assign(window, exports);+ }+ })()`);+ await win.webContents.executeJavaScript(`(${runCases.toString()})()`);+ console.log("Attribute carrier cases passed");+ } catch (error) {+ console.error(error);+ exitCode = 1;+ } finally {+ win.destroy();+ app.exit(exitCode);+ }+};++if (process.versions.electron && process.type === "browser") {+ runElectron().catch(error => {+ console.error(error);+ require("electron").app.exit(1);+ });+} else {+ require("node:test").it("keeps database item identity separate from the physical attribute carrier", {+ skip: process.platform === "linux" && !process.env.DISPLAY && !process.env.WAYLAND_DISPLAY,+ timeout: 45000,+ }, async () => {+ const env = {...process.env};+ delete env.ELECTRON_RUN_AS_NODE;+ const profile = mkdtempSync(path.join(os.tmpdir(), "siyuan-av-carrier-"));+ try {+ const {stdout} = await require("node:util").promisify(require("node:child_process").execFile)(+ require("electron"), [__filename, profile], {env, windowsHide: true, timeout: 40000});+ assert.match(stdout, /Attribute carrier cases passed/);+ } finally {+ assert.equal(path.dirname(path.resolve(profile)), path.resolve(os.tmpdir()));+ assert.ok(path.basename(profile).startsWith("siyuan-av-carrier-"));+ rmSync(profile, {recursive: true, force: true, maxRetries: 5, retryDelay: 100});+ }+ });+}diff --git a/app/src/editor/databaseRow.ts b/app/src/editor/databaseRow.tsindex d76a5a19e3..6962dd4634 100644--- a/app/src/editor/databaseRow.ts+++ b/app/src/editor/databaseRow.ts@@ -82,7 +82,7 @@ export const newDatabaseRowModel = (options: {currentElement: matchedElement,});},- {avID: data.avID, itemID: data.itemID, valueID: data.valueID});+ {avID: data.avID, itemID: data.itemID, valueID: data.valueID, databaseBlockID: data.blockID});};const model = new Custom({app: options.app,diff --git a/app/src/protyle/render/av/blockAttr.ts b/app/src/protyle/render/av/blockAttr.tsindex 64761d4d19..a8969d8189 100644--- a/app/src/protyle/render/av/blockAttr.ts+++ b/app/src/protyle/render/av/blockAttr.ts@@ -125,7 +125,7 @@ const handleRichTextInteraction = (protyle: IProtyle, event: MouseEvent) => {};export const renderAVAttribute = (element: HTMLElement, id: string, protyle: IProtyle, cb?: (element: HTMLElement) => void,- row?: { avID: string, itemID: string, valueID: string }) => {+ row?: { avID: string, itemID: string, valueID: string, databaseBlockID: string }) => {const renderID = (++attributeViewRenderID).toString();element.dataset.avAttributeRenderId = renderID;fetchPost("/api/av/getAttributeViewKeys", row ? {id, avID: row.avID, itemID: row.itemID, valueID: row.valueID} : {id}, (response) => {@@ -161,6 +161,8 @@ export const renderAVAttribute = (element: HTMLElement, id: string, protyle: IPravID: stringavName: string}) => {+ // 条目 ID 仅用于读取行数据,编辑菜单和更新时间使用真实数据库载体块。+ const blockID = row ? row.databaseBlockID : id;const primaryValue = table.keyValues.find(item => item.key.type === "block")?.values[0] || table.keyValues[0]?.values[0];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)}'>@@ -194,15 +196,17 @@ export const renderAVAttribute = (element: HTMLElement, id: string, protyle: IPrinnerHTML += `<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>`;- html += `<div data-av-id="${table.avID}" data-av-type="table" data-node-id="${id}" data-type="NodeAttributeView">${innerHTML}</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>`;+ html += tableHTML;if (element.innerHTML) {// 防止 blockElement 找不到- const blockElement = element.querySelector(`[data-node-id="${id}"][data-av-id="${table.avID}"]`);+ const blockElement = element.querySelector<HTMLElement>(`[data-attribute-id="${id}"][data-av-id="${table.avID}"]`);if (blockElement) {+ blockElement.dataset.nodeId = blockID;blockElement.innerHTML = innerHTML;} else {- element.insertAdjacentHTML("beforeend", `<div data-av-id="${table.avID}" data-av-type="table" data-node-id="${id}" data-type="NodeAttributeView">${innerHTML}</div>`);+ element.insertAdjacentHTML("beforeend", tableHTML);}}});@@ -517,7 +521,7 @@ export const renderAVAttribute = (element: HTMLElement, id: string, protyle: IPr}renderAVRichTextElements(element);tables.forEach((table: IAVAttributeTableData) => {- const blockElement = element.querySelector<HTMLElement>(`[data-node-id="${id}"][data-av-id="${table.avID}"]`);+ const blockElement = element.querySelector<HTMLElement>(`[data-attribute-id="${id}"][data-av-id="${table.avID}"]`);if (blockElement) {attributeTableData.set(blockElement, table);}diff --git a/app/src/protyle/render/av/cell.ts b/app/src/protyle/render/av/cell.tsindex 1fcf4b6c2f..f978d8bc2e 100644--- a/app/src/protyle/render/av/cell.ts+++ b/app/src/protyle/render/av/cell.ts@@ -1070,6 +1070,7 @@ export const updateCellsValue = async (protyle: IProtyle, nodeElement: HTMLElemedoOperations.push(createAVCellUpdateOperation({valueID: cellId,avID,+ blockID: id,keyID: colId,rowID,data: cellValue@@ -1078,6 +1079,7 @@ export const updateCellsValue = async (protyle: IProtyle, nodeElement: HTMLElemeundoOperations.push(createAVCellUpdateOperation({valueID: cellId,avID,+ blockID: id,keyID: colId,rowID,data: oldValuediff --git a/app/src/protyle/render/av/cellValue.ts b/app/src/protyle/render/av/cellValue.tsindex bafd2bcf47..6433c41afb 100644--- a/app/src/protyle/render/av/cellValue.ts+++ b/app/src/protyle/render/av/cellValue.ts@@ -176,6 +176,7 @@ export const createAVStableTextCell = (options: {export const createAVCellUpdateOperation = (options: {valueID?: string;avID?: string;+ blockID?: string;keyID: string;rowID: string;data: IAVCellValue;@@ -183,6 +184,7 @@ export const createAVCellUpdateOperation = (options: {action: "updateAttrViewCell",id: options.valueID || "",avID: options.avID,+ ...(options.blockID ? {blockID: options.blockID} : {}),keyID: options.keyID,rowID: options.rowID,data: options.data,diff --git a/app/src/protyle/render/av/openDatabaseRow.ts b/app/src/protyle/render/av/openDatabaseRow.tsindex a517b330a6..b4bb4d1644 100644--- a/app/src/protyle/render/av/openDatabaseRow.ts+++ b/app/src/protyle/render/av/openDatabaseRow.ts@@ -95,6 +95,7 @@ const openMobileDatabaseRow = (protyle: IProtyle, data: IDatabaseRowOpenData, tiavID: data.avID,itemID: data.itemID,valueID: data.valueID,+ databaseBlockID: data.databaseBlockID,});};context.ghostProtyle = new Protyle(protyle.app, document.createElement("div"), {diff --git a/app/src/protyle/render/av/render.ts b/app/src/protyle/render/av/render.tsindex f0aa76ba81..72d0e4e3ac 100644--- a/app/src/protyle/render/av/render.ts+++ b/app/src/protyle/render/av/render.ts@@ -1071,7 +1071,7 @@ export const refreshAV = (protyle: IProtyle, operation: IOperation) => {if (attrElement) {// 更新属性面板attrElement.removeAttribute("data-rendering");- renderAVAttribute(attrElement.parentElement, attrElement.dataset.nodeId, protyle);+ renderAVAttribute(attrElement.parentElement, attrElement.dataset.attributeId, protyle);}getAVElements(protyle, avID).forEach((item) => {item.removeAttribute("data-render");