Skip to content

Build your personal knowledge base with Trilium Notes

client/collections/board: make the icon selector focusable when editing cards

Adorian Doran406924eb3bcc
diff --git a/apps/client/src/widgets/collections/board/card.spec.tsx b/apps/client/src/widgets/collections/board/card.spec.tsx
index 37caf69dcc..c41ce2acbf 100644
--- a/apps/client/src/widgets/collections/board/card.spec.tsx
+++ b/apps/client/src/widgets/collections/board/card.spec.tsx
@@ -301,6 +301,58 @@ describe("Board card", () => {
expect(openInPopup).not.toHaveBeenCalled();
});
+ it("hands the icon picker focus on Shift+Tab, leaving the editor standing", async () => {
+ const { first } = await renderBoard();
+ const put = vi.spyOn(server, "put").mockResolvedValue(undefined);
+ // Held on to: the editor takes the place of the title the lookup goes by.
+ const element = card(first);
+
+ await act(async () => { press(element, "F2"); });
+ const editor = element.querySelector<HTMLTextAreaElement>("textarea");
+ if (!editor) throw new Error("expected the title editor");
+ const picker = element.querySelector<HTMLButtonElement>(".title-editor-icon button");
+ if (!picker) throw new Error("expected the icon picker");
+
+ await act(async () => {
+ editor.focus();
+ press(editor, "Tab", { shiftKey: true });
+ });
+
+ expect(document.activeElement).toBe(picker);
+ // The blur the move costs neither closes the editor nor writes the title.
+ expect(element.querySelector("textarea")).toBeTruthy();
+ expect(put).not.toHaveBeenCalled();
+
+ // Space opens the picker, and the board must not read it as the one that opens the card.
+ const openInPopup = vi.spyOn(appContext, "triggerCommand").mockReturnValue(undefined);
+ await act(async () => { press(picker, " "); });
+ expect(openInPopup).not.toHaveBeenCalled();
+ expect(element.querySelector("textarea")).toBeTruthy();
+ });
+
+ it("ends the edit once the picker hands focus outside the field", async () => {
+ const { first } = await renderBoard();
+ // Held on to: the editor takes the place of the title the lookup goes by.
+ const element = card(first);
+
+ await act(async () => { press(element, "F2"); });
+ const editor = element.querySelector<HTMLTextAreaElement>("textarea");
+ if (!editor) throw new Error("expected the title editor");
+
+ await act(async () => {
+ editor.focus();
+ press(editor, "Tab", { shiftKey: true });
+ });
+ expect(element.querySelector("textarea")).toBeTruthy();
+
+ const picker = element.querySelector<HTMLButtonElement>(".title-editor-icon button");
+ await act(async () => {
+ picker?.dispatchEvent(new FocusEvent("focusout", { bubbles: true }));
+ });
+
+ expect(element.querySelector("textarea")).toBeFalsy();
+ });
+
describe("picking several cards out", () => {
it("marks a card on Ctrl and click, and lets go of it on the next one", async () => {
const { first, second } = await renderBoard();
diff --git a/apps/client/src/widgets/collections/board/index.tsx b/apps/client/src/widgets/collections/board/index.tsx
index ea76a13a3e..4769183ba3 100644
--- a/apps/client/src/widgets/collections/board/index.tsx
+++ b/apps/client/src/widgets/collections/board/index.tsx
@@ -1875,6 +1875,16 @@ export function TitleEditor({
* that would have ended the edit being spent.
*/
const isHoldingOpen = useRef(false);
+ /** The box holding the field and the picker, against which focus is told inside from outside. */
+ const fieldRef = useRef<HTMLDivElement>(null);
+ const iconRef = useRef<HTMLSpanElement>(null);
+ /**
+ * Whether the icon picker holds focus, which Shift+Tab hands to it.
+ *
+ * Set before focus moves rather than when the picker receives it: the field blurs first, and
+ * that blur is what would close the editor.
+ */
+ const isIconFocused = useRef(false);
const holdOpen = useMemo<HoldOpen>(() => ({
onOpened: () => { isHoldingOpen.current = true; },
onClosed: () => {
@@ -1920,6 +1930,17 @@ export function TitleEditor({
return;
}
+ if (e.key === "Tab" && e.shiftKey && icon) {
+ const button = iconRef.current?.querySelector("button");
+ if (button) {
+ e.preventDefault();
+ e.stopPropagation();
+ isIconFocused.current = true;
+ button.focus();
+ return;
+ }
+ }
+
if (e.key === "Enter" && saveAndContinue) {
e.preventDefault();
e.stopPropagation();
@@ -2038,7 +2059,7 @@ export function TitleEditor({
}
const onBlur = (newValue: string) => {
- if (isHoldingOpen.current) {
+ if (isHoldingOpen.current || isIconFocused.current) {
return;
}
@@ -2061,6 +2082,41 @@ export function TitleEditor({
}
};
+ /**
+ * Ends the edit once the picker hands focus on to something outside the field's own box.
+ * Focus going back to the field, or into the picker's menu, leaves the editor where it is.
+ */
+ function iconFocusOut(e: JSX.TargetedFocusEvent<HTMLSpanElement>) {
+ isIconFocused.current = false;
+
+ const next = e.relatedTarget;
+ if (isHoldingOpen.current || (next instanceof Node && fieldRef.current?.contains(next))) {
+ return;
+ }
+
+ onBlur(inputRef.current?.value ?? "");
+ }
+
+ /** Leaves the editor from the picker, which Escape does from the field itself. */
+ function iconKeyDown(e: JSX.TargetedKeyboardEvent<HTMLSpanElement>) {
+ if (e.key !== "Escape" || isHoldingOpen.current) {
+ return;
+ }
+
+ e.preventDefault();
+ e.stopPropagation();
+ shouldDismiss.current = true;
+
+ const target = returnFocusTo?.current ?? focusElRef.current;
+ if (target instanceof HTMLElement) {
+ target.focus();
+ return;
+ }
+
+ isIconFocused.current = false;
+ dismiss();
+ }
+
/**
* Saves what was typed and reports whether `save` accepted it.
*
@@ -2119,7 +2175,7 @@ export function TitleEditor({
};
return (
- <div className={clsx("title-editor-field", {
+ <div ref={fieldRef} className={clsx("title-editor-field", {
"with-submit": saveAndContinue,
"with-footer": !!footer
})}>
@@ -2127,7 +2183,12 @@ export function TitleEditor({
blur arrives before the picker reports itself open, and losing focus is what
closes the editor. */}
{icon && (
- <span onMouseDown={(e) => e.preventDefault()}>
+ <span
+ ref={iconRef}
+ onMouseDown={(e) => e.preventDefault()}
+ onFocusOut={iconFocusOut}
+ onKeyDown={iconKeyDown}
+ >
<IconPickerButton
className="title-editor-icon"
icon={icon.current}
diff --git a/apps/client/src/widgets/collections/board/keyboard.ts b/apps/client/src/widgets/collections/board/keyboard.ts
index 0ccdb7bb88..5edb8de935 100644
--- a/apps/client/src/widgets/collections/board/keyboard.ts
+++ b/apps/client/src/widgets/collections/board/keyboard.ts
@@ -199,8 +199,9 @@ export function useBoardKeyboard({
// direction, the harder form, or the further reach of something the board already does.
if (e.shiftKey && !shiftIsOurs(e)) return;
- // An editor is open on the thing that is focused, and every key belongs to it.
- if (target?.closest("input, textarea")) return;
+ // An editor is open on the thing that is focused, and every key belongs to it — the icon
+ // picker and the buttons beside the field included, which Shift+Tab reaches.
+ if (target?.closest("input, textarea, .title-editor-field")) return;
// Alt on its own is how a reader goes back and forward through notes, which is expected to
// work over a board as over anything else.
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Kanban Board.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Kanban Board.html
index 5125e78f46..7405768be0 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Kanban Board.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Kanban Board.html
@@ -428,9 +428,10 @@ class="admonition tip">
<li><strong>Using the context menu:</strong> Open the card's context menu and
select&nbsp;<span class="tn-icon bx bx-rename"></span> <strong>Edit title &amp; icon</strong>.</li>
</ul>
-<p>You can then enter a new name and select another icon. Click or tap anywhere
- outside the card, or press <kbd>Enter</kbd>, to apply the change. Press <kbd>Esc</kbd> to
- discard the changes.</p>
+<p>You can then enter a new name and select another icon. To move the keyboard
+ focus from the title field to the icon selector, press <kbd>Shift</kbd> + <kbd>Tab</kbd>.
+ Click or tap anywhere outside the card, or press <kbd>Enter</kbd>, to apply
+ the change. Press <kbd>Esc</kbd> to discard the changes.</p>
<p>Each card can be tinted with a different color. Open the card's context
menu and select the desired color. If no color is assigned, the card inherits
the column color.</p>
diff --git a/docs/User Guide/User Guide/Collections/Kanban Board.md b/docs/User Guide/User Guide/Collections/Kanban Board.md
index 30e5e5d294..c673e7c364 100644
--- a/docs/User Guide/User Guide/Collections/Kanban Board.md
+++ b/docs/User Guide/User Guide/Collections/Kanban Board.md
@@ -268,7 +268,7 @@ To change the title or icon of a card, use one of the following methods:
* **Using the keyboard:** Select a card, and then press <kbd>F2</kbd>.
* **Using the context menu:** Open the card's context menu and select <span class="tn-icon bx bx-rename"></span> **Edit title & icon**.
-You can then enter a new name and select another icon. Click or tap anywhere outside the card, or press <kbd>Enter</kbd>, to apply the change. Press <kbd>Esc</kbd> to discard the changes.
+You can then enter a new name and select another icon. To move the keyboard focus from the title field to the icon selector, press <kbd>Shift</kbd> + <kbd>Tab</kbd>. Click or tap anywhere outside the card, or press <kbd>Enter</kbd>, to apply the change. Press <kbd>Esc</kbd> to discard the changes.
Each card can be tinted with a different color. Open the card's context menu and select the desired color. If no color is assigned, the card inherits the column color.