Skip to content

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

:art: https://github.com/siyuan-note/siyuan/issues/17830

Vanessaf1232c18538d
diff --git a/app/src/assets/scss/business/_av.scss b/app/src/assets/scss/business/_av.scss
index a3a9c4fbd5..7efeaff5ab 100644
--- a/app/src/assets/scss/business/_av.scss
+++ b/app/src/assets/scss/business/_av.scss
@@ -139,10 +139,6 @@
overflow: auto hidden;
}
- &__trim-spacer {
- pointer-events: none;
- }
-
&__row {
display: flex;
border-bottom: 1px solid var(--b3-theme-surface-lighter);
diff --git a/app/src/protyle/render/av/gallery/render.ts b/app/src/protyle/render/av/gallery/render.ts
index 4fdcca45d4..6f3c8911c2 100644
--- a/app/src/protyle/render/av/gallery/render.ts
+++ b/app/src/protyle/render/av/gallery/render.ts
@@ -16,7 +16,7 @@ import {getPageSize} from "../groups";
import {activeBlur} from "../../../../mobile/util/keyboardToolbar";
/// #endif
import {renderKanban} from "../kanban/render";
-import {bindAutoLoadScroll} from "../trim";
+import {initVirtualScroll} from "../virtualScroll";
interface IIds {
groupId: string,
@@ -275,7 +275,7 @@ export const afterRenderGallery = (options: ITableOptions) => {
/// #endif
}
});
- bindAutoLoadScroll(options);
+ initVirtualScroll(options);
};
export const renderGallery = async (options: {
diff --git a/app/src/protyle/render/av/render.ts b/app/src/protyle/render/av/render.ts
index 1e7bc55f0f..f4f301d73d 100644
--- a/app/src/protyle/render/av/render.ts
+++ b/app/src/protyle/render/av/render.ts
@@ -1,11 +1,11 @@
import {fetchSyncPost} from "../../../util/fetch";
import {getColIconByType} from "./col";
import {Constants} from "../../../constants";
-import {addDragFill, cellScrollIntoView, popTextCell, renderCell} from "./cell";
+import {addDragFill, cellScrollIntoView, popTextCell} from "./cell";
import {unicode2Emoji} from "../../../emoji";
import {focusBlock} from "../../util/selection";
import {hasClosestBlock, hasClosestByAttribute, hasClosestByClassName} from "../../util/hasClosest";
-import {stickyRow, updateHeader} from "./row";
+import {getRowHTML, stickyRow, updateHeader} from "./row";
import {getCalcValue} from "./calc";
import {renderAVAttribute} from "./blockAttr";
import {addClearButton} from "../../../util/addClearButton";
@@ -23,7 +23,7 @@ import {showMessage} from "../../../dialog/message";
import {activeBlur} from "../../../mobile/util/keyboardToolbar";
/// #endif
import {renderKanban} from "./kanban/render";
-import {bindAutoLoadScroll} from "./trim";
+import {initVirtualScroll} from "./virtualScroll";
interface IIds {
groupId: string,
@@ -181,7 +181,7 @@ style="width: ${column.width || "200px"}">${getCalcValue(column) || `<svg><use x
calcHTML += "</div>";
}
});
- contentHTML += `<div class="block__icons" style="min-height: auto">
+ contentHTML += `<div class="block__icons" style="min-height: auto" data-pinindex="${pinIndex}">
<div class="block__icon block__icon--show" data-type="av-header-more"><svg><use xlink:href="#iconMore"></use></svg></div>
<div class="fn__space"></div>
<div class="block__icon block__icon--show ariaLabel" aria-label="${window.siyuan.languages.newCol}" data-type="av-header-add" data-position="4south"><svg><use xlink:href="#iconAdd"></use></svg></div>
@@ -193,36 +193,7 @@ style="width: ${column.width || "200px"}">${getCalcValue(column) || `<svg><use x
e.setAttribute(Constants.ATTRIBUTE_V_SCROLL, "true");
return true;
}
- contentHTML += `<div class="av__row" data-id="${row.id}">`;
- if (pinIndex > -1) {
- contentHTML += '<div class="av__colsticky"><div class="av__firstcol"><svg><use xlink:href="#iconUncheck"></use></svg></div>';
- } else {
- contentHTML += '<div class="av__colsticky"><div class="av__firstcol"><svg><use xlink:href="#iconUncheck"></use></svg></div></div>';
- }
-
- row.cells.forEach((cell, index) => {
- if (data.columns[index].hidden) {
- return;
- }
- // https://github.com/siyuan-note/siyuan/issues/10262
- let checkClass = "";
- if (cell.valueType === "checkbox") {
- checkClass = cell.value?.checkbox?.checked ? " av__cell-check" : " av__cell-uncheck";
- }
- contentHTML += `<div class="av__cell${checkClass}" data-id="${cell.id}" data-col-id="${data.columns[index].id}"
-data-wrap="${data.columns[index].wrap}"
-data-dtype="${data.columns[index].type}"
-${cell.value?.isDetached ? ' data-detached="true"' : ""}
-style="width: ${data.columns[index].width || "200px"};
-${cell.valueType === "number" ? "text-align: right;" : ""}
-${cell.bgColor ? `background-color:${cell.bgColor};` : ""}
-${cell.color ? `color:${cell.color};` : ""}">${renderCell(cell.value, rowIndex, data.showIcon)}</div>`;
-
- if (pinIndex === index) {
- contentHTML += "</div>";
- }
- });
- contentHTML += "<div></div></div>";
+ contentHTML += getRowHTML(data, row, rowIndex, pinIndex);
});
return `${contentHTML}<div class="av__row--util${data.rowCount > data.rows.length ? " av__readonly--show" : ""}">
<div class="av__colsticky">
@@ -452,7 +423,7 @@ const afterRenderTable = (options: ITableOptions) => {
/// #endif
}
});
- bindAutoLoadScroll(options);
+ initVirtualScroll(options);
};
export const avRender = async (element: Element, protyle: IProtyle, cb?: (data: IAV) => void, renderAll = true, avData?: IAV) => {
diff --git a/app/src/protyle/render/av/row.ts b/app/src/protyle/render/av/row.ts
index a280f5db83..54b88d4247 100644
--- a/app/src/protyle/render/av/row.ts
+++ b/app/src/protyle/render/av/row.ts
@@ -10,6 +10,40 @@ import {insertGalleryItemAnimation} from "./gallery/item";
import {clearSelect} from "../../util/clear";
import {isCustomAttr} from "./blockAttr";
+export const getRowHTML = (data: IAVView, row: IAVRow, rowIndex: number, pinIndex: number) => {
+ let html = `<div class="av__row" data-index="${rowIndex}" data-id="${row.id}">`;
+ if (pinIndex > -1) {
+ html += '<div class="av__colsticky"><div class="av__firstcol"><svg><use xlink:href="#iconUncheck"></use></svg></div>';
+ } else {
+ html += '<div class="av__colsticky"><div class="av__firstcol"><svg><use xlink:href="#iconUncheck"></use></svg></div></div>';
+ }
+
+ row.cells.forEach((cell, index) => {
+ const column = (data as IAVTable).columns[index];
+ if (column.hidden) {
+ return;
+ }
+ // https://github.com/siyuan-note/siyuan/issues/10262
+ let checkClass = "";
+ if (cell.valueType === "checkbox") {
+ checkClass = cell.value?.checkbox?.checked ? " av__cell-check" : " av__cell-uncheck";
+ }
+ html += `<div class="av__cell${checkClass}" data-id="${cell.id}" data-col-id="${column.id}"
+data-wrap="${column.wrap}"
+data-dtype="${column.type}"
+${cell.value?.isDetached ? ' data-detached="true"' : ""}
+style="width: ${column.width || "200px"};
+${cell.valueType === "number" ? "text-align: right;" : ""}
+${cell.bgColor ? `background-color:${cell.bgColor};` : ""}
+${cell.color ? `color:${cell.color};` : ""}">${renderCell(cell.value, rowIndex, data.showIcon)}</div>`;
+
+ if (pinIndex === index) {
+ html += "</div>";
+ }
+ });
+ return html + "<div></div></div>";
+};
+
export const getFieldIdByCellElement = (cellElement: Element, viewType: TAVView): string => {
if (isCustomAttr(cellElement)) {
return cellElement.getAttribute("data-row-id");
diff --git a/app/src/protyle/render/av/trim.ts b/app/src/protyle/render/av/trim.ts
deleted file mode 100644
index 15b8873a9d..0000000000
--- a/app/src/protyle/render/av/trim.ts
+++ /dev/null
@@ -1,143 +0,0 @@
-import {avRender} from "./render";
-import {Constants} from "../../../constants";
-
-const dataStore = new Map<string, {
- protyle: IProtyle;
- data: IAV;
-}>();
-
-const trimPending = new WeakSet<HTMLElement>();
-
-const doTrim = (blockElement: HTMLElement, elementRect: DOMRect, protyle: IProtyle): void => {
- const bodies = blockElement.querySelectorAll(".av__body:not(.fn__none)") as NodeListOf<HTMLElement>;
-
- bodies.forEach((bodyEl: HTMLElement) => {
- const dataRows = Array.from(bodyEl.querySelectorAll(
- ".av__row:not(.av__row--header):not(.av__row--footer):not(.av__row--util)"
- )) as HTMLElement[];
-
- const viewportHeight = elementRect.height;
- const buffer = viewportHeight * 3;
- const topLimit = elementRect.top - buffer;
- const bottomLimit = elementRect.bottom + buffer;
-
- const toRemoveAbove: HTMLElement[] = [];
- const toRemoveBelow: HTMLElement[] = [];
- let firstVisibleIndex = -1;
- let lastVisibleIndex = -1;
-
- for (let i = 0; i < dataRows.length; i++) {
- const rect = dataRows[i].getBoundingClientRect();
- if (rect.bottom < topLimit) {
- toRemoveAbove.push(dataRows[i]);
- } else if (rect.top > bottomLimit) {
- toRemoveBelow.push(dataRows[i]);
- } else {
- if (firstVisibleIndex === -1) {
- firstVisibleIndex = i;
- }
- lastVisibleIndex = i;
- }
- }
-
- if (toRemoveAbove.length === 0 && toRemoveBelow.length === 0) {
- return;
- }
-
- const existingTopSpacer = bodyEl.querySelector(".av__trim-spacer--top") as HTMLElement;
- const existingBottomSpacer = bodyEl.querySelector(".av__trim-spacer--bottom") as HTMLElement;
-
- if (existingTopSpacer) {
- const spacerRect = existingTopSpacer.getBoundingClientRect();
- if (spacerRect.bottom > elementRect.top + viewportHeight) {
- blockElement.removeAttribute("data-render");
- avRender(blockElement, protyle);
- return;
- }
- }
- if (existingBottomSpacer) {
- const spacerRect = existingBottomSpacer.getBoundingClientRect();
- if (spacerRect.top < elementRect.bottom - viewportHeight) {
- blockElement.removeAttribute("data-render");
- avRender(blockElement, protyle);
- return;
- }
- }
-
- const header = bodyEl.querySelector(".av__row--header") as HTMLElement;
- const headerBottom = header ? header.offsetTop + header.offsetHeight : 0;
-
- if (toRemoveAbove.length > 0 && firstVisibleIndex >= 0) {
- const firstVisibleRow = dataRows[firstVisibleIndex];
- if (firstVisibleRow) {
- const totalGap = firstVisibleRow.offsetTop - headerBottom;
-
- if (existingTopSpacer) {
- existingTopSpacer.style.height = totalGap + "px";
- } else if (totalGap > 0) {
- const topSpacer = document.createElement("div");
- topSpacer.className = "av__trim-spacer av__trim-spacer--top";
- topSpacer.style.height = totalGap + "px";
- topSpacer.style.width = "100%";
- topSpacer.style.flexShrink = "0";
- if (header) {
- header.after(topSpacer);
- } else {
- bodyEl.prepend(topSpacer);
- }
- }
- }
- }
-
- if (toRemoveBelow.length > 0 && lastVisibleIndex >= 0) {
- const lastVisibleRow = dataRows[lastVisibleIndex];
- const footer = bodyEl.querySelector(".av__row--footer") as HTMLElement;
- const util = bodyEl.querySelector(".av__row--util") as HTMLElement;
- const endRef = footer || util;
-
- if (lastVisibleRow && endRef) {
- const totalGap = endRef.offsetTop - lastVisibleRow.offsetTop - lastVisibleRow.offsetHeight;
-
- if (existingBottomSpacer) {
- existingBottomSpacer.style.height = totalGap + "px";
- } else if (totalGap > 0) {
- const bottomSpacer = document.createElement("div");
- bottomSpacer.className = "av__trim-spacer av__trim-spacer--bottom";
- bottomSpacer.style.height = totalGap + "px";
- bottomSpacer.style.width = "100%";
- bottomSpacer.style.flexShrink = "0";
- endRef.before(bottomSpacer);
- }
- }
- }
-
- toRemoveAbove.forEach(row => row.remove());
- toRemoveBelow.forEach(row => row.remove());
- });
-};
-
-export const trimAVRows = (blockElement: HTMLElement, elementRect: DOMRect, protyle: IProtyle, isUp:boolean): void => {
- if (blockElement.getAttribute(Constants.ATTRIBUTE_V_SCROLL) !== "true") {
- return;
- }
- if (trimPending.has(blockElement)) {
- return;
- }
- trimPending.add(blockElement);
- requestAnimationFrame(() => {
- trimPending.delete(blockElement);
- doTrim(blockElement, elementRect, protyle);
- });
-};
-
-export const bindAutoLoadScroll = (options: {
- protyle: IProtyle,
- blockElement: HTMLElement,
- data: IAV,
-}): void => {
- dataStore.set(options.blockElement.getAttribute("data-av-id") +
- options.blockElement.getAttribute("custom-sy-av-view"), {
- protyle: options.protyle,
- data: options.data,
- });
-};
diff --git a/app/src/protyle/render/av/virtualScroll.ts b/app/src/protyle/render/av/virtualScroll.ts
new file mode 100644
index 0000000000..1a3783ebb2
--- /dev/null
+++ b/app/src/protyle/render/av/virtualScroll.ts
@@ -0,0 +1,145 @@
+import {Constants} from "../../../constants";
+import {getRowHTML} from "./row";
+
+const BUFFER_RATIO = 3;
+
+interface IBodyState {
+ renderedStart: number;
+ renderedEnd: number;
+ view: IAVView;
+ pinIndex: number;
+}
+
+const dataStore = new Map<string, {
+ protyle: IProtyle;
+ data: IAV;
+}>();
+const bodyStates = new WeakMap<HTMLElement, IBodyState>();
+const trimPending = new WeakSet<HTMLElement>();
+let lastScrollTop: number;
+
+const doTrim = (blockElement: HTMLElement, elementRect: DOMRect): void => {
+ const viewportHeight = elementRect.bottom - elementRect.top;
+ const buffer = viewportHeight * BUFFER_RATIO;
+ const topLimit = elementRect.top - buffer;
+ const bottomLimit = elementRect.bottom + buffer;
+ const blockRect = blockElement.getBoundingClientRect();
+
+ const protyle = dataStore.get(blockElement.getAttribute("data-av-id") + blockElement.getAttribute(Constants.CUSTOM_SY_AV_VIEW)).protyle;
+ const isScrollingUp = lastScrollTop && lastScrollTop > protyle.contentElement.scrollTop;
+ lastScrollTop = protyle.contentElement.scrollTop;
+
+ if ((blockRect.bottom < elementRect.top && !isScrollingUp) || (blockRect.top > elementRect.bottom && isScrollingUp)) {
+ return;
+ }
+
+ const bodies = blockElement.querySelectorAll(".av__body:not(.fn__none)") as NodeListOf<HTMLElement>;
+ bodies.forEach((bodyEl: HTMLElement) => {
+ const state = bodyStates.get(bodyEl);
+ const dataRows = (state.view as IAVTable).rows;
+ const currentRows = bodyEl.querySelectorAll(".av__row:not(.av__row--header):not(.av__row--footer):not(.av__row--util)") as NodeListOf<HTMLElement>;
+
+ let firstVisibleIndex: number;
+ let lastVisibleIndex: number;
+ const toRemoveAbove: HTMLElement[] = [];
+ const toRemoveBelow: HTMLElement[] = [];
+
+ for (let i = 0; i < currentRows.length; i++) {
+ const rect = currentRows[i].getBoundingClientRect();
+ if (rect.top > topLimit) {
+ if (typeof firstVisibleIndex === "undefined") {
+ firstVisibleIndex = parseInt(currentRows[i].getAttribute("data-index"));
+ }
+ } else {
+ if (!isScrollingUp) {
+ toRemoveAbove.push(currentRows[i]);
+ }
+ }
+ if (rect.bottom < bottomLimit) {
+ lastVisibleIndex = parseInt(currentRows[i].getAttribute("data-index"));
+ } else {
+ if (isScrollingUp) {
+ toRemoveBelow.push(currentRows[i]);
+ }
+ }
+ if (i === currentRows.length - 1 && !isScrollingUp && rect.bottom < bottomLimit) {
+ lastVisibleIndex = Math.min(state.renderedEnd + Math.ceil((bottomLimit - rect.bottom) / 36), dataRows.length - 1); // 行高
+ }
+ if (i === 0 && isScrollingUp && rect.top > topLimit) {
+ firstVisibleIndex = Math.max(0, state.renderedStart - Math.ceil((rect.top - topLimit) / 36));
+ }
+ }
+ toRemoveBelow.forEach(row => row.remove());
+ if (!isScrollingUp) {
+ if (toRemoveAbove.length > 0) {
+ toRemoveAbove.forEach(row => row.remove());
+ state.renderedStart = state.renderedStart + toRemoveAbove.length;
+ }
+ if (lastVisibleIndex > state.renderedEnd) {
+ let rowsHTML = "";
+ for (let i = state.renderedEnd; i < lastVisibleIndex; i++) {
+ rowsHTML += getRowHTML(state.view, dataRows[i], i, state.pinIndex);
+ }
+ bodyEl.querySelector(".av__row--util").insertAdjacentHTML("beforebegin", rowsHTML);
+ state.renderedEnd = lastVisibleIndex;
+ }
+ } else {
+ if (toRemoveAbove.length > 0) {
+ toRemoveAbove.forEach(row => row.remove());
+ state.renderedEnd = state.renderedEnd - toRemoveAbove.length;
+ }
+ if (firstVisibleIndex < state.renderedStart) {
+ let rowsHTML = "";
+ for (let i = firstVisibleIndex; i < state.renderedStart; i++) {
+ rowsHTML += getRowHTML(state.view, dataRows[i], i, state.pinIndex);
+ }
+ bodyEl.querySelector(".av__row--header").insertAdjacentHTML("afterend", rowsHTML);
+ state.renderedStart = firstVisibleIndex;
+ }
+ }
+ bodyStates.set(bodyEl, state);
+ });
+};
+
+const getBodyData = (bodyEl: HTMLElement) => {
+ const avEl = bodyEl.closest(".av") as HTMLElement;
+ if (!avEl) return null;
+ const stored = dataStore.get(avEl.getAttribute("data-av-id") + avEl.getAttribute(Constants.CUSTOM_SY_AV_VIEW));
+ if (!stored) return null;
+
+ const groupId = bodyEl.dataset.groupId;
+ return groupId ? stored.data.view.groups.find((g: IAVView) => g.id === groupId) : stored.data.view;
+};
+
+export const trimAVRows = (blockElement: HTMLElement, elementRect: DOMRect): void => {
+ if (blockElement.getAttribute(Constants.ATTRIBUTE_V_SCROLL) !== "true" || trimPending.has(blockElement)) {
+ return;
+ }
+ trimPending.add(blockElement);
+ requestAnimationFrame(() => {
+ trimPending.delete(blockElement);
+ doTrim(blockElement, elementRect);
+ });
+};
+
+export const initVirtualScroll = (options: {
+ protyle: IProtyle,
+ blockElement: HTMLElement,
+ data: IAV,
+}): void => {
+ dataStore.set(options.blockElement.getAttribute("data-av-id") +
+ options.blockElement.getAttribute(Constants.CUSTOM_SY_AV_VIEW), {
+ protyle: options.protyle,
+ data: options.data,
+ });
+
+ options.blockElement.querySelectorAll(".av__body").forEach((bodyEl: HTMLElement) => {
+ const state: IBodyState = {
+ renderedStart: 0,
+ pinIndex: parseInt(bodyEl.querySelector(".av__row--header > .block__icons")?.getAttribute("data-pinindex")),
+ renderedEnd: bodyEl.querySelectorAll(".av__row:not(.av__row--header):not(.av__row--footer):not(.av__row--util)").length - 1,
+ view: getBodyData(bodyEl),
+ };
+ bodyStates.set(bodyEl, state);
+ });
+};
diff --git a/app/src/protyle/scroll/event.ts b/app/src/protyle/scroll/event.ts
index e456b459c5..f51d0060a7 100644
--- a/app/src/protyle/scroll/event.ts
+++ b/app/src/protyle/scroll/event.ts
@@ -5,7 +5,7 @@ import {onGet} from "../util/onGet";
import {isMobile} from "../../util/functions";
import {hasClosestBlock, hasClosestByClassName} from "../util/hasClosest";
import {stickyRow} from "../render/av/row";
-import {trimAVRows} from "../render/av/trim";
+import {trimAVRows} from "../render/av/virtualScroll";
let getIndexTimeout: number;
export const scrollEvent = (protyle: IProtyle, element: HTMLElement) => {
@@ -27,7 +27,7 @@ export const scrollEvent = (protyle: IProtyle, element: HTMLElement) => {
return;
}
stickyRow(item, elementRect, "all");
- trimAVRows(item, elementRect, protyle, protyle.scroll.lastScrollTop > element.scrollTop);
+ trimAVRows(item, elementRect);
});
if (!protyle.element.classList.contains("block__edit") && !isMobile()) {