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 Doran41247b1724c1
diff --git a/apps/client/src/widgets/collections/board/board_drag.ts b/apps/client/src/widgets/collections/board/board_drag.tsindex f93df7108f..25bc5bb081 100644--- a/apps/client/src/widgets/collections/board/board_drag.ts+++ b/apps/client/src/widgets/collections/board/board_drag.ts@@ -362,25 +362,7 @@ export function useBoardDrag(}};- /**- * Where the columns stood before the carried one was taken out of the row, which is what the board- * places the gap and the columns that step aside for it against.- */-function row(held: Gesture & { kind: "column" }) {- const boxes = held.measurement?.columns ?? [];- const last = boxes[boxes.length - 1];- // Read off the row rather than from the stylesheet: what stands between two columns is the- // same everywhere, and one pair is enough to say how much.- const gap = boxes.length > 1 ? boxes[1].left - (boxes[0].left + boxes[0].width) : 0;- const lefts = boxes.map(({ left }) => left);- if (last) {- lefts.push(last.left + last.width + gap);- }-- return { lefts, stride: (boxes[held.index]?.width ?? 0) + gap };-}--/** Set between a tap and the `touchend` the browser would make mouse events from. */+ /** Set between a tap and the `touchend` the browser would make mouse events from. */let justTapped = false;const onTouchEnd = (event: TouchEvent) => {@@ -471,6 +453,24 @@ function row(held: Gesture & { kind: "column" }) {return { isDragging, remeasure };}+/**+ * Where the columns stood before the carried one was taken out of the row, which is what the board+ * places the gap and the columns that step aside for it against.+ */+function row(held: Gesture & { kind: "column" }) {+ const boxes = held.measurement?.columns ?? [];+ const last = boxes[boxes.length - 1];+ // Read off the row rather than from the stylesheet: what stands between two columns is the+ // same everywhere, and one pair is enough to say how much.+ const gap = boxes.length > 1 ? boxes[1].left - (boxes[0].left + boxes[0].width) : 0;+ const lefts = boxes.map(({ left }) => left);+ if (last) {+ lefts.push(last.left + last.width + gap);+ }++ return { lefts, stride: (boxes[held.index]?.width ?? 0) + gap };+}+/*** The place a carried card would take in a column, counting that column as the board holds it.*diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsxindex 24d33eb50d..6fb45eee19 100644--- a/apps/client/src/widgets/collections/board/column.tsx+++ b/apps/client/src/widgets/collections/board/column.tsx@@ -29,12 +29,11 @@ const HAND_OVER_MS = 2000;const STOCK_GAP_HEIGHT = 40;/**- * How many cards below the gap are told to stand aside for it.+ * The least a card can stand, which is one line of its title with the padding and gap around it.*- * Only the ones the reader can see have to move, and a column shows a handful at a time. Telling- * every card below the gap instead is thousands of them on a long column, and each one costs.+ * Says how many cards a column can show at once, which is how many below the gap have to move.*/-const CARDS_ASIDE = 30;+const MIN_CARD_HEIGHT = 32;/** How long an open takes. Matches `--board-expand-duration` in the board's own rules. */export const EXPAND_MS = 200;@@ -103,9 +102,8 @@ export default function Column({/*** How far the column stands aside for one being carried, in pixels.*- * Written as a transform rather than drawn by putting the carried column's place among the- * others: the row is left as it stands for the length of the gesture, so a step of it costs- * the browser a transform apiece instead of laying out every card on the board again.+ * The row keeps its order for the length of the gesture, so a step costs a transform per+ * column rather than a fresh layout of every card on the board.*/standsAside?: number,/** What a new card is made from, and how the reader picks something else. */@@ -180,25 +178,9 @@ export default function Column({const headerRef = useRef<HTMLHeadingElement>(null);const contentRef = useRef<HTMLDivElement>(null);const scrollFade = useScrollFade(contentRef);- // Cards slide to follow the drop gap opening and closing. No card opens out of nothing: one- // 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, 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.- // Measured only when the column's own cards have changed, which is the only thing that moves- // them. Anything else that redraws the board, scrolling it sideways above all, would otherwise- // have every column read a position for every card it holds.+ // Cards slide to follow the drop gap opening and closing. Measured only when the column's own+ // cards have changed: reading one position costs a layout of the whole board, and anything+ // else that redraws it would have every column read one per card.const measured = useRef<unknown>();const cardsChanged = measured.current !== columnItems;measured.current = columnItems;@@ -206,19 +188,13 @@ export default function Column({selector: ".board-note",// Paused where nothing has moved the cards, so the places it knows are still good.paused: !cardsChanged,- // Switched off outright for the length of a gesture, which is what makes the commit that- // ends one record where the cards landed rather than slide them there: the places it knew- // are from before the drag, and a card let go has already been carried to its own.+ // Off for the length of a gesture, so the commit that ends one records where the cards+ // landed rather than sliding them there.disabled: !!draggedCard || !!draggedColumn || !!isResizing});- // The gap is opened without touching what the column holds. Putting an element among the- // cards, taking one out, or carrying one to another place all cost the same: the board- // restyles every element it holds, which on a large one is most of a second. Changing the- // size of an element that stays put, and moving one that stands outside the flow, cost- // nothing, so the gap is a standing element that slides and the cards beside it are- // transformed. The cards carry a transform transition already, so the sliding is the- // browser's to do.+ // The gap is a standing element that slides, and the cards beside it are transformed: putting+ // one among the cards, or taking one out, restyles every element the board holds.const gapRef = useRef<HTMLDivElement>(null);const roomRef = useRef<HTMLDivElement>(null);/** Whether a card was being carried at the previous commit. */@@ -226,9 +202,8 @@ export default function Column({/** Which cards were last told to stand aside, so only what changed is written. */const aside = useRef({ from: 0, until: 0, room: 0 });useLayoutEffect(() => {- // The two commits a drag opens and closes on. A card is drawn in either one in the place- // its own transform already had it, so easing would slide it from somewhere it never- // stood; every commit in between moves the cards for real and eases as usual.+ // The commits a drag opens and closes on, where a card is already standing where it is+ // being drawn. Every commit in between moves the cards for real and eases as usual.const atOnce = carried.current !== !!draggedCard;carried.current = !!draggedCard;@@ -239,6 +214,9 @@ export default function Column({const cards = area.querySelectorAll<HTMLElement>(".board-note");const height = draggedCard?.height ?? STOCK_GAP_HEIGHT;const room = dropIndex === null ? 0 : height + cardSpacing();+ // Read with the places below, before anything is written: every card the column can show+ // moves, and the ones past its foot are left alone whatever it holds.+ const reach = Math.ceil(area.clientHeight / MIN_CARD_HEIGHT) + 1;// Read before anything is written, and `offsetTop` is no business of a transform anyway.if (dropIndex !== null) {@@ -266,7 +244,7 @@ export default function Column({}const from = dropIndex;- const until = Math.min(cards.length, from + CARDS_ASIDE);+ const until = Math.min(cards.length, from + reach);const last = aside.current;// A step of a drag moves the gap by a card, so only the few cards it passed change what// they are told; the rest of the window is already standing where it should.@@ -309,9 +287,8 @@ export default function Column({/*** Whether the cards have been drawn, which they stay once they have been.*- * A column collapsed when the board opens draws none of them, so a reader who keeps a long one- * closed pays nothing for it. Past the first open they are held in the page and hidden rather- * than taken out, closing costing a fraction of what building them again does.+ * A column collapsed when the board opens draws none of them; past the first open they are+ * hidden rather than taken out, which is what makes closing one cheap.*/const [ isDrawn, setIsDrawn ] = useState(!isCollapsed);if (!isDrawn && !isCollapsed) {@@ -673,12 +650,11 @@ export default function Column({/*** Puts a card where a transform says, easing it there unless the board is drawing a still frame.*- * A still frame is one the board draws an element in the place its own transform already had it,- * which is what a drag's lift and its drop both are. The transition is suppressed on the card- * rather than by a rule under the board's own class: such a rule matches every card on the board,- * and Chrome restyles all of them for the few that move.+ * Suppressed on the card rather than by a rule under the board's own class, which would match+ * every card on it.** @param transform what to write, or `null` to put the card back where the column draws it.+ * @param atOnce whether the card is already standing where it is being put, as at a lift or a drop.*/export function placeCard(card: HTMLElement, transform: string | null, atOnce: boolean) {if (atOnce) {@@ -692,19 +668,27 @@ export function placeCard(card: HTMLElement, transform: string | null, atOnce: bcard.style.transform = transform;}- if (!atOnce || restoring !== undefined) {+ if (!atOnce) {return;}+ // Started afresh on every call: a lift and the drop that follows it a frame later would+ // otherwise be put back on the first one's schedule, before the drop has been drawn.+ if (restoring !== undefined) {+ cancelAnimationFrame(restoring);+ }+// Put back a frame later than the one that draws them, since a frame's callbacks run before// the styles it paints are worked out.- restoring = requestAnimationFrame(() => requestAnimationFrame(() => {- restoring = undefined;- for (const held of settling) {- held.style.removeProperty("transition");- }- settling.clear();- }));+ restoring = requestAnimationFrame(() => {+ restoring = requestAnimationFrame(() => {+ restoring = undefined;+ for (const held of settling) {+ held.style.removeProperty("transition");+ }+ settling.clear();+ });+ });}/** The cards whose transition is suppressed, waiting for the frame that puts it back. */diff --git a/apps/client/src/widgets/collections/board/drag_measure.spec.ts b/apps/client/src/widgets/collections/board/drag_measure.spec.tsindex 724ef2576e..6c730a3367 100644--- a/apps/client/src/widgets/collections/board/drag_measure.spec.ts+++ b/apps/client/src/widgets/collections/board/drag_measure.spec.ts@@ -109,20 +109,44 @@ 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", () => {+ it("reads the ends of a column once it knows what the cards between 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(); };- }+ const count = () => {+ let reads = 0;+ for (const note of board.querySelectorAll<HTMLElement>(".board-note")) {+ const box = note.getBoundingClientRect.bind(note);+ note.getBoundingClientRect = () => { reads++; return box(); };+ }++ return () => reads;+ };- // 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.+ // Where a column's cards begin is still its own to say, so the first of them is read, and+ // the last says whether counting up from it still lands where the column does. The one+ // between them is placed by the count alone.+ let reads = count();expect(measureBoard(board).columns.map((column) => column.cards)).toEqual(expected);- expect(reads).toBe(2);+ expect(reads()).toBe(4);+ });++ /**+ * Nothing announces that a card stands taller than it did: a title can grow a line and an+ * attribute can be promoted onto it while the board is open.+ */+ it("reads every card again once what it remembers no longer lands where the column does", () => {+ const board = buildBoard({ cardCounts: [ 3 ] });+ measureBoard(board);++ // The middle card grows, which moves the one under it. Counting up from the first would+ // put both of them 20px short, the height it remembers for the middle one being the old.+ const cards = board.querySelectorAll<HTMLElement>(".board-note");+ place(cards[1], { left: 0, top: 90, width: 100, height: 80 });+ place(cards[2], { left: 0, top: 180, width: 100, height: 50 });++ expect(measureBoard(board).columns[0].cards)+ .toEqual([ { top: 10, height: 50 }, { top: 70, height: 80 }, { top: 160, height: 50 } ]);});it("hands back each column's card area, and counts a column holding none", () => {diff --git a/apps/client/src/widgets/collections/board/drag_measure.ts b/apps/client/src/widgets/collections/board/drag_measure.tsindex fd4eae5899..62825b6f7a 100644--- a/apps/client/src/widgets/collections/board/drag_measure.ts+++ b/apps/client/src/widgets/collections/board/drag_measure.ts@@ -70,9 +70,8 @@ export function toAreaY(area: HTMLElement, clientY: number): number {* 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.+ * A height holds until the card's own content changes, which {@link measureCards} checks for, or+ * until the window does, which drops the lot.*/const heights = new Map<string, number>();@@ -96,28 +95,53 @@ export function forgetCardHeights() {/*** The cards of one column, in the area's content space.*- * 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 the carried card leaves room for stands outside the column's flow, so it displaces no- * card and none of its room has to be given back: what is measured is the column as it stands.+ * A card whose height is 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. The+ * dragged card is counted with the rest, so the index this leads to names a place in the list the+ * board holds.*/-function measureCards(area: HTMLElement | null) {+function measureCards(area: HTMLElement | null): CardBox[] {if (!area) {return [];}+ const counted = countCards(area, false);+ const { cards, elements } = counted;+ const last = cards.length - 1;++ // A card whose title or attributes changed stands a different height, and nothing says so, so+ // where the last card really is settles whether what was remembered still holds.+ if (counted.borrowed && last > 0) {+ const at = elements[last].getBoundingClientRect().top - counted.top;+ if (Math.abs(at - cards[last].top) > 1) {+ for (const element of elements) {+ heights.delete(element.dataset.noteId ?? "");+ }++ return countCards(area, true).cards;+ }+ }++ return cards;+}++/**+ * Walks a column's cards, reading what is not known and counting up from what is.+ *+ * @param readEvery whether to read every card rather than the first and the unknown ones alone.+ */+function countCards(area: HTMLElement, readEvery: boolean) {const top = area.getBoundingClientRect().top - area.scrollTop;const cards: CardBox[] = [];+ const elements: HTMLElement[] = [];/** 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;+ /** Whether any card's height came from what was remembered rather than from the page. */+ let borrowed = false;for (const child of area.children) {if (!(child instanceof HTMLElement)) {@@ -135,9 +159,9 @@ function measureCards(area: HTMLElement | null) {// 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) {+ if (height === undefined || !anchored || readEvery) {const box = child.getBoundingClientRect();- height ??= box.height;+ height = box.height;at = box.top - top;anchored = true;if (noteId && box.height) {@@ -147,12 +171,15 @@ function measureCards(area: HTMLElement | null) {spacing = at - foot;}foot = at + height;+ } else {+ borrowed = true;}const stands = at ?? next;cards.push({ top: stands, height });+ elements.push(child);next = stands + height + (spacing ?? 0);}- return cards;+ return { cards, elements, borrowed, top };}diff --git a/apps/client/src/widgets/collections/board/index.css b/apps/client/src/widgets/collections/board/index.cssindex c4ce56377b..fa16ffb86f 100644--- a/apps/client/src/widgets/collections/board/index.css+++ b/apps/client/src/widgets/collections/board/index.css@@ -519,9 +519,8 @@ body.mobile .board-view-container .board-add-column {overscroll-behavior-y: contain;/* What the gap is placed against, and what a card's `offsetTop` is then counted from. */position: relative;- /* Pinned rather than left to stretch: a column animates its own width open, and a body that- follows it lays out every card in it on every frame, their titles rewrapping as it grows.- Matches the column's content box, so a column standing still is drawn no differently. */+ /* Pinned rather than left to stretch, a body that follows the column's width laying out every+ card in it on each frame of an open. Matches the column's content box. */width: calc(var(--board-column-width) - 2 * var(--board-column-border-width));}@@ -742,9 +741,8 @@ body.mobile .board-view-container .board-add-column {}}-/* Outside the column's flow and standing there for good. Carrying it into the cards, or out of- them, is a change to what the column holds, and the board restyles every element it holds for- that; sliding it instead costs nothing. */+/* Outside the column's flow and standing there for good: carrying it into the cards, or out of+ them, restyles every element the board holds. */.board-drop-placeholder {position: absolute;/* It stands over the cards whether it is showing or not, and a press belongs to the card under@@ -771,8 +769,7 @@ body.mobile .board-view-container .board-add-column {}/* The room the gap takes out of the column, so it still scrolls past its last card while one is- held there. It stands in the flow at no height, and is given one for as long as the gap needs- it: growing an element that stays put costs nothing, where adding one does not. */+ held there. Grown from nothing rather than added, an element that stays put costing nothing. */.board-drop-room {height: 0;flex-shrink: 0;@@ -922,9 +919,8 @@ body.mobile .column-drop-placeholder {overflow: hidden;}-/* The cards stay in the page while the column is a strip, and this is what stops them being drawn.- Taking them out instead costs a quarter of a second to close and over a second to open, both of- which land on the frames the width is animating across. */+/* What stops a strip's cards being drawn while they stay in the page. Taking them out instead+ lands on the frames the width is animating across. */.board-view-container .board-column.collapsed > .board-column-content {content-visibility: hidden;}@@ -952,15 +948,10 @@ body.mobile .column-drop-placeholder {width var(--board-expand-duration) ease;}-/* The one frame the board is drawn in as a drag lifts something or lets it go. What the drag- carried aside by a transform already stands where the gap, or the new order, puts it, so the- transform has nothing left to ease: given a transition it would carry the element a full width,- or a column's width, from a place it never stood in. The gap is included so that it takes the- place of the card at once rather than fading into it. Written against the selectors above, which- it has to outweigh.-- The cards are left out: a rule here reaches every one of them, and Chrome restyles all of them- for the few that move. `placeCard` writes this on those few instead. */+/* The one frame the board is drawn in as a drag lifts something or lets it go, where everything+ already stands where it is about to be drawn. Written against the selectors above, which it has+ to outweigh. The cards are left out, a rule here reaching every one of them: `placeCard` writes+ the same thing on the few that move. */.board-view-container.board-still .board-drop-placeholder,.board-view-container.board-still .board-column,.board-view-container.board-still .board-column.collapsed:not(.board-drag-preview),@@ -991,9 +982,8 @@ body.mobile .column-drop-placeholder {width: calc(var(--board-column-width) - 2 * var(--board-column-border-width));}-/* The heading stays where it is, and is revealed with the column; only the cards wait. Hidden by- one property on the body that scrolls them rather than by one on each card: a rule reaching- every card restyles the whole column. */+/* The heading is revealed with the column; only the cards wait. Hidden by one property on the+ body that scrolls them, a rule reaching every card restyling the whole column. */.board-view-container .board-column.expanding > .board-column-content,.board-view-container .board-column.expanding > .board-new-item {opacity: 0;diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsxindex cc48013376..38cd4443d0 100644--- a/apps/client/src/widgets/collections/board/index.tsx+++ b/apps/client/src/widgets/collections/board/index.tsx@@ -592,10 +592,8 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC// card's own dimming are drawn from one place whichever brought the card here.const { isDragging: isDraggingItem, remeasure } = useBoardDrag(containerRef, {onCardStart: (card) => {- // The card leaves the flow as it is lifted, and the gap opens in its place by pushing- // the cards below it down again. Under their transition that reads as the column- // closing up and then sliding back open, so the frame the gap opens in runs without- // one and the column is left looking untouched.+ // The card leaves the flow and the gap opens in its place, which eased would read as+ // the column closing up and sliding back open.holdStill();setDraggedCard({noteId: card.noteId,@@ -616,9 +614,8 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC}},onCardEnd: (card, position) => {- // The cards below the gap carry a `translateY` that the drop replaces with the layout- // the reorder gives them. Cleared under their transition, they slide up from a place- // they never stood in, so the frame the new order is drawn in runs without one.+ // The reorder gives the cards below the gap the layout their `translateY` was+ // standing in for, so clearing it under a transition would slide them up.holdStill();// Put back here rather than left to the columns to draw: the board is about to move a// card, hide the gap and close the room it took, and if those reach the screen in@@ -766,19 +763,28 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC// landed among them all once some are archived and hidden. Translated here so a reorder leaves// every hidden column where it was rather than herding them to the end./**- * Draws the next frame without transitions.+ * Draws the next frame without transitions, for the lift and the drop, where every element is+ * already standing where it is about to be drawn.*- * A drag opens its gap with transforms, and the board draws the elements those transforms- * already moved: at the lift the gap replaces the card that has left the flow, and at the drop- * the reorder replaces the gap. Eased, each element would set off from a place it never stood- * in. Taken off a frame later than the one that draws it, since a frame's callbacks run before- * the styles it paints are worked out.+ * Let go a frame later than the one that draws it, a frame's callbacks running before the+ * styles it paints are worked out.*/+ const stillFor = useRef<number>();const holdStill = useCallback(() => {const container = containerRef.current;container?.classList.add("board-still");- requestAnimationFrame(() => requestAnimationFrame(- () => container?.classList.remove("board-still")));+ // Started afresh on every call: a lift and the drop that follows it a frame later would+ // otherwise be let go on the first one's schedule, before the drop has been drawn.+ if (stillFor.current !== undefined) {+ cancelAnimationFrame(stillFor.current);+ }++ stillFor.current = requestAnimationFrame(() => {+ stillFor.current = requestAnimationFrame(() => {+ stillFor.current = undefined;+ container?.classList.remove("board-still");+ });+ });}, []);const handleColumnDrop = useCallback((fromIndex: number, toIndex: number, animate = true) => {@@ -978,10 +984,8 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC* Puts every card back where the column draws it, closes every gap and gives back the room they* took.*- * The columns do this for themselves as they are drawn, but a drop moves a card, hides a gap and- * closes its room in one go, and those reaching the screen in separate frames is a gap the reader- * sees open where nothing is being carried. Done here first, the board is already tidy whichever- * way the drawing falls.+ * The columns do this for themselves as they are drawn, but a drop is three changes at once and+ * the reader sees a gap standing open if they reach the screen in separate frames.*/function closeGaps(container: HTMLElement | null) {if (!container) {