Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: allow creating a card at the top of the column, while using the new card button

Adorian Doran44a996ece7b8
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 205c89f22d..6273a677a0 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -3398,6 +3398,9 @@
"add-new-column": "Add new column",
"create-new-note": "Create note",
"create-new-column": "Create column",
+ "create-hold-hint": "Right click or hold for more options.",
+ "create-at-top": "Create at top",
+ "create-at-bottom": "Create at bottom",
"add-column-before": "Before",
"add-column-after": "After",
"new-column": "New column",
diff --git a/apps/client/src/widgets/collections/board/api.spec.ts b/apps/client/src/widgets/collections/board/api.spec.ts
index dd2478f250..9ab3bb654c 100644
--- a/apps/client/src/widgets/collections/board/api.spec.ts
+++ b/apps/client/src/widgets/collections/board/api.spec.ts
@@ -696,6 +696,30 @@ describe("BoardApi card operations", () => {
expect(logged).toHaveBeenCalled();
});
+ /**
+ * A column is drawn in the order its notes stand in, so the head of one is the card before its
+ * first, not the first child of the board: the columns share one list between them.
+ */
+ it("makes a card at the head of a column against that column's first card", async () => {
+ const { api, items } = createBoardWithCards();
+ vi.mocked(note_create.createNote).mockResolvedValue({
+ note: buildNote({ title: "Created" }), branch: { branchId: "createdBranch" }
+ } as never);
+
+ await api.createNewItem("Done", "First of all", "top");
+ expect(note_create.createNote).toHaveBeenCalledWith(
+ expect.any(String),
+ expect.objectContaining({
+ target: "before", targetBranchId: items[0].branch.branchId
+ }));
+
+ // An empty column has nothing to be placed against, and the card is simply made.
+ await api.createNewItem("Nowhere", "Only one", "top");
+ expect(note_create.createNote).toHaveBeenLastCalledWith(
+ expect.any(String),
+ expect.not.objectContaining({ target: "before" }));
+ });
+
it("hands the editing state straight through to the board", () => {
const { api, editing } = createBoardWithCards();
diff --git a/apps/client/src/widgets/collections/board/api.ts b/apps/client/src/widgets/collections/board/api.ts
index 27214da9ec..62856f7d08 100644
--- a/apps/client/src/widgets/collections/board/api.ts
+++ b/apps/client/src/widgets/collections/board/api.ts
@@ -20,6 +20,9 @@ import {
} from "./columns";
import { ColumnMap } from "./data";
+/** Which end of a column a new card is made at. */
+export type CardPlacement = "top" | "bottom";
+
/** One write's claim on a column, held until that write lands or is taken back. */
interface ColumnClaim {
/**
@@ -160,13 +163,25 @@ export default class BoardApi {
this.statusAttribute = statusAttribute.replace(/^[~#]/, "");
}
- async createNewItem(column: string, title: string) {
+ /**
+ * Creates a card at one end of a column.
+ *
+ * Cards are drawn in the order the board's children stand in, so a card created under the
+ * board lands at the bottom. The top is created before the column's own first card: all
+ * columns share one list of children, so the board's first child is not this column's.
+ */
+ async createNewItem(column: string, title: string, placement: CardPlacement = "bottom") {
+ const first = placement === "top"
+ ? this.byColumn?.get(column)?.[0]?.branch.branchId
+ : undefined;
+
try {
const { note } = await note_create.createNote(this.parentNote.noteId, {
activate: false,
title,
isProtected: this.parentNote.isProtected,
- attributes: this.groupingFor(column)
+ attributes: this.groupingFor(column),
+ ...(first ? { target: "before", targetBranchId: first } : {})
});
return note?.noteId;
diff --git a/apps/client/src/widgets/collections/board/card.tsx b/apps/client/src/widgets/collections/board/card.tsx
index d51075a727..876b7a6e2c 100644
--- a/apps/client/src/widgets/collections/board/card.tsx
+++ b/apps/client/src/widgets/collections/board/card.tsx
@@ -121,9 +121,9 @@ function Card({
setTitle(note.title);
}, [ note ]);
- // A new card can be below the fold on a full column. One at the end takes the column to its
- // end, so it lands clear of the fade the scrolling body draws over its bottom edge; one
- // inserted between others is only brought into view.
+ // A new card can be out of sight on a full column. One at either end takes the column to that
+ // end, so it lands clear of the fade the scrolling body draws over its edges; one inserted
+ // between others is only brought into view.
useLayoutEffect(() => {
if (!isNew) {
return;
@@ -136,10 +136,12 @@ function Card({
}
const bring = () => {
- if (card.nextElementSibling) {
- card.scrollIntoView?.({ block: "nearest" });
- } else {
+ if (!card.nextElementSibling) {
content.scrollTop = content.scrollHeight;
+ } else if (!card.previousElementSibling) {
+ content.scrollTop = 0;
+ } else {
+ card.scrollIntoView?.({ block: "nearest" });
}
};
diff --git a/apps/client/src/widgets/collections/board/column.tsx b/apps/client/src/widgets/collections/board/column.tsx
index 352a51ae2e..e72fe3000b 100644
--- a/apps/client/src/widgets/collections/board/column.tsx
+++ b/apps/client/src/widgets/collections/board/column.tsx
@@ -450,9 +450,9 @@ function AddNewItem({ column, api, isCreating, setIsCreating, onCreated, onCreat
<TitleEditor
currentValue={initialTitle}
placeholder={t("board_view.new-item-placeholder")}
- save={async (title) => {
+ save={async (title, placement) => {
onCreating();
- onCreated(await api.createNewItem(column, title));
+ onCreated(await api.createNewItem(column, title, placement));
}}
dismiss={() => setIsCreating(false)}
mode="multiline" isNewItem
@@ -464,6 +464,7 @@ function AddNewItem({ column, api, isCreating, setIsCreating, onCreated, onCreat
onClick: addExistingItem
}}
submitTitle={t("board_view.create-new-note")}
+ canPlace
/>
)}
</div>
diff --git a/apps/client/src/widgets/collections/board/context_menu.ts b/apps/client/src/widgets/collections/board/context_menu.ts
index a2d402f771..581ba38222 100644
--- a/apps/client/src/widgets/collections/board/context_menu.ts
+++ b/apps/client/src/widgets/collections/board/context_menu.ts
@@ -11,7 +11,7 @@ import { getArchiveMenuItem } from "../../../menus/context_menu_utils";
import { t } from "../../../services/i18n";
import { escapeHtml } from "../../../services/utils";
import ColorPicker from "../../react/ColorPicker";
-import Api from "./api";
+import Api, { CardPlacement } from "./api";
import { INBOX_COLUMN } from "./columns";
/** What the column menu is opened for: the column itself, and what it can be asked to do. */
@@ -152,6 +152,31 @@ export function openColumnContextMenu(api: Api, event: ContextMenuEvent, column:
});
}
+/** Offers both ends of a column for the card the button below it is about to create. */
+export function openCreateCardMenu(
+ x: number, y: number, create: (placement: CardPlacement) => void
+) {
+ contextMenu.show({
+ x,
+ y,
+ items: [
+ {
+ title: t("board_view.create-at-top"),
+ uiIcon: "bx bx-vertical-top",
+ shortcut: "Shift+Enter",
+ handler: () => create("top")
+ },
+ {
+ title: t("board_view.create-at-bottom"),
+ uiIcon: "bx bx-vertical-bottom",
+ shortcut: "Enter",
+ handler: () => create("bottom")
+ }
+ ],
+ selectMenuItemHandler() {}
+ });
+}
+
/**
* Where a column can be sent, each place named by what the column would come to stand after.
*
diff --git a/apps/client/src/widgets/collections/board/index.css b/apps/client/src/widgets/collections/board/index.css
index d74ec5661c..e244328361 100644
--- a/apps/client/src/widgets/collections/board/index.css
+++ b/apps/client/src/widgets/collections/board/index.css
@@ -534,6 +534,18 @@ body.mobile .board-view-container .board-add-column {
color: var(--muted-text-color);
}
+/* The tooltip is drawn at the end of the page, so it is named rather than nested in the board. */
+.tooltip.title-editor-submit-tooltip {
+ .action {
+ display: block;
+ padding-bottom: 4px;
+ }
+
+ .hint {
+ opacity: 0.75;
+ }
+}
+
/* Room for the button, so a long title runs up to it rather than under it. Stated after the rule
above, whose shorthand padding would otherwise take this back. */
.board-view-container .title-editor-with-submit textarea.form-control,
diff --git a/apps/client/src/widgets/collections/board/index.spec.tsx b/apps/client/src/widgets/collections/board/index.spec.tsx
index 48b98d36ad..6127f8c673 100644
--- a/apps/client/src/widgets/collections/board/index.spec.tsx
+++ b/apps/client/src/widgets/collections/board/index.spec.tsx
@@ -133,6 +133,7 @@ describe("Collapsed board columns", () => {
let container: HTMLElement | undefined;
afterEach(() => {
+ vi.useRealTimers();
saved.length = 0;
if (container) {
render(null, container);
@@ -1228,7 +1229,7 @@ describe("Board column rename", () => {
await flush();
});
- expect(created).toHaveBeenCalledWith("Doing", "First");
+ expect(created).toHaveBeenCalledWith("Doing", "First", "bottom");
expect(slot?.querySelector("textarea")).toBe(editor);
expect(editor.value).toBe("");
expect(document.activeElement).toBe(editor);
@@ -1241,7 +1242,7 @@ describe("Board column rename", () => {
});
expect(created).toHaveBeenCalledTimes(2);
- expect(created).toHaveBeenLastCalledWith("Doing", "Second");
+ expect(created).toHaveBeenLastCalledWith("Doing", "Second", "bottom");
});
/**
@@ -1315,8 +1316,9 @@ describe("Board column rename", () => {
it("reveals a card inserted next to another one, without leaving its place", async () => {
const { note, container } = await setup();
- // A second card in the same column, so the one standing in for the insert has a card after
- // it and the column is brought to it rather than run to its end.
+ // Two more cards in the same column, so the one standing in for the insert has a card on
+ // either side and the column is brought to it rather than run to either end.
+ addCard(note, "Doing");
addCard(note, "Doing");
await act(async () => {
render(
@@ -1334,12 +1336,14 @@ describe("Board column rename", () => {
await act(async () => { await flush(); });
const column = container.querySelectorAll<HTMLElement>(".board-column")[1];
- const [ first, second ] = column.querySelectorAll<HTMLElement>(".board-note");
- const noteId = first?.getAttribute("data-note-id");
- if (!first || !second || !noteId) throw new Error("expected a column of two cards");
+ const [ first, middle, last ] = column.querySelectorAll<HTMLElement>(".board-note");
+ const noteId = middle?.getAttribute("data-note-id");
+ if (!first || !middle || !last || !noteId) {
+ throw new Error("expected a column of three cards");
+ }
const scrolled = vi.fn();
- Object.defineProperty(first, "scrollIntoView",
+ Object.defineProperty(middle, "scrollIntoView",
{ value: scrolled, configurable: true, writable: true });
const content = column.querySelector<HTMLElement>(".board-column-content");
Object.defineProperty(content, "scrollHeight",
@@ -1348,11 +1352,11 @@ describe("Board column rename", () => {
.mockResolvedValue({ noteId } as never);
await act(async () => {
- second.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
+ last.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
await flush();
});
- expect(first.classList.contains("appearing")).toBe(true);
+ expect(middle.classList.contains("appearing")).toBe(true);
expect(scrolled).toHaveBeenCalled();
expect(content?.scrollTop).toBe(0);
});
@@ -1537,12 +1541,142 @@ describe("Board column rename", () => {
await flush();
});
- expect(created).toHaveBeenCalledWith("Doing", "From the button");
+ expect(created).toHaveBeenCalledWith("Doing", "From the button", "bottom");
expect(editor.value).toBe("");
expect(slot?.querySelector("textarea")).toBe(editor);
expect(document.activeElement).toBe(editor);
});
+ /**
+ * Cards are made at the end of a column, and at its head for the reader adding what comes next
+ * rather than what comes last. Both ends are reached from the field: Enter for the end, and
+ * Shift+Enter or the button's own menu for the head.
+ */
+ it("makes a card at the head of the column on Shift+Enter", async () => {
+ const { container } = await setup();
+ 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");
+
+ await type(editor, "On top");
+ await act(async () => {
+ editor.dispatchEvent(new KeyboardEvent("keydown",
+ { key: "Enter", shiftKey: true, bubbles: true }));
+ await flush();
+ });
+
+ expect(created).toHaveBeenCalledWith("Doing", "On top", "top");
+ // The field is emptied for the next one, as it is for a card made at the end.
+ expect(editor.value).toBe("");
+ });
+
+ it("offers both ends of the column from the button's own menu", async () => {
+ const { container } = await setup();
+ const created = vi.spyOn(BoardApi.prototype, "createNewItem")
+ .mockResolvedValue(undefined);
+ created.mockClear();
+ const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
+ 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");
+ const button = slot?.querySelector<HTMLElement>(".title-editor-submit");
+ if (!editor || !button) throw new Error("expected the editor and its button");
+
+ // Nothing typed: there is nowhere to put a card that is not being made.
+ button.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
+ expect(show).not.toHaveBeenCalled();
+
+ await type(editor, "Either end");
+ button.dispatchEvent(new MouseEvent("contextmenu", { bubbles: true }));
+
+ const items = show.mock.calls.at(-1)?.[0].items ?? [];
+ const entries = items.flatMap(item => item && "title" in item && "handler" in item
+ ? [ { title: item.title, shortcut: "shortcut" in item ? item.shortcut : undefined,
+ handler: item.handler } ]
+ : []);
+ expect(entries.map(entry => [ entry.title, entry.shortcut ])).toEqual([
+ [ "board_view.create-at-top", "Shift+Enter" ],
+ [ "board_view.create-at-bottom", "Enter" ]
+ ]);
+
+ await act(async () => {
+ entries[0]?.handler?.({} as never, {} as never);
+ await flush();
+ });
+ expect(created).toHaveBeenCalledWith("Doing", "Either end", "top");
+ expect(editor.value).toBe("");
+
+ show.mockRestore();
+ });
+
+ /** A finger has no second button, so it holds the create button to reach the same menu. */
+ it("opens the placement menu on a hold, and gives it up for a scroll", async () => {
+ const { container } = await setup();
+ const show = vi.spyOn(contextMenu, "show").mockImplementation(async () => {});
+ 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");
+ const button = slot?.querySelector<HTMLElement>(".title-editor-submit");
+ if (!editor || !button) throw new Error("expected the editor and its button");
+ await type(editor, "Held");
+
+ // Only now: the board is drawn through timers of its own, which would never come round.
+ vi.useFakeTimers();
+
+ const press = (type: string, at: number, pointerType = "touch") =>
+ button.dispatchEvent(new PointerEvent(type, {
+ bubbles: true, pointerType, clientX: at, clientY: at
+ }));
+
+ // A finger that walks away is scrolling the board, and the menu is not what it asked for.
+ press("pointerdown", 0);
+ press("pointermove", 40);
+ vi.advanceTimersByTime(1000);
+ expect(show).not.toHaveBeenCalled();
+
+ press("pointerdown", 0);
+ vi.advanceTimersByTime(1000);
+ expect(show).toHaveBeenCalled();
+
+ // The press ends in a click of its own, which must not reach the page and close the menu.
+ const reached = vi.fn();
+ document.addEventListener("click", reached);
+ button.dispatchEvent(new MouseEvent("click", { bubbles: true }));
+ document.removeEventListener("click", reached);
+ expect(reached).not.toHaveBeenCalled();
+
+ // A mouse has its own way there, so holding it down offers nothing.
+ show.mockClear();
+ press("pointerdown", 0, "mouse");
+ vi.advanceTimersByTime(1000);
+ expect(show).not.toHaveBeenCalled();
+
+ vi.useRealTimers();
+ show.mockRestore();
+ });
+
/**
* With nothing typed there is nothing to save, so the button reaches for a note that exists
* already instead, and goes back to making one as soon as the field holds anything.
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index a1877d522f..422c1d1b7c 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -3,6 +3,7 @@ import "./index.css";
import clsx from "clsx";
import { createContext, Fragment, TargetedKeyboardEvent } from "preact";
+import { JSX } from "preact/jsx-runtime";
import { createPortal, RefObject } from "preact/compat";
import {
Dispatch, StateUpdater, useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState
@@ -14,7 +15,7 @@ import type LoadResults from "../../../services/load_results";
import { isIMEComposing } from "../../../services/shortcuts";
import type { ShortcutHintDefinition } from "../../../services/shortcut_hints";
import toast from "../../../services/toast";
-import { isMobile } from "../../../services/utils";
+import { escapeHtml, isMobile } from "../../../services/utils";
import CollectionProperties from "../../note_bars/CollectionProperties";
import FormTextArea from "../../react/FormTextArea";
import FormTextBox from "../../react/FormTextBox";
@@ -30,13 +31,14 @@ import ActionButton from "../../react/ActionButton";
import { useDragPan } from "../../react/drag_pan";
import { FLIP_SETTLE_MS, useFlip } from "../../react/flip";
import { ViewModeProps } from "../interface";
-import Api, { getPendingWrites, PendingColumnWrites, settleColumn } from "./api";
+import Api, { CardPlacement, 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 Column from "./column";
import ColumnLimitDialog from "./column_limit";
+import { openCreateCardMenu } from "./context_menu";
import { applyCardMove, ColumnMap, getBoardData } from "./data";
import { useBoardKeyboard } from "./keyboard";
@@ -153,6 +155,12 @@ export const BoardDragStateContext = createContext<BoardDragState>({
* board handles itself (see `keyboard.ts` and the card and column handlers), none of them
* rebindable, so each is listed literally rather than through a registered action.
*/
+/** How long a finger stays on the create button before it offers where to put the card. */
+const HOLD_TO_PLACE_MS = 500;
+
+/** How far a finger can wander during that and still be holding rather than scrolling. */
+const HOLD_SLACK_PX = 10;
+
const BOARD_HINTS: ShortcutHintDefinition = [
{
titleKey: "board_view.hints.navigation",
@@ -799,11 +807,11 @@ function AddNewColumn({ api, isInRelationMode, columnCount, onCreated }: {
export function TitleEditor({
currentValue, placeholder, save, dismiss, mode, isNewItem, selectOnFocus = true,
- saveAndContinue = false, returnFocusTo, abandon, whenEmpty, submitTitle
+ saveAndContinue = false, returnFocusTo, abandon, whenEmpty, submitTitle, canPlace
}: {
currentValue?: string;
placeholder?: string;
- save: (newValue: string) => void | Promise<void>;
+ save: (newValue: string, placement?: CardPlacement) => void | Promise<void>;
dismiss: () => void;
isNewItem?: boolean;
mode?: "normal" | "multiline" | "relation";
@@ -825,6 +833,11 @@ export function TitleEditor({
whenEmpty?: { title: string, onClick?: () => void };
/** What the button beside the field makes, for the reader hovering over it. */
submitTitle?: string;
+ /**
+ * Whether the button also offers which end of the column to make it at: a menu on a right
+ * click or a hold, and Shift+Enter for the top. Only for a `save` that reads the placement.
+ */
+ canPlace?: boolean;
/**
* 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
@@ -844,6 +857,13 @@ export function TitleEditor({
const focusElRef = useRef<Element>(null);
const dismissOnNextRefreshRef = useRef(false);
const shouldDismiss = useRef(false);
+ const held = useRef<number>();
+ /** Where on the screen the finger went down, against which a scroll is told from a hold. */
+ const heldFrom = useRef<{ x: number, y: number }>();
+ /** Whether the menu was opened by a hold, whose press ends in a click the menu must survive. */
+ const openedByHold = useRef(false);
+
+ useEffect(() => () => window.clearTimeout(held.current), []);
// 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.
@@ -876,7 +896,7 @@ export function TitleEditor({
if (e.key === "Enter" && saveAndContinue) {
e.preventDefault();
e.stopPropagation();
- submit();
+ submit(canPlace && e.shiftKey ? "top" : "bottom");
return;
}
@@ -902,18 +922,78 @@ export function TitleEditor({
}
};
- /** Saves what is in the editor and empties it, leaving it open for whatever comes next. */
- function submit() {
+ /**
+ * Saves what is in the editor and empties it, leaving it open for whatever comes next.
+ *
+ * @param placement which end of the column to save at, for an editor that offers both.
+ * @param typed what to save, for a menu that read the field when it opened rather than now.
+ */
+ function submit(placement?: CardPlacement, typed?: string) {
const input = inputRef.current;
- if (input?.value.trim()) {
- commit(input.value);
- input.value = "";
+ const value = typed ?? input?.value ?? "";
+ if (value.trim()) {
+ commit(value, placement);
+ if (input) {
+ input.value = "";
+ }
}
input?.focus();
setIsEmpty(true);
}
+ /** Offers both ends of the column, saving what the field held when the menu was opened. */
+ function openPlacementMenu(e: JSX.TargetedMouseEvent<HTMLElement>) {
+ e.preventDefault();
+ e.stopPropagation();
+ cancelHold();
+
+ const typed = inputRef.current?.value ?? "";
+ openCreateCardMenu(e.pageX, e.pageY, (placement) => submit(placement, typed));
+ }
+
+ /** Opens the same menu for a finger, which has no second button to open it with. */
+ function holdToPlace(e: JSX.TargetedPointerEvent<HTMLElement>) {
+ if (e.pointerType === "mouse") {
+ return;
+ }
+
+ const { pageX, pageY, clientX, clientY } = e;
+ cancelHold();
+ heldFrom.current = { x: clientX, y: clientY };
+ held.current = window.setTimeout(() => {
+ openedByHold.current = true;
+ const typed = inputRef.current?.value ?? "";
+ openCreateCardMenu(pageX, pageY, (placement) => submit(placement, typed));
+ }, HOLD_TO_PLACE_MS);
+ }
+
+ function cancelHold() {
+ window.clearTimeout(held.current);
+ heldFrom.current = undefined;
+ }
+
+ /** Gives up on a hold the finger has walked away from, which is a scroll and not a press. */
+ function holdMoved(e: JSX.TargetedPointerEvent<HTMLElement>) {
+ const from = heldFrom.current;
+ if (from && Math.hypot(e.clientX - from.x, e.clientY - from.y) > HOLD_SLACK_PX) {
+ cancelHold();
+ }
+ }
+
+ function pressed(e: JSX.TargetedMouseEvent<HTMLElement>) {
+ cancelHold();
+
+ // A hold ends in a click, which would reach the page and close the menu it just opened.
+ if (openedByHold.current) {
+ openedByHold.current = false;
+ e.stopPropagation();
+ return;
+ }
+
+ submit("bottom");
+ }
+
const onBlur = (newValue: string) => {
if (saveAndContinue) {
abandon?.(newValue);
@@ -932,8 +1012,8 @@ export function TitleEditor({
// The editor is closing either way, and what a save writes has already been put back by
// whatever could not write it; all that is left is to say so rather than to reject unhandled,
// which is what a save reaching nobody used to do.
- function commit(newValue: string) {
- Promise.resolve(save(newValue)).catch((e) => {
+ function commit(newValue: string, placement?: CardPlacement) {
+ Promise.resolve(save(newValue, placement)).catch((e) => {
console.error("Failed to save what the board editor was given:", e);
toast.showError(t("board_view.save-error"));
});
@@ -958,12 +1038,19 @@ export function TitleEditor({
return field;
}
+ // A placement applies only to the button that creates. With nothing typed, the button
+ // stands for whatever the caller offers instead.
+ const offersPlacement = !!canPlace && !isEmpty;
+ const madeBy = submitTitle ?? t("board_view.add-new-item");
const offered = isEmpty && whenEmpty
- ? { icon: "bx bx-folder-open", ...whenEmpty }
+ ? { icon: "bx bx-folder-open", title: whenEmpty.title, onClick: whenEmpty.onClick }
: {
icon: "bx bx-plus-circle",
- title: submitTitle ?? t("board_view.add-new-item"),
- onClick: submit
+ title: offersPlacement
+ ? `<span class="action">${escapeHtml(madeBy)}</span>`
+ + `<span class="hint">${escapeHtml(t("board_view.create-hold-hint"))}</span>`
+ : madeBy,
+ onClick: pressed
};
return (
@@ -971,11 +1058,20 @@ export function TitleEditor({
{field}
{/* The press must not take focus out of the field first: losing it is what closes
the editor, and it would be gone before the click arrived. */}
- <span onMouseDown={(e) => e.preventDefault()}>
+ <span
+ onMouseDown={(e) => e.preventDefault()}
+ onPointerDown={offersPlacement ? holdToPlace : undefined}
+ onPointerUp={cancelHold}
+ onPointerMove={holdMoved}
+ onPointerCancel={cancelHold}
+ onContextMenu={offersPlacement ? openPlacementMenu : undefined}
+ >
<ActionButton
className="title-editor-submit"
icon={offered.icon}
text={offered.title}
+ tooltipHtml={offersPlacement}
+ tooltipClass={offersPlacement ? "title-editor-submit-tooltip" : undefined}
onClick={offered.onClick}
/>
</span>