Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: improve column dragging/collapsing/expanding performance

Adorian Doran69ed71e3555c
diff --git a/apps/client/src/widgets/collections/board/board_drag.ts b/apps/client/src/widgets/collections/board/board_drag.ts
index 0222c74a84..457ec56369 100644
--- a/apps/client/src/widgets/collections/board/board_drag.ts
+++ b/apps/client/src/widgets/collections/board/board_drag.ts
@@ -147,8 +147,9 @@ export function useBoardDrag(
held.active = true;
// Measured before what is carried is taken out of the flow, so the places it can land
- // are the ones the board is showing.
- held.measurement = measureBoard(container);
+ // are the ones the board is showing. A column is placed among the columns alone, so
+ // the cards are left unmeasured for one.
+ held.measurement = measureBoard(container, held.kind === "card");
// Held from here on, so the gesture keeps the pointer wherever it goes. Taken at the
// press instead, it would carry the click away from what was pressed.
container.setPointerCapture?.(held.pointerId);
@@ -428,7 +429,7 @@ export function useBoardDrag(
return;
}
- const measurement = measureBoard(container);
+ const measurement = measureBoard(container, held.kind === "card");
// A column that was measured with cards keeps them. The board now holds the gap where the
// carried 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
diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsx
index 4cf69a4d82..2f35a86adb 100644
--- a/apps/client/src/widgets/collections/board/column.tsx
+++ b/apps/client/src/widgets/collections/board/column.tsx
@@ -26,7 +26,7 @@ import { useScrollFade } from "../../react/scroll_fade";
const HAND_OVER_MS = 2000;
/** How long an open takes. Matches `--board-expand-duration` in the board's own rules. */
-const EXPAND_MS = 200;
+export const EXPAND_MS = 200;
import NoteLink from "../../react/NoteLink";
import { BoardActionsContext, BoardDragStateContext, TitleEditor } from ".";
import BoardApi from "./api";
@@ -59,6 +59,7 @@ export default function Column({
keepCollapsed,
isActive,
isPeeked,
+ isResizing,
nested,
limit,
columnItems,
@@ -84,6 +85,8 @@ export default function Column({
isActive?: boolean,
/** Whether the board is showing every collapsed column at once, which opens this one too. */
isPeeked?: boolean,
+ /** Whether a column is still taking its new width, during which no column's cards move. */
+ isResizing?: boolean,
/** What a new card is made from, and how the reader picks something else. */
cardTemplates: CardTemplates,
/** Whether the inbox also collects notes deeper than the board's direct children. */
@@ -145,7 +148,8 @@ export default function Column({
const [ isRevealed, setIsRevealed ] = useState(false);
const { setColumnNameToEdit, setColumnLimitToEdit, setActiveColumn } =
useContext(BoardActionsContext);
- const { branchIdToEdit, columnNameToEdit, draggedCard } = useContext(BoardDragStateContext);
+ const { branchIdToEdit, columnNameToEdit, draggedCard, draggedColumn } =
+ useContext(BoardDragStateContext);
// Asked about this column alone: where the gap stands changes on every step of a drag, and a
// column that the answer does not concern is left as it is rather than drawn again.
const dropIndex = useDropIndex(column);
@@ -163,6 +167,9 @@ export default function Column({
// of any column the gap has stood in. The rest are left unmeasured, measuring one costing a
// layout of the whole board; paused rather than switched off, so a column the gap reaches in
// one step still knows where its cards stood and slides them from there.
+ //
+ // A column being carried, and a column changing width, move no card inside any column, so
+ // every column is left unmeasured for the length of either.
const heldGap = useRef(false);
if (!draggedCard) {
heldGap.current = false;
@@ -171,7 +178,9 @@ export default function Column({
}
useFlip(contentRef, {
selector: ".board-note",
- paused: !!draggedCard && column !== draggedCard.fromColumn && !heldGap.current
+ paused: draggedCard
+ ? column !== draggedCard.fromColumn && !heldGap.current
+ : !!draggedColumn || !!isResizing
});
const { handleDragOver, handleDragLeave, handleDrop } = useDragging({
column, columnIndex, columnItems, isEditing, api, parentNote
diff --git a/apps/client/src/widgets/collections/board/drag_measure.ts b/apps/client/src/widgets/collections/board/drag_measure.ts
index 1bc14ea5dc..be98711adc 100644
--- a/apps/client/src/widgets/collections/board/drag_measure.ts
+++ b/apps/client/src/widgets/collections/board/drag_measure.ts
@@ -9,12 +9,16 @@ export interface BoardMeasurement {
}
/**
- * Measures every column and card on the board.
+ * Measures every column on the board, and the cards in each of them.
*
* Called once when a drag starts. A gesture reads two rectangles per move afterwards, whatever the
* board holds, where measuring per move would read one for every card on it.
+ *
+ * @param withCards whether to measure the cards as well, which only a card is placed against. A
+ * column is placed against the column boxes alone, and reading a rectangle per card for one is a
+ * pass over the whole board that nothing goes on to look at.
*/
-export function measureBoard(container: HTMLElement): BoardMeasurement {
+export function measureBoard(container: HTMLElement, withCards = true): BoardMeasurement {
const origin = container.getBoundingClientRect().left - container.scrollLeft;
const columns: ColumnBox[] = [];
const areas = new Map<string, HTMLElement>();
@@ -38,7 +42,7 @@ export function measureBoard(container: HTMLElement): BoardMeasurement {
width: rect.width,
top: rect.top,
height: rect.height,
- cards: measureCards(area)
+ cards: withCards ? measureCards(area) : []
});
}
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index f2fa2d8b05..b2e8986b91 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -44,7 +44,7 @@ import { movesColumn } from "./drag_geometry";
import { BoardDropStateContext, DropStateStore } from "./drop_state";
import BoardApi from "./api";
import { DEFAULT_COLUMN_ICON, DEFAULT_GROUP_BY, getStatusDefinition, INBOX_COLUMN } from "./columns";
-import Column from "./column";
+import Column, { EXPAND_MS } from "./column";
import { currentCardTemplate, DEFAULT_CARD_TEMPLATES } from "./card_templates";
import ColumnLimitDialog from "./column_limit";
import BoardProperties from "./properties";
@@ -470,6 +470,21 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
/** Until when a column move can still be settling, which is when `useFlip` slides columns. */
const columnMovedUntil = useRef(0);
+ // Which columns stand narrow, as a line, so that one opening or closing is read off a single
+ // comparison. A column changing width hides or shows its own cards and moves no card inside
+ // any other, so there is nothing for any column to measure while it is happening.
+ const columnResizingUntil = useRef(0);
+ const columnWidths = useRef<string>();
+ const widths = shownColumns
+ .map(column => storedColumns.get(column)?.collapsed
+ && column !== activeColumn && !isPeekingAll ? "1" : "0")
+ .join("");
+ if (columnWidths.current !== undefined && columnWidths.current !== widths) {
+ columnResizingUntil.current = Date.now() + EXPAND_MS;
+ }
+ columnWidths.current = widths;
+ const isResizingColumns = Date.now() < columnResizingUntil.current;
+
const boardDragState = useMemo<BoardDragState>(() => ({
branchIdToEdit,
columnNameToEdit,
@@ -812,6 +827,7 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
keepCollapsed={storedColumns.get(column)?.keepCollapsed}
isActive={activeColumn === column}
isPeeked={isPeekingAll}
+ isResizing={isResizingColumns}
cardTemplates={cardTemplates}
nested={storedColumns.get(column)?.nested}
limit={storedColumns.get(column)?.limit}