Skip to content

Build your personal knowledge base with Trilium Notes

fix(attributes): copy, paste and escape from the toolbar's focus

Elian Doran5450d9287b24
diff --git a/apps/client/src/widgets/react/FormCheckbox.tsx b/apps/client/src/widgets/react/FormCheckbox.tsx
index 4df3f03a48..99f0005411 100644
--- a/apps/client/src/widgets/react/FormCheckbox.tsx
+++ b/apps/client/src/widgets/react/FormCheckbox.tsx
@@ -19,11 +19,13 @@ interface FormCheckboxProps {
*/
indeterminate?: boolean;
disabled?: boolean;
+ /** `-1` keeps the box out of the tab order, for one that a pointer reaches through its row. */
+ tabIndex?: number;
onChange(newValue: boolean): void;
containerStyle?: CSSProperties;
}
-export default function FormCheckbox({ name, disabled, label, currentValue, indeterminate, onChange, hint, containerStyle }: FormCheckboxProps) {
+export default function FormCheckbox({ name, disabled, label, currentValue, indeterminate, tabIndex, onChange, hint, containerStyle }: FormCheckboxProps) {
const labelRef = useRef<HTMLLabelElement>(null);
const inputRef = useRef<HTMLInputElement>(null);
const id = useUniqueName(name);
@@ -73,6 +75,7 @@ export default function FormCheckbox({ name, disabled, label, currentValue, inde
checked={currentValue || false}
value="1"
disabled={disabled}
+ tabIndex={tabIndex}
onChange={handleChange} />
{label}
</label>
diff --git a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx b/apps/client/src/widgets/sidebar/AttributeList.spec.tsx
index 5ec039ec51..1e9aa894d0 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx
+++ b/apps/client/src/widgets/sidebar/AttributeList.spec.tsx
@@ -1136,6 +1136,10 @@ describe("AttributeList", () => {
// Every row offers one — the stylesheet is what shows it only over the row being pointed at
// — but the rows Trilium keeps for itself offer none, being carried nowhere.
expect(container.querySelectorAll(".attribute-kind-check")).toHaveLength(7);
+ // The shared `FormCheckbox`, kept out of the tab order: the rows are reached by pointer.
+ const input = container.querySelector<HTMLInputElement>(".attribute-kind-check .form-checkbox input.form-check-input");
+ expect(input).not.toBeNull();
+ expect(input?.tabIndex).toBe(-1);
checkbox(0).click();
expect(selectedNames()).toEqual([ "author" ]);
@@ -1225,6 +1229,25 @@ describe("AttributeList", () => {
expect(box?.indeterminate).toBe(false);
});
+ it("answers the copy, paste and escape keys with the focus in the toolbar, as in the cards", async () => {
+ renderPanel(noteWithAttributes());
+ const box = selectAllBox();
+ expect(box).not.toBeNull();
+
+ // Selecting from the toolbar's checkbox leaves the focus there, outside every card.
+ checkbox(0).click();
+ const copied = copyFrom(box as HTMLElement);
+ expect(copied.defaultPrevented).toBe(true);
+ expect(copied.written["text/plain"]).toBe("#author=Elian");
+
+ act(() => void box?.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true })));
+ expect(selectedNames()).toEqual([]);
+
+ await act(async () => pasteInto(box as HTMLElement, { "text/plain": "#tag=new" }));
+ expect(put).toHaveBeenCalledOnce();
+ expect(namesIn(container)).toContain("tag");
+ });
+
it("lets a note that cannot be edited be copied from, but not deleted from or pasted onto", async () => {
const note = noteWithAttributes();
note.isContentWritable = false;
diff --git a/apps/client/src/widgets/sidebar/AttributeList.tsx b/apps/client/src/widgets/sidebar/AttributeList.tsx
index 540f7683b5..f1929e2f5e 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.tsx
+++ b/apps/client/src/widgets/sidebar/AttributeList.tsx
@@ -3,7 +3,7 @@ import "./AttributeList.css";
import { promotedAttributeDefinitionParser } from "@triliumnext/commons";
import clsx from "clsx";
-import { ComponentChildren, createPortal } from "preact";
+import { ComponentChildren, createPortal, HTMLAttributes } from "preact";
import { useContext, useEffect, useRef, useState } from "preact/hooks";
import FAttribute from "../../entities/fattribute";
@@ -606,10 +606,10 @@ export default function AttributeList() {
}
/**
- * What every card carries so that the copy and paste keys reach the panel rather than the note
- * behind it: a clipboard event goes to whatever holds the focus, so a press anywhere in a card
- * takes it. Presses inside an open editor are left alone — taking the focus off a field would
- * commit the very edit the press is part of.
+ * What every card and the toolbar carry so that the copy and paste keys reach the panel rather
+ * than the note behind it: a clipboard event goes to whatever holds the focus, so a press anywhere
+ * in a card or the toolbar takes it. Presses inside an open editor are left alone — taking the
+ * focus off a field would commit the very edit the press is part of.
*/
const clipboardProps = {
tabIndex: -1,
@@ -765,6 +765,7 @@ export default function AttributeList() {
held={canEdit ? heldAttributes : []}
canAdd={canEdit}
canDelete={canEdit && selectedAttributes().every((candidate) => owned.current.includes(candidate))}
+ keyboardProps={clipboardProps}
onAdd={addAttribute}
onCopy={() => void copyPickedAttributes(selectedAttributes())}
onPaste={() => void applyPaste(heldAttributes)}
@@ -1259,11 +1260,9 @@ function AttributeRow({ attribute, note, active, valueEditor, isSystem, selected
onSelect(e.shiftKey);
}}
>
- {/* The theme's checkbox (`label.tn-checkbox`) without `FormCheckbox`'s label
- text, which the icon slot has no room for. */}
- <label class="tn-checkbox">
- <input type="checkbox" checked={selected} tabIndex={-1} />
- </label>
+ {/* The click handler above selects, since it reads the shift key that
+ `onChange` does not carry. */}
+ <FormCheckbox label="" currentValue={selected ?? false} tabIndex={-1} onChange={() => {}} />
</span>
)}
</span>
@@ -1588,7 +1587,7 @@ function getClipboardValue(attribute: Attribute) {
* Deleting is offered only when every selected row is the note's own. On a note that cannot be edited, a
* lock and "Read-only" take the place of the paste and add buttons.
*/
-function AttributeToolbar({ selectionCount, selectableCount, readOnly, held, canAdd, canDelete, onAdd, onCopy, onPaste, onClearClipboard, onDelete, onSelectAll, onClearSelection }: {
+function AttributeToolbar({ selectionCount, selectableCount, readOnly, held, canAdd, canDelete, keyboardProps, onAdd, onCopy, onPaste, onClearClipboard, onDelete, onSelectAll, onClearSelection }: {
selectionCount: number;
/** How many rows can be selected; the checkbox is left out when there are none. */
selectableCount: number;
@@ -1598,6 +1597,8 @@ function AttributeToolbar({ selectionCount, selectableCount, readOnly, held, can
held: Attribute[];
canAdd: boolean;
canDelete: boolean;
+ /** The cards' copy, paste and Escape handlers, which the focus left in the toolbar must reach too. */
+ keyboardProps: HTMLAttributes<HTMLDivElement>;
onAdd: (attrType: AttributeKind, e: MouseEvent) => void;
onCopy: () => void;
onPaste: () => void;
@@ -1610,7 +1611,7 @@ function AttributeToolbar({ selectionCount, selectableCount, readOnly, held, can
const allSelected = isSelecting && selectionCount >= selectableCount;
return (
- <div class={clsx("attribute-toolbar", isSelecting && "selecting")} role="toolbar">
+ <div class={clsx("attribute-toolbar", isSelecting && "selecting")} role="toolbar" {...keyboardProps}>
{selectableCount > 0 && (
<FormCheckbox
name="attribute-select-all"