Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: performance improvements

Adorian Doranc040eded4fb6
diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsx
index 89e589e7d8..939e95359f 100644
--- a/apps/client/src/widgets/collections/board/column.tsx
+++ b/apps/client/src/widgets/collections/board/column.tsx
@@ -186,7 +186,7 @@ export default function Column({
// `isNew` stays true until another column is added, so the reveal is recorded here rather than
// replayed on every redraw of the board.
const [ isRevealed, setIsRevealed ] = useState(false);
- const { setColumnNameToEdit, setColumnLimitToEdit, setActiveColumn } =
+ const { setColumnNameToEdit, setColumnLimitToEdit, setActiveColumn, setInsertingColumn } =
useContext(BoardActionsContext);
const { branchIdToEdit, columnNameToEdit, draggedCard, draggedColumn } =
useContext(BoardDragStateContext);
@@ -516,6 +516,16 @@ export default function Column({
return () => window.clearTimeout(timer);
}, [ insertedNoteId, columnItems ]);
+ // The board raises its backdrop for any open field, and each column holds its own.
+ useEffect(() => {
+ setInsertingColumn(column, !!insertBefore);
+ return () => setInsertingColumn(column, false);
+ }, [ column, insertBefore, setInsertingColumn ]);
+
+ // Whether the title being edited is one of this column's, which lifts the mask below.
+ const hasEditedCard = !!branchIdToEdit
+ && !!columnItems?.some(({ branch }) => branch.branchId === branchIdToEdit);
+
// The field a card is inserted in, drawn where the reader asked for the card. The same field
// as the one below the column, so a card is made the same way wherever it goes.
const insertField = insertBefore && (
@@ -539,6 +549,7 @@ export default function Column({
// The class the themes key a hue off, worn here as anywhere else that carries one.
"with-hue": hue !== undefined,
"board-column-archived": archived,
+ "editing-open": hasEditedCard || !!insertBefore,
"over-limit": isOverLimit,
collapsed: isCollapsed,
"quick-collapse": isCollapsingByHand,
diff --git a/apps/client/src/widgets/collections/board/index.css b/apps/client/src/widgets/collections/board/index.css
index 7ee40c7acc..f8d6062a8a 100644
--- a/apps/client/src/widgets/collections/board/index.css
+++ b/apps/client/src/widgets/collections/board/index.css
@@ -678,10 +678,9 @@ body.mobile .board-view-container .board-add-column {
transition: opacity 0.3s ease;
}
-/* Matched on the field rather than on state in `index.tsx`: each column holds its own editing
- state. `inserting` marks only the field that opens between two cards, so the one at the foot of
- a column leaves the backdrop down. */
-.board-view:has(.board-note.editing, .board-new-item.inserting) .board-edit-backdrop {
+/* `editing-open` is set by `index.tsx` for a card being renamed or a field open between two cards;
+ the field at the foot of a column leaves the backdrop down. */
+.board-view.editing-open .board-edit-backdrop {
opacity: 1;
}
@@ -710,8 +709,7 @@ body.mobile .board-view-container .board-add-column {
/* `motion-disabled` sets `transition: none !important` on everything, so `.board-edit-backdrop`
fires no `transitionend` and `frozen` is never set. There is no fade to protect either, so the
same fields freeze the board as soon as they open. */
-body#trilium-app.motion-disabled
- .board-view:has(.board-note.editing, .board-new-item.inserting) :is(
+body#trilium-app.motion-disabled .board-view.editing-open :is(
.board-column > h3,
.board-column > .board-new-item,
.board-column-content > *:not(.editing)
@@ -723,8 +721,7 @@ body#trilium-app.motion-disabled
layer of its own, which the field being typed into could not rise out of. Taken off the one
column that holds it, for as long as it stands. `!important` because `useScrollFade` writes the
mask on the element itself. */
-.board-view-container .board-column:has(.board-note.editing, .board-new-item.inserting)
- .board-column-content {
+.board-view-container .board-column.editing-open .board-column-content {
mask: none !important;
}
diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsx
index 6a9cb5774e..456ee10e43 100644
--- a/apps/client/src/widgets/collections/board/index.spec.tsx
+++ b/apps/client/src/widgets/collections/board/index.spec.tsx
@@ -3223,6 +3223,83 @@ describe("Board editors and menus", () => {
await act(async () => { await flush(); });
}
+ /**
+ * The backdrop and the frozen cards are driven by classes rather than by `:has()`: a `:has()`
+ * naming a descendant makes every card insertion invalidate the whole board.
+ */
+ it("marks the board and the one column while a card title is being edited", async () => {
+ const board = await renderBoard();
+ const view = board.querySelector<HTMLElement>(".board-view");
+ const columns = board.querySelectorAll<HTMLElement>(".board-column");
+ const card = columns[0]?.querySelector<HTMLElement>(".board-note");
+ if (!view || !card || columns.length < 2) throw new Error("expected a card in two columns");
+
+ expect(view.classList.contains("editing-open")).toBe(false);
+
+ await act(async () => {
+ card.dispatchEvent(new KeyboardEvent("keydown", { key: "F2", bubbles: true }));
+ await flush();
+ });
+
+ expect(view.classList.contains("editing-open")).toBe(true);
+ expect(columns[0].classList.contains("editing-open")).toBe(true);
+ expect(columns[1].classList.contains("editing-open")).toBe(false);
+
+ const editor = card.querySelector<HTMLTextAreaElement>("textarea");
+ if (!editor) throw new Error("expected the card editor to be open");
+
+ await act(async () => {
+ editor.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
+ await flush();
+ });
+
+ expect(view.classList.contains("editing-open")).toBe(false);
+ expect(columns[0].classList.contains("editing-open")).toBe(false);
+ });
+
+ it("marks a field opened between cards, and leaves the one at a column's foot unmarked", async () => {
+ const board = await renderBoard();
+ const view = board.querySelector<HTMLElement>(".board-view");
+ const columns = board.querySelectorAll<HTMLElement>(".board-column");
+ const card = columns[0]?.querySelector<HTMLElement>(".board-note");
+ if (!view || !card || columns.length < 2) throw new Error("expected a card in two columns");
+
+ await act(async () => {
+ card.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
+ await flush();
+ });
+
+ expect(board.querySelector(".board-new-item.inserting")).toBeTruthy();
+ expect(view.classList.contains("editing-open")).toBe(true);
+ expect(columns[0].classList.contains("editing-open")).toBe(true);
+ expect(columns[1].classList.contains("editing-open")).toBe(false);
+
+ const field = columns[0].querySelector<HTMLTextAreaElement>(
+ ".board-new-item.inserting textarea");
+ if (!field) throw new Error("expected the insert field to be open");
+
+ await act(async () => {
+ field.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
+ await flush();
+ });
+
+ expect(view.classList.contains("editing-open")).toBe(false);
+
+ // The field below a column makes a card the same way but leaves the board undimmed.
+ const footer = columns[1].querySelector<HTMLElement>(".board-new-item");
+ if (!footer) throw new Error("expected the column footer");
+
+ await act(async () => {
+ footer.dispatchEvent(new MouseEvent("click", { bubbles: true }));
+ await flush();
+ });
+
+ expect(footer.classList.contains("editing")).toBe(true);
+ expect(footer.classList.contains("inserting")).toBe(false);
+ expect(view.classList.contains("editing-open")).toBe(false);
+ expect(columns[1].classList.contains("editing-open")).toBe(false);
+ });
+
async function renderBoard() {
const note = buildNote({
title: "Board",
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index 3e5dc68f1d..9aff522c5a 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -192,6 +192,13 @@ interface BoardActions {
setDraggedColumn: (column: ColumnDrag | null) => void;
setDropPosition: (position: ColumnDrag | null) => void;
setDropTarget: (target: string | null) => void;
+ /**
+ * Reports whether a column holds an open insert field, which the board reads to raise the
+ * backdrop. Held here rather than matched with `:has()` in the stylesheet: a `:has()` naming a
+ * descendant makes every card insertion invalidate the whole board, which on a column of
+ * thousands is hundreds of milliseconds of style recalculation per card added.
+ */
+ setInsertingColumn: (column: string, isOpen: boolean) => void;
}
/**
@@ -221,7 +228,8 @@ export const BoardActionsContext = createContext<BoardActions>({
setDraggedCard: () => undefined,
setDraggedColumn: () => undefined,
setDropPosition: () => undefined,
- setDropTarget: () => undefined
+ setDropTarget: () => undefined,
+ setInsertingColumn: () => undefined
});
/**
@@ -251,6 +259,9 @@ export const BoardKeptCardsContext = createContext<Set<string>>(new Set());
* board handles itself (see `keyboard.ts` and the card and column handlers), none of them
* rebindable, so each is listed literally rather than through a registered action.
*/
+/** Stands for a board where no column has an insert field open, so no render allocates a set. */
+const NO_COLUMNS: ReadonlySet<string> = new Set();
+
/** How long a finger stays on the create button before it offers where to put the card. */
const HOLD_TO_PLACE_MS = 500;
@@ -389,6 +400,24 @@ export default function BoardView({
const [ isEditingProperties, setIsEditingProperties ] = useState(false);
/** Adds `frozen`, which takes `pointer-events` off the cards. Set once the backdrop has faded in. */
const [ isFrozen, setIsFrozen ] = useState(false);
+ /** The columns holding an open insert field, which each column reports for itself. */
+ const [ insertingColumns, setInsertingColumns ] = useState<ReadonlySet<string>>(NO_COLUMNS);
+ const setInsertingColumn = useCallback((column: string, isOpen: boolean) => {
+ setInsertingColumns((current) => {
+ if (current.has(column) === isOpen) {
+ return current;
+ }
+
+ const next = new Set(current);
+ if (isOpen) {
+ next.add(column);
+ } else {
+ next.delete(column);
+ }
+
+ return next;
+ });
+ }, []);
const selectColumn = useCallback<Dispatch<StateUpdater<string | undefined>>>((column) => {
setIsPeekingAll(false);
setActiveColumn(column);
@@ -571,10 +600,11 @@ export default function BoardView({
setDraggedCard,
setDraggedColumn,
setDropPosition,
- setDropTarget
+ setDropTarget,
+ setInsertingColumn
}), [
setBranchIdToEdit, setColumnNameToEdit, setColumnLimitToEdit, selectColumn,
- setDraggedCard, setDraggedColumn, setDropPosition, setDropTarget
+ setDraggedCard, setDraggedColumn, setDropPosition, setDropTarget, setInsertingColumn
]);
// Read off the config rather than off `columns`, which the resolver hands back as names alone.
@@ -1156,10 +1186,12 @@ export default function BoardView({
: undefined;
return (
- <div className={clsx("board-view", { frozen: isFrozen })}>
+ <div className={clsx("board-view", {
+ frozen: isFrozen,
+ "editing-open": branchIdToEdit !== undefined || insertingColumns.size > 0
+ })}>
{/* Dims the board while a title is being typed, with the edited card lifted above it.
- Always rendered so that it can fade in. `index.css` picks the fields that raise it
- with `:has()`, since each column holds its own editing state.
+ Always rendered so that it can fade in.
`frozen` waits for the fade to finish: setting it restyles every card, which in the
same frame drops the fade's frames. */}