Skip to content

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

:bug: Stabilize database item navigation after delayed rendering https://github.com/siyuan-note/siyuan/issues/12022

Danielddca975a296c
diff --git a/app/src/block/Panel.ts b/app/src/block/Panel.ts
index 1d3580774b..25a05c0256 100644
--- a/app/src/block/Panel.ts
+++ b/app/src/block/Panel.ts
@@ -18,7 +18,7 @@ import {resize} from "../protyle/util/resize";
import {checkFold} from "../util/noRelyPCFunction";
import {updateHotkeyAfterTip} from "../protyle/util/compatibility";
import {getTopBarHeight} from "../layout/getTopBarHeight";
-import {activateAVLocate} from "../protyle/render/av/locate";
+import {activateAVLocateWithRetry} from "../protyle/render/av/locate";
export class BlockPanel {
public element: HTMLElement;
@@ -192,7 +192,7 @@ export class BlockPanel {
after: (editor) => {
const refDef = this.refDefs[index];
if (refDef.avItemID) {
- activateAVLocate(editor.protyle, refDef.refID, {
+ activateAVLocateWithRetry(editor.protyle, refDef.refID, {
itemID: refDef.avItemID,
viewID: refDef.avViewID,
groupID: refDef.avGroupID,
diff --git a/app/src/protyle/render/av/locate.ts b/app/src/protyle/render/av/locate.ts
index b10000232b..6f58e8ff11 100644
--- a/app/src/protyle/render/av/locate.ts
+++ b/app/src/protyle/render/av/locate.ts
@@ -25,6 +25,7 @@ const queuedLocateRequests = new Map<string, {
activationToken?: symbol,
}>();
const renderTokens = new WeakMap<HTMLElement, symbol>();
+const renderedAVData = new WeakMap<HTMLElement, IAV>();
const highlightTokens = new WeakMap<HTMLElement, symbol>();
const highlightStates = new WeakMap<HTMLElement, {element: HTMLElement, className: string, timer: number}>();
@@ -85,6 +86,48 @@ export const isCurrentAVRender = (blockElement: HTMLElement, token: symbol) => {
return renderTokens.get(blockElement) === token;
};
+const getLocalAVLocateData = (data: IAV | undefined, request: IAVLocateRequest) => {
+ if (!data || (request.viewID && request.viewID !== data.viewID)) {
+ return;
+ }
+ const findTarget = (view: IAVTable | IAVGallery | IAVKanban, groupID = "") => {
+ const items = data.viewType === "table" ? (view as IAVTable).rows : (view as IAVGallery | IAVKanban).cards;
+ const localIndex = items?.findIndex(item => item.id === request.itemID) ?? -1;
+ if (localIndex < 0) {
+ return;
+ }
+ const offset = data.target?.status === "visible" && (data.target.groupID || "") === groupID ? data.target.offset : 0;
+ return {
+ status: "visible" as const,
+ itemID: request.itemID,
+ groupID: groupID || undefined,
+ index: offset + localIndex,
+ offset,
+ pageSize: view.pageSize,
+ };
+ };
+ const view = data.view as IAVTable | IAVGallery | IAVKanban;
+ let target: IAVRenderTarget | undefined;
+ if (view.groups?.length > 0) {
+ const groups = request.groupID ? [
+ ...view.groups.filter(group => group.id === request.groupID),
+ ...view.groups.filter(group => group.id !== request.groupID),
+ ] : view.groups;
+ for (const group of groups as Array<IAVTable | IAVGallery | IAVKanban>) {
+ target = findTarget(group, group.id);
+ if (target) {
+ break;
+ }
+ }
+ } else {
+ target = findTarget(view);
+ }
+ if (!target) {
+ return;
+ }
+ return {...data, target};
+};
+
export const queueAVLocateRequest = (blockID: string, request: IAVLocateRequest) => {
const previous = queuedLocateRequests.get(blockID);
if (previous) {
@@ -101,16 +144,30 @@ export const queueAVLocateRequest = (blockID: string, request: IAVLocateRequest)
export const activateAVLocate = (protyle: IProtyle, blockID: string, request?: IAVLocateRequest) => {
const blockElement = protyle?.wysiwyg.element.querySelector(`.av[data-node-id="${blockID}"]`) as HTMLElement;
- if (!request || !blockElement) {
+ if (!request || !blockElement || blockElement.getAttribute("data-render") !== "true") {
return false;
}
clearLocatedHighlight(blockElement);
locateRequests.set(blockElement, request);
blockElement.removeAttribute("data-render");
- import("./render").then(({avRender}) => avRender(blockElement, protyle));
+ const localData = getLocalAVLocateData(renderedAVData.get(blockElement), request);
+ import("./render").then(({avRender}) => avRender(blockElement, protyle, undefined, true, localData));
return true;
};
+export const activateAVLocateWithRetry = (protyle: IProtyle, blockID: string, request: IAVLocateRequest) => {
+ const timeout = performance.now() + locateQueueTimeout;
+ const retry = () => {
+ if (activateAVLocate(protyle, blockID, request)) {
+ return;
+ }
+ if (protyle.element.isConnected && performance.now() < timeout) {
+ window.setTimeout(retry, 50);
+ }
+ };
+ retry();
+};
+
export const activateQueuedAVLocate = (protyle: IProtyle, blockID: string) => {
const queued = queuedLocateRequests.get(blockID);
if (!queued || !protyle) {
@@ -181,6 +238,7 @@ export const getAVLocateParams = (blockElement: HTMLElement, enabled = true) =>
export const prepareAVLocate = (blockElement: HTMLElement, data: IAV, resetData: {
virtualData: { [key: string]: IAVVirtualData },
}) => {
+ renderedAVData.set(blockElement, data);
const request = getAVLocateRequest(blockElement);
if (!blockElement.isConnected || !request || !data.target || data.target.itemID !== request.itemID) {
return;
diff --git a/app/src/protyle/render/av/virtualScroll.ts b/app/src/protyle/render/av/virtualScroll.ts
index b8a7f620f4..b30cb2d92e 100644
--- a/app/src/protyle/render/av/virtualScroll.ts
+++ b/app/src/protyle/render/av/virtualScroll.ts
@@ -81,7 +81,8 @@ const doTrim = (blockElement: HTMLElement, elementRect: DOMRect): void => {
// 数据行数不超过 trim 有效范围(视口 + 上下 buffer)时不 trim(如看板中较短的分组),
// 全部渲染即可,避免短列因 trim 导致 spacer 抖动或全部移除后无法回填
const trimRange = viewportHeight + buffer * 2;
- if (dataRows.length <= Math.ceil(trimRange / Math.max(state.rowHeight || currentRows[0].offsetHeight, 1))) {
+ if (bodyEl.dataset.avLocateWindow !== "true" &&
+ dataRows.length <= Math.ceil(trimRange / Math.max(state.rowHeight || currentRows[0].offsetHeight, 1))) {
// 清理可能残留的 spacer 和状态,恢复全部渲染
const spacerEl = bodyEl.querySelector(".av__spacer") as HTMLElement;
if (spacerEl) {