Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: fix some issues

Adorian Dorana3b84b414fcb
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 b6b642132a..0c5bc7ac16 100644
--- a/apps/client/src/widgets/collections/board/board_drag.spec.tsx
+++ b/apps/client/src/widgets/collections/board/board_drag.spec.tsx
@@ -92,6 +92,20 @@ describe("useBoardDrag, carrying a card", () => {
expect(reached).toHaveBeenCalledTimes(1);
});
+ /**
+ * The gap holds the space the cards will fill, so the column they land in is already the size
+ * it will be. Sized for one of them, it would resize around the rest as they arrive.
+ */
+ it("holds a gap the size of everything being carried", () => {
+ setup({ carried: [ "n1", "n2" ] });
+
+ press(card("n1"), 50, 60);
+ move(90, 90);
+
+ // Both cards stand 50 tall, and the stylesheet the spacing comes from is not loaded here.
+ expect(calls.start[0]?.height).toBe(100);
+ });
+
/** Two cards on the move leave one card behind the copy. */
it("stacks one card behind a pair", () => {
setup({ carried: [ "n1", "n2" ] });
diff --git a/apps/client/src/widgets/collections/board/board_drag.ts b/apps/client/src/widgets/collections/board/board_drag.ts
index 71f3cc69c0..328e4911e0 100644
--- a/apps/client/src/widgets/collections/board/board_drag.ts
+++ b/apps/client/src/widgets/collections/board/board_drag.ts
@@ -45,7 +45,10 @@ export interface DraggedCard {
noteIds: string[];
fromColumn: string;
index: number;
- /** How tall it stands, so the gap held open for it is the size it will fill. */
+ /**
+ * How tall the cards being carried stand together, so the gap held open for them is the space
+ * they will fill and the column they land in does not resize around them.
+ */
height: number;
}
@@ -549,16 +552,17 @@ function startCard(
if (!element || !columnElement || !noteId) return null;
const cards = [ ...columnElement.querySelectorAll(".board-note") ];
+ const noteIds = carriedWith(noteId);
return {
kind: "card",
element,
menuTarget: element,
card: {
noteId,
- noteIds: carriedWith(noteId),
+ noteIds,
fromColumn: columnElement.dataset.column ?? "",
index: cards.indexOf(element),
- height: element.getBoundingClientRect().height
+ height: carriedHeight(element, noteIds)
},
position: null,
inside: false,
@@ -566,6 +570,23 @@ function startCard(
};
}
+/**
+ * How much room the cards being carried take together: their own heights, and the space between
+ * each pair of them.
+ *
+ * Measured from the board rather than from the column, since a selection can reach across columns.
+ * A card the board is no longer drawing counts for nothing.
+ */
+function carriedHeight(element: HTMLElement, noteIds: string[]) {
+ const board = element.closest(".board-view-container") ?? element.ownerDocument;
+ const spacing = parseFloat(getComputedStyle(element).marginBottom) || 0;
+
+ return noteIds.reduce((total, noteId) => {
+ const card = board.querySelector<HTMLElement>(`.board-note[data-note-id="${noteId}"]`);
+ return total + (card?.getBoundingClientRect().height ?? 0);
+ }, spacing * (noteIds.length - 1));
+}
+
/**
* What a press on a column heading starts. The heading is the handle, as it was when the browser
* carried the column; a press anywhere else in the column belongs to what stands there.
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index ecf42b3387..48e18c9969 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -21,6 +21,7 @@ import { ContextMenuEvent } from "../../../menus/context_menu";
import { isIMEComposing } from "../../../services/shortcuts";
import type { ShortcutHintDefinition } from "../../../services/shortcut_hints";
import toast from "../../../services/toast";
+import ws from "../../../services/ws";
import { escapeHtml, isMobile } from "../../../services/utils";
import { type NoteTypeOption, resolveNoteTypeOptions } from "../../../services/note_types";
import { type PromotedAttributeSetting, resolvePromotedAttributes } from "../promoted_attributes";
@@ -829,19 +830,16 @@ export default function BoardView({
setLandedNoteId(card.noteId);
}
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.
+ // As `holdMove` does for a move made by the keyboard, and for the same reason: the
+ // board is held where the drop has drawn it until `froca` has the changes.
refreshSeqRef.current++;
- // 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.
api.moveWithinBoard(
carried.map((item) => ({
noteId: item.note.noteId,
branchId: item.branch.branchId
})),
position.column, position.index)
+ .then(settled)
.finally(() => { movesInFlight.current--; });
}
setDraggedCard(null);
@@ -1054,7 +1052,13 @@ export default function BoardView({
// put the cards back where they came from as it resolves.
refreshSeqRef.current++;
- return done.finally(() => { movesInFlight.current--; });
+ // Held until `froca` has the changes, not merely until the server has answered for them:
+ // the answers come back over HTTP and the changes over the websocket, so a refresh let
+ // through in between reads a board with some of the cards moved and the rest still where
+ // they were, and draws that.
+ return done
+ .then(settled)
+ .finally(() => { movesInFlight.current--; });
}, [ allByColumn ]);
/** Sends cards to the end of another column, which is where the keyboard puts them. */
@@ -1288,6 +1292,23 @@ export default function BoardView({
* Naming the winning check, rather than returning a boolean, is what lets the profiler attribute a
* redraw to a cause.
*/
+/** How long the board waits for a move's changes before drawing again regardless. */
+const SETTLE_TIMEOUT_MS = 10000;
+
+/**
+ * Waits for `froca` to hold what a move has written.
+ *
+ * `waitForMaxKnownEntityChangeId` never settles while the websocket delivers nothing, and the board
+ * holds its refreshes until this resolves: unlimited, a connection gone quiet would stop the board
+ * redrawing for the rest of the session.
+ */
+function settled() {
+ return Promise.race([
+ ws.waitForMaxKnownEntityChangeId(),
+ new Promise<void>((resolve) => { window.setTimeout(resolve, SETTLE_TIMEOUT_MS); })
+ ]);
+}
+
/**
* Puts every card back where the column draws it, closes every gap and gives back the room they
* took.