Skip to content

Build your personal knowledge base with Trilium Notes

feat(attributes): pick a row out by pressing anywhere on it while picking

Elian Doran5bf3ec7281fa
diff --git a/apps/client/src/widgets/sidebar/AttributeList.css b/apps/client/src/widgets/sidebar/AttributeList.css
index 1422f431a1..1e099fff00 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.css
+++ b/apps/client/src/widgets/sidebar/AttributeList.css
@@ -224,11 +224,17 @@
}
/* A label's value is edited in place, so the preview itself is a click target of its own: a text
- cursor over it says as much, where the row around it — which opens the popup — points. */
-.attribute-list-panel .attribute-value.editable {
+ cursor over it says as much, where the row around it — which opens the popup — points. Not while
+ rows are being picked out: the whole row picks then, its value and its links along with it, so
+ nothing on it should offer what it will not do. */
+.attribute-list-panel:not(.selecting) .attribute-value.editable {
cursor: text;
}
+.attribute-list-panel.selecting .attribute-row :is(a, .attribute-value) {
+ cursor: pointer;
+}
+
/* A flag's press toggles rather than types (see startValueEdit), and the pointer says so. */
.attribute-list-panel .attribute-value.editable.flag {
cursor: pointer;
diff --git a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx b/apps/client/src/widgets/sidebar/AttributeList.spec.tsx
index d21f69bbc7..9e1e92fbe2 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx
+++ b/apps/client/src/widgets/sidebar/AttributeList.spec.tsx
@@ -846,12 +846,40 @@ describe("AttributeList", () => {
pick(3, { shiftKey: true });
expect(selectedNames()).toEqual([ "author", "cssClass", "template", "inheritedLabel" ]);
- // A press with nothing held down is about the one attribute it landed on, and takes over.
+ // While rows are being picked out the whole row picks, so a press with nothing held down
+ // picks too rather than opening the form and letting the selection go.
+ act(() => rows()[1].click());
+ expect(selectedNames()).toEqual([ "author", "template", "inheritedLabel" ]);
+ expect(document.querySelector(".attr-detail")).toBeNull();
+
+ // With none picked out it is the one attribute it landed on again.
+ act(() => barButton("bx bx-x")?.click());
act(() => rows()[1].click());
expect(selectedNames()).toEqual([]);
expect(document.querySelector(".attr-detail")).not.toBeNull();
});
+ it("keeps a press on a picked row from whatever it lands on within it", () => {
+ renderPanel(noteWithAttributes());
+
+ // Nothing picked out: the value is its own way in, which opens the field over it.
+ act(() => rows()[0].querySelector<HTMLElement>(".attribute-value")?.click());
+ expect(container.querySelector(".attribute-value-editor")).not.toBeNull();
+
+ pick(2, { ctrlKey: true });
+ expect(selectedNames()).toEqual([ "template" ]);
+
+ // Picked out, the same press picks the row instead — and is refused besides, so that a
+ // relation's value, which is a link to the note it points at, is not navigated to either.
+ const press = new MouseEvent("click", { bubbles: true, cancelable: true });
+ act(() => void rows()[0].querySelector(".attribute-value")?.dispatchEvent(press));
+
+ expect(press.defaultPrevented).toBe(true);
+ // Read in the order the rows are drawn, whichever order they were picked out in.
+ expect(selectedNames()).toEqual([ "author", "template" ]);
+ expect(container.querySelectorAll(".attribute-value-editor")).toHaveLength(0);
+ });
+
it("copies the picked rows as the text the attributes editor spells them out in", () => {
renderPanel(noteWithAttributes());
diff --git a/apps/client/src/widgets/sidebar/AttributeList.tsx b/apps/client/src/widgets/sidebar/AttributeList.tsx
index e69ae878cd..a256a39daf 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.tsx
+++ b/apps/client/src/widgets/sidebar/AttributeList.tsx
@@ -973,6 +973,8 @@ function AttributeRow({ attribute, note, active, valueEditor, isSystem, selected
const kindIcon = getKindIcon(attribute, attrType);
const kindTooltip = getKindTooltip(attribute, attrType, isSystem);
const rowClass = clsx("attribute-row", active && "active", valueEditor && "editing", selected && "selected");
+ /** Whether the whole row picks, which is what it does once rows are being picked out. */
+ const selectsWholeRow = !!selecting && !!onSelect;
/**
* A phone has no modifier to hold down, so it has the press itself held instead: held long enough,
@@ -1009,20 +1011,62 @@ function AttributeRow({ attribute, note, active, valueEditor, isSystem, selected
};
}, [ onSelect ]);
+ /**
+ * Once rows are being picked out, the whole row is what picks: a checkbox is a small thing to
+ * aim at, and the press that missed it would otherwise open the form and let the selection go.
+ * Taken in the capture phase and kept there, so that nothing the row holds acts on the press
+ * instead — a value that would open its editor, a relation's target that would be navigated to.
+ */
+ function selectFromRow(e: MouseEvent) {
+ // The release of the press that picked the row out, which has had its say already.
+ if (heldDown.current) {
+ heldDown.current = false;
+ e.preventDefault();
+ e.stopPropagation();
+ return;
+ }
+
+ // The checkbox is left its own press: refused, its tick is put back after this has run,
+ // which would leave the box a press behind the selection it is drawn from.
+ if (e.target instanceof Element && e.target.closest(".attribute-kind-check")) {
+ return;
+ }
+
+ e.preventDefault();
+ e.stopPropagation();
+ onSelect?.(e.shiftKey);
+ }
+
+ // A menu item takes no capture handler of its own, so a phone's rows are bound theirs by hand.
+ useEffect(() => {
+ const row = rowRef.current;
+ if (!IS_MOBILE || !row || !selectsWholeRow) return;
+
+ row.addEventListener("click", selectFromRow, true);
+ return () => row.removeEventListener("click", selectFromRow, true);
+ // The handler itself is left out: it is written anew on every render, where what it does
+ // turns only on the two below.
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [ selectsWholeRow, onSelect ]);
+
function open(e: MouseEvent) {
// Keep the container's closing handler from undoing this.
e.stopPropagation();
- // The release of the press that picked the row out, which has had its say already.
+ // Already answered above, by the press that picked the row out or the one that let it go.
+ if (selecting && onSelect) {
+ return;
+ }
+
if (heldDown.current) {
heldDown.current = false;
return;
}
// Held down, the modifiers that pick rows out mean the press is about the selection rather
- // than about the one attribute the popup would show. On a phone, where there are none to
- // hold, a tap means that too once rows are being picked out.
- if (onSelect && (e.ctrlKey || e.metaKey || e.shiftKey || (IS_MOBILE && selecting))) {
+ // than about the one attribute the popup would show — which is how the first row is picked
+ // out, there being no row yet whose whole face picks.
+ if (onSelect && (e.ctrlKey || e.metaKey || e.shiftKey)) {
// Nothing of the press is the browser's here: a shift-press would otherwise take the text
// between the two rows as a selection, over the rows being picked out.
e.preventDefault();
@@ -1114,7 +1158,13 @@ function AttributeRow({ attribute, note, active, valueEditor, isSystem, selected
}
return (
- <li ref={rowRef} class={rowClass} onClick={open} onContextMenu={onShowMenu}>
+ <li
+ ref={rowRef}
+ class={rowClass}
+ onClickCapture={selectsWholeRow ? selectFromRow : undefined}
+ onClick={open}
+ onContextMenu={onShowMenu}
+ >
{/* The icon is what carries the row's one tooltip: everything else about a row is already
written on it, and what the icon (badge and all) stands for is exactly what is not. */}
<span class={clsx("attribute-kind", markerClass)} title={kindTooltip}>