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 Doranfc4cb57c0a4b
diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.tsindex 56d866e1bd..65ad2abc8e 100644--- a/apps/client/src/widgets/collections/board/api.spec.ts+++ b/apps/client/src/widgets/collections/board/api.spec.ts@@ -513,6 +513,28 @@ describe("BoardApi card operations", () => {.toHaveBeenLastCalledWith([ second.branch.branchId ], first.branch.branchId);});+ /**+ * That memory stands in for what the column map does not show yet, so it is worth exactly as+ * long as the map: kept across a refresh it names a card that is no longer last, and the next+ * one is filed behind it rather than at the end.+ */+ it("forgets what it sent to a column once the board has drawn that column again", async () => {+ const { api, items } = createBoardWithCards();+ const [ first, second, third ] = items;++ await api.moveToColumnEnd(first.note.noteId, first.branch.branchId, "To Do");++ // The refresh that catches up, and with it a card that now stands last in that column.+ api.update(+ new Map([ [ "To Do", [ first, third ] ], [ "Done", [ second ] ] ]),+ [ "To Do", "Done" ], first.note.getParentNotes()[0], "status", {}, () => {}, () => {});++ // Behind what the board now shows last, not behind what this sent before it.+ await api.moveToColumnEnd(second.note.noteId, second.branch.branchId, "To Do");+ expect(branches.moveAfterBranch)+ .toHaveBeenLastCalledWith([ second.branch.branchId ], third.branch.branchId);+ });+it("moves nothing for a card dropped where it is, or one it cannot find", async () => {const { api, items } = createBoardWithCards();const [ first ] = items;diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.tsindex 0387af3626..ac6db6b9d4 100644--- a/apps/client/src/widgets/collections/board/api.ts+++ b/apps/client/src/widgets/collections/board/api.ts@@ -125,8 +125,7 @@ export default class BoardApi {* Points the api at the board as it now stands.** A refresh calls this instead of building a new api, so that the object every card holds keeps- * its identity and a move redraws only the cards whose position changed. What the api works out- * for itself, such as {@link sentToColumnEnd}, is kept.+ * its identity and a move redraws only the cards whose position changed.*/update(byColumn: ColumnMap | undefined,@@ -138,6 +137,13 @@ export default class BoardApi {setBranchIdToEdit: (branchId: string | undefined) => void,statusDefinition?: BoardStatusDefinition) {+ // What was sent to the end of a column stands in for what the map does not show yet, so it+ // is given up with the map it stands in for. Kept across a refresh, it would name a branch+ // that is no longer last and put the next card before it.+ if (byColumn !== this.byColumn) {+ this.sentToColumnEnd.clear();+ }+this.byColumn = byColumn;this.columns = columns;this.parentNote = parentNote;diff --git a/apps/client/src/widgets/collections/board/keyboard.spec.tsx b/apps/client/src/widgets/collections/board/keyboard.spec.tsxindex 896f57ddae..63d852da14 100644--- a/apps/client/src/widgets/collections/board/keyboard.spec.tsx+++ b/apps/client/src/widgets/collections/board/keyboard.spec.tsx@@ -12,6 +12,7 @@ import appContext from "../../../components/app_context";import Component from "../../../components/component";import attributes from "../../../services/attributes";import branches from "../../../services/branches";+import { FLIP_DURATION_MS, FLIP_SETTLE_MS } from "../../react/flip";import contextMenu from "../../../menus/context_menu";import froca from "../../../services/froca";import server from "../../../services/server";@@ -60,6 +61,7 @@ describe("Board keyboard", () => {container.remove();container = undefined;}+ vi.useRealTimers();});describe("moving about", () => {@@ -84,6 +86,53 @@ describe("Board keyboard", () => {expect(focusedName(board)).toBe("To Do");});+ /**+ * What a move sends focus to is carried back to where it came from and let go, which turns+ * the move into a slide. The browser scrolls to where a thing is drawn, so a scroll asked+ * for while that slide runs finds it where it started and leaves the column alone.+ */+ it("brings what it focuses into view once the slide it made has run", async () => {+ const board = await renderBoard();+ const seen = vi.fn();+ for (const card of board.querySelectorAll(".board-note")) {+ Object.defineProperty(card, "scrollIntoView", {+ value: seen, configurable: true, writable: true+ });+ }++ // Only around the press: the board is drawn with timers of its own to wait for.+ vi.useFakeTimers();+ focusHeader(board, 0);+ press(board, "ArrowDown");+ expect(seen).not.toHaveBeenCalled();++ // Not while the slide is still running, which is the whole point of the wait.+ act(() => { vi.advanceTimersByTime(FLIP_DURATION_MS - 20); });+ expect(seen).not.toHaveBeenCalled();++ act(() => { vi.advanceTimersByTime(FLIP_SETTLE_MS); });+ expect(seen).toHaveBeenCalledWith({ block: "nearest", inline: "nearest" });+ });++ it("takes the column to its end for the last card, which a fade stands over", async () => {+ const board = await renderBoard();+ const content = board.querySelector<HTMLElement>(".board-column-content");+ if (!content) throw new Error("expected a scrollable column body");+ Object.defineProperty(content, "scrollHeight", {+ value: 640, configurable: true, writable: true+ });+ content.scrollTop = 0;++ vi.useFakeTimers();+ focusHeader(board, 0);+ press(board, "ArrowDown");+ press(board, "ArrowDown");+ act(() => { vi.advanceTimersByTime(FLIP_SETTLE_MS + 10); });++ expect(focusedName(board)).toBe("Second");+ expect(content.scrollTop).toBe(640);+ });+it("crosses to the first card of the next column rather than to its header", async () => {const board = await renderBoard();focusHeader(board, 0);diff --git a/apps/client/src/widgets/collections/board/keyboard.ts b/apps/client/src/widgets/collections/board/keyboard.tsindex e74fb5fa72..ba38fa4c92 100644--- a/apps/client/src/widgets/collections/board/keyboard.ts+++ b/apps/client/src/widgets/collections/board/keyboard.ts@@ -2,6 +2,7 @@ import { RefObject } from "preact";import { useCallback, useLayoutEffect, useRef } from "preact/hooks";import branches from "../../../services/branches";+import { FLIP_SETTLE_MS } from "../../react/flip";import BoardApi from "./api";import { ColumnMap } from "./data";@@ -102,7 +103,7 @@ export function useBoardKeyboard({// board has drawn it in its new place, and the hold is done with.const element = findInColumn(container, pending.intent.column, pending.intent.part);if (element) {- element.focus();+ reveal(element);pendingFocus.current = null;}return;@@ -117,7 +118,9 @@ export function useBoardKeyboard({return;}- element?.focus();+ if (element) {+ reveal(element);+ }});/**@@ -367,7 +370,7 @@ function walk(container: HTMLElement, from: Spot, key: string) {const element = elementAt(container, next);if (!element) return false;- element.focus();+ reveal(element);return true;}@@ -470,6 +473,40 @@ function move(};}+/** The reveal waiting to run, so that a newer one takes its place rather than joining it. */+let pendingReveal: number | undefined;++/**+ * Focuses an element and brings it into view once it has come to rest.+ *+ * A moved element is carried back to where it was and let go, which turns the move into a slide,+ * and for as long as that slide runs the element is drawn between the two places. The browser+ * scrolls to where a thing is drawn, so asking any earlier scrolls to where it came from, decides+ * it is in view already, and leaves the column standing where it was.+ */+function reveal(element: HTMLElement) {+ element.focus({ preventScroll: true });++ // Only the last one asked for: keys pressed faster than a slide runs would otherwise each+ // scroll to where the card stood at the time, which is nowhere it is by the time they land.+ window.clearTimeout(pendingReveal);+ pendingReveal = window.setTimeout(() => {+ if (!element.isConnected) {+ return;+ }++ // The last card takes the column to its end rather than only into view: what stands under+ // it is the gap it carries and the fade the column draws over its bottom edge, and coming+ // to rest against those leaves it behind them.+ const content = element.closest<HTMLElement>(".board-column-content");+ if (content && !element.nextElementSibling) {+ content.scrollTop = content.scrollHeight;+ } else {+ element.scrollIntoView({ block: "nearest", inline: "nearest" });+ }+ }, FLIP_SETTLE_MS);+}+/*** Moves the column focus is in, answering with what to put focus back on. Whatever was focused* stays focused: the browser blurs an element it moves, so it would otherwise be left focused ondiff --git a/apps/client/src/widgets/react/flip.ts b/apps/client/src/widgets/react/flip.tsindex c54b329a53..db2cdc6611 100644--- a/apps/client/src/widgets/react/flip.ts+++ b/apps/client/src/widgets/react/flip.ts@@ -4,8 +4,8 @@ import { useCallback, useEffect, useLayoutEffect, useRef } from "preact/hooks";/** How long a slide or a growth takes. Matches the transitions the board's own rules carry. */export const FLIP_DURATION_MS = 200;-/** How long after a growth the places it moved are read again, once it has settled. */-const SETTLE_MS = FLIP_DURATION_MS + 40;+/** How long until a slide or a growth has run its course and what it moved stands still. */+export const FLIP_SETTLE_MS = FLIP_DURATION_MS + 40;/*** How far a child moves before it is taken to have moved at all.@@ -136,7 +136,7 @@ export function useFlip(settled.current = window.setTimeout(() => {settling.current = false;seen.current = read();- }, SETTLE_MS);+ }, FLIP_SETTLE_MS);});useEffect(() => () => window.clearTimeout(settled.current), []);@@ -204,7 +204,7 @@ function open(element: HTMLElement, size: number, axis: Axis) {element.style.overflow = "";element.style[property] = "";}- }, SETTLE_MS);+ }, FLIP_SETTLE_MS);});}