Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/collections/board: implement virtualization
Adorian Doran1d2d39f6b3f3
diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsxindex 89d1617fdf..82fcc6baaf 100644--- a/apps/client/src/widgets/collections/board/column.tsx+++ b/apps/client/src/widgets/collections/board/column.tsx@@ -172,23 +172,26 @@ export default function Column({// made in the footer is shown by the scroll to the end and by its fade, and one made among the// others takes the place its field was standing in.//- // While a card is carried, the cards that move are those of the column it came from and those- // of any column the gap has stood in. The rest are left unmeasured, measuring one costing a- // layout of the whole board; paused rather than switched off, so a column the gap reaches in- // one step still knows where its cards stood and slides them from there.+ // While a card is carried, the cards that move are those of the column it came from, the one+ // the gap stands in, and the one it has just left, whose cards close up behind it. Every other+ // column is left unmeasured, measuring one costing a layout of the whole board; paused rather+ // than switched off, so a column the gap reaches in one step still knows where its cards stood+ // and slides them from there.+ //+ // Just left, rather than ever held: a column keeps whatever it last measured, so one the gap+ // passed through earlier has nothing more to say. Kept on, a column of thousands measures+ // again every time anything redraws the board for the rest of the gesture.//// A column being carried, and a column changing width, move no card inside any column, so// every column is left unmeasured for the length of either.const heldGap = useRef(false);- if (!draggedCard) {- heldGap.current = false;- } else if (dropIndex !== null) {- heldGap.current = true;- }+ const holdsGap = dropIndex !== null;+ const justLeftGap = heldGap.current && !holdsGap;+ heldGap.current = holdsGap;useFlip(contentRef, {selector: ".board-note",paused: draggedCard- ? column !== draggedCard.fromColumn && !heldGap.current+ ? column !== draggedCard.fromColumn && !holdsGap && !justLeftGap: !!draggedColumn || !!isResizing});const { handleDragOver, handleDragLeave, handleDrop } = useDragging({diff --git a/apps/client/src/widgets/collections/board/drag_measure.spec.ts b/apps/client/src/widgets/collections/board/drag_measure.spec.tsindex 0d06591369..57cae87402 100644--- a/apps/client/src/widgets/collections/board/drag_measure.spec.ts+++ b/apps/client/src/widgets/collections/board/drag_measure.spec.ts@@ -1,10 +1,14 @@-import { afterEach, describe, expect, it } from "vitest";+import { afterEach, beforeEach, describe, expect, it } from "vitest";import { cardInsertionIndex, columnAt } from "./drag_geometry";-import { measureBoard, toAreaY, toBoardX } from "./drag_measure";+import { forgetCardHeights, measureBoard, toAreaY, toBoardX } from "./drag_measure";let container: HTMLElement | undefined;+// What has been measured outlives one board, which is the point of it, so each test starts with+// nothing remembered.+beforeEach(forgetCardHeights);+afterEach(() => {container?.remove();container = undefined;@@ -38,6 +42,7 @@ function buildBoard({ scrollLeft = 200, areaScrollTop = 20, cardCounts = [ 2, 1for (let card = 0; card < cards; card++) {const note = document.createElement("div");note.className = "board-note";+ note.dataset.noteId = `note-${index}-${card}`;area.appendChild(note);// Stated where the card stands in the area's content, then drawn where that leaves it// on screen once the area has been scrolled.@@ -104,6 +109,22 @@ describe("measureBoard", () => {expect(measureBoard(buildBoard({ areaScrollTop: 90 })).columns[0].cards).toEqual(expected);});+ it("reads one card per column once it knows what the rest of them measure", () => {+ const board = buildBoard({ cardCounts: [ 3, 2 ] });+ const expected = measureBoard(board).columns.map((column) => column.cards);++ let reads = 0;+ for (const note of board.querySelectorAll<HTMLElement>(".board-note")) {+ const box = note.getBoundingClientRect.bind(note);+ note.getBoundingClientRect = () => { reads++; return box(); };+ }++ // Where a column's cards begin is still its own to say, so the first of them is read.+ // The four under it are placed by counting up from it instead.+ expect(measureBoard(board).columns.map((column) => column.cards)).toEqual(expected);+ expect(reads).toBe(2);+ });+it("hands back each column's card area, and counts a column holding none", () => {const board = buildBoard({ cardCounts: [ 0, 1 ] });diff --git a/apps/client/src/widgets/collections/board/drag_measure.ts b/apps/client/src/widgets/collections/board/drag_measure.tsindex be98711adc..c58f8e9be5 100644--- a/apps/client/src/widgets/collections/board/drag_measure.ts+++ b/apps/client/src/widgets/collections/board/drag_measure.ts@@ -62,11 +62,37 @@ export function toAreaY(area: HTMLElement, clientY: number): number {return clientY - area.getBoundingClientRect().top + area.scrollTop;}+/**+ * What each card measures, kept by note so that a column can be placed against without reading a+ * rectangle for every card in it.+ *+ * A card's height is settled by its content and by the column's width, and a column is a fixed+ * width the reader cannot change, so a height holds until the card itself changes. The one thing+ * that moves it is the width a phone gives a column, which follows the size of the window.+ */+const heights = new Map<string, number>();++/** What stands between two cards, which is one rule the board over and so is read once. */+let spacing: number | undefined;++/** Drops what has been measured, for a window whose size has changed under it. */+export function forgetCardHeights() {+ heights.clear();+ spacing = undefined;+}+/*** The cards of one column, in the area's content space.*- * The dragged card is measured with the rest: the index this leads to names a place in the list the- * board holds, which is the list a move is expressed against.+ * A card whose height is already known is placed by counting up from the one above it rather than+ * by reading a rectangle of its own, which on a column of thousands is the whole cost of a drag.+ * One that is not is read and remembered. The dragged card is counted with the rest, so the index+ * this leads to names a place in the list the board holds, which is the list a move is expressed+ * against.+ *+ * The gap standing open for the carried card holds the cards under it a place lower than they are+ * measured to be. What a place is counted against is the column without the drag's own doing in+ * it, so the room the gap takes is given back to everything below it.*/function measureCards(area: HTMLElement | null) {if (!area) {@@ -75,22 +101,53 @@ function measureCards(area: HTMLElement | null) {const top = area.getBoundingClientRect().top - area.scrollTop;const cards: CardBox[] = [];- // The gap standing open for the carried card holds the cards under it a place lower than they- // are measured to be. What a place is counted against is the column without the drag's own- // doing in it, so the room the gap takes is given back to everything below it.let gap = 0;+ /** Where the next card stands if it has to be counted rather than read. */+ let next = 0;+ /** The foot of the last card read, which the next one read settles the spacing against. */+ let foot: number | undefined;+ /** Whether where the column's cards begin has been read yet. */+ let anchored = false;for (const child of area.children) {- const rect = child.getBoundingClientRect();+ if (!(child instanceof HTMLElement)) {+ continue;+ }if (child.classList.contains("board-drop-placeholder")) {- gap += rect.height + Number.parseFloat(getComputedStyle(child).marginBottom || "0");+ const box = child.getBoundingClientRect();+ gap += box.height + Number.parseFloat(getComputedStyle(child).marginBottom || "0");continue;}- if (child.classList.contains("board-note")) {- cards.push({ top: rect.top - top - gap, height: rect.height });+ if (!child.classList.contains("board-note")) {+ continue;}++ const noteId = child.dataset.noteId ?? "";+ let height = heights.get(noteId);+ let at: number | undefined;++ // The first card is always read, whatever is known of it: where a column's cards begin is+ // its own, and the ones under it are counted up from there. A card whose height is not+ // known yet is read as well, and remembered.+ if (height === undefined || !anchored) {+ const box = child.getBoundingClientRect();+ height ??= box.height;+ at = box.top - top - gap;+ anchored = true;+ if (noteId && box.height) {+ heights.set(noteId, box.height);+ }+ if (spacing === undefined && foot !== undefined) {+ spacing = at - foot;+ }+ foot = at + height;+ }++ const stands = at ?? next;+ cards.push({ top: stands, height });+ next = stands + height + (spacing ?? 0);}return cards;diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsxindex a077884ecb..b07856dd6f 100644--- a/apps/client/src/widgets/collections/board/index.tsx+++ b/apps/client/src/widgets/collections/board/index.tsx@@ -41,6 +41,7 @@ import { ViewModeProps } from "../interface";import Api, { getPendingWrites, PendingColumnWrites, settleColumn } from "./api";import { useBoardDrag } from "./board_drag";import { columnGapStandsAside, columnStandsAside, movesColumn } from "./drag_geometry";+import { forgetCardHeights } from "./drag_measure";import { BoardDropStateContext, DropStateStore } from "./drop_state";import BoardApi from "./api";import { DEFAULT_COLUMN_ICON, DEFAULT_GROUP_BY, getStatusDefinition, INBOX_COLUMN } from "./columns";@@ -474,6 +475,14 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC/** Until when a column move can still be settling, which is when `useFlip` slides columns. */const columnMovedUntil = useRef(0);+ // What a card measures is kept between drags, which holds while a column is the width it was+ // measured at. A phone gives a column a share of the window, so a window that changes size+ // takes the heights with it.+ useEffect(() => {+ window.addEventListener("resize", forgetCardHeights);+ return () => window.removeEventListener("resize", forgetCardHeights);+ }, []);+// Which columns stand narrow, as a line, so that one opening or closing is read off a single// comparison. A column changing width hides or shows its own cards and moves no card inside// any other, so there is nothing for any column to measure while it is happening.