Skip to content

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

:bug: Refresh database row tabs after updates https://github.com/siyuan-note/siyuan/issues/18983

Daniel6976714d987c
diff --git a/app/src/editor/databaseRow.ts b/app/src/editor/databaseRow.ts
index 3867ff07a7..d76a5a19e3 100644
--- a/app/src/editor/databaseRow.ts
+++ b/app/src/editor/databaseRow.ts
@@ -5,6 +5,7 @@ import {renderAVAttribute} from "../protyle/render/av/blockAttr";
import {Protyle} from "../protyle";
import {getEditorHorizontalPadding} from "../protyle/ui/padding";
import {searchMarkRender} from "../protyle/render/searchMarkRender";
+import {registerDatabaseRowRefresh} from "../protyle/render/av/databaseRowRefresh";
export const newDatabaseRowModel = (options: {
app: App,
@@ -25,6 +26,7 @@ export const newDatabaseRowModel = (options: {
let contextProtyle: IProtyle;
let ghostProtyle: Protyle;
let resizeObserver: ResizeObserver;
+ let unregisterRefresh: () => void;
let destroyed = false;
const updateTitle = (custom: Custom, bodyElement: HTMLElement) => {
const primaryElement = bodyElement.querySelector<HTMLElement>('[data-primary="true"] [data-cell-value]');
@@ -114,6 +116,10 @@ export const newDatabaseRowModel = (options: {
return;
}
contextProtyle = editor.protyle;
+ unregisterRefresh = registerDatabaseRowRefresh(contextProtyle.id, {
+ getAVID: () => (customModel.data as typeof options.data).avID,
+ refresh: () => render(customModel),
+ });
custom.element.append(contextProtyle.highlight.styleElement);
render(custom);
},
@@ -122,6 +128,7 @@ export const newDatabaseRowModel = (options: {
destroy() {
destroyed = true;
resizeObserver?.disconnect();
+ unregisterRefresh?.();
ghostProtyle?.destroy();
},
update() {
diff --git a/app/src/protyle/index.ts b/app/src/protyle/index.ts
index 42b77a0c1d..8e84388ae6 100644
--- a/app/src/protyle/index.ts
+++ b/app/src/protyle/index.ts
@@ -54,6 +54,10 @@ import {setEditMode} from "./util/setEditMode";
import {waitForPendingTransactions} from "./util/transactionQueue";
import {applyViewFoldStates, invalidateViewFoldRequests} from "./util/viewFold";
import {setFullscreen as setFullscreenState} from "./breadcrumb/action";
+import {
+ queueDatabaseRowRefresh,
+ queueDatabaseRowRefreshForOperations
+} from "./render/av/databaseRowRefresh";
export class Protyle {
@@ -175,25 +179,7 @@ export class Protyle {
if (this.protyle.databaseAttributePanel?.hasDatabase(data.data.id)) {
this.protyle.databaseAttributePanel.refresh();
}
- /// #if MOBILE
- document.querySelectorAll<HTMLElement>(
- `.protyle-db-row--mobile[data-protyle-id="${this.protyle.id}"] .protyle-db-row__body > [data-av-id="${data.data.id}"]`
- ).forEach((item) => {
- renderAVAttribute(item.parentElement as HTMLElement, item.dataset.nodeId, this.protyle, undefined, {
- avID: data.data.id,
- itemID: item.dataset.nodeId,
- valueID: "",
- });
- });
- /// #endif
- /// #if !MOBILE
- getAllModels().custom.forEach((item) => {
- if (item.type === "siyuan-database-row" && (item.data.avID === data.data.id ||
- item.element.querySelector(`[data-av-id="${data.data.id}"]`))) {
- item.update?.();
- }
- });
- /// #endif
+ queueDatabaseRowRefresh(this.protyle.id, data.data.id);
break;
case "addLoading":
if (data.data === this.protyle.block.rootID) {
@@ -355,6 +341,7 @@ export class Protyle {
if (data.context?.undoState) {
syncMirrorFromBroadcast(data.context.undoState);
}
+ queueDatabaseRowRefreshForOperations(this.protyle.id, data.data[0]?.doOperations || []);
if (!this.protyle.preview.element.classList.contains("fn__none") &&
data.context?.rootIDs?.includes(this.protyle.block.rootID)) {
this.protyle.preview.render(this.protyle);
diff --git a/app/src/protyle/render/av/databaseRowRefresh.test.ts b/app/src/protyle/render/av/databaseRowRefresh.test.ts
new file mode 100644
index 0000000000..bef7fe48cc
--- /dev/null
+++ b/app/src/protyle/render/av/databaseRowRefresh.test.ts
@@ -0,0 +1,80 @@
+import {describe, it} from "node:test";
+import * as assert from "node:assert/strict";
+import {
+ queueDatabaseRowRefresh,
+ queueDatabaseRowRefreshForOperations,
+ registerDatabaseRowRefresh,
+} from "./databaseRowRefresh";
+
+const waitForRefresh = () => new Promise((resolve) => setTimeout(resolve, 150));
+
+describe("database row refresh", () => {
+ it("refreshes only the matching database and coalesces requests", async () => {
+ let avID = "av-a";
+ let refreshCount = 0;
+ const unregister = registerDatabaseRowRefresh("protyle-a", {
+ getAVID: () => avID,
+ refresh: () => refreshCount++,
+ });
+
+ queueDatabaseRowRefresh("protyle-a", "av-b");
+ queueDatabaseRowRefresh("protyle-a", "av-a");
+ queueDatabaseRowRefresh("protyle-a", "av-a");
+ await waitForRefresh();
+ assert.equal(refreshCount, 1);
+
+ avID = "av-b";
+ queueDatabaseRowRefresh("protyle-a", "av-a");
+ queueDatabaseRowRefresh("protyle-a", "av-b");
+ await waitForRefresh();
+ assert.equal(refreshCount, 2);
+
+ unregister();
+ queueDatabaseRowRefresh("protyle-a", "av-b");
+ await waitForRefresh();
+ assert.equal(refreshCount, 2);
+ });
+
+ it("resolves database IDs from value and name operations", async () => {
+ let avID = "av-template";
+ let refreshCount = 0;
+ const unregister = registerDatabaseRowRefresh("protyle-b", {
+ getAVID: () => avID,
+ refresh: () => refreshCount++,
+ });
+
+ queueDatabaseRowRefreshForOperations("protyle-b", [{
+ action: "updateAttrViewColTemplate",
+ avID: "av-template",
+ }] as IOperation[]);
+ await waitForRefresh();
+ assert.equal(refreshCount, 1);
+
+ avID = "av-name";
+ queueDatabaseRowRefreshForOperations("protyle-b", [{
+ action: "setAttrViewName",
+ id: "av-name",
+ }] as IOperation[]);
+ await waitForRefresh();
+ assert.equal(refreshCount, 2);
+ unregister();
+ });
+
+ it("does not unregister a newer registration with stale cleanup", async () => {
+ let refreshCount = 0;
+ const unregisterOld = registerDatabaseRowRefresh("protyle-c", {
+ getAVID: () => "av-old",
+ refresh: () => refreshCount++,
+ });
+ const unregisterNew = registerDatabaseRowRefresh("protyle-c", {
+ getAVID: () => "av-new",
+ refresh: () => refreshCount++,
+ });
+
+ unregisterOld();
+ queueDatabaseRowRefresh("protyle-c", "av-new");
+ await waitForRefresh();
+ assert.equal(refreshCount, 1);
+ unregisterNew();
+ });
+});
diff --git a/app/src/protyle/render/av/databaseRowRefresh.ts b/app/src/protyle/render/av/databaseRowRefresh.ts
new file mode 100644
index 0000000000..b02045d7ab
--- /dev/null
+++ b/app/src/protyle/render/av/databaseRowRefresh.ts
@@ -0,0 +1,55 @@
+interface IDatabaseRowRefreshRegistration {
+ getAVID: () => string;
+ refresh: () => void;
+ timeout?: ReturnType<typeof setTimeout>;
+}
+
+const refreshRegistrations = new Map<string, IDatabaseRowRefreshRegistration>();
+
+export const registerDatabaseRowRefresh = (protyleID: string, options: {
+ getAVID: () => string;
+ refresh: () => void;
+}) => {
+ const previous = refreshRegistrations.get(protyleID);
+ if (typeof previous?.timeout !== "undefined") {
+ clearTimeout(previous.timeout);
+ }
+ const registration: IDatabaseRowRefreshRegistration = options;
+ refreshRegistrations.set(protyleID, registration);
+ return () => {
+ if (refreshRegistrations.get(protyleID) !== registration) {
+ return;
+ }
+ if (typeof registration.timeout !== "undefined") {
+ clearTimeout(registration.timeout);
+ }
+ refreshRegistrations.delete(protyleID);
+ };
+};
+
+export const queueDatabaseRowRefresh = (protyleID: string, avID: string) => {
+ const registration = refreshRegistrations.get(protyleID);
+ if (!registration || registration.getAVID() !== avID) {
+ return;
+ }
+ if (typeof registration.timeout !== "undefined") {
+ clearTimeout(registration.timeout);
+ }
+ registration.timeout = setTimeout(() => {
+ registration.timeout = undefined;
+ if (refreshRegistrations.get(protyleID) === registration && registration.getAVID() === avID) {
+ registration.refresh();
+ }
+ }, 100);
+};
+
+export const queueDatabaseRowRefreshForOperations = (protyleID: string, operations: IOperation[]) => {
+ const avIDs = new Set<string>();
+ operations.forEach((operation) => {
+ const avID = operation.action === "setAttrViewName" ? operation.id : operation.avID;
+ if (avID) {
+ avIDs.add(avID);
+ }
+ });
+ avIDs.forEach((avID) => queueDatabaseRowRefresh(protyleID, avID));
+};
diff --git a/app/src/protyle/render/av/dateFormatMenu.ts b/app/src/protyle/render/av/dateFormatMenu.ts
index 1c72c7c774..1084c37417 100644
--- a/app/src/protyle/render/av/dateFormatMenu.ts
+++ b/app/src/protyle/render/av/dateFormatMenu.ts
@@ -1,7 +1,7 @@
import {Menu} from "../../../plugin/Menu";
import {transaction} from "../../wysiwyg/transaction";
import {Constants} from "../../../constants";
-import {getAllEditor, getAllModels} from "../../../layout/getAll";
+import {getAllEditor} from "../../../layout/getAll";
import {formatDateDisplay, getLabelByDateFormat} from "./dateFormat";
const refreshDatabaseAttributePanels = (protyle: IProtyle, avID: string) => {
@@ -11,13 +11,6 @@ const refreshDatabaseAttributePanels = (protyle: IProtyle, avID: string) => {
editor.protyle.databaseAttributePanel.refresh();
}
});
- /// #if !MOBILE
- getAllModels().custom.forEach((model) => {
- if (model.type === "siyuan-database-row" && model.data.avID === avID) {
- model.update?.();
- }
- });
- /// #endif
};
export const formatDate = (options: {
diff --git a/app/src/protyle/render/av/number.ts b/app/src/protyle/render/av/number.ts
index 750cd4df1f..2d72275164 100644
--- a/app/src/protyle/render/av/number.ts
+++ b/app/src/protyle/render/av/number.ts
@@ -1,7 +1,7 @@
import {Menu} from "../../../plugin/Menu";
import {transaction} from "../../wysiwyg/transaction";
import {Constants} from "../../../constants";
-import {getAllEditor, getAllModels} from "../../../layout/getAll";
+import {getAllEditor} from "../../../layout/getAll";
const refreshDatabaseAttributePanels = (protyle: IProtyle, avID: string) => {
protyle.databaseAttributePanel?.refresh();
@@ -10,13 +10,6 @@ const refreshDatabaseAttributePanels = (protyle: IProtyle, avID: string) => {
editor.protyle.databaseAttributePanel.refresh();
}
});
- /// #if !MOBILE
- getAllModels().custom.forEach((model) => {
- if (model.type === "siyuan-database-row" && model.data.avID === avID) {
- model.update?.();
- }
- });
- /// #endif
};
const addFormatItem = (options: {
diff --git a/app/src/protyle/render/av/openDatabaseRow.ts b/app/src/protyle/render/av/openDatabaseRow.ts
index c77d722007..a517b330a6 100644
--- a/app/src/protyle/render/av/openDatabaseRow.ts
+++ b/app/src/protyle/render/av/openDatabaseRow.ts
@@ -12,6 +12,7 @@ import {zoomOut} from "../../../menus/protyle";
import {Custom} from "../../../layout/dock/Custom";
/// #endif
import {searchMarkRender} from "../searchMarkRender";
+import {registerDatabaseRowRefresh} from "./databaseRowRefresh";
export interface IDatabaseRowOpenData {
avID: string;
@@ -55,6 +56,7 @@ const closeMobileDatabaseRow = () => {
const openMobileDatabaseRow = (protyle: IProtyle, data: IDatabaseRowOpenData, title: string) => {
closeMobileDatabaseRow();
const context: { ghostProtyle?: Protyle } = {};
+ let unregisterRefresh: () => void;
const dialog = new Dialog({
content: `<div class="protyle-db-row protyle-db-row--mobile protyle-content">
<div class="protyle-db-row__title"><svg><use xlink:href="#iconDatabase"></use></svg><span></span></div>
@@ -65,26 +67,48 @@ const openMobileDatabaseRow = (protyle: IProtyle, data: IDatabaseRowOpenData, ti
containerClassName: "b3-dialog__container--database-row",
disableAnimation: true,
destroyCallback() {
+ unregisterRefresh?.();
context.ghostProtyle?.destroy();
},
});
const rowElement = dialog.element.querySelector<HTMLElement>(".protyle-db-row");
rowElement.dataset.protyleId = protyle.id;
rowElement.querySelector(".protyle-db-row__title span").textContent = title;
+ const render = (contextProtyle: IProtyle) => {
+ const previousBodyElement = rowElement.querySelector<HTMLElement>(".protyle-db-row__body");
+ if (!previousBodyElement) {
+ return;
+ }
+ const bodyElement = document.createElement("div");
+ bodyElement.className = "custom-attr protyle-db-row__body";
+ previousBodyElement.replaceWith(bodyElement);
+ renderAVAttribute(bodyElement, data.itemID, contextProtyle, (element) => {
+ const primaryElement = element.querySelector<HTMLElement>('[data-primary="true"] [data-cell-value]');
+ if (primaryElement?.dataset.cellValue) {
+ const value = JSON.parse(decodeURIComponent(primaryElement.dataset.cellValue)) as IAVCellValue;
+ const currentTitle = value.block?.content || window.siyuan.languages.untitled;
+ data.title = currentTitle;
+ rowElement.querySelector(".protyle-db-row__title span").textContent = currentTitle;
+ }
+ highlightDatabaseRow(contextProtyle, rowElement, data);
+ }, {
+ avID: data.avID,
+ itemID: data.itemID,
+ valueID: data.valueID,
+ });
+ };
context.ghostProtyle = new Protyle(protyle.app, document.createElement("div"), {
blockId: data.databaseBlockID,
notebookId: data.notebookID,
after(editor) {
const contextProtyle = editor.protyle;
rowElement.dataset.protyleId = contextProtyle.id;
- rowElement.append(contextProtyle.highlight.styleElement);
- renderAVAttribute(rowElement.querySelector<HTMLElement>(".protyle-db-row__body"), data.itemID, contextProtyle, () => {
- highlightDatabaseRow(contextProtyle, rowElement, data);
- }, {
- avID: data.avID,
- itemID: data.itemID,
- valueID: data.valueID,
+ unregisterRefresh = registerDatabaseRowRefresh(contextProtyle.id, {
+ getAVID: () => data.avID,
+ refresh: () => render(contextProtyle),
});
+ rowElement.append(contextProtyle.highlight.styleElement);
+ render(contextProtyle);
},
});
};