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: fix some issues
Adorian Doranc92508f144c6
diff --git a/apps/client/src/widgets/collections/board/board_drag.ts b/apps/client/src/widgets/collections/board/board_drag.tsindex 2b5ebceec3..c6a907c65b 100644--- a/apps/client/src/widgets/collections/board/board_drag.ts+++ b/apps/client/src/widgets/collections/board/board_drag.ts@@ -547,8 +547,9 @@ function row(held: Gesture & { kind: "column" }) {/*** The place a carried card would take in a column.*- * A windowed column is asked what it is drawing with now: the boxes a gesture measures at its start- * only estimate the cards it was not drawing then, and an auto-scroll draws them for real.+ * For a windowed column this reads the column's current `ColumnModel`: the boxes `measureBoard`+ * takes at the start of a gesture only estimate the cards that were not drawn then, and an+ * auto-scroll goes on to draw them at their own heights.*/function placeAt(area: HTMLElement, y: number, card: DraggedCard, column: ColumnBox): number {const model = getColumnModel(area);@@ -556,7 +557,8 @@ function placeAt(area: HTMLElement, y: number, card: DraggedCard, column: Columnreturn placeIn(column.cards, y, card, column.value);}- return placeInModel(model, y, column.value === card.fromColumn ? card.index : undefined);+ return placeInModel(+ model, y - column.origin, column.value === card.fromColumn ? card.index : undefined);}/**@@ -601,8 +603,8 @@ function startCard(const noteId = element?.dataset.noteId;if (!element || !columnElement || !noteId) return null;- // A windowed column draws a slice of its cards, so the card says which place it holds rather- // than being counted among the ones on screen.+ // A windowed column draws a slice of its cards, so the index comes from `data-index` rather+ // than from the card's position among the ones on screen.const stated = element.dataset.index;const place = stated === undefined ? Number.NaN : Number(stated);const index = Number.isFinite(place)diff --git a/apps/client/src/widgets/collections/board/drag_geometry.spec.ts b/apps/client/src/widgets/collections/board/drag_geometry.spec.tsindex 90e54a9ec7..534ef7a9f4 100644--- a/apps/client/src/widgets/collections/board/drag_geometry.spec.ts+++ b/apps/client/src/widgets/collections/board/drag_geometry.spec.ts@@ -12,6 +12,7 @@ function columns(cards: Record<string, CardBox[]> = {}): ColumnBox[] {left: index * 120,width: 100,top: 0,+ origin: 0,height: 400,cards: cards[value] ?? []}));@@ -79,7 +80,7 @@ describe("columnCovers", () => {*/it("tells the column from the empty space below a short one", () => {const strip: ColumnBox = {- value: "Parked", left: 0, width: 36, top: 0, height: 90, cards: []+ value: "Parked", left: 0, width: 36, top: 0, height: 90, origin: 0, cards: []};expect(columnCovers(strip, 18, 0)).toBe(true);diff --git a/apps/client/src/widgets/collections/board/drag_geometry.ts b/apps/client/src/widgets/collections/board/drag_geometry.tsindex e50fcb9781..e892092c36 100644--- a/apps/client/src/widgets/collections/board/drag_geometry.ts+++ b/apps/client/src/widgets/collections/board/drag_geometry.ts@@ -27,6 +27,14 @@ export interface ColumnBox {*/top: number;height: number;+ /**+ * Where the column's first card begins, in the space {@link toAreaY} reads a point into.+ *+ * The column's own top padding, which the boxes below already include but a place counted from+ * the heights alone does not. Left out, every such place is short by it, and a point near a+ * boundary falls into the slot below the one the gap is drawn at.+ */+ origin: number;/*** The cards as drawn, in order, the dragged one included. Counting it keeps the index in the* same terms as the list the board holds, which is what a move is expressed in.diff --git a/apps/client/src/widgets/collections/board/drag_measure.spec.ts b/apps/client/src/widgets/collections/board/drag_measure.spec.tsindex c24f14bc99..7c4f861563 100644--- a/apps/client/src/widgets/collections/board/drag_measure.spec.ts+++ b/apps/client/src/widgets/collections/board/drag_measure.spec.ts@@ -355,6 +355,33 @@ describe("measuring a windowed column", () => {expect(column.cards).toHaveLength(2);});+ /**+ * A place counted from the heights alone starts at zero, while a point read into the area's+ * space starts at the column's own top padding. Losing that shifts every place by it, and a+ * point near a boundary lands in the slot below the one the gap is drawn at.+ */+ it("reports where the column's cards begin, which is its own padding", () => {+ const board = buildWindowed({ above: 2400 });+ const area = board.querySelector<HTMLElement>(".board-column-content");+ const spacer = area?.querySelector<HTMLElement>(".board-window-spacer");+ if (!area || !spacer) throw new Error("expected a spacer at the head of the cards");++ // The head spacer begins 8px into the area, which is what the column is padded by.+ place(spacer, { left: 0, top: 48, width: 100, height: 2400 });++ const [ column ] = measureBoard(board).columns;++ expect(column.origin).toBe(8);+ });++ it("reports no origin for a column measured without its cards", () => {+ const board = buildWindowed();++ const [ column ] = measureBoard(board, false).columns;++ expect(column.origin).toBe(0);+ });+it("counts a column that states a window but is drawing none of it", () => {const board = buildWindowed({ total: 60, from: 60, above: 3600, below: 0 });const area = board.querySelector<HTMLElement>(".board-column-content");diff --git a/apps/client/src/widgets/collections/board/drag_measure.ts b/apps/client/src/widgets/collections/board/drag_measure.tsindex f2181d7e17..09fa86d56d 100644--- a/apps/client/src/widgets/collections/board/drag_measure.ts+++ b/apps/client/src/widgets/collections/board/drag_measure.ts@@ -47,6 +47,7 @@ export function measureBoard(container: HTMLElement, withCards = true): BoardMeawidth: rect.width,top: rect.top,height: rect.height,+ origin: withCards ? contentOrigin(area) : 0,cards: withCards ? measureCards(area) : []});}@@ -54,6 +55,22 @@ export function measureBoard(container: HTMLElement, withCards = true): BoardMeareturn { columns, areas };}+/**+ * 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.+ */+function contentOrigin(area: HTMLElement | null) {+ const spacer = area?.querySelector<HTMLElement>(".board-window-spacer");+ if (!area || !spacer) {+ return 0;+ }++ return spacer.getBoundingClientRect().top+ - (area.getBoundingClientRect().top - area.scrollTop);+}+/** A point in the board's content space, which its horizontal scrolling does not move columns in. */export function toBoardX(container: HTMLElement, clientX: number): number {return clientX - container.getBoundingClientRect().left + container.scrollLeft;@@ -63,9 +80,9 @@ 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.*- * The same answer {@link cardInsertionIndex} gives for a column drawn in full, and the same- * treatment of the carried card: it is out of the flow while it is held, so the cards below it- * stand one place higher and the index is counted back afterwards.+ * 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+ * 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@@ -244,7 +261,7 @@ function countCards(area: HTMLElement, readEvery: boolean) {return { cards: [ ...above, ...cards, ...below ], elements, borrowed, top, drawnFrom: window.from };}-/** What the column says it is drawing, or nothing where it draws all of itself. */+/** The window a column states on its card area, or nothing where it draws every card. */function readWindow(area: HTMLElement) {const stated = area.dataset.windowCount;const total = stated === undefined ? Number.NaN : Number(stated);diff --git a/apps/client/src/widgets/collections/board/keyboard.ts b/apps/client/src/widgets/collections/board/keyboard.tsindex b1103695d9..a95d263e1a 100644--- a/apps/client/src/widgets/collections/board/keyboard.ts+++ b/apps/client/src/widgets/collections/board/keyboard.ts@@ -107,7 +107,7 @@ export function useBoardKeyboard({sendCardsToColumn, moveCardsWithin}: BoardKeyboardOptions) {const pendingFocus = useRef<PendingFocus | null>(null);- /** The card a column has been asked to draw, so it is not asked for again each render. */+ /** 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. */const lastSpot = useRef<Spot | null>(null);@@ -145,7 +145,7 @@ export function useBoardKeyboard({}// The card has landed in a windowed column that is not drawing it, so there is nothing to- // focus yet. The column is asked to scroll to it, which draws it.+ // focus yet. `askForCard` scrolls that column, which draws the card.//// Waited for here rather than left to this effect running again: the scroll re-renders the// column alone, and the board this effect belongs to is not drawn again by it.@@ -470,15 +470,16 @@ function walk(container: HTMLElement, from: Spot, key: string): Spot | null {}// A windowed column draws a slice of its cards, and the walk has stepped onto one outside it.- // The column is asked to bring it into view, which is what puts it in the page to be focused.+ // `askForCard` scrolls that column, which puts the card in the page so it can be focused.if (next.kind === "item") {const column = columnsOf(container)[next.column];const value = column?.dataset.column;if (!column || value === undefined) return null;- // Drawn before the ask returns, so the card is focused in this keystroke. Left to a later- // frame, the scroll can take the card being walked from out of the page first, and focus- // falls to nothing: the next key then finds nowhere to walk from and the board scrolls.+ // `immediate` draws the card before this returns, so it is focused in this keystroke.+ // Deferred to a later frame, the scroll can unmount the card being walked from, leaving+ // `document.activeElement` on the body: `spotOf` then finds no spot and the browser+ // scrolls the board instead.askForCard(container, value, next.item, true);const arrived = cardAt(column, next.item);if (arrived) {diff --git a/apps/client/src/widgets/collections/board/windowing.ts b/apps/client/src/widgets/collections/board/windowing.tsindex ccfd4c692d..ce0bc183d1 100644--- a/apps/client/src/widgets/collections/board/windowing.ts+++ b/apps/client/src/widgets/collections/board/windowing.ts@@ -129,9 +129,9 @@ export function resolveHeights(* column's cards at another's average puts its spacers, its scrollbar and its scroll destinations* all in the wrong place.*- * Answers nothing until enough of them have been measured, and the caller then holds what it is- * given for good. It has to stop moving: the spacer above the window holds the reader's place and- * is counted from this, so a figure that kept being revised would slide the column as they read.+ * 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.** @param held what the column has already settled on, which is answered back unchanged.*/@@ -258,7 +258,7 @@ export function useColumnWindow(//// 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- // it is already the one they asked for.+ // the scroll position that moved it is the one the reader chose.const held = above.current;const shift = held.from === bounds.from ? bounds.above - held.height : 0;above.current = { from: bounds.from, height: bounds.above };@@ -316,8 +316,8 @@ export function useColumnWindow(// Placed a little above the foot of the area, so the card is not left under its own edge.area.scrollTop = Math.max(0, offset - area.clientHeight / 2);- // Moved here rather than left to the scroll event the write raises: that event arrives a- // frame later, and the card has to be drawn for the ask to have been worth making.+ // Set here rather than left to the scroll event the write above raises: that event+ // arrives a frame later, and the card must be drawn for this call to have any effect.const moved = { top: area.scrollTop, viewport: area.clientHeight };if (immediate) {// Drawn before this returns, for a keyboard walk that focuses the card in the same@@ -352,10 +352,10 @@ export function getColumnModel(area: HTMLElement) {const models = new WeakMap<HTMLElement, ColumnModel>();/**- * Asks the column drawing `column` to bring a card into view.+ * Raises {@link REVEAL_CARD} so the column drawing `column` scrolls a card into view.** The keyboard walks a column by index and can step onto a card outside the window, which is not- * in the page to be focused. The column answers by scrolling to it, which draws it.+ * in the page to be focused. The column handles the event by scrolling to it, which draws it.*/export function askForCard(container: HTMLElement, column: string, index: number, immediate = false@@ -370,12 +370,12 @@ export interface RevealCardDetail {column: string;index: number;/**- * Whether the card must be in the page by the time the ask returns.+ * Whether the card must be in the page by the time {@link askForCard} returns.** For a keyboard walk, which has to focus it in the same keystroke: focus left on nothing* while a frame is waited for makes the next key find no card to walk from, and the browser- * scrolls the board instead. Not for an ask made while the board is drawing, where drawing- * again in the middle of a commit is not safe.+ * scrolls the board instead. Not set when the call is made from a layout effect, where+ * drawing again in the middle of a commit is not safe.*/immediate?: boolean;}