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: Stabilize bottom backlink refresh state https://github.com/siyuan-note/siyuan/issues/18319
Daniel4a8154f9a9f2
diff --git a/app/src/editor/util.ts b/app/src/editor/util.tsindex 69eba8a109..95de1b2f80 100644--- a/app/src/editor/util.ts+++ b/app/src/editor/util.ts@@ -709,10 +709,7 @@ export const updateBacklinkGraph = (models: IModels, protyle: IProtyle) => {}const backlinkKeyword = item.inputsElement[0].value;const backmentionKeyword = item.inputsElement[1].value;- item.saveStatus();- item.blockId = blockId;- item.rootId = protyle.block.rootID;- item.render(undefined);+ item.prepareForBlock(blockId, protyle.block.rootID);item.inputsElement[0].value = backlinkKeyword;item.inputsElement[1].value = backmentionKeyword;item.markDirty();diff --git a/app/src/layout/dock/Backlink.ts b/app/src/layout/dock/Backlink.tsindex bb4fe94ffb..292c43d670 100644--- a/app/src/layout/dock/Backlink.ts+++ b/app/src/layout/dock/Backlink.ts@@ -14,6 +14,7 @@ import {isSupportCSSHL, searchMarkRender} from "../../protyle/render/searchMarkRimport {getDocDisplayName, isEncryptedBox} from "../../util/pathName";import {getAllModels} from "../getAll";import {hideElements} from "../../protyle/ui/hideElements";+import {shouldRenderBacklinkResponse, shouldSaveBacklinkStatus} from "./backlinkRefresh";export class Backlink extends Model {public element: HTMLElement;@@ -42,7 +43,11 @@ export class Backlink extends Model {private dirty = false;private destroyed = false;private refreshQueued = false;+ private searchQueued = false;private ignoreFocusOnNextRefresh = false;+ private requestID = 0;+ private showingLoading = false;+ private contextRequestVersions = [0, 0];private ownerFocusoutListener?: (event: FocusEvent) => void;constructor(options: {@@ -133,7 +138,7 @@ export class Backlink extends Model {item.addEventListener("blur", (event: KeyboardEvent) => {const inputElement = event.target as HTMLInputElement;inputElement.classList.add("fn__none");- const filterIconElement = inputElement.nextElementSibling;+ const filterIconElement = inputElement.parentElement.querySelector('[data-type="search"]');if (inputElement.value) {filterIconElement.classList.add("block__icon--active");filterIconElement.setAttribute("aria-label", window.siyuan.languages.filter + " " + inputElement.value);@@ -257,6 +262,7 @@ export class Backlink extends Model {});// 为了快捷键的 dispatchthis.element.querySelector('[data-type="collapse"]').addEventListener("click", () => {+ this.cancelContextRequests(this.tree.element, false);this.hideEditorGutters(this.tree.element);this.tree.element.querySelectorAll(".protyle").forEach(item => {item.classList.add("fn__none");@@ -297,6 +303,7 @@ export class Backlink extends Model {event.stopPropagation();break;case "mCollapse":+ this.cancelContextRequests(this.mTree.element, true);this.hideEditorGutters(this.mTree.element);this.mTree.element.querySelectorAll(".protyle").forEach(item => {item.classList.add("fn__none");@@ -418,6 +425,7 @@ export class Backlink extends Model {private setBottomLayout(element: HTMLElement, listElement: HTMLElement) {const folded = !listElement.classList.contains("fn__none");if (folded) {+ this.cancelContextRequests(listElement, listElement === this.mTree.element);this.hideEditorGutters(listElement);}listElement.classList.toggle("fn__none", folded);@@ -560,13 +568,15 @@ export class Backlink extends Model {} else {const keyword = isMention ? this.inputsElement[1].value : this.inputsElement[0].value;const blockId = this.blockId;+ const contextRequestVersion = this.contextRequestVersions[isMention ? 1 : 0];fetchPost(isMention ? "/api/ref/getBackmentionDoc" : "/api/ref/getBacklinkDoc", {defID: blockId,refTreeID: docId,highlight: !isSupportCSSHL(),keyword,}, (response) => {- if (this.destroyed || blockId !== this.blockId || !liElement.isConnected) {+ if (this.destroyed || blockId !== this.blockId || !liElement.isConnected ||+ contextRequestVersion !== this.contextRequestVersions[isMention ? 1 : 0]) {return;}svgElement.removeAttribute("disabled");@@ -608,6 +618,13 @@ export class Backlink extends Model {});}+ private cancelContextRequests(element: Element, isMention: boolean) {+ this.contextRequestVersions[isMention ? 1 : 0]++;+ element.querySelectorAll(".b3-list-item__arrow[disabled]").forEach(item => {+ item.removeAttribute("disabled");+ });+ }+private updateBottomBacklinkSpacing() {if (this.type !== "bottom") {return;@@ -621,14 +638,55 @@ export class Backlink extends Model {if (this.type !== "bottom") {return;}+ this.showingLoading = true;+ this.cancelContextRequests(this.tree.element, false);+ this.cancelContextRequests(this.mTree.element, true);this.editors.forEach(item => item.destroy());this.editors = [];const loadingHTML = '<div class="backlinkList__loading"><img width="32px" height="32px" src="/stage/loading-pure.svg"></div>';this.tree.element.innerHTML = loadingHTML;this.mTree.element.innerHTML = loadingHTML;+ this.element.querySelector(".listCount").textContent = "";+ this.element.querySelector(".listMCount").textContent = "";this.updateBottomBacklinkSpacing();}+ public prepareForBlock(blockId: string, rootId: string) {+ if (!this.showingLoading) {+ this.saveStatus();+ }+ this.requestID++;+ this.refreshQueued = false;+ this.searchQueued = false;+ this.dirty = false;+ this.ignoreFocusOnNextRefresh = false;+ this.notebookId = "";+ this.blockId = blockId;+ this.rootId = rootId;+ this.element.querySelector('.block__icon[data-type="refresh"] svg').classList.remove("fn__rotate");+ this.showBottomLoading();+ }++ private finishRequest(requestID: number) {+ if (this.destroyed || requestID !== this.requestID) {+ return false;+ }+ this.element.querySelector('.block__icon[data-type="refresh"] svg').classList.remove("fn__rotate");+ return true;+ }++ private runQueuedRequest() {+ if (this.refreshQueued) {+ this.refresh();+ return true;+ }+ if (this.searchQueued) {+ this.searchBacklinks();+ return true;+ }+ return false;+ }+public refresh() {const element = this.element.querySelector('.block__icon[data-type="refresh"] svg');if (!this.blockId) {@@ -636,7 +694,9 @@ export class Backlink extends Model {}if (element.classList.contains("fn__rotate")) {if (!this.refreshQueued && this.type === "bottom") {- this.saveStatus();+ if (!this.showingLoading) {+ this.saveStatus();+ }this.showBottomLoading();}this.refreshQueued = true;@@ -645,22 +705,36 @@ export class Backlink extends Model {this.refreshQueued = false;element.classList.add("fn__rotate");this.dirty = false;+ const requestID = ++this.requestID;+ let responseHandled = false;fetchPost("/api/ref/refreshBacklink", {id: this.blockId,}, () => {- if (this.destroyed) {+ responseHandled = true;+ if (!this.finishRequest(requestID)) {return;}- element.classList.remove("fn__rotate");- this.searchBacklinks();+ if (!this.runQueuedRequest()) {+ this.searchBacklinks();+ }+ }).finally(() => {+ if (responseHandled || !this.finishRequest(requestID)) {+ return;+ }+ if (!this.runQueuedRequest()) {+ // 即使重建索引失败,也重新读取已有的反链数据并释放加载状态。+ this.searchBacklinks();+ }});}private searchBacklinks(init = false) {const element = this.element.querySelector('.block__icon[data-type="refresh"] svg');if (element.classList.contains("fn__rotate")) {+ this.searchQueued = true;return;}+ this.searchQueued = false;element.classList.add("fn__rotate");this.dirty = false;this.ignoreFocusOnNextRefresh = false;@@ -685,22 +759,31 @@ export class Backlink extends Model {if (isEncryptedBox(notebookId)) {param.notebook = notebookId;}+ const requestID = ++this.requestID;+ let responseHandled = false;fetchPost("/api/ref/getBacklink2", param, response => {- if (this.destroyed || blockId !== this.blockId) {+ responseHandled = true;+ if (!this.finishRequest(requestID) || blockId !== this.blockId) {return;}- if (this.refreshQueued) {- element.classList.remove("fn__rotate");- this.refresh();+ if (!shouldRenderBacklinkResponse(this.refreshQueued, this.searchQueued)) {+ this.runQueuedRequest();return;}- if (!init) {+ if (shouldSaveBacklinkStatus(init, this.showingLoading)) {this.saveStatus();}this.render(response.data);if (this.type === "bottom" && this.dirty) {this.refreshIfVisible();}+ }).finally(() => {+ if (responseHandled || !this.finishRequest(requestID) || blockId !== this.blockId) {+ return;+ }+ if (!this.runQueuedRequest() && this.type === "bottom" && this.dirty) {+ this.refreshIfVisible();+ }});}@@ -757,6 +840,7 @@ export class Backlink extends Model {mk: ""};}+ this.showingLoading = false;this.editors.forEach(item => {item.destroy();diff --git a/app/src/layout/dock/backlinkRefresh.test.ts b/app/src/layout/dock/backlinkRefresh.test.tsindex 445d8b1db6..5708748ab6 100644--- a/app/src/layout/dock/backlinkRefresh.test.ts+++ b/app/src/layout/dock/backlinkRefresh.test.ts@@ -1,6 +1,10 @@import {describe, it} from "node:test";import * as assert from "node:assert/strict";-import {shouldForceBacklinkRefresh} from "./backlinkRefresh";+import {+ shouldForceBacklinkRefresh,+ shouldRenderBacklinkResponse,+ shouldSaveBacklinkStatus+} from "./backlinkRefresh";describe("shouldForceBacklinkRefresh", () => {it("refreshes reference changes in the owner document while it is focused", () => {@@ -12,3 +16,19 @@ describe("shouldForceBacklinkRefresh", () => {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);+ assert.equal(shouldRenderBacklinkResponse(true, false), false);+ assert.equal(shouldRenderBacklinkResponse(false, true), false);+ });+});++describe("shouldSaveBacklinkStatus", () => {+ it("does not replace saved state with the loading placeholder", () => {+ assert.equal(shouldSaveBacklinkStatus(false, false), true);+ assert.equal(shouldSaveBacklinkStatus(true, false), false);+ assert.equal(shouldSaveBacklinkStatus(false, true), false);+ });+});diff --git a/app/src/layout/dock/backlinkRefresh.ts b/app/src/layout/dock/backlinkRefresh.tsindex 8110070b3c..305d2a4e53 100644--- a/app/src/layout/dock/backlinkRefresh.ts+++ b/app/src/layout/dock/backlinkRefresh.ts@@ -1,3 +1,11 @@export const shouldForceBacklinkRefresh = (ownerRootID: string, changedRootID: string) => {return Boolean(ownerRootID && ownerRootID === changedRootID);};++export const shouldRenderBacklinkResponse = (refreshQueued: boolean, searchQueued: boolean) => {+ return !refreshQueued && !searchQueued;+};++export const shouldSaveBacklinkStatus = (init: boolean, showingLoading: boolean) => {+ return !init && !showingLoading;+};