Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: add commands to collapse and expand all columns

Adorian Doran24d3345a4e5a
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index eac69cc246..e91f536bcb 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3414,6 +3414,8 @@
"rename-column": "Rename column",
"collapse-column": "Collapse column",
"keep-column-collapsed": "Keep the column collapsed",
+ "collapse-all-columns": "Collapse all columns",
+ "expand-all-columns": "Expand all columns",
"collapse-hint": "Double click to collapse",
"set-limit": "Set limit...",
"set-limit-ok": "Set limit",
diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.ts
index 5a9735d960..d5276e85a7 100644
--- a/apps/client/src/widgets/collections/board/api.spec.ts
+++ b/apps/client/src/widgets/collections/board/api.spec.ts
@@ -1189,6 +1189,27 @@ describe("collapsing a column", () => {
expect(saved.at(-1)?.columns).toEqual([ { value: "To Do" } ]);
});
+ /**
+ * A column drawn from the definition or from a value its cards carry has no stored entry until
+ * something is picked for it, and collapsing the board is what picks for all of them at once.
+ */
+ it("collapses every column, and opens the ones that are not kept collapsed", async () => {
+ const { api, saved } = createApi(
+ { columns: [ { value: "To Do", keepCollapsed: true } ] }, [ "To Do", "Done" ]);
+
+ await api.setAllColumnsCollapsed(true);
+ expect(saved.at(-1)?.columns).toEqual([
+ { value: "To Do", keepCollapsed: true, collapsed: true },
+ { value: "Done", collapsed: true }
+ ]);
+
+ await api.setAllColumnsCollapsed(false);
+ expect(saved.at(-1)?.columns).toEqual([
+ { value: "To Do", keepCollapsed: true, collapsed: true },
+ { value: "Done" }
+ ]);
+ });
+
it("leaves the column's other properties alone", async () => {
const { api, saved } = createApi(
{ columns: [ { value: "To Do", icon: "bx bx-star", limit: 3 } ] }, [ "To Do" ]);
diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts
index 651e3dba56..f3216634fe 100644
--- a/apps/client/src/widgets/collections/board/api.ts
+++ b/apps/client/src/widgets/collections/board/api.ts
@@ -537,6 +537,34 @@ export default class BoardApi {
this.updateColumn(column, { collapsed });
}
+ /**
+ * Collapses every column, or opens the ones that are not kept collapsed.
+ *
+ * One write for the board: a column resolved from the definition or from a value its cards
+ * carry is drawn without ever having been stored, so this is also where it gets an entry.
+ */
+ async setAllColumnsCollapsed(collapsed: boolean) {
+ const stored = new Map((this.viewConfig?.columns ?? []).map(col => [ col.value, col ]));
+ const order = [ ...stored.keys() ];
+ for (const derived of this.columns) {
+ if (!stored.has(derived)) {
+ order.push(derived);
+ }
+ }
+
+ this.storeColumns(order.map(value => {
+ const column = { ...(stored.get(value) ?? { value }) };
+ if (collapsed) {
+ column.collapsed = true;
+ } else if (!column.keepCollapsed) {
+ // A column kept collapsed keeps the flag: opening it is what the peek is for.
+ delete column.collapsed;
+ }
+
+ return column;
+ }));
+ }
+
/** Whether a column collapses again once it has been opened. */
isColumnKeptCollapsed(column: string) {
return !!this.viewConfig?.columns?.find(col => col.value === column)?.keepCollapsed;
diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsx
index 3bf379bf51..a971a1cc18 100644
--- a/apps/client/src/widgets/collections/board/column.tsx
+++ b/apps/client/src/widgets/collections/board/column.tsx
@@ -57,6 +57,7 @@ export default function Column({
collapsed,
keepCollapsed,
isActive,
+ isPeeked,
nested,
limit,
columnItems,
@@ -78,6 +79,8 @@ export default function Column({
keepCollapsed?: boolean,
/** Whether this is the column the reader is working in, which opens it while it is collapsed. */
isActive?: boolean,
+ /** Whether the board is showing every collapsed column at once, which opens this one too. */
+ isPeeked?: boolean,
/** Whether the inbox also collects notes deeper than the board's direct children. */
nested?: boolean,
/** The note limit, absent if disabled. */
@@ -167,7 +170,7 @@ export default function Column({
// Read here rather than in the badge: the column body shows an outline as well.
const isOverLimit = limit !== undefined && (columnItems?.length ?? 0) > limit;
- const isCollapsed = !!collapsed && !isActive;
+ const isCollapsed = !!collapsed && !isActive && !isPeeked;
// A column opened to take a dragged card takes its width at once, and its cards with it.
const opensAtOnce = !!draggedCard || dropTarget === column;
diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.ts
index b76bb47e72..74a5d98ed8 100644
--- a/apps/client/src/widgets/collections/board/context_menu.ts
+++ b/apps/client/src/widgets/collections/board/context_menu.ts
@@ -165,6 +165,33 @@ export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column:
});
}
+/** What the board itself offers, for a press on the ground the columns stand on. */
+export function openBoardContextMenu(
+ event: ContextMenuEvent,
+ board: { onCollapseAll: () => void, onExpandAll: () => void }
+) {
+ event.preventDefault();
+ event.stopPropagation();
+
+ contextMenu.show({
+ x: event.pageX,
+ y: event.pageY,
+ items: [
+ {
+ title: t("board_view.collapse-all-columns"),
+ uiIcon: "bx bx-collapse-alt",
+ handler: board.onCollapseAll
+ },
+ {
+ title: t("board_view.expand-all-columns"),
+ uiIcon: "bx bx-expand-alt",
+ handler: board.onExpandAll
+ }
+ ],
+ selectMenuItemHandler() {}
+ });
+}
+
/** Offers both ends of a column for the card the button below it is about to create. */
export function openCreateCardMenu(x: number, y: number, create: (atStart: boolean) => void) {
contextMenu.show({
diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsx
index 3ad8aa88c2..2fd0ada67a 100644
--- a/apps/client/src/widgets/collections/board/index.spec.tsx
+++ b/apps/client/src/widgets/collections/board/index.spec.tsx
@@ -434,6 +434,61 @@ describe("Collapsed board columns", () => {
expect(isCollapsed(mountPoint, 0)).toBe(false);
});
+ /**
+ * The board's own menu, for a press on the ground the columns stand on. Expanding shows every
+ * column at once; a column that is kept collapsed is only peeked, and closes as the reader
+ * settles on one column, which is what a single peek does.
+ */
+ it("collapses and expands every column from the board's menu", async () => {
+ const { mountPoint } = await setup({ keepCollapsed: true });
+ const board = mountPoint.querySelector<HTMLElement>(".board-view-container");
+ const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
+
+ board?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
+ const entries = (show.mock.calls.at(-1)?.[0].items ?? []).flatMap(item =>
+ item && "uiIcon" in item && "handler" in item
+ ? [ { icon: item.uiIcon, handler: item.handler } ]
+ : []);
+ expect(entries.map(entry => entry.icon))
+ .toEqual([ "bx bx-collapse-alt", "bx bx-expand-alt" ]);
+
+ await act(async () => {
+ entries[0]?.handler?.({} as never, {} as never);
+ await flush();
+ });
+ expect(isCollapsed(mountPoint, 0)).toBe(true);
+ expect(isCollapsed(mountPoint, 1)).toBe(true);
+ expect(saved.at(-1)?.columns?.map(column => column.collapsed)).toEqual([ true, true ]);
+
+ await act(async () => {
+ entries[1]?.handler?.({} as never, {} as never);
+ await flush();
+ });
+ expect(isCollapsed(mountPoint, 0)).toBe(false);
+ expect(isCollapsed(mountPoint, 1)).toBe(false);
+ // The one kept collapsed keeps the flag, and shuts again as soon as a column is selected.
+ expect(saved.at(-1)?.columns?.map(column => column.collapsed))
+ .toEqual([ true, undefined ]);
+
+ await select(mountPoint, 1);
+ expect(isCollapsed(mountPoint, 0)).toBe(true);
+ expect(isCollapsed(mountPoint, 1)).toBe(false);
+
+ show.mockRestore();
+ });
+
+ /** A column and a card answer for their own presses, and the board does not speak over them. */
+ it("leaves a press inside a column to the column", async () => {
+ const { mountPoint } = await setup();
+ const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
+
+ columnAt(mountPoint, 1).querySelector(".board-column-content")
+ ?.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
+
+ expect(show).not.toHaveBeenCalled();
+ show.mockRestore();
+ });
+
/** The first of the two clicks opens the strip; collapsing it again would undo that. */
it("leaves a strip open when it is double clicked open", async () => {
const { mountPoint } = await setup();
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index a4232094fb..18e7773255 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -12,6 +12,7 @@ import {
import FNote from "../../../entities/fnote";
import { t } from "../../../services/i18n";
import type LoadResults from "../../../services/load_results";
+import { ContextMenuEvent } from "../../../menus/context_menu";
import { isIMEComposing } from "../../../services/shortcuts";
import type { ShortcutHintDefinition } from "../../../services/shortcut_hints";
import toast from "../../../services/toast";
@@ -38,7 +39,7 @@ import BoardApi from "./api";
import { DEFAULT_GROUP_BY, getStatusDefinition, INBOX_COLUMN } from "./columns";
import Column from "./column";
import ColumnLimitDialog from "./column_limit";
-import { openCreateColumnMenu } from "./context_menu";
+import { openBoardContextMenu, openCreateColumnMenu } from "./context_menu";
import { applyCardMove, ColumnMap, getBoardData } from "./data";
import { useBoardKeyboard } from "./keyboard";
@@ -232,6 +233,18 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
const [ columnNameToEdit, setColumnNameToEdit ] = useState<string>();
const [ columnLimitToEdit, setColumnLimitToEdit ] = useState<string>();
const [ activeColumn, setActiveColumn ] = useState<string>();
+ /**
+ * Whether every collapsed column is open at once, which "Expand all columns" asks for.
+ *
+ * Held apart from `activeColumn`, which names one column: a column drawn open by this is not
+ * the column the reader is working in, and is closed by the same signal a single peek is, which
+ * is a column being selected or focused.
+ */
+ const [ isPeekingAll, setIsPeekingAll ] = useState(false);
+ const selectColumn = useCallback<Dispatch<StateUpdater<string | undefined>>>((column) => {
+ setIsPeekingAll(false);
+ setActiveColumn(column);
+ }, []);
// 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);
@@ -289,17 +302,37 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
const api = apiRef.current.api;
// Every member is one of useState's own setters, so this value is built once and never changes
// identity -- a drag cannot reach anything that reads only this.
+ const openBoardMenu = useCallback((event: ContextMenuEvent) => {
+ // Only the ground the columns stand on. A column and a card answer for their own presses,
+ // and what they leave alone, such as the button that makes a card, is left alone here too.
+ if ((event.target as HTMLElement)?.closest(".board-column, .board-add-column")) {
+ return;
+ }
+
+ openBoardContextMenu(event, {
+ onCollapseAll: () => {
+ setIsPeekingAll(false);
+ api.setAllColumnsCollapsed(true);
+ },
+ onExpandAll: () => {
+ // Opened for good where the column is not kept collapsed, and peeked where it is.
+ setIsPeekingAll(true);
+ api.setAllColumnsCollapsed(false);
+ }
+ });
+ }, [ api ]);
+
const boardActions = useMemo<BoardActions>(() => ({
setBranchIdToEdit,
setColumnNameToEdit,
setColumnLimitToEdit,
- setActiveColumn,
+ setActiveColumn: selectColumn,
setDraggedCard,
setDraggedColumn,
setDropPosition,
setDropTarget
}), [
- setBranchIdToEdit, setColumnNameToEdit, setColumnLimitToEdit, setActiveColumn,
+ setBranchIdToEdit, setColumnNameToEdit, setColumnLimitToEdit, selectColumn,
setDraggedCard, setDraggedColumn, setDropPosition, setDropTarget
]);
@@ -427,7 +460,7 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
// one merely passed near keeps to itself, and one already opened stays open, since
// closing it under a drag would move every column after it.
if (position && inside && storedColumns.get(position.column)?.collapsed) {
- setActiveColumn(position.column);
+ selectColumn(position.column);
}
},
onCardEnd: (card, position) => {
@@ -546,7 +579,7 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
// The board is not drawn afresh for another note, so the column opened on one would otherwise
// still be open on the next, over whatever that board stores for a column of the same name.
- useEffect(() => setActiveColumn(undefined), [ parentNote ]);
+ useEffect(() => selectColumn(undefined), [ parentNote, selectColumn ]);
useEffect(refresh, [
parentNote, noteIds, viewConfig, statusAttributeWithPrefix, statusDefinition, inboxEnabled
@@ -615,6 +648,7 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
panning: isPanning
})}
onKeyDown={handleKeyDown}
+ onContextMenu={openBoardMenu}
onWheel={onWheelHorizontalScroll}
>
{/* The columns are keyed by value, so a reorder moves each column's
@@ -647,6 +681,7 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC
collapsed={storedColumns.get(column)?.collapsed}
keepCollapsed={storedColumns.get(column)?.keepCollapsed}
isActive={activeColumn === column}
+ isPeeked={isPeekingAll}
nested={storedColumns.get(column)?.nested}
limit={storedColumns.get(column)?.limit}
columnIndex={index}