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: update comments
Adorian Doranb32a3a3aad91
diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsxindex 94a529360f..399475fd6b 100644--- a/apps/client/src/widgets/collections/board/column.tsx+++ b/apps/client/src/widgets/collections/board/column.tsx@@ -278,8 +278,8 @@ export default function Column({const last = drawn[drawn.length - 1];const top = standing? standing.offsetTop- // A gap above the window stands at the first drawn card; one below it stands- // after the last, which is where the cards run out either way.+ // Above the window, place the gap at the first rendered card; below it, after+ // the last.: (placeOf(dropIndex) < 0? (cards[0]?.offsetTop ?? 0): (last ? last.offsetTop + last.offsetHeight + cardSpacing() : 0));@@ -539,9 +539,8 @@ export default function Column({return () => window.clearTimeout(timer);}, [ insertedNoteId, columnItems ]);- // A card just made at the foot of a long column stands outside the window, and the card's own- // effect that scrolls to it cannot run while it is not drawn. The column brings it into view- // instead, which is what draws it.+ // A card created at the foot of a long column falls outside the window, so the card's own+ // scroll effect never runs. Scroll to it here, which renders it.const arrived = createdNoteId ?? landedNoteId;useEffect(() => {if (!isWindowed || !arrived) return;diff --git a/apps/client/src/widgets/collections/board/drag_measure.ts b/apps/client/src/widgets/collections/board/drag_measure.tsindex 09fa86d56d..65a53c2b10 100644--- a/apps/client/src/widgets/collections/board/drag_measure.ts+++ b/apps/client/src/widgets/collections/board/drag_measure.ts@@ -58,8 +58,8 @@ export function measureBoard(container: HTMLElement, withCards = true): BoardMea/*** Where a column's first card begins, in the space {@link toAreaY} reads a point into.*- * Taken from the spacer standing at the head of the cards, which begins where they do whatever the- * column is drawing. Read once per gesture: a column's padding does not change while one runs.+ * Read from the leading `.board-window-spacer`, which begins where the cards do whether or not the+ * column is windowed. Read once per gesture: the padding does not change while one runs.*/function contentOrigin(area: HTMLElement | null) {const spacer = area?.querySelector<HTMLElement>(".board-window-spacer");@@ -80,20 +80,20 @@ export function toBoardX(container: HTMLElement, clientX: number): number {* The place a carried card would take in a windowed column, counted from what the column lays its* cards out with rather than from the page.*- * Matches {@link cardInsertionIndex} for a column drawn in full, and treats the carried card the- * same way: it is out of the flow while held, so the cards below it stand one place higher and the+ * Matches {@link cardInsertionIndex} for a fully rendered column, and treats the dragged card the+ * same way: it is out of the flow while held, so the cards below it shift up one place and the* index is converted back afterwards.*- * Worked out per move rather than once per gesture. A window that has moved under an auto-scroll- * draws cards the gesture had only estimated, and an index counted from the estimate drifts further- * from the gap the longer the scroll runs.+ * Computed per pointer move rather than once per gesture. An auto-scroll moves the window onto+ * cards the gesture had only estimated, and an index computed from those estimates drifts further+ * from the drop placeholder the longer the scroll runs.** @param carrying the place the carried card holds here, or `undefined` if it came from elsewhere.*/export function placeInModel(model: ColumnModel, y: number, carrying: number | undefined): number {const { heights, spacing } = model;let offset = 0;- /** How many cards have been passed, the carried one not counted among them. */+ /** How many cards have been passed, excluding the dragged one. */let place = 0;for (const [ index, height ] of heights.entries()) {@@ -104,7 +104,7 @@ export function placeInModel(model: ColumnModel, y: number, carrying: number | uconst foot = offset + height;const isLast = index === heights.length - 1|| (carrying === heights.length - 1 && index === heights.length - 2);- // Halfway into the gap below a card is where the next place begins.+ // The next slot begins halfway into the gap below a card.const boundary = isLast ? foot : foot + spacing / 2;if (y < boundary) {return carrying !== undefined && place >= carrying ? place + 1 : place;@@ -193,9 +193,8 @@ function measureCards(area: HTMLElement | null): CardBox[] {function countCards(area: HTMLElement, readEvery: boolean) {const top = area.getBoundingClientRect().top - area.scrollTop;const cards: CardBox[] = [];- // A windowed column draws a slice of itself. The cards it leaves undrawn are counted in below- // and above the ones it does, so an index here still names a place in the column rather than a- // place among what happens to be on screen.+ // A windowed column renders a slice of its cards. Boxes for the unrendered ones are added+ // above and below, so an index here is a position in the column, not among the visible cards.const window = readWindow(area);const elements: HTMLElement[] = [];/** Where the next card stands if it has to be counted rather than read. */@@ -249,9 +248,8 @@ function countCards(area: HTMLElement, readEvery: boolean) {return { cards, elements, borrowed, top, drawnFrom: 0 };}- // The undrawn cards are spread evenly over the spacer standing for them: precise enough for a- // drop, which can only land where the reader can see, and it keeps every index in the column's- // own terms.+ // Spread the unrendered cards evenly across the spacer covering them. Precise enough for a+ // drop, which can only land where the reader can see, and it keeps every index in column terms.const above = spread(window.from, 0, window.above);const below = spread(window.total - window.from - cards.length,@@ -278,7 +276,7 @@ function readWindow(area: HTMLElement) {};}-/** Stands `count` cards evenly across a run of pixels, for the ones a column is not drawing. */+/** Returns `count` boxes spread evenly across `span` pixels, for cards a column does not render. */function spread(count: number, from: number, span: number): CardBox[] {if (count <= 0) {return [];diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsxindex 107ed42a7b..f4019ed4e4 100644--- a/apps/client/src/widgets/collections/board/index.tsx+++ b/apps/client/src/widgets/collections/board/index.tsx@@ -260,7 +260,7 @@ export const BoardKeptCardsContext = createContext<Set<string>>(new Set());* board handles itself (see `keyboard.ts` and the card and column handlers), none of them* rebindable, so each is listed literally rather than through a registered action.*/-/** Stands for a board where no column has an insert field open, so no render allocates a set. */+/** Shared empty set for a board with no insert field open, so no render allocates one. */const NO_COLUMNS: ReadonlySet<string> = new Set();/** How long a finger stays on the create button before it offers where to put the card. */diff --git a/apps/client/src/widgets/collections/board/keyboard.ts b/apps/client/src/widgets/collections/board/keyboard.tsindex a95d263e1a..6862b90b11 100644--- a/apps/client/src/widgets/collections/board/keyboard.ts+++ b/apps/client/src/widgets/collections/board/keyboard.ts@@ -109,7 +109,7 @@ export function useBoardKeyboard({const pendingFocus = useRef<PendingFocus | null>(null);/** The card `askForCard` has already run for, so it does not run again on every render. */const asked = useRef<string | null>(null);- /** Where the reader last stood, for a key pressed while focus is between two draws. */+ /** The last spot walked to, for a key pressed while focus is between two renders. */const lastSpot = useRef<Spot | null>(null);// Every render, since a redraw is the only thing that takes focus away here and more than one@@ -201,8 +201,8 @@ export function useBoardKeyboard({const spot = spotOf(container, document.activeElement)// Focus can be left on nothing by a redraw that took the card it was on out of the- // page. The walk carries on from where it last stood rather than letting the key- // through, which would scroll the board instead of moving along it.+ // page. Fall back to the previous spot rather than letting the key through, which+ // would scroll the board instead of moving along it.?? (NAVIGATION_KEYS.includes(e.key) ? lastSpot.current : null);if (!spot) return;diff --git a/apps/client/src/widgets/collections/board/windowing.ts b/apps/client/src/widgets/collections/board/windowing.tsindex ce0bc183d1..47ee47f590 100644--- a/apps/client/src/widgets/collections/board/windowing.ts+++ b/apps/client/src/widgets/collections/board/windowing.ts@@ -1,11 +1,10 @@/**- * Which of a column's cards are drawn, for a column holding more of them than the page can carry.+ * Picks which of a column's cards to render, for columns holding more than the page can carry.*- * A board keeps every card of every column in the page, and the cost of that is not the elements- * inside a card but the card itself: the style rules the page holds are matched against each one,- * so a column of thousands makes scrolling, dragging and hovering it cost hundreds of milliseconds.- * Drawing only what the reader can see, with a spacer standing for the rest, takes those back to- * the frame budget.+ * Without this the board renders every card of every column. The cost is the `.board-note` element+ * itself, not its contents: the page's CSS rules are matched against each one, so a column of+ * 10,000 cards costs hundreds of milliseconds per style recalculation. Rendering only the visible+ * cards, with a spacer div for the rest, brings scrolling, dragging and hovering back to 60fps.*/import { RefObject } from "preact";@@ -16,47 +15,47 @@ import {import { cardSpacing } from "./drag_measure";-/** What a card stands at before anything has measured it: one line of title with its padding. */+/** Height assumed for a card before it is measured: one line of title plus its padding. */export const NOMINAL_CARD_HEIGHT = 64;-/** How far past each edge of the area cards are still drawn, so scrolling has somewhere to go. */+/** Extra pixels rendered past each edge of the area, so a scroll has cards ready. */export const OVERSCAN_PX = 800;-/** The window's edges move in steps of this many cards, so a scroll swaps them in batches. */+/** The window's edges move in steps of this many cards, batching re-renders. */export const WINDOW_CHUNK = 25;-/** What a column holds before it is worth windowing at all. */+/** Minimum cards in a column before windowing is worth its complexity. */export const WINDOW_THRESHOLD = 120;export interface ColumnWindow {- /** The first card drawn. */+ /** Index of the first card rendered. */from: number;- /** One past the last card drawn. */+ /** Index one past the last card rendered. */until: number;- /** What the spacer above the drawn cards stands at, in pixels. */+ /** Height of the spacer above the rendered cards, in pixels. */above: number;- /** What the spacer below them stands at, in pixels. */+ /** Height of the spacer below them, in pixels. */below: number;}export interface WindowInput {- /** What each card stands at, in the order the column holds them. */+ /** Height of each card, in the column's own order. */heights: readonly number[];- /** What stands between one card and the next. */+ /** Vertical gap between one card and the next. */spacing: number;scrollTop: number;- /** What the area drawing the cards stands at. */+ /** Height of the scrolling area the cards are rendered in. */viewport: number;overscan?: number;- /** How many cards the window's edges move by. One card, for a test that wants no batching. */+ /** Cards per step of the window's edges. Pass 1 in a test to disable batching. */chunk?: number;}/**- * The cards to draw at a scroll position, and what the spacers around them stand at.+ * Returns the cards to render at a scroll position, and the height of the spacer at each end.*- * The edges are rounded outwards to whole chunks so that scrolling by a card does not redraw the- * column: a window only changes once the reader has passed a chunk of them.+ * `from` and `until` are rounded outwards to whole chunks, so scrolling by one card does not+ * re-render the column: the window changes only after the reader passes a chunk.*/export function computeWindow({heights, spacing, scrollTop, viewport, overscan = OVERSCAN_PX, chunk = WINDOW_CHUNK@@ -71,7 +70,7 @@ export function computeWindow({let first = count;let last = -1;let offset = 0;- /** Where each drawn edge stands, kept while walking so neither is measured a second time. */+ /** Offset of each card, accumulated once so neither edge is computed twice. */const edges: number[] = [];for (const [index, height] of heights.entries()) {@@ -87,8 +86,8 @@ export function computeWindow({}edges.push(offset);- // Nothing is in view when the column is scrolled past its own end, which a shrinking column- // leaves the reader at. The last chunk is drawn instead, so the column is never blank.+ // A column that has shrunk can leave `scrollTop` past its own end, where no card matches.+ // Render the last chunk instead, so the column is never blank.if (last < 0 || first > last) {first = Math.max(0, count - 1);last = count - 1;@@ -101,17 +100,14 @@ export function computeWindow({from,until,above: edges[from] ?? 0,- // The spacing below the last drawn card is its own margin, so the spacer stands for what- // is left after it.+ // The last rendered card supplies its own bottom margin, so the spacer covers only what+ // follows it.below: Math.max(0, (edges[count] ?? 0) - (edges[until] ?? edges[count] ?? 0))};}/**- * What each card stands at, measured where it has been drawn and estimated where it has not.- *- * The estimate is the average of what has been measured, so a column of tall cards is not counted- * as a column of short ones once any of them has been seen.+ * Returns each card's height: the measured value where there is one, `nominal` otherwise.*/export function resolveHeights(noteIds: readonly string[],@@ -122,18 +118,17 @@ export function resolveHeights(}/**- * What a column counts an unmeasured card at, from the cards of that column already measured.+ * Returns the height to assume for a column's unmeasured cards, averaged over its measured ones.*- * A column's own cards, not every card the page has drawn: two boards, or two columns of one- * board, can carry different attributes and stand at quite different heights, and counting one- * column's cards at another's average puts its spacers, its scrollbar and its scroll destinations- * all in the wrong place.+ * Averages only `noteIds`, not every card the page has measured. Two boards, or two columns of one+ * board, can have different promoted attributes and so different heights; using another column's+ * average gives the wrong spacer heights, scrollbar length and `scrollToCard` destinations.*- * Returns `undefined` until enough of them have been measured; the caller then keeps the first- * figure it gets. It must stop changing: the spacer above the window is counted from it and is- * what holds the reader's place, so revising it would slide the column while they read.+ * Returns `undefined` until `ESTIMATE_SAMPLE` cards are measured. The caller then keeps the first+ * value: `bounds.above` derives from it and holds the reader's scroll position, so changing it+ * later shifts the column under them.*- * @param held what the column has already settled on, which is answered back unchanged.+ * @param held a value the column already settled on, returned unchanged.*/export function estimateFor(noteIds: readonly string[],@@ -161,32 +156,31 @@ export function estimateFor(const ESTIMATE_SAMPLE = 12;/**- * Bumped when what has been measured is dropped, so every column settles on a fresh estimate- * rather than holding one taken at a width the board no longer has.+ * Incremented by `forgetWindowHeights` so each column recomputes its estimate instead of keeping+ * one measured at a column width the board no longer has.*/let generation = 0;/**- * Whether two windows draw the same cards.+ * Returns whether two windows render the same cards.*- * The spacers are left out: they follow from the heights, and a measurement that only corrects- * them must not count as a change that redraws the column.+ * Ignores `above` and `below`: they derive from the heights, and a measurement that only corrects+ * them must not count as a re-render.*/export function sameWindow(a: ColumnWindow, b: ColumnWindow) {return a.from === b.from && a.until === b.until;}/**- * The window a column draws, kept in step with its scrolling and with what its cards measure.+ * Tracks which cards a column renders, following its scroll position and its measured heights.*- * Heights are learned as cards are drawn and remembered by note, so a card that has been on screen- * once is counted at its own height afterwards rather than at the estimate. Correcting a height- * above the reader would slide the column under them, so the scroll position is moved by the same- * amount in the frame the correction lands.+ * Measures each card as it is rendered and caches the height by note id, so a card seen once uses+ * its real height afterwards. A correction above the viewport shifts the content below it, so+ * `scrollTop` is adjusted by the same amount in that frame.*- * @param areaRef the scrolling element the cards are drawn in.- * @param noteIds the column's cards, in the order it holds them.- * @param enabled whether this column is windowed at all.+ * @param areaRef the column's scrolling element.+ * @param noteIds the column's cards, in order.+ * @param enabled whether to window this column at all.*/export function useColumnWindow(areaRef: RefObject<HTMLElement>,@@ -195,7 +189,7 @@ export function useColumnWindow() {const [ revision, setRevision ] = useState(0);const [ scroll, setScroll ] = useState({ top: 0, viewport: 0 });- /** What this column counts its unmeasured cards at, once its own have settled it. */+ /** Height this column assumes for its unmeasured cards, once its own have settled it. */const estimate = useRef<{ at: number, value?: number }>({ at: generation });if (estimate.current.at !== generation) {estimate.current = { at: generation };@@ -205,7 +199,7 @@ export function useColumnWindow(const settled = estimate.current.value;const heights = useMemo(() => resolveHeights(noteIds, cardHeights, settled ?? NOMINAL_CARD_HEIGHT),- // `revision` stands for the measurements, which live outside the render.+ // `revision` tracks `cardHeights`, which is mutated outside the render.// eslint-disable-next-line react-hooks/exhaustive-deps[ noteIds, revision, settled ]);const spacing = cardSpacing() || DEFAULT_SPACING;@@ -217,13 +211,13 @@ export function useColumnWindow(: { from: 0, until: noteIds.length, above: 0, below: 0 }), [ enabled, heights, spacing, scroll.top, scroll.viewport, noteIds.length ]);- /** What the last commit drew, so a change of window can be told from a change of spacer. */+ /** The previous commit's window, to distinguish a window change from a spacer correction. */const drawn = useRef(bounds);const changed = enabled && !sameWindow(drawn.current, bounds);drawn.current = bounds;- // Follows the column's own scrolling. Read in the event rather than through state, so the- // window keeps up with the finger instead of trailing it by a frame.+ // Reads the scroll position in the listener rather than from state, so the window keeps up+ // with the pointer instead of trailing it by a frame.useEffect(() => {const area = areaRef.current;if (!area || !enabled) return;@@ -244,7 +238,7 @@ export function useColumnWindow(};}, [ areaRef, enabled ]);- /** What the spacer above stood at last, and for which first card, so a correction is spotted. */+ /** The previous `above` and `from`, used to detect an estimate correction. */const above = useRef({ from: bounds.from, height: bounds.above });useLayoutEffect(() => {const area = areaRef.current;@@ -253,12 +247,11 @@ export function useColumnWindow(return;}- // A correction to what the undrawn cards are counted at moves everything below it, so the- // column is scrolled by as much to leave what the reader is looking at where it was.+ // Correcting the estimate changes `bounds.above`, which shifts every card below it, so+ // `scrollTop` moves by the same amount to keep the viewport on the same cards.//- // Only where the same cards are still drawn: a window that moved because the reader- // scrolled has a different spacer above it by rights, and the scroll position that moved- // the scroll position that moved it is the one the reader chose.+ // Only when `bounds.from` is unchanged. A window that moved because the reader scrolled+ // has a different `above` by rights, and their scroll position is already correct.const held = above.current;const shift = held.from === bounds.from ? bounds.above - held.height : 0;above.current = { from: bounds.from, height: bounds.above };@@ -270,8 +263,8 @@ export function useColumnWindow(for (const card of area.querySelectorAll<HTMLElement>(".board-note")) {const noteId = card.dataset.noteId;const height = card.offsetHeight;- // A card holding the field its title is typed in stands taller than the card does, and- // a carried one is out of the flow at no height at all.+ // A card with its title editor open is taller than the card itself, and a dragged one+ // is out of the flow with no height.if (!noteId || !height || card.classList.contains("editing")) continue;const known = cardHeights.get(noteId);@@ -286,8 +279,8 @@ export function useColumnWindow(}});- // Published after each commit, so a gesture reads what the column is drawing with now rather- // than what it was drawing with when the gesture started.+ // Republished after each commit, so `placeInModel` reads the current heights rather than the+ // ones measured when a drag started.useLayoutEffect(() => {const area = areaRef.current;if (!area) return;@@ -300,8 +293,8 @@ export function useColumnWindow(}, [ areaRef, enabled, heights, spacing ]);/**- * Brings a card into the window, for one the column has just made where the reader is not- * looking. The scroll is what moves the window: it is worked out from the same heights.+ * Scrolls a card into the window, for one created outside it. The scroll moves the window,+ * and the offset comes from the same `heights`.*/const scrollToCard = useCallback((index: number, immediate = false) => {const area = areaRef.current;@@ -332,19 +325,18 @@ export function useColumnWindow(}/**- * What a column lays its cards out from: the heights the spacers are counted with, and the gap- * between one card and the next.+ * The heights a column lays its cards out from, and the gap between one card and the next.*- * Published so a drag can work out where a card would land without reading the page. The page is- * the wrong source mid-gesture: the carried card is out of the flow, the gap stands over the cards,- * and the ones below it are moved aside by a transform.+ * Published so a drag can compute where a card would land without reading the DOM. The DOM is+ * wrong mid-gesture: the dragged card is out of the flow, the drop placeholder overlays the cards,+ * and the ones below it are offset by a transform.*/export interface ColumnModel {heights: readonly number[];spacing: number;}-/** The model a column is drawing with, or nothing where it draws all of its cards. */+/** Returns the model a column renders with, or `undefined` if it renders every card. */export function getColumnModel(area: HTMLElement) {return models.get(area);}@@ -380,19 +372,19 @@ export interface RevealCardDetail {immediate?: boolean;}-/** Drops what has been measured, for a window whose size has changed under it. */+/** Clears the measured heights, for a window resize that changes card widths. */export function forgetWindowHeights() {cardHeights.clear();generation++;}/**- * What each card measured, by note, kept for as long as the page is open.+ * Measured height of each card, by note id, for the lifetime of the page.*- * Shared by every board: a card keeps its height wherever it is drawn, and a column that has never- * been scrolled still counts its cards at what they stood at somewhere else.+ * Shared across boards: a note has the same height wherever it is rendered, so a column opened for+ * the first time can reuse heights measured elsewhere.*/const cardHeights = new Map<string, number>();-/** What stands between two cards before a drag has measured it. Matches `.board-note`'s margin. */+/** Gap between two cards before a drag measures it. Matches `.board-note`'s bottom margin. */const DEFAULT_SPACING = 9;