Skip to content

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

:bug: Prevent database row details from flickering after edits https://github.com/siyuan-note/siyuan/issues/19715

Daniel4f4ea35f94a3
diff --git a/app/src/editor/databaseRow.ts b/app/src/editor/databaseRow.ts
index 6962dd4634..ee17619fa8 100644
--- a/app/src/editor/databaseRow.ts
+++ b/app/src/editor/databaseRow.ts
@@ -28,6 +28,7 @@ export const newDatabaseRowModel = (options: {
let resizeObserver: ResizeObserver;
let unregisterRefresh: () => void;
let destroyed = false;
+ let renderVersion = 0;
const updateTitle = (custom: Custom, bodyElement: HTMLElement) => {
const primaryElement = bodyElement.querySelector<HTMLElement>('[data-primary="true"] [data-cell-value]');
if (!primaryElement?.dataset.cellValue) {
@@ -58,11 +59,16 @@ export const newDatabaseRowModel = (options: {
return;
}
const data = custom.data as typeof options.data;
+ const currentRenderVersion = ++renderVersion;
const bodyElement = document.createElement("div");
bodyElement.className = "custom-attr protyle-db-row__body";
- previousBodyElement.replaceWith(bodyElement);
- updateLayout(custom);
renderAVAttribute(bodyElement, data.itemID, contextProtyle, (element) => {
+ if (destroyed || currentRenderVersion !== renderVersion || !previousBodyElement.isConnected) {
+ return;
+ }
+ // 保留当前内容,待属性和反链加载完成后一次替换,避免刷新期间出现空白。
+ previousBodyElement.replaceWith(element);
+ updateLayout(custom);
updateTitle(custom, element);
if (!data.keywords?.length) {
return;
diff --git a/app/src/protyle/render/av/openDatabaseRow.ts b/app/src/protyle/render/av/openDatabaseRow.ts
index 5bf61dd769..b528709769 100644
--- a/app/src/protyle/render/av/openDatabaseRow.ts
+++ b/app/src/protyle/render/av/openDatabaseRow.ts
@@ -57,6 +57,7 @@ const openMobileDatabaseRow = (protyle: Pick<IProtyle, "app">, data: IDatabaseRo
closeMobileDatabaseRow();
const context: { ghostProtyle?: Protyle } = {};
let unregisterRefresh: () => void;
+ let renderVersion = 0;
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>
@@ -67,6 +68,7 @@ const openMobileDatabaseRow = (protyle: Pick<IProtyle, "app">, data: IDatabaseRo
containerClassName: "b3-dialog__container--database-row",
disableAnimation: true,
destroyCallback() {
+ renderVersion++;
unregisterRefresh?.();
context.ghostProtyle?.destroy();
},
@@ -78,10 +80,15 @@ const openMobileDatabaseRow = (protyle: Pick<IProtyle, "app">, data: IDatabaseRo
if (!previousBodyElement) {
return;
}
+ const currentRenderVersion = ++renderVersion;
const bodyElement = document.createElement("div");
bodyElement.className = "custom-attr protyle-db-row__body";
- previousBodyElement.replaceWith(bodyElement);
renderAVAttribute(bodyElement, data.itemID, contextProtyle, (element) => {
+ if (currentRenderVersion !== renderVersion || !previousBodyElement.isConnected) {
+ return;
+ }
+ // 保留当前内容,待属性和反链加载完成后一次替换,避免刷新期间出现空白。
+ previousBodyElement.replaceWith(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;
diff --git a/app/tests/avAttributeCarrier.test.js b/app/tests/avAttributeCarrier.test.js
index 6f7df1890a..d705d58174 100644
--- a/app/tests/avAttributeCarrier.test.js
+++ b/app/tests/avAttributeCarrier.test.js
@@ -92,6 +92,110 @@ const runCases = async () => {
assert.equal(element.firstElementChild.dataset.attributeId, "document-block");
assert.deepEqual(requests.filter(request => request.url.endsWith("getAttributeViewKeys")).at(-1).data,
{id: "document-block"});
+
+ // 使用实际详情刷新函数,延迟接口完成并交错返回,检查内容连续性和过期响应。
+ let lifecycle;
+ let initialize;
+ let refresh;
+ const pendingRenders = [];
+ Object.assign(window, {
+ Custom: class {
+ constructor(options) {
+ lifecycle = options;
+ this.data = options.data;
+ this.element = document.createElement("div");
+ this.tab = {updateTitle: () => {}};
+ document.body.append(this.element);
+ options.init(this);
+ }
+ },
+ Dialog: class {
+ constructor(options) {
+ lifecycle = options;
+ this.element = document.createElement("div");
+ this.element.innerHTML = options.content;
+ document.body.append(this.element);
+ this.destroy = () => {
+ options.destroyCallback();
+ this.element.remove();
+ };
+ window.siyuan.dialogs.push(this);
+ }
+ },
+ Protyle: class {
+ constructor(_app, _element, options) {
+ initialize = () => options.after({protyle: {
+ id: "row-editor", highlight: {styleElement: document.createElement("style")},
+ }});
+ }
+ destroy() {}
+ },
+ getEditorHorizontalPadding: () => ({left: 8, right: 8}),
+ registerDatabaseRowRefresh: (_id, options) => {
+ refresh = options.refresh;
+ return () => {};
+ },
+ highlightDatabaseRow: () => {},
+ renderAVAttribute: (body, _id, _protyle, callback) => {
+ pendingRenders.push(text => {
+ const value = encodeURIComponent(JSON.stringify({block: {content: text}}));
+ body.innerHTML = `<div data-primary="true"><span data-cell-value="${value}">${text}</span></div>`;
+ callback(body);
+ });
+ },
+ });
+ window.siyuan.config = {editor: {fullWidth: true}};
+ window.siyuan.dialogs = [];
+ for (const mobile of [false, true]) {
+ const data = {avID: "database", blockID: "carrier", databaseBlockID: "carrier",
+ itemID: "item", valueID: "primary", title: "Initial"};
+ document.body.replaceChildren();
+ if (mobile) {
+ window.openMobileDatabaseRow({app: {}}, data, data.title);
+ } else {
+ window.newDatabaseRowModel({app: {}, tab: {}, data});
+ }
+ initialize();
+ pendingRenders.shift()("Original");
+ const currentBody = () => document.querySelector(".protyle-db-row__body");
+ const original = currentBody();
+ for (const value of ["Updated text", "Updated label"]) {
+ const previous = currentBody();
+ refresh();
+ assert.equal(currentBody(), previous, "keep the visible body while fetching");
+ assert.ok(currentBody().textContent);
+ pendingRenders.shift()(value);
+ assert.equal(currentBody().textContent, value);
+ assert.equal(document.querySelector(".protyle-db-row__title span").textContent, value);
+ }
+ assert.notEqual(currentBody(), original);
+ for (const newestFirst of [false, true]) {
+ const previous = currentBody();
+ refresh();
+ refresh();
+ const older = pendingRenders.shift();
+ const newer = pendingRenders.shift();
+ if (newestFirst) {
+ newer("Latest");
+ older("Outdated");
+ } else {
+ older("Outdated");
+ assert.equal(currentBody(), previous);
+ newer("Latest");
+ }
+ assert.equal(currentBody().textContent, "Latest");
+ assert.equal(document.querySelector(".protyle-db-row__title span").textContent, "Latest");
+ }
+ refresh();
+ const previous = currentBody();
+ if (mobile) {
+ lifecycle.destroyCallback();
+ } else {
+ lifecycle.destroy();
+ }
+ pendingRenders.shift()("After close");
+ assert.equal(currentBody(), previous, "ignore responses after disposal");
+ }
};
const runElectron = async () => {
@@ -118,7 +222,9 @@ const runElectron = async () => {
// 执行实际渲染、菜单请求和事务组装函数,仅隔离网络、富文本显示与编辑器外壳。
const sources = [extract("cellValue"), extract("dragFillValue", ["rebindAVCellValue"]),
extract("blockAttr", ["renderAVAttribute", "renderAttributeViewBacklinks"]),
- extract("openMenuPanel", ["openMenuPanel"]), extract("cell", ["updateCellsValue"])].map(source =>
+ extract("openMenuPanel", ["openMenuPanel"]), extract("cell", ["updateCellsValue"]),
+ extract("../../../editor/databaseRow", ["newDatabaseRowModel"]),
+ extract("openDatabaseRow", ["closeMobileDatabaseRow", "openMobileDatabaseRow"])].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(`(() => {