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/ui/sortable card: improve scrolling
Adorian Doraned1a6c9fae1d
diff --git a/apps/client/src/widgets/react/SortableCard.tsx b/apps/client/src/widgets/react/SortableCard.tsxindex 27118be366..0bb0d52c48 100644--- a/apps/client/src/widgets/react/SortableCard.tsx+++ b/apps/client/src/widgets/react/SortableCard.tsx@@ -9,7 +9,7 @@ import {import { t } from "../../services/i18n";import { Card, type CardProps } from "./Card";-import { createEdgeScroller, type ScrollTarget } from "./edge_scroll";+import { createEdgeScroller, type Insets, type ScrollTarget } from "./edge_scroll";import { useFlip } from "./flip";import Icon from "./Icon";@@ -29,12 +29,13 @@ const TOUCH_HOLD_MS = 400;const TOUCH_SLACK_PX = 8;/**- * How fast a card walks its scroller with the pointer at the very edge, in pixels a second.+ * How near an edge the pointer comes before the card starts to walk, in pixels.*- * Twice what a board carries a card at: a card is scrolled through in one stretch, where a board is- * walked along while the reader reads the columns it passes.+ * Wider under a finger than the 60 a mouse is given: a thumb covers what it is pointing at, and+ * the strip it has to find is the strip it cannot see.*/-const SCROLL_SPEED = 2700;+const SCROLL_MARGIN = 60;+const TOUCH_SCROLL_MARGIN = 100;/** What a press belongs to rather than to the segment holding it. */const CONTROLS = "button, a, input, select, textarea, label";@@ -117,6 +118,8 @@ export function SortableCard<T extends SortableItem>({const pointerRef = useRef({ x: 0, y: 0 });/** The carry as it now stands, for the scrolling to place the segment again by. */const dragToRef = useRef<(clientY: number) => void>(() => {});+ /** What stands over the edges of the screen while a segment is carried, measured as it starts. */+ const reachRef = useRef<Insets>({ top: 0, bottom: 0, left: 0, right: 0 });/*** Walks whatever the card is scrolled inside while a segment is carried to an edge of it.*@@ -125,12 +128,16 @@ export function SortableCard<T extends SortableItem>({* the frames it asks for outlive the render that started them.*/const scroller = useMemo(() => createEdgeScroller({- speed: SCROLL_SPEED,+ margin: matchMedia("(pointer: coarse)").matches+ ? TOUCH_SCROLL_MARGIN+ : SCROLL_MARGIN,+ reach: () => reachRef.current,// The pointer has not moved, but the card has moved under it.onScroll: () => dragToRef.current(pointerRef.current.y)}), []);useEffect(() => () => scroller.stop(), [ scroller ]);+/*** The order while a segment is being carried, which the caller is told of once it lands.*@@ -243,6 +250,8 @@ export function SortableCard<T extends SortableItem>({return;}+ reachRef.current = fixedChrome(segment);+// Refused where the pointer is already gone, a finger lifted during the rest above all.// Touch captures to the segment of its own accord, so the carry stands without it.try {@@ -547,6 +556,37 @@ export function SortableCard<T extends SortableItem>({);}+/**+ * How much of each edge of the screen is under something fixed, a toolbar or a bar of tabs.+ *+ * A finger cannot be carried under a toolbar, so an edge behind one is an edge the card would+ * never be walked from. Read from what stands at the middle of each edge, which is where a bar+ * spanning the screen is found, and measured once per carry: chrome does not come and go during+ * one.+ */+function fixedChrome(carried: HTMLElement): Insets {+ const insets = { top: 0, bottom: 0, left: 0, right: 0 };+ const middle = window.innerWidth / 2;++ for (const edge of [ "top", "bottom" ] as const) {+ const y = edge === "top" ? 1 : window.innerHeight - 1;++ for (const element of document.elementsFromPoint?.(middle, y) ?? []) {+ // Whatever the segment is drawn inside stands over nothing: it is what is being+ // carried, and the pointer is on it.+ if (element.contains(carried) || getComputedStyle(element).position !== "fixed") {+ continue;+ }++ const box = element.getBoundingClientRect();+ const depth = edge === "top" ? box.bottom : window.innerHeight - box.top;+ insets[edge] = Math.max(insets[edge], Math.min(depth, window.innerHeight / 3));+ }+ }++ return insets;+}+/*** What the card is scrolled inside, nearest first, which is what a carry to an edge walks along.*diff --git a/apps/client/src/widgets/react/edge_scroll.spec.ts b/apps/client/src/widgets/react/edge_scroll.spec.tsindex 6092be6b5d..1fcff17899 100644--- a/apps/client/src/widgets/react/edge_scroll.spec.ts+++ b/apps/client/src/widgets/react/edge_scroll.spec.ts@@ -1,6 +1,6 @@import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";-import { canScroll, createEdgeScroller, edgePull } from "./edge_scroll";+import { canScroll, createEdgeScroller, edgePull, type Insets } from "./edge_scroll";describe("edgePull", () => {/** A box from 100 to 500, with the pull reaching 50 in from each edge. */@@ -135,6 +135,56 @@ describe("createEdgeScroller", () => {expect(column.scrollTop).toBeGreaterThan(200);});+ /**+ * A container reaching under a fixed toolbar has an edge no finger can be carried to: the pull+ * is measured against what is left of the screen, so resting against the bar pulls as hard as+ * resting on the edge would.+ */+ it("measures the pull from the edge of what is left of the screen", () => {+ const bar = 56;+ // As far down as a finger goes with a bar that deep over the foot of the screen.+ const finger = window.innerHeight - bar - 2;++ const walked = (reach?: () => Insets) => {+ // A pane filling the screen, and so reaching under the bar itself.+ const element = container();+ element.getBoundingClientRect = () => new DOMRect(0, 0, 400, window.innerHeight);++ scroller = createEdgeScroller({ margin: 100, speed: 1000, reach });+ scroller.update([ { element, axis: "y" } ], 200, finger);+ vi.advanceTimersByTime(300);+ const walkedBy = element.scrollTop - 200;+ scroller.stop();+ return walkedBy;+ };++ const past = walked();+ const upTo = walked(() => ({ top: 0, bottom: bar, left: 0, right: 0 }));++ expect(past).toBeGreaterThan(0);+ // Nearly the whole pull rather than the little left over past the bar.+ expect(upTo).toBeGreaterThan(past * 1.8);+ });++ /**+ * The app's own scrolling containers are styled `scroll-behavior: smooth`, which would make an+ * animation of every frame's step: each one replaces the last before it has arrived, and the+ * walk crawls at a fraction of the speed it was asked for.+ */+ it("asks for each step instantly, however the container is styled", () => {+ const element = container();+ const asked: unknown[] = [];+ element.scrollTo = (options: unknown) => { asked.push(options); };+ scroller = createEdgeScroller();++ scroller.update([ { element, axis: "y" } ], 200, 395);+ vi.advanceTimersByTime(100);++ expect(asked.length).toBeGreaterThan(0);+ expect(asked.every((options) => (options as ScrollToOptions).behavior === "instant"))+ .toBe(true);+ });+it("says so after each frame that moved something", () => {const element = container();const onScroll = vi.fn();diff --git a/apps/client/src/widgets/react/edge_scroll.ts b/apps/client/src/widgets/react/edge_scroll.tsindex b5915f97cb..c783511ebd 100644--- a/apps/client/src/widgets/react/edge_scroll.ts+++ b/apps/client/src/widgets/react/edge_scroll.ts@@ -21,10 +21,26 @@ export interface ScrollTarget {export interface EdgeScrollOptions {margin?: number;speed?: number;+ /**+ * What the pointer cannot be carried past at each edge of the screen, read afresh each frame.+ *+ * A container reaching under a fixed toolbar, or past the foot of the screen altogether, has an+ * edge no finger can reach: the pull is measured against what is left of it instead, so resting+ * against the toolbar pulls as hard as resting on the edge would.+ */+ reach?: () => Insets;/** Called after each frame that moved something, the places on screen having changed. */onScroll?: () => void;}+/** How far in from each edge of the screen the pointer can be carried. */+export interface Insets {+ top: number;+ bottom: number;+ left: number;+ right: number;+}+/*** How hard a point pulls a box towards one of its edges.*@@ -53,6 +69,48 @@ export function edgePull(near: number, far: number, position: number, margin: nureturn 0;}+/**+ * Moves a container along by a step of the walk.+ *+ * Asked for instantly however the container is styled: `scroll-behavior: smooth` would otherwise+ * make an animation of every frame's step, each one replacing the last before it had arrived, and+ * the walk would crawl at a fraction of the speed it was asked for. The app's own scrolling+ * containers are styled that way.+ */+function walk(element: HTMLElement, axis: "x" | "y", distance: number) {+ const to = axis === "x"+ ? { left: element.scrollLeft + distance }+ : { top: element.scrollTop + distance };++ if (element.scrollTo) {+ element.scrollTo({ ...to, behavior: "instant" });+ return;+ }++ if (axis === "x") {+ element.scrollLeft += distance;+ } else {+ element.scrollTop += distance;+ }+}++/** What the pointer can reach of the screen, which is the screen less whatever stands over it. */+function reachable(insets?: Insets) {+ const { top = 0, bottom = 0, left = 0, right = 0 } = insets ?? {};+ return new DOMRect(left, top,+ Math.max(0, window.innerWidth - left - right),+ Math.max(0, window.innerHeight - top - bottom));+}++/** What the two boxes have in common, which is as much of the container as the pointer reaches. */+function intersect(box: DOMRect, within: DOMRect) {+ const left = Math.max(box.left, within.left);+ const top = Math.max(box.top, within.top);+ return new DOMRect(left, top,+ Math.max(0, Math.min(box.right, within.right) - left),+ Math.max(0, Math.min(box.bottom, within.bottom) - top));+}+/** Whether a container has anywhere left to go in the given direction. */export function canScroll(element: HTMLElement, axis: "x" | "y", pull: number): boolean {const [ offset, size, content ] = axis === "x"@@ -63,7 +121,7 @@ export function canScroll(element: HTMLElement, axis: "x" | "y", pull: number):}export function createEdgeScroller({- margin = MARGIN, speed = SPEED, onScroll+ margin = MARGIN, speed = SPEED, reach, onScroll}: EdgeScrollOptions = {}) {let targets: ScrollTarget[] = [];let point = { x: 0, y: 0 };@@ -75,24 +133,21 @@ export function createEdgeScroller({previous = now;let moved = false;+ const within = reachable(reach?.());+for (const { element, axis } of targets) {// The page's own scroller is the window: its element's box is the whole document,// which starts above the screen as soon as anything has been scrolled.const box = element === document.scrollingElement- ? new DOMRect(0, 0, window.innerWidth, window.innerHeight)- : element.getBoundingClientRect();+ ? within+ : intersect(element.getBoundingClientRect(), within);const pull = axis === "x"? edgePull(box.left, box.right, point.x, margin): edgePull(box.top, box.bottom, point.y, margin);if (!pull || !canScroll(element, axis, pull)) continue;- const distance = pull * speed * elapsed / 1000;- if (axis === "x") {- element.scrollLeft += distance;- } else {- element.scrollTop += distance;- }+ walk(element, axis, pull * speed * elapsed / 1000);moved = true;}