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

Daniel5c5f177c7793
diff --git a/app/src/protyle/render/av/action.ts b/app/src/protyle/render/av/action.ts
index 3b3fc8c4e7..5f109794dc 100644
--- a/app/src/protyle/render/av/action.ts
+++ b/app/src/protyle/render/av/action.ts
@@ -368,10 +368,6 @@ 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");
diff --git a/app/src/protyle/render/av/row.ts b/app/src/protyle/render/av/row.ts
index bed8378736..72938abeb1 100644
--- a/app/src/protyle/render/av/row.ts
+++ b/app/src/protyle/render/av/row.ts
@@ -20,7 +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";
+import {getAVSelectStat, getAvBodyData, resetAVRowSelect, updateAVRowSelect} from "./virtualScroll";
export const getRowHTML = (options: {
data: IAVView
@@ -236,19 +236,25 @@ export const selectRow = (checkElement: Element, type: "toggle" | "select" | "un
resetAVRowSelect(bodyElement, []);
}
} else {
- const allRowIds: string[] = [];
+ // 全选:范围以当前已加载的分页行(dataStore 的 view.rows)为准,而非仅 DOM 内已渲染的行。
+ // 虚拟滚动下被 trim 掉的行 ID 一并存入快照,回填时由 restoreSelect 恢复高亮。
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);
- }
}
});
+ const allRowIds: string[] = [];
if (bodyElement) {
+ const view = getAvBodyData(bodyElement) as IAVTable;
+ if (view?.rows) {
+ view.rows.forEach((row: IAVRow) => {
+ if (row.id) {
+ allRowIds.push(row.id);
+ }
+ });
+ }
resetAVRowSelect(bodyElement, allRowIds);
}
}
@@ -277,10 +283,13 @@ export const updateHeader = (rowElement: HTMLElement) => {
if (!blockElement) {
return;
}
- const selectCount = rowElement.parentElement.querySelectorAll(".av__row--select:not(.av__row--header)").length;
- const count = rowElement.parentElement.querySelectorAll(".av__row:not(.av__row--header)").length;
+ const bodyElement = rowElement.parentElement as HTMLElement;
+ // 虚拟滚动下 DOM 内只有渲染窗口的行,直接计数会低估;优先用选中快照与已加载行总数。
+ const stat = getAVSelectStat(bodyElement);
+ const selectCount = stat ? stat.selectCount : bodyElement.querySelectorAll(".av__row--select:not(.av__row--header)").length;
+ const count = stat ? stat.loadedCount : bodyElement.querySelectorAll(".av__row:not(.av__row--header)").length;
- const headElement = rowElement.parentElement.firstElementChild;
+ const headElement = bodyElement.firstElementChild;
const headUseElement = headElement.querySelector("use");
if (count === selectCount && count !== 0) {
@@ -295,13 +304,12 @@ export const updateHeader = (rowElement: HTMLElement) => {
}
const counterElement = blockElement.querySelector(".av__counter");
- const allCount = blockElement.querySelectorAll(".av__row--select:not(.av__row--header)").length;
- if (allCount === 0) {
+ if (selectCount === 0) {
counterElement.classList.add("fn__none");
return;
}
counterElement.classList.remove("fn__none");
- counterElement.innerHTML = `${allCount} ${window.siyuan.languages.selected}`;
+ counterElement.innerHTML = `${selectCount} ${window.siyuan.languages.selected}`;
};
export const setPage = (blockElement: Element) => {
diff --git a/app/src/protyle/render/av/virtualScroll.ts b/app/src/protyle/render/av/virtualScroll.ts
index 5b8fa402cf..345e4f0351 100644
--- a/app/src/protyle/render/av/virtualScroll.ts
+++ b/app/src/protyle/render/av/virtualScroll.ts
@@ -337,6 +337,21 @@ export const resetAVRowSelect = (bodyEl: HTMLElement, rowIds: string[]): void =>
state.selectedRowIds = new Set(rowIds);
};
+// 返回某 body 的选中统计,供虚拟滚动场景下 updateHeader 显示真实计数。
+// 虚拟滚动时 DOM 内只有渲染窗口的行,直接查 DOM 会低估选中数;此处改用 selectedRowIds 快照与
+// 已加载分页行总数(state.view.rows)计算。非虚拟滚动(无 state)时返回 null 表示回退到 DOM 计数。
+export const getAVSelectStat = (bodyEl: HTMLElement): { selectCount: number, loadedCount: number } | null => {
+ const state = bodyStates.get(bodyEl);
+ if (!state || !state.selectedRowIds) {
+ return null;
+ }
+ const dataRows = state.view ? ((state.view as IAVTable).rows || (state.view as IAVKanban).cards || []) : [];
+ return {
+ selectCount: state.selectedRowIds.size,
+ loadedCount: dataRows.length,
+ };
+};
+
export const trimAVRows = (blockElement: HTMLElement, elementRect: DOMRect): void => {
if (blockElement.getAttribute(Constants.ATTRIBUTE_V_SCROLL) !== "true" || trimPending.has(blockElement)) {
return;