Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: fix some issues

Adorian Dorand38f3c27d907
diff --git a/apps/client/src/widgets/collections/board/board_drag.spec.tsx b/apps/client/src/widgets/collections/board/board_drag.spec.tsx
index 7f7e9d2bc4..d7a48bbcce 100644
--- a/apps/client/src/widgets/collections/board/board_drag.spec.tsx
+++ b/apps/client/src/widgets/collections/board/board_drag.spec.tsx
@@ -363,6 +363,24 @@ describe("useBoardDrag, carrying a card", () => {
expect(calls.start).toHaveLength(1);
});
+ /** A lift is not yet a carry: only a card taken some way from where it was is being carried. */
+ it("marks the board as carrying once the lifted card has travelled", () => {
+ setup();
+
+ press(card("n1"), 100, 100, "touch");
+ act(() => { vi.advanceTimersByTime(500); });
+ expect(board.classList.contains("board-dragging")).toBe(true);
+ expect(board.classList.contains("board-carrying")).toBe(false);
+
+ move(110, 105);
+ expect(board.classList.contains("board-carrying")).toBe(false);
+ move(125, 100);
+ expect(board.classList.contains("board-carrying")).toBe(true);
+
+ release(125, 100, "touch");
+ expect(board.classList.contains("board-carrying")).toBe(false);
+ });
+
it("lets a finger that moves off scroll instead of carrying the card", () => {
setup();
diff --git a/apps/client/src/widgets/collections/board/board_drag.ts b/apps/client/src/widgets/collections/board/board_drag.ts
index e439db5a71..943acdca38 100644
--- a/apps/client/src/widgets/collections/board/board_drag.ts
+++ b/apps/client/src/widgets/collections/board/board_drag.ts
@@ -22,6 +22,12 @@ const TOUCH_DELAY_MS = 400;
/** How far a finger may stray in that time and still be resting rather than scrolling. */
const TOUCH_TOLERANCE = 8;
+/**
+ * How far a lifted card travels before it counts as carried, which is when what stands over the
+ * board for it goes. A press that ripens and lets go where it was leaves all of that in place.
+ */
+const CARRY_THRESHOLD = 20;
+
/** How much a carried card shrinks. Written with the movement, a class could not add to it. */
const DRAG_SCALE = 0.9;
@@ -162,6 +168,7 @@ export function useBoardDrag(
window.clearTimeout(held.dwell);
scroller.stop();
container.classList.remove("board-dragging");
+ container.classList.remove("board-carrying");
held.preview?.remove();
held.element.style.display = "";
if (held.frame !== undefined) {
@@ -414,6 +421,7 @@ export function useBoardDrag(
lastY: event.clientY,
touch: event.pointerType !== "mouse",
active: false,
+ carried: false,
preview: undefined,
measurement: undefined,
frame: undefined,
@@ -446,6 +454,12 @@ export function useBoardDrag(
activate();
}
+ const fromLift = Math.hypot(event.clientX - held.startX, event.clientY - held.startY);
+ if (!held.carried && fromLift >= CARRY_THRESHOLD) {
+ held.carried = true;
+ container.classList.add("board-carrying");
+ }
+
// Written in a frame of its own, and only the transform: a move that redrew the board
// would redraw every card on it.
if (held.frame === undefined) {
@@ -876,6 +890,8 @@ type Gesture = (CardSubject | ColumnSubject) & {
lastY: number;
/** Whether the press has to ripen before it carries anything. */
touch: boolean;
+ /** Whether what was lifted has travelled `CARRY_THRESHOLD` from where it was picked up. */
+ carried: boolean;
/** Whether it has, and something is being carried. */
active: boolean;
/** The copy that follows the pointer, what it stands for staying where it is. */
diff --git a/apps/client/src/widgets/collections/board/card.tsx b/apps/client/src/widgets/collections/board/card.tsx
index 7b2c560a70..b775b91029 100644
--- a/apps/client/src/widgets/collections/board/card.tsx
+++ b/apps/client/src/widgets/collections/board/card.tsx
@@ -104,20 +104,26 @@ function Card({
/** Whether the card holds the focus, which on mobile floats its toolbar over the board. */
const [ isFocused, setIsFocused ] = useState(false);
// Focus moving within the card or onto the toolbar keeps the toolbar; anywhere else takes it.
+ // So does the lift: it hides the card's element, which blurs it, and the card is focused
+ // again once it is let go, so the rail stands through a press that ripens and lets go.
const handleFocusOut = useCallback((e: FocusEvent) => {
+ if (isDragging) {
+ return;
+ }
+
const next = e.relatedTarget instanceof Element ? e.relatedTarget : null;
if (next && (cardRef.current?.contains(next) || next.closest(".board-card-toolbar"))) {
return;
}
setIsFocused(false);
- }, []);
+ }, [ isDragging ]);
/**
* Whether the card's rail is wanted. Off the card while its title is edited, since the rename
- * it offers is under way, while it is carried, since the board is being rearranged under the
- * finger, and in selection mode, where the board's own rail acts on the selection instead.
+ * it offers is under way, and in selection mode, where the board's own rail acts on the
+ * selection instead. A card carried away hides it by class (see card_toolbar.css).
*/
- const isRailWanted = isMobile() && isFocused && !isEditing && !isDragging && !isSelecting;
+ const isRailWanted = isMobile() && isFocused && !isEditing && !isSelecting;
// The rail stands for the card, so it goes when the card is scrolled off the screen entirely
// and comes back with it.
const isOnScreen = useIsOnScreen(cardRef, isRailWanted);
diff --git a/apps/client/src/widgets/collections/board/card_toolbar.css b/apps/client/src/widgets/collections/board/card_toolbar.css
index dbed50ecf3..b39756a3fd 100644
--- a/apps/client/src/widgets/collections/board/card_toolbar.css
+++ b/apps/client/src/widgets/collections/board/card_toolbar.css
@@ -49,10 +49,10 @@
}
}
-/* Hidden while a card is carried, under the class board_drag.ts sets on the container itself: a
- class rendered from state changes the attribute mid-drag and drops that one, and its auto-scroll.
- Visibility rather than display, which restarts the entrance when the drag ends. */
-.board-view-container.board-dragging .board-card-toolbar {
+/* Hidden once a lifted card is carried away, under the class board_drag.ts sets on the container
+ itself: a class rendered from state changes the attribute mid-drag and drops the drag's own,
+ and its auto-scroll. Visibility rather than display, which restarts the entrance afterwards. */
+.board-view-container.board-carrying .board-card-toolbar {
visibility: hidden;
}
diff --git a/apps/client/src/widgets/collections/board/card_toolbar.spec.ts b/apps/client/src/widgets/collections/board/card_toolbar.spec.ts
new file mode 100644
index 0000000000..78b6d0c8f1
--- /dev/null
+++ b/apps/client/src/widgets/collections/board/card_toolbar.spec.ts
@@ -0,0 +1,45 @@
+import { describe, expect, it } from "vitest";
+
+import { RailStand } from "./card_toolbar";
+
+describe("RailStand", () => {
+ /**
+ * One rail at a time, the latest to claim: the one it takes over from is hidden meanwhile and
+ * stands again once the newcomer has gone, so two rails wanted at once never overlap.
+ */
+ it("shows the latest claimant and hands the stand back when it goes", () => {
+ const stand = new RailStand();
+ const first = {};
+ const second = {};
+ expect(stand.isTaken).toBe(false);
+
+ stand.claim(first);
+ expect(stand.isTaken).toBe(true);
+ expect(stand.isSuperseded(first)).toBe(false);
+
+ stand.claim(second);
+ expect(stand.isSuperseded(first)).toBe(true);
+ expect(stand.isSuperseded(second)).toBe(false);
+
+ stand.release(second);
+ expect(stand.isSuperseded(first)).toBe(false);
+ expect(stand.isTaken).toBe(true);
+
+ stand.release(first);
+ expect(stand.isTaken).toBe(false);
+ });
+
+ it("tells its listeners of a change, and only of a change", () => {
+ const stand = new RailStand();
+ const token = {};
+ let told = 0;
+ stand.subscribe(() => { told++; });
+
+ stand.claim(token);
+ stand.claim(token);
+ stand.release(token);
+ stand.release(token);
+
+ expect(told).toBe(2);
+ });
+});
diff --git a/apps/client/src/widgets/collections/board/card_toolbar.tsx b/apps/client/src/widgets/collections/board/card_toolbar.tsx
index efdaa6fe25..13d97a31ee 100644
--- a/apps/client/src/widgets/collections/board/card_toolbar.tsx
+++ b/apps/client/src/widgets/collections/board/card_toolbar.tsx
@@ -15,39 +15,36 @@ import OverlayControlGroup, { OverlayControlButton } from "../../react/OverlayCo
export const RAIL_EXIT_MS = 400;
/**
- * Which rail stands on a board, one at a time. A rail arriving while another stands takes over in
- * place, neither sliding in over it nor waiting for it to slide off: the one it replaces is dropped
- * at once. A rail keeps its stand while it slides off, so a newcomer takes over from that too.
+ * Which rail stands on a board: one at a time, the latest to claim it. A rail arriving while
+ * another stands takes over in place, neither sliding in over it nor waiting for it to slide off.
+ * The one it replaces is hidden, and shown again once the newcomer has gone, unless it was on its
+ * way off itself, in which case it is dropped for good (see {@link Rail}).
*/
export class RailStand {
- private owner: object | null = null;
- /** The rails another took over from, which stay dropped even once the stand is free again. */
- private superseded = new WeakSet<object>();
+ /** Every rail that stands, in the order they claimed the stand; the last one is shown. */
+ private claimants: object[] = [];
private listeners = new Set<() => void>();
- /** Whether any rail stands, the one leaving included. */
+ /** Whether any rail stands, one leaving included. */
get isTaken() {
- return this.owner !== null;
+ return this.claimants.length > 0;
}
+ /** Whether a later rail stands over this one. */
isSuperseded(token: object) {
- return this.superseded.has(token);
+ const index = this.claimants.indexOf(token);
+ return index >= 0 && index < this.claimants.length - 1;
}
claim(token: object) {
- if (this.owner === token) return;
- if (this.owner !== null) {
- this.superseded.add(this.owner);
- }
- this.owner = token;
+ if (this.claimants.at(-1) === token) return;
+ this.claimants = [ ...this.claimants.filter((other) => other !== token), token ];
this.notify();
}
- /** Gives the stand up, unless another rail has taken it over meanwhile. */
release(token: object) {
- this.superseded.delete(token);
- if (this.owner !== token) return;
- this.owner = null;
+ if (!this.claimants.includes(token)) return;
+ this.claimants = this.claimants.filter((other) => other !== token);
this.notify();
}
@@ -253,8 +250,19 @@ function Rail({ host, isLeaving, onFocusOut, children }: RailProps) {
const isSuperseded = useSyncExternalStore(
useCallback((listener: () => void) => stand.subscribe(listener), [ stand ]),
useCallback(() => stand.isSuperseded(token), [ stand, token ]));
-
+ // A rail taken over from while on its way off has nothing to come back for: it gives the
+ // stand up and stays gone, rather than reappearing to finish its slide once the newcomer goes.
+ const isDropped = useRef(false);
if (isLeaving && isSuperseded) {
+ isDropped.current = true;
+ }
+ useEffect(() => {
+ if (isDropped.current) {
+ stand.release(token);
+ }
+ }, [ stand, token, isLeaving, isSuperseded ]);
+
+ if (isDropped.current || isSuperseded) {
return null;
}
diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsx
index 37efb505da..b5fbad944b 100644
--- a/apps/client/src/widgets/collections/board/column.tsx
+++ b/apps/client/src/widgets/collections/board/column.tsx
@@ -40,7 +40,8 @@ const MIN_CARD_HEIGHT = 32;
export const EXPAND_MS = 200;
import NoteLink from "../../react/NoteLink";
import {
- BoardActionsContext, BoardDragStateContext, BoardOverlayHostContext, TitleEditor
+ BoardActionsContext, BoardDragStateContext, BoardOverlayHostContext, BoardSelectionModeContext,
+ TitleEditor
} from ".";
import BoardApi from "./api";
import Card from "./card";
@@ -530,8 +531,10 @@ export default function Column({
setIsHeaderFocused(false);
}, []);
- // Off the heading while its title is edited, since the rename it offers is under way.
- const isRailWanted = isMobile() && isHeaderFocused && !isEditing;
+ const isSelecting = useContext(BoardSelectionModeContext);
+ // Off the heading while its title is edited, since the rename it offers is under way, and in
+ // selection mode, where the board's own rail stands for the selection.
+ const isRailWanted = isMobile() && isHeaderFocused && !isEditing && !isSelecting;
const isHeaderOnScreen = useIsOnScreen(headerRef, isRailWanted);
const isRailShown = isRailWanted && isHeaderOnScreen;
// Kept drawn while it slides off.
diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsx
index d929c7c36f..7a7cbc9420 100644
--- a/apps/client/src/widgets/collections/board/index.spec.tsx
+++ b/apps/client/src/widgets/collections/board/index.spec.tsx
@@ -4941,6 +4941,45 @@ describe("Card toolbar on mobile", () => {
expect(toolbar()).toBeNull();
});
+ /**
+ * A drag measures the board as it stands, so nothing may scroll it under the finger. The focus
+ * held for the card last dropped is revealed again whenever the board redraws, and lifting a
+ * card redraws it: that reveal scrolled the board to the earlier card, and the lifted one
+ * landed in a neighbouring column.
+ */
+ it("reveals no earlier focus while a card is carried", async () => {
+ await setup();
+ vi.useFakeTimers();
+ const scrolled = vi.fn();
+ Object.defineProperty(card("tool1"), "scrollIntoView",
+ { value: scrolled, configurable: true });
+ const pressOn = (element: HTMLElement, type: string) => element.dispatchEvent(
+ new PointerEvent(type, {
+ bubbles: true, pointerType: "touch", clientX: 10, clientY: 10
+ }));
+ const settle = () => act(async () => { vi.advanceTimersByTime(1000); });
+
+ // The first drop leaves its focus on the first card, revealed once the board settles.
+ await act(async () => { pressOn(card("tool1"), "pointerdown"); });
+ await settle();
+ await act(async () => { pressOn(card("tool1"), "pointercancel"); });
+ await settle();
+ expect(document.activeElement).toBe(card("tool1"));
+ expect(scrolled).toHaveBeenCalledTimes(1);
+
+ // Lifting another card redraws the board. The first card is left where it is, however
+ // long the second is held.
+ await act(async () => { pressOn(card("tool2"), "pointerdown"); });
+ await settle();
+ const board = container.querySelector(".board-view-container");
+ expect(board?.classList.contains("board-dragging")).toBe(true);
+ await settle();
+ expect(scrolled).toHaveBeenCalledTimes(1);
+
+ await act(async () => { pressOn(card("tool2"), "pointercancel"); });
+ await settle();
+ });
+
/**
* The rail stands for the card, so a card scrolled off the screen entirely takes it along,
* and brings it back. Watched only while the rail is wanted: the observer goes with the focus.
@@ -5042,14 +5081,14 @@ describe("Card toolbar on mobile", () => {
* go: with no geometry under happy-dom a release is a drop at the head of a column, and the
* move it starts runs on past the test.
*/
- it("is hidden while the card is carried, and back once the drop is aborted", async () => {
+ it("stands through a lift, hides once the card is carried, and is back after", async () => {
await setup();
await focus("tool1");
vi.useFakeTimers();
const element = card("tool1");
- const press = (type: string) => element.dispatchEvent(new PointerEvent(type, {
- bubbles: true, pointerType: "touch", clientX: 10, clientY: 10
+ const press = (type: string, x = 10) => element.dispatchEvent(new PointerEvent(type, {
+ bubbles: true, pointerType: "touch", clientX: x, clientY: 10
}));
const board = () => container.querySelector(".board-view-container");
@@ -5057,14 +5096,24 @@ describe("Card toolbar on mobile", () => {
press("pointerdown");
vi.advanceTimersByTime(1000);
});
- // The drag's own class, which the rail hides under. It must survive the board's redraw
- // on activation, or the auto-scroll it keeps working stops with it.
+ // The drag's own class, which must survive the board's redraw on activation, or the
+ // auto-scroll it keeps working stops with it. The rail stands: nothing has moved yet.
expect(board()?.classList.contains("board-dragging")).toBe(true);
+ expect(board()?.classList.contains("board-carrying")).toBe(false);
+ expect(toolbar()?.classList.contains("leaving")).toBe(false);
- await act(async () => { press("pointercancel"); });
+ // Carried once it has come far enough from where it was lifted, which is what hides the
+ // rail (by class, see card_toolbar.css); a wobble under the finger does not.
+ await act(async () => { press("pointermove", 22); });
+ expect(board()?.classList.contains("board-carrying")).toBe(false);
+ await act(async () => { press("pointermove", 35); });
+ expect(board()?.classList.contains("board-carrying")).toBe(true);
+
+ await act(async () => { press("pointercancel", 35); });
expect(board()?.classList.contains("board-dragging")).toBe(false);
+ expect(board()?.classList.contains("board-carrying")).toBe(false);
expect(document.activeElement).toBe(card("tool1"));
- expect(toolbar()).not.toBeNull();
+ expect(toolbar()?.classList.contains("leaving")).toBe(false);
});
});
@@ -5209,6 +5258,20 @@ describe("Selection mode on mobile", () => {
openCard.mockRestore();
});
+ /** The selection rail stands for the mode; a heading focused meanwhile floats no rail. */
+ it("keeps the selection rail alone when a heading is focused", async () => {
+ await setup();
+ await startSelecting();
+ const heading = container.querySelector<HTMLElement>(".board-column h3");
+ if (!heading) throw new Error("expected a heading");
+
+ await act(async () => { heading.focus(); });
+ expect(container.querySelectorAll(".board-card-toolbar")).toHaveLength(1);
+ expect([ ...container.querySelectorAll(".board-card-toolbar button") ]
+ .map(button => [ ...button.classList ].find(name => name.startsWith("bx-"))))
+ .toEqual([ "bx-trash", "bx-dots-vertical-rounded" ]);
+ });
+
it("selects the column of the last picked card, and acts on the whole selection", async () => {
await setup();
const deleteNotes = vi.spyOn(branches, "deleteNotes").mockResolvedValue(false);
diff --git a/apps/client/src/widgets/collections/board/keyboard.ts b/apps/client/src/widgets/collections/board/keyboard.ts
index 6862b90b11..efab3418a6 100644
--- a/apps/client/src/widgets/collections/board/keyboard.ts
+++ b/apps/client/src/widgets/collections/board/keyboard.ts
@@ -118,6 +118,10 @@ export function useBoardKeyboard({
const pending = pendingFocus.current;
const container = containerRef.current;
if (!pending || !container) return;
+ // Nothing is revealed while something is carried: the drag measures the board as it
+ // stands, and a reveal scrolls it out from under the finger. The intent keeps until the
+ // drop, which names what to focus next.
+ if (container.classList.contains("board-dragging")) return;
if (!("noteId" in pending.intent)) {
// Moving a focused element blurs it, so the header is focused again by name once the
@@ -643,7 +647,9 @@ function reveal(element: HTMLElement) {
// scroll to where the card stood when they fired.
window.clearTimeout(pendingReveal);
pendingReveal = window.setTimeout(() => {
- if (!element.isConnected) {
+ // Nor scrolled to once something is being carried, for a reveal that fired just before.
+ if (!element.isConnected
+ || element.closest(".board-view-container")?.classList.contains("board-dragging")) {
return;
}