Skip to content

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

:art: Defer bottom backlink refresh while editing https://github.com/siyuan-note/siyuan/issues/18319

Daniel6f4c52a10fb8
diff --git a/app/src/dialog/processSystem.ts b/app/src/dialog/processSystem.ts
index 7ef0787857..79e37cb701 100644
--- a/app/src/dialog/processSystem.ts
+++ b/app/src/dialog/processSystem.ts
@@ -20,7 +20,6 @@ import type {App} from "../index";
import {saveScroll} from "../protyle/scroll/saveScroll";
import {isInAndroid, isInHarmony, isInIOS, setStorageVal} from "../protyle/util/compatibility";
import {Plugin} from "../plugin";
-import {shouldForceBacklinkRefresh} from "../layout/dock/backlinkRefresh";
const BACKLINK_REFRESH_DELAY = 3600;
let backlinkRefreshTimeout: number;
@@ -67,9 +66,8 @@ export const setDefRefCount = (data: {
item.blockId !== data.rootID && item.blockId !== data.blockID && !data.defIDs?.includes(item.blockId))) {
return;
}
- const ignoreFocus = shouldForceBacklinkRefresh(item.rootId, data.rootID);
- item.markDirty(ignoreFocus);
- item.refreshIfVisible(ignoreFocus);
+ item.markDirty();
+ item.refreshIfVisible();
});
/// #endif
getAllEditor().forEach(editor => {
diff --git a/app/src/layout/dock/Backlink.ts b/app/src/layout/dock/Backlink.ts
index 569beac235..a180834ebb 100644
--- a/app/src/layout/dock/Backlink.ts
+++ b/app/src/layout/dock/Backlink.ts
@@ -15,6 +15,7 @@ import {getDocDisplayName, isEncryptedBox} from "../../util/pathName";
import {getAllModels} from "../getAll";
import {hideElements} from "../../protyle/ui/hideElements";
import {
+ shouldDeferBottomBacklinkRefresh,
shouldHideBottomBacklinks,
shouldRenderBacklinkResponse,
shouldSaveBacklinkStatus
@@ -48,7 +49,6 @@ export class Backlink extends Model {
private destroyed = false;
private refreshQueued = false;
private searchQueued = false;
- private ignoreFocusOnNextRefresh = false;
private requestID = 0;
private showingLoading = false;
private contextRequestVersions = [0, 0];
@@ -667,7 +667,6 @@ export class Backlink extends Model {
this.refreshQueued = false;
this.searchQueued = false;
this.dirty = false;
- this.ignoreFocusOnNextRefresh = false;
this.notebookId = "";
this.blockId = blockId;
this.rootId = rootId;
@@ -745,7 +744,6 @@ export class Backlink extends Model {
this.searchQueued = false;
element.classList.add("fn__rotate");
this.dirty = false;
- this.ignoreFocusOnNextRefresh = false;
// 解析当前反链面板所属 box:优先用已记录的 notebookId,首次为空时按 rootId 在已打开的编辑器里查找
let notebookId = this.notebookId;
if (!notebookId && this.rootId) {
@@ -978,20 +976,14 @@ export class Backlink extends Model {
element.querySelector("use").setAttribute("xlink:href", folded ? "#iconRight" : "#iconDown");
}
- public markDirty(ignoreFocus = false) {
+ public markDirty() {
this.dirty = true;
- if (ignoreFocus) {
- this.ignoreFocusOnNextRefresh = true;
- }
}
public refreshIfVisible(ignoreFocus = false) {
if (this.type !== "bottom" || !this.dirty) {
return;
}
- if (ignoreFocus) {
- this.ignoreFocusOnNextRefresh = true;
- }
if (!this.element.isConnected || this.ownerProtyle.element.getClientRects().length === 0) {
return;
}
@@ -999,7 +991,10 @@ export class Backlink extends Model {
this.searchBacklinks();
return;
}
- if (!this.ignoreFocusOnNextRefresh && this.ownerProtyle.element.contains(document.activeElement)) {
+ if (shouldDeferBottomBacklinkRefresh(
+ this.ownerProtyle.element.contains(document.activeElement),
+ ignoreFocus
+ )) {
return;
}
if (this.element.classList.contains("fn__none") ||
diff --git a/app/src/layout/dock/backlinkRefresh.test.ts b/app/src/layout/dock/backlinkRefresh.test.ts
index 9b962297e7..a433bd7cb3 100644
--- a/app/src/layout/dock/backlinkRefresh.test.ts
+++ b/app/src/layout/dock/backlinkRefresh.test.ts
@@ -1,23 +1,12 @@
import {describe, it} from "node:test";
import * as assert from "node:assert/strict";
import {
- shouldForceBacklinkRefresh,
+ shouldDeferBottomBacklinkRefresh,
shouldHideBottomBacklinks,
shouldRenderBacklinkResponse,
shouldSaveBacklinkStatus
} from "./backlinkRefresh";
-describe("shouldForceBacklinkRefresh", () => {
- it("refreshes reference changes in the owner document while it is focused", () => {
- assert.equal(shouldForceBacklinkRefresh("owner", "owner"), true);
- });
-
- it("keeps unrelated document changes deferred", () => {
- assert.equal(shouldForceBacklinkRefresh("owner", "other"), false);
- assert.equal(shouldForceBacklinkRefresh("", "other"), false);
- });
-});
-
describe("shouldRenderBacklinkResponse", () => {
it("skips a stale response when a newer search or refresh is queued", () => {
assert.equal(shouldRenderBacklinkResponse(false, false), true);
@@ -34,6 +23,17 @@ describe("shouldSaveBacklinkStatus", () => {
});
});
+describe("shouldDeferBottomBacklinkRefresh", () => {
+ it("defers automatic refreshes while the owner editor is focused", () => {
+ assert.equal(shouldDeferBottomBacklinkRefresh(true, false), true);
+ assert.equal(shouldDeferBottomBacklinkRefresh(false, false), false);
+ });
+
+ it("allows explicit refreshes to bypass the focus guard", () => {
+ assert.equal(shouldDeferBottomBacklinkRefresh(true, true), false);
+ });
+});
+
describe("shouldHideBottomBacklinks", () => {
it("hides the bottom area only when backlinks and mentions are both empty", () => {
assert.equal(shouldHideBottomBacklinks(0, 0, "", ""), true);
diff --git a/app/src/layout/dock/backlinkRefresh.ts b/app/src/layout/dock/backlinkRefresh.ts
index 2ca502cebe..7fd96ec9b5 100644
--- a/app/src/layout/dock/backlinkRefresh.ts
+++ b/app/src/layout/dock/backlinkRefresh.ts
@@ -1,7 +1,3 @@
-export const shouldForceBacklinkRefresh = (ownerRootID: string, changedRootID: string) => {
- return Boolean(ownerRootID && ownerRootID === changedRootID);
-};
-
export const shouldRenderBacklinkResponse = (refreshQueued: boolean, searchQueued: boolean) => {
return !refreshQueued && !searchQueued;
};
@@ -10,6 +6,10 @@ export const shouldSaveBacklinkStatus = (init: boolean, showingLoading: boolean)
return !init && !showingLoading;
};
+export const shouldDeferBottomBacklinkRefresh = (ownerFocused: boolean, ignoreFocus: boolean) => {
+ return ownerFocused && !ignoreFocus;
+};
+
export const shouldHideBottomBacklinks = (
linkRefsCount: number,
mentionsCount: number,