Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: performance improvements

Adorian Doran6c49b6c9fe87
diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsx
index cab436480d..a711b388ad 100644
--- a/apps/client/src/widgets/collections/board/column.tsx
+++ b/apps/client/src/widgets/collections/board/column.tsx
@@ -221,9 +221,17 @@ export default function Column({
// browser's to do.
const gapRef = useRef<HTMLDivElement>(null);
const roomRef = useRef<HTMLDivElement>(null);
+ /** Whether a card was being carried at the previous commit. */
+ const carried = useRef(false);
/** Which cards were last told to stand aside, so only what changed is written. */
const aside = useRef({ from: 0, until: 0, room: 0 });
useLayoutEffect(() => {
+ // The two commits a drag opens and closes on. A card is drawn in either one in the place
+ // its own transform already had it, so easing would slide it from somewhere it never
+ // stood; every commit in between moves the cards for real and eases as usual.
+ const atOnce = carried.current !== !!draggedCard;
+ carried.current = !!draggedCard;
+
const area = contentRef.current;
const gap = gapRef.current;
if (!area || !gap) return;
@@ -250,7 +258,7 @@ export default function Column({
if (dropIndex === null) {
for (const card of cards) {
if (card.style.transform) {
- card.style.removeProperty("transform");
+ placeCard(card, null, atOnce);
}
}
aside.current = { from: 0, until: 0, room: 0 };
@@ -265,16 +273,19 @@ export default function Column({
const afresh = last.room !== room;
for (let index = last.from; index < last.until; index++) {
if (afresh || index < from || index >= until) {
- cards[index]?.style.removeProperty("transform");
+ const card = cards[index];
+ if (card) {
+ placeCard(card, null, atOnce);
+ }
}
}
for (let index = from; index < until; index++) {
if (afresh || index < last.from || index >= last.until) {
- cards[index].style.transform = `translateY(${room}px)`;
+ placeCard(cards[index], `translateY(${room}px)`, atOnce);
}
}
aside.current = { from, until, room };
- }, [ dropIndex, draggedCard?.height, columnItems ]);
+ }, [ dropIndex, draggedCard, columnItems ]);
const { handleDragOver, handleDragLeave, handleDrop } = useDragging({
column, columnIndex, columnItems, isEditing, api, parentNote
});
@@ -647,6 +658,47 @@ export default function Column({
);
}
+/**
+ * Puts a card where a transform says, easing it there unless the board is drawing a still frame.
+ *
+ * A still frame is one the board draws an element in the place its own transform already had it,
+ * which is what a drag's lift and its drop both are. The transition is suppressed on the card
+ * rather than by a rule under the board's own class: such a rule matches every card on the board,
+ * and Chrome restyles all of them for the few that move.
+ *
+ * @param transform what to write, or `null` to put the card back where the column draws it.
+ */
+export function placeCard(card: HTMLElement, transform: string | null, atOnce: boolean) {
+ if (atOnce) {
+ card.style.transition = "none";
+ settling.add(card);
+ }
+
+ if (transform === null) {
+ card.style.removeProperty("transform");
+ } else {
+ card.style.transform = transform;
+ }
+
+ if (!atOnce || restoring !== undefined) {
+ return;
+ }
+
+ // Put back a frame later than the one that draws them, since a frame's callbacks run before
+ // the styles it paints are worked out.
+ restoring = requestAnimationFrame(() => requestAnimationFrame(() => {
+ restoring = undefined;
+ for (const held of settling) {
+ held.style.removeProperty("transition");
+ }
+ settling.clear();
+ }));
+}
+
+/** The cards whose transition is suppressed, waiting for the frame that puts it back. */
+const settling = new Set<HTMLElement>();
+let restoring: number | undefined;
+
/**
* The editor a new card is named in, standing below the column or between two of its cards.
*
diff --git a/apps/client/src/widgets/collections/board/drag.spec.tsx b/apps/client/src/widgets/collections/board/drag.spec.tsx
index 587e962eaa..1ea72cccc9 100644
--- a/apps/client/src/widgets/collections/board/drag.spec.tsx
+++ b/apps/client/src/widgets/collections/board/drag.spec.tsx
@@ -317,14 +317,21 @@ describe("Board drag and drop", () => {
it("opens the gap without a transition as a card is lifted", async () => {
const { columns, board } = await renderBoard();
await act(async () => { await settle(); });
- const second = columns[0].querySelectorAll<HTMLElement>(".board-note")[1];
+ const [ first, second ] = columns[0].querySelectorAll<HTMLElement>(".board-note");
// Watched rather than read afterwards: the frame that puts the transitions back has run by
// the time the gesture returns.
- expect(await classesWhile(board, async () => {
- await pointer(second, "pointerdown", 50, 150);
- await pointer(columns[0], "pointermove", 50, 60);
- })).toContain("board-still");
+ const eased = await transitionsWhile(first, async () => {
+ expect(await classesWhile(board, async () => {
+ await pointer(second, "pointerdown", 50, 150);
+ await pointer(columns[0], "pointermove", 50, 60);
+ })).toContain("board-still");
+ });
+
+ // The card that steps aside carries the suppression itself: a rule under the board's class
+ // would reach every card on it.
+ expect(first.style.transform).toBe("translateY(100px)");
+ expect(eased).toContain("none");
});
/**
@@ -335,7 +342,7 @@ describe("Board drag and drop", () => {
it("takes the transforms off without a transition once a card has landed", async () => {
const { columns, board } = await renderBoard();
await act(async () => { await settle(); });
- const second = columns[0].querySelectorAll<HTMLElement>(".board-note")[1];
+ const [ first, second ] = columns[0].querySelectorAll<HTMLElement>(".board-note");
await pointer(second, "pointerdown", 50, 150);
await pointer(columns[0], "pointermove", 50, 60);
@@ -343,11 +350,31 @@ describe("Board drag and drop", () => {
// The lift suppresses transitions too, so the release is watched on its own: left running,
// this would pass on what the lift did.
expect(board.classList.contains("board-still")).toBe(false);
+ expect(first.style.transform).toBe("translateY(100px)");
- expect(await classesWhile(board, () => pointer(columns[0], "pointerup", 50, 60)))
- .toContain("board-still");
+ const eased = await transitionsWhile(first, async () => {
+ expect(await classesWhile(board, () => pointer(columns[0], "pointerup", 50, 60)))
+ .toContain("board-still");
+ });
+
+ // Put back where the column draws it, and without easing its way there.
+ expect(first.style.transform).toBe("");
+ expect(eased).toContain("none");
});
+ it("puts the suppressed transitions back once the frame that moved the cards has passed",
+ async () => {
+ const { columns } = await renderBoard();
+ await act(async () => { await settle(); });
+ const [ first, second ] = columns[0].querySelectorAll<HTMLElement>(".board-note");
+
+ await pointer(second, "pointerdown", 50, 150);
+ await pointer(columns[0], "pointermove", 50, 60);
+ await act(async () => { await frames(); });
+
+ expect(first.style.transition).toBe("");
+ });
+
function place(
element: HTMLElement | null,
box: { left: number, top: number, width: number, height: number }
@@ -847,14 +874,31 @@ describe("Board column reordering", () => {
});
/** Every class the element wears at any point while `run` is going on. */
-async function classesWhile(element: HTMLElement, run: () => Promise<void>) {
+function classesWhile(element: HTMLElement, run: () => Promise<void>) {
+ return seenWhile(element, "class", () => element.classList, run);
+}
+
+/** Every inline `transition` the element carries at any point while `run` is going on. */
+function transitionsWhile(element: HTMLElement, run: () => Promise<void>) {
+ return seenWhile(element, "style", () => [ element.style.transition ], run);
+}
+
+/**
+ * What `read` returns each time the watched attribute changes, gathered while `run` goes on.
+ *
+ * Read as it happens rather than afterwards: what a still frame writes is taken back a frame or
+ * two later, so by the time the gesture returns there is nothing left to find.
+ */
+async function seenWhile(
+ element: HTMLElement, attribute: string, read: () => Iterable<string>, run: () => Promise<void>
+) {
const seen = new Set<string>();
const watch = new MutationObserver(() => {
- for (const name of element.classList) {
- seen.add(name);
+ for (const value of read()) {
+ seen.add(value);
}
});
- watch.observe(element, { attributes: true, attributeFilter: [ "class" ] });
+ watch.observe(element, { attributes: true, attributeFilter: [ attribute ] });
await run();
watch.disconnect();
return [ ...seen ];
diff --git a/apps/client/src/widgets/collections/board/index.css b/apps/client/src/widgets/collections/board/index.css
index ab7efe4356..d3e2b5057c 100644
--- a/apps/client/src/widgets/collections/board/index.css
+++ b/apps/client/src/widgets/collections/board/index.css
@@ -507,11 +507,6 @@ body.mobile .board-view-container .board-add-column {
box-shadow: 4px 8px 16px rgba(0, 0, 0, 0.5);
}
-/* While a card is carried, so the page does not take the touch for a scroll. */
-.board-view-container.board-dragging {
- touch-action: none;
-}
-
/* Reaching the end of the board scrolls neither the page nor anything else. */
.board-view-container {
overscroll-behavior: contain;
@@ -949,10 +944,12 @@ body.mobile .column-drop-placeholder {
/* The one frame the board is drawn in as a drag lifts something or lets it go. What the drag
carried aside by a transform already stands where the gap, or the new order, puts it, so the
transform has nothing left to ease: given a transition it would carry the element a full width,
- or a card's height, from a place it never stood in. The gap is included so that it takes the
+ or a column's width, from a place it never stood in. The gap is included so that it takes the
place of the card at once rather than fading into it. Written against the selectors above, which
- it has to outweigh. */
-.board-view-container.board-still .board-note,
+ it has to outweigh.
+
+ The cards are left out: a rule here reaches every one of them, and Chrome restyles all of them
+ for the few that move. `placeCard` writes this on those few instead. */
.board-view-container.board-still .board-drop-placeholder,
.board-view-container.board-still .board-column,
.board-view-container.board-still .board-column.collapsed:not(.board-drag-preview),
@@ -976,7 +973,11 @@ body.mobile .column-drop-placeholder {
overflow: hidden;
}
-.board-view-container .board-column.expanding > * {
+/* The children are named rather than matched with `> *`: a universal selector cannot be narrowed
+ into an invalidation set, so the class going on and off restyles the whole column. */
+.board-view-container .board-column.expanding > h3,
+.board-view-container .board-column.expanding > .board-column-content,
+.board-view-container .board-column.expanding > .board-new-item {
width: calc(var(--board-column-width) - 2 * var(--board-column-border-width));
}
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index d1798fee4c..cc48013376 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -45,7 +45,7 @@ import { forgetCardHeights } from "./drag_measure";
import { BoardDropStateContext, DropStateStore } from "./drop_state";
import BoardApi from "./api";
import { DEFAULT_COLUMN_ICON, DEFAULT_GROUP_BY, getStatusDefinition, INBOX_COLUMN } from "./columns";
-import Column, { EXPAND_MS } from "./column";
+import Column, { EXPAND_MS, placeCard } from "./column";
import { currentCardTemplate, DEFAULT_CARD_TEMPLATES } from "./card_templates";
import ColumnLimitDialog from "./column_limit";
import BoardProperties from "./properties";
@@ -990,7 +990,7 @@ function closeGaps(container: HTMLElement | null) {
for (const card of container.querySelectorAll<HTMLElement>(".board-note")) {
if (card.style.transform) {
- card.style.removeProperty("transform");
+ placeCard(card, null, true);
}
}