Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
client/collections/board: allow the columns to be moved via the context menu
Adorian Doran7300cde26d30
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 3db711da52..9faf316c69 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3371,6 +3371,9 @@"add-column-placeholder": "Enter column name...","edit-note-title": "Click to edit note title","rename-column": "Rename column",+ "move-column": "Move column",+ "move-column-first": "As the first column",+ "move-column-after": "After {{column}}","change-column-icon": "Change column icon","column-menu": "Column menu","column-already-exists": "This column already exists on the board.",diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsxindex f809635904..00864f4025 100644--- a/apps/client/src/widgets/collections/board/column.tsx+++ b/apps/client/src/widgets/collections/board/column.tsx@@ -32,6 +32,9 @@ interface DragContext {export default function Column({column,columnIndex,+ columns,+ onMoveColumn,+ onFocusColumn,icon,color,archived,@@ -55,7 +58,12 @@ export default function Column({parentNote: FNote,onColumnHover?: (index: number, mouseX: number, rect: DOMRect) => void,isAnyColumnDragging?: boolean,- isInRelationMode: boolean+ isInRelationMode: boolean,+ /** The columns as drawn, which is what the menu offers to place this one among. */+ columns: string[],+ /** Moves a column to sit before the given position, as a drag onto that position would. */+ onMoveColumn: (fromIndex: number, toIndex: number) => void,+ onFocusColumn: (column: string) => void} & DragContext) {const [ isVisible, setVisible ] = useState(true);const [ isCreatingNewItem, setIsCreatingNewItem ] = useState(false);@@ -70,15 +78,26 @@ export default function Column({const openMenu = useCallback((e: ContextMenuEvent) => {openColumnContextMenu(api, e, {value: column,+ columns,+ index: columnIndex,color,archived,onEditTitle: () => setColumnNameToEdit(column),onNewItem: () => setIsCreatingNewItem(true),onAddColumn: async (direction) => {setColumnNameToEdit(await api.insertColumn(column, direction));+ },+ onMoveColumn: (toIndex) => {+ onMoveColumn(columnIndex, toIndex);+ // Asked for by name: the move draws the board again, and the heading the menu was+ // opened from is the one left standing over another column afterwards.+ onFocusColumn(column);}});- }, [ api, column, color, archived, setColumnNameToEdit ]);+ }, [+ api, column, color, archived, columns, columnIndex,+ setColumnNameToEdit, onMoveColumn, onFocusColumn+ ]);// A fully desaturated colour has no hue to tint with, and leaves the column plain.const hue = useMemo(() => {diff --git a/apps/client/src/widgets/collections/board/context_menu.spec.ts b/apps/client/src/widgets/collections/board/context_menu.spec.tsindex 803e0e8114..0e5e2cd979 100644--- a/apps/client/src/widgets/collections/board/context_menu.spec.ts+++ b/apps/client/src/widgets/collections/board/context_menu.spec.ts@@ -30,11 +30,12 @@ describe("Board column context menu", () => {function openMenu(api: BoardApi,- column: { color?: string, archived?: boolean } = {},+ column: { color?: string, archived?: boolean, columns?: string[], index?: number } = {},callbacks: {onEditTitle?: () => void,onNewItem?: () => void,- onAddColumn?: (direction: "before" | "after") => void+ onAddColumn?: (direction: "before" | "after") => void,+ onMoveColumn?: (toIndex: number) => void} = {}) {const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});@@ -47,10 +48,13 @@ describe("Board column context menu", () => {openColumnContextMenu(api, event, {value: "To Do",+ columns: [ "To Do" ],+ index: 0,...column,onEditTitle: callbacks.onEditTitle ?? (() => {}),onNewItem: callbacks.onNewItem ?? (() => {}),- onAddColumn: callbacks.onAddColumn ?? (() => {})+ onAddColumn: callbacks.onAddColumn ?? (() => {}),+ onMoveColumn: callbacks.onMoveColumn ?? (() => {})});// The spy outlives one call, so it is the menu just opened that is read back.@@ -110,10 +114,43 @@ describe("Board column context menu", () => {expect(titled.map(item => "uiIcon" in item ? item.uiIcon : undefined)).toEqual(["bx bx-edit-alt", "bx bx-plus", "bx bx-link", "bx bx-columns",- "bx bx-archive", "bx bx-trash"+ "bx bx-horizontal-left", "bx bx-archive", "bx bx-trash"]);});+ /** Every place offered has to actually move the column, or the menu promises nothing. */+ it("offers only the places that would move the column", () => {+ const api = {+ getColumnIcon: () => DEFAULT_COLUMN_ICON,+ getColumnColorClass: () => "",+ isColumnArchived: () => false+ } as unknown as BoardApi;++ /** Where each entry would send the column, in the order the submenu offers them. */+ const places = (index: number) => {+ const moved: number[] = [];+ const menu = openMenu(api, { columns: [ "To Do", "Doing", "Done" ], index }, {+ onMoveColumn: (toIndex) => moved.push(toIndex)+ });+ const entry = menu.find(item =>+ item && "uiIcon" in item && item.uiIcon === "bx bx-horizontal-left");+ if (!entry || !("items" in entry)) throw new Error("expected a move-column entry");++ for (const item of entry.items ?? []) {+ if (item && "handler" in item) item.handler?.(item, {} as never);+ }+ return moved;+ };++ // A column is placed before a position, so following the one at an index means past it.+ // From the head: no head to move to, and nothing for itself or the column it already leads.+ expect(places(0)).toEqual([ 2, 3 ]);+ // From the middle: the head, and past the tail. Following "To Do" is where it already is.+ expect(places(1)).toEqual([ 0, 3 ]);+ // From the tail: the head, and following "To Do".+ expect(places(2)).toEqual([ 0, 1 ]);+ });+it("offers both sides to put a new column on", () => {const onAddColumn = vi.fn();const parent = openMenu({} as BoardApi, {}, { onAddColumn })diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.tsindex 5984c768f1..aeb9e69b39 100644--- a/apps/client/src/widgets/collections/board/context_menu.ts+++ b/apps/client/src/widgets/collections/board/context_menu.ts@@ -2,7 +2,7 @@ import { useState } from "preact/hooks";import FNote from "../../../entities/fnote";import NoteColorPicker from "../../../menus/custom-items/NoteColorPicker";-import contextMenu, { ContextMenuEvent } from "../../../menus/context_menu";+import contextMenu, { ContextMenuEvent, MenuItem } from "../../../menus/context_menu";import link_context_menu from "../../../menus/link_context_menu";import branches from "../../../services/branches";import dialog from "../../../services/dialog";@@ -11,8 +11,13 @@ import { t } from "../../../services/i18n";import ColorPicker from "../../react/ColorPicker";import Api from "./api";-export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column: {+/** What the column menu is opened for: the column itself, and what it can be asked to do. */+interface ColumnMenuTarget {value: string;+ /** The columns as drawn, in the order they stand. */+ columns: string[];+ /** Where this column stands among them. */+ index: number;color?: string;archived?: boolean;/** Puts the title into its inline editor, the menu being the only way there besides F2. */@@ -21,7 +26,12 @@ export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column:onNewItem: () => void;/** Puts a new column on one side of this one and opens its title editor. */onAddColumn: (direction: "before" | "after") => void;-}) {+ /** Moves this column to sit before the given position among the columns as drawn. */+ onMoveColumn: (toIndex: number) => void;+}++export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column: ColumnMenuTarget) {+event.preventDefault();event.stopPropagation();@@ -71,6 +81,12 @@ export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column:]},{ kind: "separator" },+ {+ title: t("board_view.move-column"),+ uiIcon: "bx bx-horizontal-left",+ items: buildMoveColumnItems(api, column)+ },+ { kind: "separator" },column.archived? {title: t("board_view.unarchive-column"),@@ -98,6 +114,41 @@ export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column:});}+/**+ * Where a column can be sent, each place named by what the column would come to stand after.+ *+ * A place is left out where sending it there would leave the board as it stands: the column itself,+ * the one it already follows, and the head of the board for a column already at the head.+ */+function buildMoveColumnItems(api: Api, column: ColumnMenuTarget): MenuItem<string>[] {+ const head: MenuItem<string>[] = column.index > 0+ ? [ {+ title: t("board_view.move-column-first"),+ uiIcon: "bx bx-chevrons-left",+ handler: () => column.onMoveColumn(0)+ } ]+ : [];++ const after = column.columns.flatMap<MenuItem<string>>((name, index) => {+ if (index === column.index || index === column.index - 1) {+ return [];+ }++ return [ {+ title: t("board_view.move-column-after", { column: name }),+ uiIcon: api.getColumnIcon(name),+ iconColorClass: api.getColumnColorClass(name),+ badges: api.isColumnArchived(name)+ ? [ { title: t("board_view.archived-badge") } ]+ : undefined,+ // A column is placed before a position, so standing after `name` means the one past it.+ handler: () => column.onMoveColumn(index + 1)+ } ];+ });++ return [ ...head, ...after ];+}+/*** The colour a column is tinted with, picked the way a note's is.*diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsxindex fdce2cbdfe..8ca9f3bd25 100644--- a/apps/client/src/widgets/collections/board/index.tsx+++ b/apps/client/src/widgets/collections/board/index.tsx@@ -309,7 +309,7 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveCsetColumnDropPosition(null);}, [ api, columns, shownColumns ]);- const handleKeyDown = useBoardKeyboard({+ const { onKeyDown: handleKeyDown, focusColumn } = useBoardKeyboard({containerRef,columns: shownColumns,byColumn,@@ -386,6 +386,9 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveCcolor={storedColumns.get(column)?.color}archived={storedColumns.get(column)?.archived}columnIndex={index}+ columns={shownColumns}+ onMoveColumn={handleColumnDrop}+ onFocusColumn={focusColumn}columnItems={byColumn.get(column)}isDraggingColumn={draggedColumn?.column === column}onColumnHover={handleColumnHover}diff --git a/apps/client/src/widgets/collections/board/keyboard.ts b/apps/client/src/widgets/collections/board/keyboard.tsindex 89db8e0b40..ae9a13ad4d 100644--- a/apps/client/src/widgets/collections/board/keyboard.ts+++ b/apps/client/src/widgets/collections/board/keyboard.ts@@ -114,7 +114,16 @@ export function useBoardKeyboard({element?.focus();});- return useCallback((e: KeyboardEvent) => {+ /**+ * Puts focus on a column's heading once the board has drawn it again, for a move made from+ * somewhere other than the keyboard. The columns are drawn unkeyed, so an element held onto+ * across the move would be left standing over whichever column took the old one's place.+ */+ const focusColumn = useCallback((column: string) => {+ pendingFocus.current = { intent: { column, part: "header" } };+ }, []);++ const onKeyDown = useCallback((e: KeyboardEvent) => {const container = containerRef.current;const target = e.target as HTMLElement | null;if (!container || e.metaKey) return;@@ -239,6 +248,8 @@ export function useBoardKeyboard({}}}, [ containerRef, columns, byColumn, api, moveColumn, insertColumn ]);++ return { onKeyDown, focusColumn };}/** What to put focus on once a card goes: the one under it, the one over it, or the column. */