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: fix the new column input field behavior
Adorian Doran716381631607
diff --git a/apps/client/src/widgets/collections/board/card.tsx b/apps/client/src/widgets/collections/board/card.tsxindex 72248f660b..60f37e6dd2 100644--- a/apps/client/src/widgets/collections/board/card.tsx+++ b/apps/client/src/widgets/collections/board/card.tsx@@ -146,7 +146,7 @@ function Card({ref={cardRef}className={`board-note ${colorClass} ${isDragging ? 'dragging' : ''} ${isEditing ? "editing" : ""} ${isArchived ? "archived" : ""} ${isNew && !isRevealed ? "appearing" : ""}`}onAnimationEnd={(e) => {- if (e.animationName === "board-card-appear") {+ if (e.animationName === "board-item-appear") {setIsRevealed(true);}}}diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsxindex 91120861a2..d777fdbbfa 100644--- a/apps/client/src/widgets/collections/board/column.tsx+++ b/apps/client/src/widgets/collections/board/column.tsx@@ -30,6 +30,8 @@ interface DragContext {column: string;columnIndex: number,columnItems?: { note: FNote, branch: FBranch }[];+ /** Whether this is the column just added, which is revealed on arrival. */+ isNew?: boolean;}export default function Column({@@ -47,6 +49,7 @@ export default function Column({nested,limit,columnItems,+ isNew,api,parentNote,isInRelationMode@@ -78,6 +81,9 @@ export default function Column({} & DragContext) {const [ isCreatingNewItem, setIsCreatingNewItem ] = useState(false);const [ created, setCreated ] = useState<{ noteId?: string, takesFocus: boolean }>();+ // The column stays the one just added until another is, so what has already been shown is+ // remembered here rather than played again by every redraw of the board.+ const [ isRevealed, setIsRevealed ] = useState(false);// A card inserted next to another one is where the reader is working, so it is left focused; one// made in the footer is not, or every card would take focus from the editor still being typed in.const cardInserted = useCallback(@@ -191,8 +197,14 @@ export default function Column({"with-hue": hue !== undefined,"board-column-archived": archived,"over-limit": isOverLimit,- collapsed: isCollapsed+ collapsed: isCollapsed,+ appearing: isNew && !isRevealed})}+ onAnimationEnd={(e) => {+ if (e.animationName === "board-item-appear") {+ setIsRevealed(true);+ }+ }}onFocusIn={handleFocusIn}// A click and not a press: a press may be the start of a drag, which must leave the// column as it is, and a drag produces no click.diff --git a/apps/client/src/widgets/collections/board/index.css b/apps/client/src/widgets/collections/board/index.cssindex 8ea0a8d0a6..52f538d71c 100644--- a/apps/client/src/widgets/collections/board/index.css+++ b/apps/client/src/widgets/collections/board/index.css@@ -507,18 +507,20 @@ body.mobile .board-view-container .board-add-column {resize: none;}-/* A card lands at the end of a column the reader may not be looking at, so it announces itself. */-@keyframes board-card-appear {+/* A card or column lands where the reader may not be looking, so it announces itself. */+@keyframes board-item-appear {from { opacity: 0; }to { opacity: 1; }}-.board-view-container .board-note.appearing {- animation: board-card-appear 500ms ease-out;+.board-view-container .board-note.appearing,+.board-view-container .board-column.appearing {+ animation: board-item-appear 500ms ease-out;}@media (prefers-reduced-motion: reduce) {- .board-view-container .board-note.appearing {+ .board-view-container .board-note.appearing,+ .board-view-container .board-column.appearing {animation: none;}}diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsxindex 3e54a9de68..7b846b08ea 100644--- a/apps/client/src/widgets/collections/board/index.spec.tsx+++ b/apps/client/src/widgets/collections/board/index.spec.tsx@@ -518,7 +518,7 @@ describe("Board column creation", () => {return { note, container: mountPoint };}- /** Clicks "Add column", types a name and blurs the editor, as the user would. */+ /** Clicks "Add column", types a name and commits it with Enter, as the user would. */async function addColumn(container: HTMLElement, name: string) {await act(async () => {container.querySelector<HTMLElement>(".board-add-column")?.click();@@ -531,7 +531,7 @@ describe("Board column creation", () => {await act(async () => {input.focus();input.value = name;- input.blur();+ input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));await flush();});@@ -542,6 +542,58 @@ describe("Board column creation", () => {});}+ /**+ * A column is added at the right end of the board, past its edge on a board of any width, and+ * the editor waiting for the next one goes with it.+ */+ it("scrolls the board to its end as a column is added", async () => {+ const { container } = await setup();+ const board = container.querySelector<HTMLElement>(".board-view-container");+ const slot = container.querySelector<HTMLElement>(".board-add-column");+ if (!board || !slot) throw new Error("expected a board with an add-column button");++ // happy-dom lays nothing out, so the width the board scrolls within is stood in for.+ const widen = (width: number) => Object.defineProperty(board, "scrollWidth", {+ value: width, configurable: true, writable: true+ });++ widen(1200);+ await act(async () => {+ slot.click();+ await flush();+ });++ const input = slot.querySelector<HTMLInputElement>("input");+ if (!input) throw new Error("expected an inline editor for the new column");++ // The board grows by the column the write adds, and the scroll follows that rather than+ // the write, which returns before the column is drawn.+ board.scrollLeft = 0;+ widen(1450);+ await act(async () => {+ input.value = "In Progress";+ input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));+ await flush();+ });+ await act(async () => { await flush(); });++ expect(columnTitles(container)).toEqual([ "To Do", "Done", "In Progress" ]);+ expect(board.scrollLeft).toBe(1450);++ // And it fades in where it landed, once: a redraw of the board must not play it again.+ const added = container.querySelectorAll<HTMLElement>(".board-column")[2];+ expect(added.classList.contains("appearing")).toBe(true);+ expect(container.querySelectorAll(".board-column.appearing")).toHaveLength(1);++ await act(async () => {+ added.dispatchEvent(new AnimationEvent("animationend", {+ animationName: "board-item-appear", bubbles: true+ }));+ await flush();+ });+ expect(added.classList.contains("appearing")).toBe(false);+ });+it("renders a newly added column without leaving the view", async () => {const { container } = await setup();expect(columnTitles(container)).toEqual([ "To Do", "Done" ]);@@ -1207,7 +1259,7 @@ describe("Board column rename", () => {// the reveal again.await act(async () => {card.dispatchEvent(new AnimationEvent("animationend", {- animationName: "board-card-appear", bubbles: true+ animationName: "board-item-appear", bubbles: true}));await flush();});@@ -1390,6 +1442,48 @@ describe("Board column rename", () => {.toBe("Half a thought");});+ /**+ * Columns are added in runs as a board is set up, so the editor stays where it is with an empty+ * field rather than closing and having to be opened again for the next one.+ */+ it("clears the add-column editor on Enter, and closes it when it loses focus", async () => {+ const { container } = await setup();+ const added = vi.spyOn(BoardApi.prototype, "addNewColumn").mockResolvedValue(true);+ 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");++ editor.value = "Blocked";+ await act(async () => {+ editor.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));+ await flush();+ });++ expect(added).toHaveBeenCalledWith("Blocked");+ expect(slot?.querySelector("input")).toBe(editor);+ expect(editor.value).toBe("");+ expect(document.activeElement).toBe(editor);++ // Left alone, it goes back to being a button, and nothing is added from what it held.+ added.mockClear();+ editor.value = "Half a name";+ await act(async () => {+ editor.dispatchEvent(new FocusEvent("blur"));+ editor.dispatchEvent(new FocusEvent("focusout", { bubbles: true }));+ await flush();+ });++ expect(added).not.toHaveBeenCalled();+ expect(slot?.querySelector("input")).toBeNull();+ expect(slot?.classList.contains("editing")).toBe(false);+ });+it("starts the new item off with the character typed on its button", async () => {const { container } = await setup();const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]@@ -1566,7 +1660,7 @@ describe("Board editors and menus", () => {await act(async () => {input.focus();input.value = "Blocked";- input.blur();+ input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));await flush();});await act(async () => { await flush(); });@@ -1680,7 +1774,7 @@ describe("Board editors and menus", () => {await act(async () => {input.focus();input.value = name;- input.blur();+ input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));await flush();});await act(async () => { await flush(); });diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsxindex 17760ec847..88b0a6c35f 100644--- a/apps/client/src/widgets/collections/board/index.tsx+++ b/apps/client/src/widgets/collections/board/index.tsx@@ -206,6 +206,8 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveCconst [ columns, setColumns ] = useState<string[]>();const [ isInRelationMode, setIsRelationMode ] = useState(false);const [ draggedCard, setDraggedCard ] = useState<CardDrag | null>(null);+ /** The column just added, which is revealed once the board has drawn it. */+ const [ createdColumn, setCreatedColumn ] = useState<string>();const [ dropTarget, setDropTarget ] = useState<string | null>(null);const [ dropPosition, setDropPosition ] = useState<{ column: string, index: number } | null>(null);const [ draggedColumn, setDraggedColumn ] = useState<ColumnDrag | null>(null);@@ -601,6 +603,7 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveConFocusColumn={focusColumn}onFocusCard={focusCard}columnItems={byColumn.get(column)}+ isNew={column === createdColumn}/></Fragment>))}@@ -609,7 +612,12 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC)}</div>- <AddNewColumn api={api} isInRelationMode={isInRelationMode} />+ <AddNewColumn+ api={api}+ isInRelationMode={isInRelationMode}+ columnCount={shownColumns.length}+ onCreated={setCreatedColumn}+ />{/* Where what is being carried is put. Preact draws the layer and neverits contents, so the copy is not among the children it places. */}<div className="board-drag-layer" />@@ -673,8 +681,30 @@ export function findRefreshReason(loadResults: LoadResults, statusAttribute: strreturn null;}-function AddNewColumn({ api, isInRelationMode }: { api: BoardApi, isInRelationMode: boolean }) {+function AddNewColumn({ api, isInRelationMode, columnCount, onCreated }: {+ api: BoardApi,+ isInRelationMode: boolean,+ /** How many columns stand before this, which is what carries it past the board's edge. */+ columnCount: number,+ /** Names the column just made, which the board reveals as it draws it. */+ onCreated: (column: string) => void+}) {const [ isCreatingNewColumn, setIsCreatingNewColumn ] = useState(false);+ const slotRef = useRef<HTMLDivElement>(null);++ // Keyed on the count rather than done when the write returns: the column it makes room for is+ // drawn by a refresh that has yet to run at that point, so the board is not yet as wide as it+ // is about to be.+ useLayoutEffect(() => {+ if (!isCreatingNewColumn) {+ return;+ }++ const board = slotRef.current?.closest<HTMLElement>(".board-view-container");+ if (board) {+ board.scrollLeft = board.scrollWidth;+ }+ }, [ columnCount, isCreatingNewColumn ]);const addColumnCallback = useCallback(() => {setIsCreatingNewColumn(true);@@ -688,6 +718,7 @@ function AddNewColumn({ api, isInRelationMode }: { api: BoardApi, isInRelationMoreturn (<div+ ref={slotRef}className={`board-add-column ${isCreatingNewColumn ? "editing" : ""}`}onClick={addColumnCallback}onKeyDown={keydownCallback}@@ -703,12 +734,18 @@ function AddNewColumn({ api, isInRelationMode }: { api: BoardApi, isInRelationMoplaceholder={t("board_view.add-column-placeholder")}save={async (columnName) => {const created = await api.addNewColumn(columnName);- if (!created) {+ if (created) {+ onCreated(columnName);+ } else {toast.showMessage(t("board_view.column-already-exists"), undefined, "bx bx-duplicate");}}}dismiss={() => setIsCreatingNewColumn(false)}isNewItem+ // Columns are added in runs as a board is set up, so the editor is left+ // standing with an empty field. A column named by a note answers for+ // itself, since picking one is what closes that editor.+ saveAndContinue={!isInRelationMode}mode={isInRelationMode ? "relation" : "normal"}/>)}