Skip to content

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

:bug: Issues related to database scrolling loading https://github.com/siyuan-note/siyuan/issues/18010

Daniel9dcfb37851ca
diff --git a/app/src/protyle/render/av/action.ts b/app/src/protyle/render/av/action.ts
index af4d355caf..3b3fc8c4e7 100644
--- a/app/src/protyle/render/av/action.ts
+++ b/app/src/protyle/render/av/action.ts
@@ -17,6 +17,7 @@ import {
} from "./cell";
import {addCol, getColIconByType, showColMenu} from "./col";
import {deleteRow, duplicateRows, insertRows, selectRow, setPageSize, updateHeader} from "./row";
+import {resetAVRowSelect, updateAVRowSelect} from "./virtualScroll";
import {emitOpenMenu} from "../../../plugin/EventBus";
import {openMenuPanel} from "./openMenuPanel";
import {hintRef} from "../../hint/extend";
@@ -180,6 +181,10 @@ export const avClick = (protyle: IProtyle, event: MouseEvent & { target: HTMLEle
item.querySelector(".av__firstcol use").setAttribute("xlink:href", "#iconUncheck");
item.classList.remove("av__row--select");
});
+ // 同步清空虚拟滚动选中快照,避免被 trim 掉的行回填后仍带选中态
+ blockElement.querySelectorAll(".av__body").forEach((bodyEl: HTMLElement) => {
+ resetAVRowSelect(bodyEl, []);
+ });
updateHeader(rowElement);
popTextCell(protyle, [target]);
}
@@ -363,10 +368,19 @@ export const avContextmenu = (protyle: IProtyle, rowElement: HTMLElement, positi
if (avType === "table") {
if (!rowElement.classList.contains("av__row--select")) {
clearSelect(["row"], blockElement);
+ // clearSelect 清空了所有行选中,同步清空虚拟滚动选中快照
+ blockElement.querySelectorAll(".av__body").forEach((bodyEl: HTMLElement) => {
+ resetAVRowSelect(bodyEl, []);
+ });
}
clearSelect(["cell"], blockElement);
rowElement.classList.add("av__row--select");
rowElement.querySelector(".av__firstcol use").setAttribute("xlink:href", "#iconCheck");
+ const bodyElement = hasClosestByClassName(rowElement, "av__body") as HTMLElement;
+ const rowId = rowElement.getAttribute("data-id");
+ if (bodyElement && rowId) {
+ updateAVRowSelect(bodyElement, rowId, true);
+ }
updateHeader(rowElement);
} else {
if (!rowElement.classList.contains("av__gallery-item--select")) {
diff --git a/app/src/protyle/render/av/row.ts b/app/src/protyle/render/av/row.ts
index bdecf9d601..bed8378736 100644
--- a/app/src/protyle/render/av/row.ts
+++ b/app/src/protyle/render/av/row.ts
@@ -20,6 +20,7 @@ import {getColIconByType, getColNameByType} from "./col";
import {unicode2Emoji} from "../../../emoji";
import {escapeAttr} from "../../../util/escape";
import {getCompressURL} from "../../../util/image";
+import {resetAVRowSelect, updateAVRowSelect} from "./virtualScroll";
export const getRowHTML = (options: {
data: IAVView
@@ -220,6 +221,7 @@ export const selectRow = (checkElement: Element, type: "toggle" | "select" | "un
return;
}
const useElement = checkElement.querySelector("use");
+ const bodyElement = hasClosestByClassName(rowElement, "av__body") as HTMLElement;
if (rowElement.classList.contains("av__row--header") || type === "unselectAll") {
if ("#iconCheck" === useElement.getAttribute("xlink:href") || type === "unselectAll") {
rowElement.parentElement.querySelectorAll(".av__firstcol").forEach(item => {
@@ -229,22 +231,41 @@ export const selectRow = (checkElement: Element, type: "toggle" | "select" | "un
rowItemElement.classList.remove("av__row--select");
}
});
+ // 全不选:清空选中快照,避免被 trim 掉的行回填后仍带选中态
+ if (bodyElement) {
+ resetAVRowSelect(bodyElement, []);
+ }
} else {
+ const allRowIds: string[] = [];
rowElement.parentElement.querySelectorAll(".av__firstcol").forEach(item => {
item.querySelector("use").setAttribute("xlink:href", "#iconCheck");
const rowItemElement = hasClosestByClassName(item, "av__row");
if (rowItemElement) {
rowItemElement.classList.add("av__row--select");
+ const id = rowItemElement.getAttribute("data-id");
+ if (id) {
+ allRowIds.push(id);
+ }
}
});
+ if (bodyElement) {
+ resetAVRowSelect(bodyElement, allRowIds);
+ }
}
} else {
+ const rowId = rowElement.getAttribute("data-id");
if (type === "select" || (useElement.getAttribute("xlink:href") === "#iconUncheck" && type === "toggle")) {
rowElement.classList.add("av__row--select");
useElement.setAttribute("xlink:href", "#iconCheck");
+ if (bodyElement && rowId) {
+ updateAVRowSelect(bodyElement, rowId, true);
+ }
} else if (type === "unselect" || (useElement.getAttribute("xlink:href") === "#iconCheck" && type === "toggle")) {
rowElement.classList.remove("av__row--select");
useElement.setAttribute("xlink:href", "#iconUncheck");
+ if (bodyElement && rowId) {
+ updateAVRowSelect(bodyElement, rowId, false);
+ }
}
}
focusBlock(hasClosestBlock(rowElement) as HTMLElement);
diff --git a/app/src/protyle/render/av/virtualScroll.ts b/app/src/protyle/render/av/virtualScroll.ts
index 91f2d7b608..5b8fa402cf 100644
--- a/app/src/protyle/render/av/virtualScroll.ts
+++ b/app/src/protyle/render/av/virtualScroll.ts
@@ -12,6 +12,9 @@ interface IBodyState {
// 缓存的行高,避免每帧读 currentRows[0].offsetHeight(强制重排来源)。
// 表格行高在渲染后基本稳定,用缓存值做外推/分页计算即可,少量偏差不影响正确性。
rowHeight?: number;
+ // 选中行 ID 快照。trim 会移除/回填行 DOM,而选中高亮(av__row--select)是纯运行时状态、
+ // getRowHTML 不携带,故在每次 trim 处理前从现存 DOM 同步,回填后据此恢复。
+ selectedRowIds?: Set<string>;
}
const dataStore = new Map<string, {
@@ -61,6 +64,26 @@ const doTrim = (blockElement: HTMLElement, elementRect: DOMRect): void => {
}
try {
const spacerElement = bodyEl.querySelector(".av__spacer") as HTMLElement;
+ // 选中高亮是纯 DOM 运行时状态、getRowHTML 不携带。selectedRowIds 由 selectRow 等变更点
+ // 维护(见 updateAVRowSelect),trim 回填行后据此恢复,避免虚拟滚动丢失选中态。
+ if (!state.selectedRowIds) {
+ state.selectedRowIds = new Set();
+ }
+ // 给回填的行恢复选中态:遍历 body 内现存数据行,命中 selectedRowIds 的补回高亮类与选中图标。
+ const restoreSelect = () => {
+ if (state.selectedRowIds.size === 0) {
+ return;
+ }
+ bodyEl.querySelectorAll(type === "table" ? ".av__row[data-id]" : ".av__gallery-item[data-id]").forEach((row: HTMLElement) => {
+ if (state.selectedRowIds.has(row.getAttribute("data-id"))) {
+ row.classList.add(type === "table" ? "av__row--select" : "av__gallery-item--select");
+ const use = row.querySelector(".av__firstcol use") as SVGUseElement;
+ if (use) {
+ use.setAttribute("xlink:href", "#iconCheck");
+ }
+ }
+ });
+ };
let firstVisibleIndex: number;
let lastVisibleIndex: number;
const toRemoveAbove: HTMLElement[] = [];
@@ -97,6 +120,7 @@ const doTrim = (blockElement: HTMLElement, elementRect: DOMRect): void => {
if (bottomElement && bottomElement.isConnected) {
bottomElement.insertAdjacentHTML("beforebegin", rowsHTML);
}
+ restoreSelect();
state.renderedStart = 0;
state.renderedEnd = newEnd;
state.topSpacerHeight = 0;
@@ -211,6 +235,7 @@ const doTrim = (blockElement: HTMLElement, elementRect: DOMRect): void => {
if (bottomElement && bottomElement.isConnected) {
bottomElement.insertAdjacentHTML("beforebegin", rowsHTML);
}
+ restoreSelect();
state.renderedEnd = lastVisibleIndex;
}
} else {
@@ -261,6 +286,7 @@ const doTrim = (blockElement: HTMLElement, elementRect: DOMRect): void => {
spacerElement.style.height = state.topSpacerHeight + "px";
}
state.renderedStart = firstVisibleIndex;
+ restoreSelect();
}
}
} finally {
@@ -284,6 +310,33 @@ export const getAvBodyData = (bodyEl: HTMLElement): IAVView | null => {
return getBodyData(bodyEl);
};
+// 同步选中行 ID 到虚拟滚动状态。选中高亮是纯 DOM 运行时状态,trim 会移除/回填行 DOM,
+// 若不在变更点维护一份 ID 快照,被 trim 掉的选中行回填后将永久丢失选中态。
+// selectRow 等所有变更选中态的入口在改完 DOM 后需调用:selected=true 记入、false 移除。
+export const updateAVRowSelect = (bodyEl: HTMLElement, rowId: string, selected: boolean): void => {
+ const state = bodyStates.get(bodyEl);
+ if (!state) {
+ return;
+ }
+ if (!state.selectedRowIds) {
+ state.selectedRowIds = new Set();
+ }
+ if (selected) {
+ state.selectedRowIds.add(rowId);
+ } else {
+ state.selectedRowIds.delete(rowId);
+ }
+};
+
+// 全量重置某 body 的选中行 ID 快照(全选/全不选/avRender 重渲后调用)。
+export const resetAVRowSelect = (bodyEl: HTMLElement, rowIds: string[]): void => {
+ const state = bodyStates.get(bodyEl);
+ if (!state) {
+ return;
+ }
+ state.selectedRowIds = new Set(rowIds);
+};
+
export const trimAVRows = (blockElement: HTMLElement, elementRect: DOMRect): void => {
if (blockElement.getAttribute(Constants.ATTRIBUTE_V_SCROLL) !== "true" || trimPending.has(blockElement)) {
return;
@@ -319,6 +372,14 @@ export const initVirtualScroll = (options: {
});
options.blockElement.querySelectorAll(".av__body").forEach((item: HTMLElement) => {
+ // 从现存 DOM 初始化选中行 ID 快照,重渲后保留选中态
+ const selectedRowIds = new Set<string>();
+ item.querySelectorAll(options.data.viewType === "table" ? ".av__row--select" : ".av__gallery-item--select").forEach((row: HTMLElement) => {
+ const id = row.getAttribute("data-id");
+ if (id) {
+ selectedRowIds.add(id);
+ }
+ });
if (options.data.viewType === "table") {
bodyStates.set(item, {
renderedStart: parseInt(item.querySelectorAll(".av__row")[1].getAttribute("data-index")),
@@ -326,6 +387,7 @@ export const initVirtualScroll = (options: {
renderedEnd: parseInt(item.querySelector(".av__row--util").previousElementSibling.getAttribute("data-index")),
view: getBodyData(item),
topSpacerHeight: item.querySelector(".av__spacer")?.clientHeight || 0,
+ selectedRowIds,
});
} else {
bodyStates.set(item, {
@@ -333,6 +395,7 @@ export const initVirtualScroll = (options: {
renderedEnd: parseInt(item.querySelector(".av__gallery-add").previousElementSibling.getAttribute("data-index")),
view: getBodyData(item),
topSpacerHeight: item.querySelector(".av__spacer")?.clientHeight || 0,
+ selectedRowIds,
});
}
});