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 some issues
Adorian Doran0a3d233d0a72
diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsxindex beace88e6c..91120861a2 100644--- a/apps/client/src/widgets/collections/board/column.tsx+++ b/apps/client/src/widgets/collections/board/column.tsx@@ -1,5 +1,6 @@import clsx from "clsx";import { Fragment } from "preact";+import { flushSync } from "preact/compat";import {useCallback, useContext, useEffect, useMemo, useRef, useState} from "preact/hooks";@@ -295,9 +296,11 @@ export default function Column({onWheel={handleScroll}>{(columnItems ?? []).map(({ note, branch }, index) => {+ // The card being carried is out of the flow, so the gap stands in its own+ // place too: held still, which a touch does before it moves, that is where it+ // was picked up from.const showIndicatorBefore = dropPosition?.column === column &&- dropPosition.index === index &&- draggedCard?.noteId !== note.noteId;+ dropPosition.index === index;return (<Fragment key={note.noteId}>@@ -351,6 +354,8 @@ function AddNewItem({ column, api, isCreating, setIsCreating, onCreated }: {/** Names the card just made, which the column reveals once the board has drawn it. */onCreated: (noteId: string | undefined) => void}) {+ // What the editor opens with: empty to begin with, then whatever was typed into it and left+ // unsaved, so that reaching for something else and coming back does not cost the title.const [ initialTitle, setInitialTitle ] = useState("");const open = useCallback((title: string) => {@@ -362,7 +367,7 @@ function AddNewItem({ column, api, isCreating, setIsCreating, onCreated }: {if (isCreating) return;if (e.key === "Enter" && !e.ctrlKey) {- open("");+ setIsCreating(true);return;}@@ -373,12 +378,12 @@ function AddNewItem({ column, api, isCreating, setIsCreating, onCreated }: {e.preventDefault();open(e.key);}- }, [ isCreating, open ]);+ }, [ isCreating, open, setIsCreating ]);return (<divclassName={`board-new-item ${isCreating ? "editing" : ""}`}- onClick={() => open("")}+ onClick={() => flushSync(() => setIsCreating(true))}onKeyDown={handleKeyDown}tabIndex={300}>@@ -396,6 +401,7 @@ function AddNewItem({ column, api, isCreating, setIsCreating, onCreated }: {mode="multiline" isNewItemselectOnFocus={false}saveAndContinue+ abandon={setInitialTitle}/>)}</div>diff --git a/apps/client/src/widgets/collections/board/drag_measure.spec.ts b/apps/client/src/widgets/collections/board/drag_measure.spec.tsindex 2643f72047..0053eaad26 100644--- a/apps/client/src/widgets/collections/board/drag_measure.spec.ts+++ b/apps/client/src/widgets/collections/board/drag_measure.spec.ts@@ -70,6 +70,21 @@ function place(element: HTMLElement, box: { left: number, top: number, width: nu}describe("measureBoard", () => {+ it("leaves the copy being carried out of the columns it measures", () => {+ const board = buildBoard();+ const carried = board.querySelector<HTMLElement>(".board-column")?.cloneNode(true);+ if (!(carried instanceof HTMLElement)) throw new Error("expected a column to copy");++ carried.classList.add("board-drag-preview");+ board.appendChild(carried);+ place(carried, { left: 500, top: 0, width: 100, height: 400 });++ // One place per column drawn, and none for the copy: counting it would offer a place one+ // past every place the board has.+ expect(measureBoard(board).columns).toHaveLength(+ board.querySelectorAll(".board-column:not(.board-drag-preview)").length);+ });+it("places the columns past the board's own scrolling", () => {const board = buildBoard();diff --git a/apps/client/src/widgets/collections/board/drag_measure.ts b/apps/client/src/widgets/collections/board/drag_measure.tsindex d35db7c97f..93fb87b37c 100644--- a/apps/client/src/widgets/collections/board/drag_measure.ts+++ b/apps/client/src/widgets/collections/board/drag_measure.ts@@ -19,7 +19,11 @@ export function measureBoard(container: HTMLElement): BoardMeasurement {const columns: ColumnBox[] = [];const areas = new Map<string, HTMLElement>();- for (const element of container.querySelectorAll<HTMLElement>(".board-column")) {+ // The copy being carried is a column too, and measuring again mid-drag would count it as a+ // place to drop into, one past every place the board actually has.+ const drawn = container.querySelectorAll<HTMLElement>(".board-column:not(.board-drag-preview)");++ for (const element of drawn) {const value = element.dataset.column ?? "";const rect = element.getBoundingClientRect();const area = element.querySelector<HTMLElement>(".board-column-content");diff --git a/apps/client/src/widgets/collections/board/index.css b/apps/client/src/widgets/collections/board/index.cssindex 9310787744..8ea0a8d0a6 100644--- a/apps/client/src/widgets/collections/board/index.css+++ b/apps/client/src/widgets/collections/board/index.css@@ -450,6 +450,19 @@ body.mobile .board-view-container .board-add-column {touch-action: none;}+/* Reaching the end of the board or of a column scrolls neither the page nor anything else. */+.board-view-container,+.board-view-container .board-column-content {+ overscroll-behavior: contain;+}++/* And while something is carried, the end does not spring back either: the copy is positioned+ against the window, which an overscroll moves on WebKit, taking the copy with it. */+.board-view-container.board-dragging,+.board-view-container.board-dragging .board-column-content {+ overscroll-behavior: none;+}+/* The gap opening and closing moves content above whatever the browser has anchored to, and itshifts the column to keep that still. The shift moves the card under the pointer, which moves thegap, which shifts the column again: at the foot of a tall column the two chase each other and itdiff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsxindex 60fae8aff2..3e54a9de68 100644--- a/apps/client/src/widgets/collections/board/index.spec.tsx+++ b/apps/client/src/widgets/collections/board/index.spec.tsx@@ -1349,6 +1349,47 @@ describe("Board column rename", () => {expect(cameFrom).not.toHaveBeenCalled();});+ /**+ * The footer editor stays open between cards, so it is left behind with something typed in it+ * often enough that saving on the way out would make cards nobody asked for.+ */+ it("gives up what is typed in the new-item editor when it loses focus", async () => {+ const { container } = await setup();+ // Cleared: the spy outlives the test that first stood it up, and its calls with it.+ const created = vi.spyOn(BoardApi.prototype, "createNewItem")+ .mockResolvedValue(undefined);+ created.mockClear();+ const slot = container.querySelectorAll<HTMLElement>(".board-column")[1]+ .querySelector<HTMLElement>(".board-new-item");++ await act(async () => {+ slot?.click();+ await flush();+ });++ const editor = slot?.querySelector<HTMLTextAreaElement>("textarea");+ if (!editor) throw new Error("expected the new-item editor to be open");++ editor.value = "Half a thought";+ await act(async () => {+ editor.dispatchEvent(new FocusEvent("blur"));+ editor.dispatchEvent(new FocusEvent("focusout", { bubbles: true }));+ await flush();+ });++ expect(created).not.toHaveBeenCalled();+ expect(slot?.querySelector("textarea")).toBeNull();+ expect(slot?.classList.contains("editing")).toBe(false);++ // What was typed is waiting in the editor when it is opened again.+ await act(async () => {+ slot?.click();+ await flush();+ });+ expect(slot?.querySelector<HTMLTextAreaElement>("textarea")?.value)+ .toBe("Half a thought");+ });+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]diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsxindex 5dab73aea5..17760ec847 100644--- a/apps/client/src/widgets/collections/board/index.tsx+++ b/apps/client/src/widgets/collections/board/index.tsx@@ -435,11 +435,14 @@ export default function BoardView({ note: parentNote, noteIds, viewConfig, saveC});// A column opened to take the card moves every column after it, which the measurement predates.+ // Only for a card: a carried column is measured among the columns as they stood when it was+ // picked up, which is the list the place it would take is counted against, and measuring again+ // with it out of the flow would count one place fewer than the board has.useLayoutEffect(() => {- if (isDraggingItem) {+ if (isDraggingItem && !draggedColumn) {remeasure();}- }, [ isDraggingItem, remeasure, activeColumn, shownColumns ]);+ }, [ isDraggingItem, draggedColumn, remeasure, activeColumn, shownColumns ]);// Only the board's own background, so a press on a column, a card or the button that adds one// is left to whatever it belongs to. Suppressed while a card is carried: the gesture owns the@@ -715,7 +718,7 @@ function AddNewColumn({ api, isInRelationMode }: { api: BoardApi, isInRelationMoexport function TitleEditor({currentValue, placeholder, save, dismiss, mode, isNewItem, selectOnFocus = true,- saveAndContinue = false, returnFocusTo+ saveAndContinue = false, returnFocusTo, abandon}: {currentValue?: string;placeholder?: string;@@ -725,9 +728,14 @@ export function TitleEditor({mode?: "normal" | "multiline" | "relation";/*** Whether Enter saves and clears the editor rather than closing it, so that a run of cards can- * be typed one after another. Escape and clicking away still close it.+ * be typed one after another. Enter is then the only thing that saves: Escape and losing focus+ * both give up what was typed and close the editor, since an editor that stays open between+ * cards is left behind often enough that saving on the way out would make cards nobody asked+ * for.*/saveAndContinue?: boolean;+ /** What was typed and not saved, handed over so that reopening the editor can carry it back. */+ abandon?: (typed: string) => void;/*** Where focus goes when the editor closes, instead of back to whatever held it before. A card* whose editor was opened for it rather than by it names itself, so that closing does not light@@ -746,7 +754,9 @@ export function TitleEditor({const dismissOnNextRefreshRef = useRef(false);const shouldDismiss = useRef(false);- useEffect(() => {+ // Laid out rather than deferred: with the open drawn synchronously, this puts focus on the+ // editor inside the press that asked for it, which is what opens a phone's keyboard.+ useLayoutEffect(() => {focusElRef.current = document.activeElement !== document.body ? document.activeElement : null;inputRef.current?.focus();@@ -798,6 +808,12 @@ export function TitleEditor({};const onBlur = (newValue: string) => {+ if (saveAndContinue) {+ abandon?.(newValue);+ dismiss();+ return;+ }+if (!shouldDismiss.current && newValue.trim() && (newValue !== currentValue || isNewItem)) {commit(newValue);dismissOnNextRefreshRef.current = true;