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/ui/icon picker: allow setting the icon before creating a new column
Adorian Doran1ecb13de61e4
diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.tsindex d5276e85a7..596601e329 100644--- a/apps/client/src/widgets/collections/board/api.spec.ts+++ b/apps/client/src/widgets/collections/board/api.spec.ts@@ -1189,6 +1189,18 @@ describe("collapsing a column", () => {expect(saved.at(-1)?.columns).toEqual([ { value: "To Do" } ]);});+ /** The icon goes in with the column, rather than as a second write and a second refresh. */+ it("stores a new column with the icon picked for it", async () => {+ const { api, saved } = createApi({ columns: [ { value: "To Do" } ] }, [ "To Do" ]);++ await api.addNewColumn("Blocked", false, "bx bx-star");+ expect(saved.at(-1)?.columns)+ .toEqual([ { value: "To Do" }, { value: "Blocked", icon: "bx bx-star" } ]);++ await api.addNewColumn("Doing", true, "bx bx-run");+ expect(saved.at(-1)?.columns?.[0]).toEqual({ value: "Doing", icon: "bx bx-run" });+ });+/*** 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.diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.tsindex fcc1dd9408..6a19f3e8e5 100644--- a/apps/client/src/widgets/collections/board/api.ts+++ b/apps/client/src/widgets/collections/board/api.ts@@ -293,7 +293,7 @@ export default class BoardApi {** @param atStart whether it goes at the head of the board rather than after the last column.*/- async addNewColumn(columnName: string, atStart = false) {+ async addNewColumn(columnName: string, atStart = false, icon?: string) {if (!columnName.trim()) {return;}@@ -304,8 +304,12 @@ export default class BoardApi {if (columns.some(col => col.value === columnName)) return false;settleColumn(this.pending, columnName);+ // The icon goes in with the column rather than after it: a write of its own would be a+ // second refresh of the board for a column that has only just been drawn.+ const added: BoardColumnData = icon ? { value: columnName, icon } : { value: columnName };+if (!atStart) {- this.storeColumns([ ...columns, { value: columnName } ]);+ this.storeColumns([ ...columns, added ]);return true;}@@ -321,7 +325,7 @@ export default class BoardApi {const byValue = new Map(columns.map(col => [ col.value, col ]));const placed: BoardColumnData[] = order.map(value => byValue.get(value) ?? { value });- placed.splice(order[0] === INBOX_COLUMN ? 1 : 0, 0, { value: columnName });+ placed.splice(order[0] === INBOX_COLUMN ? 1 : 0, 0, added);this.storeColumns(placed);return true;}diff --git a/apps/client/src/widgets/collections/board/index.css b/apps/client/src/widgets/collections/board/index.cssindex fe06c39c8a..704ef0b0dd 100644--- a/apps/client/src/widgets/collections/board/index.css+++ b/apps/client/src/widgets/collections/board/index.css@@ -745,16 +745,24 @@ body.mobile .column-drop-placeholder {font-size: 1.2em;}-.board-add-column input {- background: var(--main-background-color);+/* The box is the slot, as a column's own heading is the box while its title is edited, and the+ field inside carries nothing of its own. The card's padding and line, so the icon beside it lands+ where the shared rule above puts it. */+.board-add-column.editing {+ background-color: var(--main-background-color);border: 1px solid var(--main-text-color);- border-radius: 4px;- padding: 0.5em;+}++.board-add-column input {+ background: transparent;+ border: none;+ outline: none;+ padding: var(--card-padding);+ line-height: var(--card-line-height);color: var(--main-text-color);font-family: inherit;font-size: inherit;width: 100%;- text-align: center;}/* The menu is drawn outside the board, so the columns it lists are named rather than reacheddiff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsxindex cc4fa48431..32a7bc64e8 100644--- a/apps/client/src/widgets/collections/board/index.spec.tsx+++ b/apps/client/src/widgets/collections/board/index.spec.tsx@@ -1915,7 +1915,7 @@ describe("Board column rename", () => {await flush();});- expect(added).toHaveBeenCalledWith("Blocked", false);+ expect(added).toHaveBeenCalledWith("Blocked", false, undefined);expect(slot?.querySelector("input")).toBe(editor);expect(editor.value).toBe("");expect(document.activeElement).toBe(editor);@@ -1957,6 +1957,42 @@ describe("Board column rename", () => {expect(editor.value).toBe("Blocked");});+ /**+ * The same picker the column's own heading carries, so a column is given its icon as it is+ * named. It is kept for the next column, as the card editor keeps its own.+ */+ it("makes a column with the icon picked in the editor, and keeps it for the next", async () => {+ const { container } = await setup();+ const added = vi.spyOn(BoardApi.prototype, "addNewColumn").mockResolvedValue(true);+ added.mockClear();+ const slot = container.querySelector<HTMLElement>(".board-add-column");++ await act(async () => {+ slot?.click();+ await flush();+ });++ const editor = slot?.querySelector<HTMLInputElement>("input");+ if (!editor) throw new Error("expected the add-column editor to be open");+ // What a column is drawn with, until something else is picked.+ expect(slot?.querySelector(".title-editor-icon button")?.className)+ .toContain(DEFAULT_COLUMN_ICON);++ await act(async () => {+ slot?.querySelector<HTMLElement>(".title-editor-icon button")?.click();+ await flush();+ });+ await type(editor, "Starred");+ await act(async () => {+ editor.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));+ await flush();+ });++ expect(added).toHaveBeenCalledWith("Starred", false, PICKED_ICON);+ expect(editor.value).toBe("");+ expect(slot?.querySelector(".title-editor-icon button")?.className).toContain(PICKED_ICON);+ });+/** The same two ends a card's button offers, named for a board that runs across. */it("offers both ends of the board from the add-column button", async () => {const { container } = await setup();@@ -1999,7 +2035,7 @@ describe("Board column rename", () => {entries[0]?.handler?.({} as never, {} as never);await flush();});- expect(added).toHaveBeenCalledWith("Blocked", true);+ expect(added).toHaveBeenCalledWith("Blocked", true, undefined);expect(editor.value).toBe("");// Shift+Enter says the same thing from the field itself.@@ -2010,7 +2046,7 @@ describe("Board column rename", () => {{ key: "Enter", shiftKey: true, bubbles: true }));await flush();});- expect(added).toHaveBeenCalledWith("Also first", true);+ expect(added).toHaveBeenCalledWith("Also first", true, undefined);show.mockRestore();});diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsxindex 570ef13c74..ccdf290c3b 100644--- a/apps/client/src/widgets/collections/board/index.tsx+++ b/apps/client/src/widgets/collections/board/index.tsx@@ -37,7 +37,7 @@ import Api, { getPendingWrites, PendingColumnWrites, settleColumn } from "./api"import { useBoardDrag } from "./board_drag";import { movesColumn } from "./drag_geometry";import BoardApi from "./api";-import { DEFAULT_GROUP_BY, getStatusDefinition, INBOX_COLUMN } from "./columns";+import { DEFAULT_COLUMN_ICON, DEFAULT_GROUP_BY, getStatusDefinition, INBOX_COLUMN } from "./columns";import Column from "./column";import ColumnLimitDialog from "./column_limit";import { openBoardContextMenu, openCreateColumnMenu } from "./context_menu";@@ -782,6 +782,9 @@ function AddNewColumn({ api, isInRelationMode, columnCount, onCreated }: {onCreated: (column: string) => void}) {const [ isCreatingNewColumn, setIsCreatingNewColumn ] = useState(false);+ // Kept between columns, as the card editor keeps its own: a run of columns is often a run of+ // the same kind of column.+ const [ icon, setIcon ] = useState(DEFAULT_COLUMN_ICON);const slotRef = useRef<HTMLDivElement>(null);// Keyed on the count rather than done when the write returns: the column it makes room for is@@ -827,7 +830,8 @@ function AddNewColumn({ api, isInRelationMode, columnCount, onCreated }: {<TitleEditorplaceholder={t("board_view.add-column-placeholder")}save={async (columnName, atStart) => {- const created = await api.addNewColumn(columnName, atStart);+ const created = await api.addNewColumn(columnName, atStart,+ icon !== DEFAULT_COLUMN_ICON ? icon : undefined);if (created) {onCreated(columnName);} else {@@ -842,6 +846,15 @@ function AddNewColumn({ api, isInRelationMode, columnCount, onCreated }: {saveAndContinue={!isInRelationMode}submitTitle={t("board_view.create-new-column")}openPlacements={openCreateColumnMenu}+ // The same picker the column's own heading carries, so a column is given+ // its icon as it is named rather than after it stands there.+ icon={{+ current: icon,+ onSelect: setIcon,+ onReset: icon !== DEFAULT_COLUMN_ICON+ ? () => setIcon(DEFAULT_COLUMN_ICON)+ : undefined+ }}mode={isInRelationMode ? "relation" : "normal"}/>)}