Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: fix some issues

Adorian Doran3577ba6707db
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 0dd2f08596..b85894e94b 100644
--- a/apps/client/src/widgets/collections/board/board_drag.spec.tsx
+++ b/apps/client/src/widgets/collections/board/board_drag.spec.tsx
@@ -103,19 +103,20 @@ describe("useBoardDrag, carrying a card", () => {
setup();
press(card("n1"), 50, 60);
- // Taken 20 below its own top edge, so its top stands at 80 on the page, which is 40 into
- // the card area: past the first card's middle at 25 and above the second's at 85.
- move(120, 100);
+ // Taken 20 below its own top edge, so its top stands at 100 on the page, which is 60 into
+ // the card area: below the card that is left standing there once this one is picked up, so
+ // it goes after it rather than before it.
+ move(120, 120);
act(() => { vi.advanceTimersByTime(20); });
- expect(calls.move.at(-1)?.position).toEqual({ column: "To Do", index: 1 });
+ expect(calls.move.at(-1)?.position).toEqual({ column: "To Do", index: 2 });
// Still over the same place: nothing is reported again.
const reported = calls.move.length;
- move(122, 102);
+ move(122, 122);
act(() => { vi.advanceTimersByTime(20); });
expect(calls.move).toHaveLength(reported);
- // Over the second column, below its only card's middle.
+ // Over the second column, below its only card.
move(320, 200);
act(() => { vi.advanceTimersByTime(20); });
expect(calls.move.at(-1)?.position).toEqual({ column: "Doing", index: 1 });
@@ -196,7 +197,7 @@ describe("useBoardDrag, carrying a card", () => {
// Taken by its very bottom, so its top stands 48 above the pointer from here on.
press(card("n1"), 50, 88);
- move(50, 150);
+ move(50, 120);
act(() => { vi.advanceTimersByTime(20); });
const heldLow = calls.move.at(-1)?.position;
@@ -205,7 +206,7 @@ describe("useBoardDrag, carrying a card", () => {
// Taken by its very top: the same pointer place leaves its top far lower down.
press(card("n1"), 50, 42);
- move(50, 150);
+ move(50, 120);
act(() => { vi.advanceTimersByTime(20); });
// One pointer place, two grips, and the card lands where the card is rather than where the
diff --git a/apps/client/src/widgets/collections/board/board_drag.ts b/apps/client/src/widgets/collections/board/board_drag.ts
index 2bd80fe0f0..84214245e2 100644
--- a/apps/client/src/widgets/collections/board/board_drag.ts
+++ b/apps/client/src/widgets/collections/board/board_drag.ts
@@ -2,7 +2,9 @@ import { RefObject } from "preact";
import { useCallback, useEffect, useRef, useState } from "preact/hooks";
import { useTrackedElement } from "../../react/hooks";
-import { cardInsertionIndex, columnAt, columnCovers, columnInsertionIndex } from "./drag_geometry";
+import {
+ type CardBox, cardInsertionIndex, columnAt, columnCovers, columnInsertionIndex
+} from "./drag_geometry";
import { type BoardMeasurement, measureBoard, toAreaY, toBoardX } from "./drag_measure";
import { createEdgeScroller, type ScrollTarget } from "./edge_scroll";
@@ -215,7 +217,9 @@ export function useBoardDrag(
const position = column
? {
column: column.value,
- index: area ? cardInsertionIndex(column.cards, toAreaY(area, topY)) : 0
+ index: area
+ ? placeIn(column.cards, toAreaY(area, topY), held.card, column.value)
+ : 0
}
: null;
// Standing on the column itself, which for a collapsed one is its heading alone: the
@@ -416,14 +420,56 @@ export function useBoardDrag(
*/
const remeasure = useCallback(() => {
const held = gesture.current;
- if (held?.active && container) {
- held.measurement = measureBoard(container);
+ if (!held?.active || !container) {
+ return;
+ }
+
+ const measurement = measureBoard(container);
+ // Each column keeps the cards it was measured with. The board now holds the gap where the
+ // card was, which stands every card below it one place lower, so reading them again would
+ // take the drag's own doing for a move of its own and the places would creep away from the
+ // card with every one it passes.
+ for (const column of measurement.columns) {
+ const before = held.measurement?.columns.find(({ value }) => value === column.value);
+ if (before) {
+ column.cards = before.cards;
+ }
}
+
+ held.measurement = measurement;
}, [ container ]);
return { isDragging, remeasure };
}
+/**
+ * The place a carried card would take in a column, counting that column as the board holds it.
+ *
+ * The cards were measured with the carried one among them, and it leaves the flow the moment it is
+ * picked up, so the column is read here as it is drawn: without that card, and with everything
+ * below it standing where the card's own place used to be. Its height is its own, so the cards
+ * below move by that and not by a card's worth, which is what a column of mixed heights turns on.
+ * The answer is then counted back into the list the board holds, which is what a move names.
+ */
+function placeIn(cards: CardBox[], y: number, card: DraggedCard, column: string): number {
+ const carried = cards[card.index];
+ if (column !== card.fromColumn || !carried) {
+ return cardInsertionIndex(cards, y);
+ }
+
+ const below = cards[card.index + 1];
+ const vacated = below ? below.top - carried.top : carried.height;
+ const drawn = cards
+ .filter((_, index) => index !== card.index)
+ .map((other, index) => index < card.index
+ ? other
+ : { top: other.top - vacated, height: other.height });
+
+ const index = cardInsertionIndex(drawn, y);
+
+ return index >= card.index ? index + 1 : index;
+}
+
/** Asks an element for its menu the way a right click does, at the place the tap landed. */
function askForMenu(element: HTMLElement, clientX: number, clientY: number) {
element.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true, clientX, clientY }));
diff --git a/apps/client/src/widgets/collections/board/drag.spec.tsx b/apps/client/src/widgets/collections/board/drag.spec.tsx
index 50224afc4d..62568d5482 100644
--- a/apps/client/src/widgets/collections/board/drag.spec.tsx
+++ b/apps/client/src/widgets/collections/board/drag.spec.tsx
@@ -178,15 +178,16 @@ describe("Board drag and drop", () => {
// drawn by. A reader could not press before that; a test can.
await act(async () => { await settle(); });
- // Taken from the first card and let go above the second's middle, so it lands between them.
- await pointer(card, "pointerdown", 50, 50);
- await pointer(columns[0], "pointermove", 50, 120);
+ // The second card, carried up until its top stands above the first, so it lands before it.
+ const second = columns[0].querySelectorAll<HTMLElement>(".board-note")[1];
+ await pointer(second, "pointerdown", 50, 150);
+ await pointer(columns[0], "pointermove", 50, 60);
await act(async () => { await settle(); });
- await pointer(columns[0], "pointerup", 50, 120);
+ await pointer(columns[0], "pointerup", 50, 60);
await act(async () => { await settle(); });
expect(branches.moveBeforeBranch)
- .toHaveBeenCalledWith([ cards[0].branchId ], cards[1].branchId);
+ .toHaveBeenCalledWith([ cards[1].branchId ], cards[0].branchId);
});
/**
diff --git a/apps/client/src/widgets/collections/board/drag_geometry.spec.ts b/apps/client/src/widgets/collections/board/drag_geometry.spec.ts
index 8d7e8f99f1..6d8fcdab79 100644
--- a/apps/client/src/widgets/collections/board/drag_geometry.spec.ts
+++ b/apps/client/src/widgets/collections/board/drag_geometry.spec.ts
@@ -92,20 +92,21 @@ describe("columnCovers", () => {
});
describe("cardInsertionIndex", () => {
- it("places the card before the one it is held over the top half of", () => {
+ it("places the card in the place its top edge has reached", () => {
const cards = stack(50, 50, 50);
expect(cardInsertionIndex(cards, 0)).toBe(0);
- expect(cardInsertionIndex(cards, 24)).toBe(0);
- // Past the first card's middle, so it goes after it.
- expect(cardInsertionIndex(cards, 26)).toBe(1);
- expect(cardInsertionIndex(cards, 74)).toBe(1);
- expect(cardInsertionIndex(cards, 76)).toBe(2);
+ expect(cardInsertionIndex(cards, 49)).toBe(0);
+ // The line between one place and the next runs where one card ends and the next begins.
+ expect(cardInsertionIndex(cards, 51)).toBe(1);
+ expect(cardInsertionIndex(cards, 99)).toBe(1);
+ expect(cardInsertionIndex(cards, 101)).toBe(2);
});
it("places the card at the end when it is held below them all", () => {
expect(cardInsertionIndex(stack(50, 50, 50), 500)).toBe(3);
- expect(cardInsertionIndex(stack(50), 40)).toBe(1);
+ expect(cardInsertionIndex(stack(50), 40)).toBe(0);
+ expect(cardInsertionIndex(stack(50), 60)).toBe(1);
});
it("places the card first in a column holding none", () => {
@@ -113,18 +114,18 @@ describe("cardInsertionIndex", () => {
expect(cardInsertionIndex([], 500)).toBe(0);
});
- /** Cards are not all one height: a tall one's middle is further down than its neighbour's. */
- it("counts each card's own middle rather than a shared height", () => {
+ /** Cards are not all one height: a tall one's place reaches much further down. */
+ it("counts each card's own height rather than a shared one", () => {
const cards = stack(20, 200, 20);
- expect(cardInsertionIndex(cards, 9)).toBe(0);
- expect(cardInsertionIndex(cards, 11)).toBe(1);
- // The tall card runs from 20 to 220, so its middle is at 120.
- expect(cardInsertionIndex(cards, 119)).toBe(1);
- expect(cardInsertionIndex(cards, 121)).toBe(2);
+ expect(cardInsertionIndex(cards, 19)).toBe(0);
+ expect(cardInsertionIndex(cards, 21)).toBe(1);
+ // The tall card runs from 20 to 220, and its place runs with it.
+ expect(cardInsertionIndex(cards, 219)).toBe(1);
+ expect(cardInsertionIndex(cards, 221)).toBe(2);
});
- /** A point above the first card is still a point above its middle. */
+ /** A point above the first card is above every place it could take. */
it("places the card first when it is held above the card area", () => {
expect(cardInsertionIndex(stack(50, 50), -80)).toBe(0);
});
diff --git a/apps/client/src/widgets/collections/board/drag_geometry.ts b/apps/client/src/widgets/collections/board/drag_geometry.ts
index 0d04752ad1..3dca6e4315 100644
--- a/apps/client/src/widgets/collections/board/drag_geometry.ts
+++ b/apps/client/src/widgets/collections/board/drag_geometry.ts
@@ -77,15 +77,23 @@ export function columnCovers(column: ColumnBox, x: number, y: number): boolean {
}
/**
- * Where a card would be inserted in a column: before the first card the point is above the middle
- * of, or at the end.
+ * Where a card would be inserted in a column: in the place the point falls in, or at the end.
+ *
+ * The line between one place and the next runs through the space between two cards, not through
+ * the middle of a card. What is carried is placed by its top edge, which reaches that space half a
+ * card before its middle does, so a middle would leave the gap trailing what is being carried.
*
* @param cards the column's cards, in order.
- * @param y in the content space of the card area holding them.
+ * @param y the top of what is carried, in the content space of the card area holding them.
*/
export function cardInsertionIndex(cards: CardBox[], y: number): number {
for (const [ index, card ] of cards.entries()) {
- if (y < card.top + card.height / 2) {
+ const next = cards[index + 1];
+ const boundary = next
+ ? (card.top + card.height + next.top) / 2
+ : card.top + card.height;
+
+ if (y < boundary) {
return index;
}
}
diff --git a/apps/client/src/widgets/collections/board/drag_measure.spec.ts b/apps/client/src/widgets/collections/board/drag_measure.spec.ts
index 0053eaad26..0d06591369 100644
--- a/apps/client/src/widgets/collections/board/drag_measure.spec.ts
+++ b/apps/client/src/widgets/collections/board/drag_measure.spec.ts
@@ -146,10 +146,10 @@ describe("reading a point against a measurement", () => {
const area = areas.get("Doing");
if (!area) throw new Error("expected a card area");
- // Its one card stands at 10 in the area's content and is 50 tall, so its middle is at 35.
- // The area is 20 scrolled and starts 40 down the page, so that middle is at 55 on screen.
- expect(cardInsertionIndex(column?.cards ?? [], toAreaY(area, 50))).toBe(0);
- expect(cardInsertionIndex(column?.cards ?? [], toAreaY(area, 60))).toBe(1);
+ // Its one card stands at 10 in the area's content and is 50 tall, so its place ends at 60.
+ // The area is 20 scrolled and starts 40 down the page, so that end is at 80 on screen.
+ expect(cardInsertionIndex(column?.cards ?? [], toAreaY(area, 70))).toBe(0);
+ expect(cardInsertionIndex(column?.cards ?? [], toAreaY(area, 90))).toBe(1);
});
it("reads a point over the same column the same way however far the board is scrolled", () => {