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: add a command to the column menu to insert a new column
Adorian Doran1255e906fe59
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex d7088301d9..465ef5c945 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -3360,6 +3360,10 @@"add-existing-item": "Add existing note...","add-existing-item-title": "Add an existing note to the board","add-existing-item-ok": "Add to column",+ "add-new-column": "Add new column",+ "add-column-before": "Before",+ "add-column-after": "After",+ "new-column": "New column","existing-item-conflict": "This note already belongs to another board that groups by “{{label}}”. Adding it here will move it to a different column on the original board as well. Add it anyway?","add-column": "Add column","add-column-placeholder": "Enter column name...",diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.tsindex 4f73ec6a8a..1d5e56ef97 100644--- a/apps/client/src/widgets/collections/board/api.spec.ts+++ b/apps/client/src/widgets/collections/board/api.spec.ts@@ -91,6 +91,38 @@ describe("BoardApi column mutations", () => {expect(saved).toHaveLength(0);});+ it("puts a new column on either side of the one it is given", async () => {+ const { api, saved } = createApi(+ { columns: [ { value: "To Do" }, { value: "Done" } ] },+ [ "To Do", "Done" ]+ );++ expect(await api.insertColumn("To Do", "after")).toBe("board_view.new-column");+ expect(saved.at(-1)?.columns?.map(col => col.value))+ .toEqual([ "To Do", "board_view.new-column", "Done" ]);++ // The second is placed against the config just written, not the list the view still shows.+ expect(await api.insertColumn("Done", "before")).toBe("board_view.new-column 2");+ expect(saved.at(-1)?.columns?.map(col => col.value)).toEqual([+ "To Do", "board_view.new-column", "board_view.new-column 2", "Done"+ ]);+ });++ it("keeps the icon of a column it puts one beside", async () => {+ const { api, saved } = createApi(+ { columns: [ { value: "To Do", icon: "bx bx-list-ul", color: "#e64d4d" } ] },+ [ "To Do" ]+ );++ await api.insertColumn("To Do", "before");++ expect(saved.at(-1)?.columns)+ .toEqual([+ { value: "board_view.new-column" },+ { value: "To Do", icon: "bx bx-list-ul", color: "#e64d4d" }+ ]);+ });+it("keeps the icon of every column it reorders", () => {const { api, saved } = createApi({ columns: [ { value: "To Do", icon: "bx bx-list-ul" }, { value: "Done" } ] },diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.tsindex 255e79f231..cea99b6b35 100644--- a/apps/client/src/widgets/collections/board/api.ts+++ b/apps/client/src/widgets/collections/board/api.ts@@ -113,6 +113,45 @@ export default class BoardApi {return true;}+ /**+ * Puts a new column beside an existing one, and hands back the name it was given for the caller+ * to open its title editor with.+ *+ * Named rather than left blank: a column is known by its value, so until it has one there is+ * nothing to place it by, nothing to store and nothing to rename. The stock name is numbered+ * where it is already taken, so adding several in a row cannot silently do nothing.+ */+ async insertColumn(relativeTo: string, direction: "before" | "after") {+ const stored = this.viewConfig?.columns ?? [];+ const taken = new Set([ ...this.columns, ...stored.map(col => col.value) ]);++ const stockName = t("board_view.new-column");+ let name = stockName;+ for (let suffix = 2; taken.has(name); suffix++) {+ name = `${stockName} ${suffix}`;+ }++ // Placed by the stored order rather than the derived one, which lags a column just added:+ // `columns` is rebuilt only when the view re-renders, while the config is written here.+ // Columns the config does not know yet keep their derived places, at the end.+ const order = stored.map(col => col.value);+ for (const derived of this.columns) {+ if (!order.includes(derived)) {+ order.push(derived);+ }+ }++ const neighbour = order.indexOf(relativeTo);+ order.splice(+ neighbour < 0 ? order.length : neighbour + (direction === "after" ? 1 : 0), 0, name);++ // Entries carry more than their name, so each is moved rather than rebuilt.+ const byValue = new Map(stored.map(col => [ col.value, col ]));+ this.storeColumns(order.map(value => byValue.get(value) ?? { value }));++ return name;+ }+async removeColumn(column: string) {// Remove the value from the notes.const noteIds = this.byColumn?.get(column)?.map(item => item.note.noteId) || [];diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsxindex 64039cf677..3bfef2e7ee 100644--- a/apps/client/src/widgets/collections/board/column.tsx+++ b/apps/client/src/widgets/collections/board/column.tsx@@ -71,7 +71,10 @@ export default function Column({color,archived,onEditTitle: () => setColumnNameToEdit(column),- onNewItem: () => setIsCreatingNewItem(true)+ onNewItem: () => setIsCreatingNewItem(true),+ onAddColumn: async (direction) => {+ setColumnNameToEdit(await api.insertColumn(column, direction));+ }});}, [ api, column, color, archived, setColumnNameToEdit ]);diff --git a/apps/client/src/widgets/collections/board/columns.spec.ts b/apps/client/src/widgets/collections/board/columns.spec.tsindex af234b200c..35db9672b9 100644--- a/apps/client/src/widgets/collections/board/columns.spec.ts+++ b/apps/client/src/widgets/collections/board/columns.spec.ts@@ -44,6 +44,22 @@ describe("resolveBoardColumns", () => {)).toEqual([ "Todo" ]);});+ /**+ * The definition is written a round trip after the config, so right after an insert or a+ * reorder it still holds the old order. Leading with it there would put the new column behind+ * every option it lists, and the refresh would persist that.+ */+ it("takes the order from the board's own list once that accounts for every column", () => {+ expect(resolveBoardColumns(+ [ "To Do", "Done" ],+ [ "To Do", "New column", "Done" ],+ [ "To Do", "Done" ]+ )).toEqual([ "To Do", "New column", "Done" ]);++ expect(resolveBoardColumns([ "To Do", "Done" ], [ "Done", "To Do" ], [ "To Do", "Done" ]))+ .toEqual([ "Done", "To Do" ]);+ });+it("keeps a renamed column in the slot the old name held", () => {expect(resolveBoardColumns([ "To Do", "Doing", "Done" ],diff --git a/apps/client/src/widgets/collections/board/columns.ts b/apps/client/src/widgets/collections/board/columns.tsindex 614db9884e..d90ce39eef 100644--- a/apps/client/src/widgets/collections/board/columns.ts+++ b/apps/client/src/widgets/collections/board/columns.ts@@ -97,11 +97,14 @@ export function canStoreColumnsInDefinition(statusDefinition: BoardStatusDefinit* The columns to show, from the choices the definition offers, the columns the board persisted, and* the values its notes actually carry.*- * The definition leads because it is the shared answer — the same list the promoted field and the- * table view offer — and the other two follow so that a column can never disappear: one the board- * arranged before it had a definition, or a value a note still holds after the option behind it was- * renamed away, both keep their place rather than taking their notes with them. Blank entries are- * dropped and the comparison is exact, since two columns differing only in case are two columns.+ * The definition leads on membership because it is the shared answer — the same list the promoted+ * field and the table view offer — and the other two follow so that a column can never disappear:+ * one the board arranged before it had a definition, or a value a note still holds after the option+ * behind it was renamed away, both keep their place rather than taking their notes with them. Blank+ * entries are dropped and the comparison is exact, since two columns differing only in case are two+ * columns.+ *+ * On order the attachment leads instead, wherever it already lists every column (see below).** @param pendingRenames what the board has just renamed each column to, or `undefined` where it* deleted one. The three sources are written one at a time, and a refresh in@@ -116,16 +119,27 @@ export function resolveBoardColumns(discoveredValues: string[],pendingRenames: ReadonlyMap<string, string | undefined> = new Map()): string[] {- const columns: string[] = [];- const seen = new Set<string>();-- for (const candidate of [ ...definitionOptions, ...persistedColumns, ...discoveredValues ]) {- const trimmed = candidate.trim();- const value = pendingRenames.has(trimmed) ? pendingRenames.get(trimmed) : trimmed;- if (!value || seen.has(value)) continue;- seen.add(value);- columns.push(value);- }+ const resolve = (candidates: string[]) => {+ const columns: string[] = [];+ const seen = new Set<string>();++ for (const candidate of candidates) {+ const trimmed = candidate.trim();+ const value = pendingRenames.has(trimmed) ? pendingRenames.get(trimmed) : trimmed;+ if (!value || seen.has(value)) continue;+ seen.add(value);+ columns.push(value);+ }++ return columns;+ };++ const columns = resolve([ ...definitionOptions, ...persistedColumns, ...discoveredValues ]);+ const persisted = resolve(persistedColumns);- return columns;+ // The definition leads on which columns there are, but not on the order once the board's own+ // list holds every one of them: that list is the arrangement made here, which a definition+ // shared with other notes cannot express, and which it lags by a round trip after every insert+ // and reorder. Equal lengths are enough to tell, the attachment being one of the sources above.+ return persisted.length === columns.length ? persisted : columns;}diff --git a/apps/client/src/widgets/collections/board/context_menu.spec.ts b/apps/client/src/widgets/collections/board/context_menu.spec.tsindex 91f5bb1f87..160ad82d7d 100644--- a/apps/client/src/widgets/collections/board/context_menu.spec.ts+++ b/apps/client/src/widgets/collections/board/context_menu.spec.ts@@ -24,7 +24,8 @@ describe("Board column context menu", () => {column: { color?: string, archived?: boolean } = {},callbacks: {onEditTitle?: () => void,- onNewItem?: () => void+ onNewItem?: () => void,+ onAddColumn?: (direction: "before" | "after") => void} = {}) {const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});@@ -39,7 +40,8 @@ describe("Board column context menu", () => {value: "To Do",...column,onEditTitle: callbacks.onEditTitle ?? (() => {}),- onNewItem: callbacks.onNewItem ?? (() => {})+ onNewItem: callbacks.onNewItem ?? (() => {}),+ onAddColumn: callbacks.onAddColumn ?? (() => {})});// The spy outlives one call, so it is the menu just opened that is read back.@@ -98,10 +100,25 @@ describe("Board column context menu", () => {const titled = openMenu({} as BoardApi).filter(item => item && "uiIcon" in item);expect(titled.map(item => "uiIcon" in item ? item.uiIcon : undefined)).toEqual([- "bx bx-edit-alt", "bx bx-plus", "bx bx-link", "bx bx-archive", "bx bx-trash"+ "bx bx-edit-alt", "bx bx-plus", "bx bx-link", "bx bx-columns",+ "bx bx-archive", "bx bx-trash"]);});+ it("offers both sides to put a new column on", () => {+ const onAddColumn = vi.fn();+ const parent = openMenu({} as BoardApi, {}, { onAddColumn })+ .find(item => item && "uiIcon" in item && item.uiIcon === "bx bx-columns");+ if (!parent || !("items" in parent)) throw new Error("expected an add-column entry");++ const sides = parent.items ?? [];+ expect(sides).toHaveLength(2);+ for (const side of sides) {+ if (side && "handler" in side) side.handler?.(side, {} as never);+ }+ expect(onAddColumn.mock.calls.flat()).toEqual([ "before", "after" ]);+ });+it("opens the column's new-item editor, the same one its button opens", () => {const onNewItem = vi.fn();const entry = openMenu({} as BoardApi, {}, { onNewItem })diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.tsindex 381167798a..90efa51ddd 100644--- a/apps/client/src/widgets/collections/board/context_menu.ts+++ b/apps/client/src/widgets/collections/board/context_menu.ts@@ -19,6 +19,8 @@ export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column:onEditTitle: () => void;/** Opens the column's own new-item editor, the same one its button opens. */onNewItem: () => void;+ /** Puts a new column on one side of this one and opens its title editor. */+ onAddColumn: (direction: "before" | "after") => void;}) {event.preventDefault();event.stopPropagation();@@ -51,6 +53,20 @@ export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column:}}},+ {+ title: t("board_view.add-new-column"),+ uiIcon: "bx bx-columns",+ items: [+ {+ title: t("board_view.add-column-before"),+ handler: () => column.onAddColumn("before")+ },+ {+ title: t("board_view.add-column-after"),+ handler: () => column.onAddColumn("after")+ }+ ]+ },{ kind: "separator" },column.archived? {diff --git a/apps/client/src/widgets/collections/board/data.spec.ts b/apps/client/src/widgets/collections/board/data.spec.tsindex 6222248f97..0e060d678e 100644--- a/apps/client/src/widgets/collections/board/data.spec.ts+++ b/apps/client/src/widgets/collections/board/data.spec.ts@@ -30,6 +30,33 @@ describe("Board data", () => {const noteIds = [...data.byColumn.values()].flat().map(item => item.note.noteId);expect(noteIds.length).toBe(3);});+ /**+ * A column inserted or dragged is written to the attachment at once and to the definition a+ * round trip later. The refresh in between must not put it back where the definition says.+ */+ it("leaves a column just placed where the board put it, writing nothing back", async () => {+ const board = buildNote({+ title: "Board",+ "#collection": "",+ "#viewType": "board",+ children: [+ { title: "First", "#status": "To Do" },+ { title: "Second", "#status": "Done" }+ ]+ });++ const data = await getBoardData(+ board,+ "status",+ { columns: [ { value: "To Do" }, { value: "New column" }, { value: "Done" } ] },+ false,+ [ "To Do", "Done" ]+ );++ expect(data.columns).toEqual([ "To Do", "New column", "Done" ]);+ expect(data.newPersistedData).toBeUndefined();+ });+/*** The notes, the view config and the definition are written one at a time, so a refresh in* between reads a source that still offers the old value. Resolution being additive, what itdiff --git a/apps/client/src/widgets/dialogs/note_picker.tsx b/apps/client/src/widgets/dialogs/note_picker.tsxindex 26f56f6010..698083970f 100644--- a/apps/client/src/widgets/dialogs/note_picker.tsx+++ b/apps/client/src/widgets/dialogs/note_picker.tsx@@ -71,7 +71,10 @@ export default function NotePickerDialog() {show={shown}stackable>- <FormGroup name="note-picker-note" label={opts.current?.message ?? t("note_picker.label")}>+ <FormGroup+ name="note-picker-note"+ label={opts.current?.message ?? t("note_picker.label")}+ ><NoteAutocompleteinputRef={inputRef}onChange={setSuggestion}