Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: fix some issues

Adorian Doran8bbab59aadb4
diff --git a/apps/client/src/widgets/collections/board/data.spec.ts b/apps/client/src/widgets/collections/board/data.spec.ts
index faf3665e4b..5a66d0ace6 100644
--- a/apps/client/src/widgets/collections/board/data.spec.ts
+++ b/apps/client/src/widgets/collections/board/data.spec.ts
@@ -3,7 +3,62 @@ import { describe, expect, it } from "vitest";
import FBranch from "../../../entities/fbranch";
import froca from "../../../services/froca";
import { buildNote } from "../../../test/easy-froca";
-import { getBoardData } from "./data";
+import { applyCardMove, type ColumnMap, getBoardData } from "./data";
+
+describe("applyCardMove", () => {
+ /** Cards named by their note id, which is all this reads them for. */
+ function board(columns: Record<string, string[]>): ColumnMap {
+ return new Map(Object.entries(columns).map(([ column, ids ]) => [
+ column,
+ ids.map((noteId) => ({ note: { noteId }, branch: { branchId: `b_${noteId}` } }))
+ ])) as unknown as ColumnMap;
+ }
+
+ const names = (map: ColumnMap, column: string) =>
+ (map.get(column) ?? []).map((item) => item.note.noteId);
+
+ it("takes a card out of one column and puts it in another", () => {
+ const next = applyCardMove(board({ A: [ "a1", "a2", "a3" ], B: [ "b1", "b2" ] }),
+ "a2", "A", "B", 1);
+
+ expect(names(next, "A")).toEqual([ "a1", "a3" ]);
+ expect(names(next, "B")).toEqual([ "b1", "a2", "b2" ]);
+ });
+
+ it("puts it at either end of the column it lands in", () => {
+ const start = applyCardMove(board({ A: [ "a1" ], B: [ "b1", "b2" ] }), "a1", "A", "B", 0);
+ expect(names(start, "B")).toEqual([ "a1", "b1", "b2" ]);
+
+ const end = applyCardMove(board({ A: [ "a1" ], B: [ "b1", "b2" ] }), "a1", "A", "B", 2);
+ expect(names(end, "B")).toEqual([ "b1", "b2", "a1" ]);
+ });
+
+ it("puts it in a column holding none", () => {
+ const next = applyCardMove(board({ A: [ "a1" ], B: [] }), "a1", "A", "B", 0);
+
+ expect(names(next, "A")).toEqual([]);
+ expect(names(next, "B")).toEqual([ "a1" ]);
+ });
+
+ /**
+ * The place counts the column as it stood, the card included, so a place beyond where the card
+ * was names one card earlier once it has been taken out.
+ */
+ it("counts a move down its own column against the list it came from", () => {
+ const start = board({ A: [ "a1", "a2", "a3" ] });
+
+ expect(names(applyCardMove(start, "a1", "A", "A", 3), "A")).toEqual([ "a2", "a3", "a1" ]);
+ expect(names(applyCardMove(start, "a1", "A", "A", 2), "A")).toEqual([ "a2", "a1", "a3" ]);
+ expect(names(applyCardMove(start, "a3", "A", "A", 0), "A")).toEqual([ "a3", "a1", "a2" ]);
+ expect(names(applyCardMove(start, "a3", "A", "A", 1), "A")).toEqual([ "a1", "a3", "a2" ]);
+ });
+
+ it("leaves the board alone for a card it does not hold", () => {
+ const start = board({ A: [ "a1" ], B: [] });
+
+ expect(applyCardMove(start, "nope", "A", "B", 0)).toBe(start);
+ });
+});
describe("Board data", () => {
it("deduplicates cloned notes", async () => {
diff --git a/apps/client/src/widgets/collections/board/data.ts b/apps/client/src/widgets/collections/board/data.ts
index 14b88c9dcf..a8106582fe 100644
--- a/apps/client/src/widgets/collections/board/data.ts
+++ b/apps/client/src/widgets/collections/board/data.ts
@@ -8,6 +8,38 @@ export type ColumnMap = Map<string, {
note: FNote;
}[]>;
+/**
+ * The columns as they stand once a card has moved, for drawing the outcome before the writes land.
+ *
+ * A card crossing columns is written twice, the value first and the branch after, and each lands a
+ * redraw of its own. The first shows the card in its new column at whatever place its old branch
+ * gives it, which is above every card already there.
+ *
+ * @param index where the card goes, counting the target column as it stands at the moment of the
+ * drop, the card itself included where it does not leave its column.
+ */
+export function applyCardMove(
+ byColumn: ColumnMap, noteId: string, from: string, to: string, index: number
+): ColumnMap {
+ const source = [ ...(byColumn.get(from) ?? []) ];
+ const at = source.findIndex((item) => item.note.noteId === noteId);
+ if (at < 0) {
+ return byColumn;
+ }
+
+ const [ moved ] = source.splice(at, 1);
+ const next = new Map(byColumn);
+ next.set(from, source);
+
+ const target = from === to ? source : [ ...(byColumn.get(to) ?? []) ];
+ // Taking the card out shifts everything after it up one, so a place beyond where it stood
+ // names one card earlier in the list left behind.
+ target.splice(from === to && index > at ? index - 1 : index, 0, moved);
+ next.set(to, target);
+
+ return next;
+}
+
/**
* @param definitionOptions the choices the board's group-by definition offers, empty when it has no
* select definition of its own to lead the column order.
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index 8e024bf475..3d869ac2e1 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -34,7 +34,7 @@ import BoardApi from "./api";
import { DEFAULT_GROUP_BY, getStatusDefinition, INBOX_COLUMN } from "./columns";
import Column from "./column";
import ColumnLimitDialog from "./column_limit";
-import { ColumnMap, getBoardData } from "./data";
+import { applyCardMove, ColumnMap, getBoardData } from "./data";
import { useBoardKeyboard } from "./keyboard";
export interface BoardViewData {
@@ -212,6 +212,9 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
const [ columnNameToEdit, setColumnNameToEdit ] = useState<string>();
const [ columnLimitToEdit, setColumnLimitToEdit ] = useState<string>();
const [ activeColumn, setActiveColumn ] = useState<string>();
+ // How many card moves are still being written. The board is drawn as they will leave it, so a
+ // redraw from the first of a move's two writes would take that back.
+ const movesInFlight = useRef(0);
/** Bumped when the definition changes, since it is read off the note rather than held in state. */
const [ definitionRevision, setDefinitionRevision ] = useState(0);
// A ref rather than state: `api` is rebuilt on every refresh, and the map has to outlive those
@@ -295,6 +298,13 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
}), [ branchIdToEdit, columnNameToEdit, draggedCard, draggedColumn, dropPosition, dropTarget ]);
function refresh() {
+ // A move under way has already been drawn where it will land. Held here rather than at each
+ // caller, since a card crossing columns changes both the note and the board's children, and
+ // either of those reaches this by a path of its own.
+ if (movesInFlight.current) {
+ return;
+ }
+
// `getBoardData` reads notes, so refreshes can resolve out of order and one issued for the
// board the user has left can arrive after the next board's. What it has to say is about
// sources no longer on screen, the pending renames it reports as settled included.
@@ -304,7 +314,9 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
parentNote, statusAttributeWithPrefix, viewConfig ?? {}, includeArchived,
statusDefinition?.options ?? [], pendingRenamesRef.current.writes.renames, inboxEnabled)
.then(({ byColumn, columns, newPersistedData, isInRelationMode, settledRenames }) => {
- if (refreshId !== refreshSeqRef.current) return;
+ if (refreshId !== refreshSeqRef.current) {
+ return;
+ }
for (const settled of settledRenames) {
settleColumn(pendingRenamesRef.current.writes, settled);
@@ -360,10 +372,23 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
},
onCardEnd: (card, position) => {
const branchId = byColumn?.get(card.fromColumn)?.[card.index]?.branch.branchId;
- if (position && branchId) {
+ if (position && branchId && byColumn) {
+ // Drawn where it landed at once, and held there until both writes are in: each of
+ // them lands a redraw, and the first would show the card at the top of the column.
+ setByColumn(applyCardMove(
+ byColumn, card.noteId, card.fromColumn, position.column, position.index));
+ movesInFlight.current++;
+ // Any refresh already on its way is about the board as it stood before the drop,
+ // and would put the card back where it came from as it resolves.
+ refreshSeqRef.current++;
api.moveWithinBoard(
card.noteId, branchId, card.index, position.index,
- card.fromColumn, position.column);
+ card.fromColumn, position.column)
+ // Nothing is asked for once the writes are in: `froca` learns of the branch
+ // move from the server a moment later, so a refresh here reads the new column
+ // with the old order and puts the card at the top of it. The change reaches the
+ // board as an entity reload, which settles it once there is something to read.
+ .finally(() => { movesInFlight.current--; });
}
setDraggedCard(null);
setDropTarget(null);