Skip to content

Build your personal knowledge base with Trilium Notes

feat(attributes): repick a relation's target straight from its row

Elian Doran800d71d2e67d
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 8b03deaf7d..3b643ff2b4 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -814,6 +814,7 @@
"delete_confirm": "Delete the attribute \"{{name}}\"?",
"add_definition": "Add a new definition",
"no_target": "No target note",
+ "change_target": "Change the target note",
"inverse_of": "inverse of {{name}}",
"type_label": "Label",
"type_relation": "Relation",
diff --git a/apps/client/src/widgets/sidebar/AttributeList.css b/apps/client/src/widgets/sidebar/AttributeList.css
index 63e0ffeb15..199f90b977 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.css
+++ b/apps/client/src/widgets/sidebar/AttributeList.css
@@ -149,9 +149,14 @@
margin-inline-start: auto;
}
-.attribute-list-panel .attribute-row:hover .attribute-delete-button,
-.attribute-list-panel .attribute-row.active .attribute-delete-button,
-.attribute-list-panel .attribute-row.editing .attribute-delete-button {
+/* Changing a relation's target is a press away too, so its pencil keeps the delete button's habits:
+ at the row's end beside it, and shown only over the row being pointed at. */
+.attribute-list-panel .attribute-edit-button {
+ flex-shrink: 0;
+ visibility: hidden;
+}
+
+.attribute-list-panel .attribute-row:is(:hover, .active, .editing) :is(.attribute-delete-button, .attribute-edit-button) {
visibility: visible;
}
diff --git a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx b/apps/client/src/widgets/sidebar/AttributeList.spec.tsx
index 7ab8457924..47f6c555ac 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx
+++ b/apps/client/src/widgets/sidebar/AttributeList.spec.tsx
@@ -37,8 +37,20 @@ interface AddMenuCall {
items: { kind?: string; handler?: () => void }[];
}
-// A relation's target is picked in an Algolia autocomplete bound to jQuery, which is not loaded here.
-vi.mock("../react/NoteAutocomplete", () => ({ default: () => null }));
+// A relation's target is picked in an Algolia autocomplete bound to jQuery, which is not loaded here:
+// a bare box stands in, reporting whatever is typed into it as the noteId picked.
+vi.mock("../react/NoteAutocomplete", async () => {
+ const { h } = await import("preact");
+
+ return {
+ default: ({ noteId, noteIdChanged }: { noteId?: string; noteIdChanged?: (noteId: string) => void }) =>
+ h("input", {
+ className: "note-autocomplete-stub",
+ value: noteId ?? "",
+ onInput: (e: Event) => noteIdChanged?.((e.target as HTMLInputElement).value)
+ })
+ };
+});
import appContext from "../../components/app_context";
import FAttribute, { FAttributeRow } from "../../entities/fattribute";
@@ -417,6 +429,45 @@ describe("AttributeList", () => {
expect(input?.step).toBe("0.01");
});
+ it("repicks a relation's target from its pencil, the value itself staying the link it is", async () => {
+ buildNote({ id: "tpl2", title: "Other template" });
+ renderPanel(noteWithAttributes());
+
+ // Only the relation row offers the pencil — a label's value is its own way in — and the
+ // relation's value stays a link rather than becoming a click target of the edit.
+ const pencils = container.querySelectorAll<HTMLElement>(".attribute-edit-button");
+ expect(pencils).toHaveLength(1);
+ expect(pencils[0].closest(".attribute-row")?.querySelector(".attribute-value.editable")).toBeNull();
+
+ act(() => pencils[0].click());
+ const stub = container.querySelector<HTMLInputElement>(".attribute-value-editor .note-autocomplete-stub");
+ expect(stub?.value).toBe("tpl");
+
+ act(() => {
+ if (stub) {
+ stub.value = "tpl2";
+ stub.dispatchEvent(new Event("input", { bubbles: true }));
+ }
+ });
+ await act(async () => {
+ stub?.dispatchEvent(new FocusEvent("focusout", { bubbles: true, relatedTarget: document.body }));
+ });
+
+ expect(put).toHaveBeenCalledOnce();
+ const [ , saved ] = put.mock.calls[0] as [ string, { name: string; value: string }[] ];
+ expect(saved.find((attribute) => attribute.name === "template")?.value).toBe("tpl2");
+ });
+
+ it("offers an empty relation's own slot for picking, there being no link in it to follow", () => {
+ renderPanel(buildNote({ id: "bare-rel", title: "Bare", "~depicts": "" }));
+
+ const placeholder = container.querySelector<HTMLElement>(".attribute-value.empty");
+ expect(placeholder?.className).toContain("editable");
+
+ act(() => placeholder?.click());
+ expect(container.querySelector(".attribute-value-editor .note-autocomplete-stub")).not.toBeNull();
+ });
+
it("shows a colour label as the colour itself, and edits it through the picker", () => {
renderPanel(buildNote({ id: "tinted", title: "Tinted", "#color": "#8000ff" }));
diff --git a/apps/client/src/widgets/sidebar/AttributeList.tsx b/apps/client/src/widgets/sidebar/AttributeList.tsx
index 90daa01a23..c5434fa107 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.tsx
+++ b/apps/client/src/widgets/sidebar/AttributeList.tsx
@@ -35,9 +35,10 @@ import RightPanelWidget, { CollapsibleWidgets } from "./RightPanelWidget";
* The note's attributes as a list, one row per attribute: the kind (label, relation, or either's
* definition) is carried by an icon instead of by the `#`/`~`/`label:` syntax the attributes editor
* spells out, and the value is shown as a preview rather than in full. Rows open the same detail
- * form the editor uses, which is where an attribute is actually edited — except an owned label's
- * value, the edit that is made again and again: pressing the preview itself edits it in place,
- * through the field its definition calls for (see {@link AttributeValueEditor}).
+ * form the editor uses, which is where an attribute is actually edited — except an owned attribute's
+ * value, the edit that is made again and again: a label's is edited in place by pressing the preview
+ * itself, through the field its definition calls for, and a relation's target is repicked from the
+ * pencil on its row, the value staying the link it is (see {@link AttributeValueEditor}).
*
* The form floats beside its row where there is room for it — a panel with a note's width beside it —
* and is a page of its own inside a master-detail host (a modal on a phone), which slides it in over
@@ -470,10 +471,11 @@ function AttributeRowList({ rows, note, activeAttribute, valueEditor, readOnly,
showOwner={!isOwned && !readOnly}
// A read-only row opens the popup as an inherited one does: to be read, not edited.
onOpen={(anchor, e) => onOpen(attribute, isOwned && !readOnly, anchor, e)}
- // A label's value is typed straight into its row; everything else — relations,
- // whose value press follows the link, definitions, whose value is a summary —
- // keeps the popup. On a phone the row is a menu item and opens its page whole.
- onEditValue={isOwned && !readOnly && !IS_MOBILE && getAttributeKind(attribute) === "label"
+ // A label's value is typed straight into its row, and a relation's target is
+ // repicked in it (from the pencil — the value itself is a link, and stays
+ // one). Definitions, whose value is a summary of settings, keep the popup. On
+ // a phone the row is a menu item and opens its page whole.
+ onEditValue={isOwned && !readOnly && !IS_MOBILE && !isDefinition(getAttributeKind(attribute))
? () => onEditValue(attribute)
: undefined}
onDelete={isOwned && !readOnly ? () => onDelete(attribute) : undefined}
@@ -546,6 +548,20 @@ function AttributeRow({ attribute, note, active, valueEditor, isSystem, showOwne
<NoteLink containerClassName="attribute-owner" notePath={attribute.noteId} noPreview />
)}
+ {/* A relation's value is a link and stays one, so its edit has a way in of its own — put
+ away while the editor is open, whose field already is the edit. */}
+ {onEditValue && attrType === "relation" && !valueEditor && (
+ <ActionButton
+ className="attribute-edit-button"
+ icon="bx bx-pencil"
+ text={t("attribute_list_panel.change_target")}
+ onClick={(e) => {
+ e.stopPropagation();
+ onEditValue();
+ }}
+ />
+ )}
+
{onDelete && (
<ActionButton
className="attribute-delete-button"
@@ -663,10 +679,21 @@ function AttributeValue({ attribute, note, attrType, onEdit }: {
onEdit?: () => void;
}) {
if (attrType === "relation") {
- // A relation just created from the add menu has no target yet.
+ // A relation just created from the add menu has no target yet — and with no link to follow,
+ // its slot is free to be the way into picking one, as a label's value is into typing.
return attribute.value
? <NoteLink containerClassName="attribute-value" notePath={attribute.value} showNoteIcon noPreview />
- : <span class="attribute-value empty">{t("attribute_list_panel.no_target")}</span>;
+ : (
+ <span
+ class={clsx("attribute-value", "empty", onEdit && "editable")}
+ onClick={onEdit ? (e) => {
+ e.stopPropagation();
+ onEdit();
+ } : undefined}
+ >
+ {t("attribute_list_panel.no_target")}
+ </span>
+ );
}
if (isDefinition(attrType)) {
diff --git a/apps/client/src/widgets/sidebar/AttributeValueEditor.css b/apps/client/src/widgets/sidebar/AttributeValueEditor.css
index d381c8b801..c23ecef29d 100644
--- a/apps/client/src/widgets/sidebar/AttributeValueEditor.css
+++ b/apps/client/src/widgets/sidebar/AttributeValueEditor.css
@@ -34,4 +34,10 @@
padding: 0 5px;
cursor: pointer;
}
+
+ /* The group the note search wraps its box in takes the room the box is owed. */
+ & .input-group {
+ flex: 1;
+ min-width: 0;
+ }
}
diff --git a/apps/client/src/widgets/sidebar/AttributeValueEditor.tsx b/apps/client/src/widgets/sidebar/AttributeValueEditor.tsx
index 4d1091d06c..f946735d8f 100644
--- a/apps/client/src/widgets/sidebar/AttributeValueEditor.tsx
+++ b/apps/client/src/widgets/sidebar/AttributeValueEditor.tsx
@@ -8,6 +8,8 @@ import type { Attribute } from "../../services/attribute_parser";
import { getBuiltinLabelSelectOptions, getBuiltinLabelValueType, isBuiltinAttribute } from "../../services/attributes";
import { t } from "../../services/i18n";
import LabelValueInput, { getTypedInputForLabel } from "../attribute_widgets/label_value_input";
+import { AUTOCOMPLETE_DROPDOWN_SELECTOR } from "../react/FormAutocomplete";
+import NoteAutocomplete from "../react/NoteAutocomplete";
interface AttributeValueEditorProps {
/** The note the label belongs to, read for the definition that says what kind of field to offer. */
@@ -22,9 +24,10 @@ interface AttributeValueEditorProps {
}
/**
- * A label's value edited in the row that previews it, without opening the detail form: the same field
- * its promoted counterpart offers — a date picker for a date, a palette for a colour, a dropdown for a
- * closed set — or a plain box where nothing says more about the value than that it is one.
+ * An attribute's value edited in the row that previews it, without opening the detail form: for a
+ * label the same field its promoted counterpart offers — a date picker for a date, a palette for a
+ * colour, a dropdown for a closed set, a plain box where nothing says more about the value than that
+ * it is one — and for a relation the note search its target is picked in everywhere else.
*
* Leaving the field is what ends the edit and keeps it, matching the attributes editor's save-on-blur;
* escape puts the value back instead. Only the value is edited here — the name, the flags and the
@@ -32,7 +35,11 @@ interface AttributeValueEditorProps {
*/
export default function AttributeValueEditor({ note, attribute, onEdit, onCommit, onRevert }: AttributeValueEditorProps) {
const containerRef = useRef<HTMLSpanElement>(null);
- const typed = useMemo(() => resolveValueField(note, attribute.name), [ note, attribute.name ]);
+ const isRelation = attribute.type === "relation";
+ // What a label is typed as; a relation's field follows from being one.
+ const typed = useMemo(
+ () => isRelation ? undefined : resolveValueField(note, attribute.name),
+ [ isRelation, note, attribute.name ]);
// The editor exists because its row was pressed, so the field is ready to type into at once — and
// what is there is selected, a short value being more often replaced than appended to. A colour
@@ -62,6 +69,14 @@ export default function AttributeValueEditor({ note, attribute, onEdit, onCommit
// Focus moving within the editor — the field to a button beside it — is not leaving it.
if (e.relatedTarget instanceof Node && editor.contains(e.relatedTarget)) return;
+ // Nor is it leaving for floating UI that belongs to the field: the note search's dropdown
+ // is appended to the body, and creating a note from it opens the note type chooser — the
+ // same company the detail popup keeps itself open over.
+ if (e.relatedTarget instanceof Element
+ && e.relatedTarget.closest(`${AUTOCOMPLETE_DROPDOWN_SELECTOR}, .algolia-autocomplete, .modal, .modal-backdrop`)) {
+ return;
+ }
+
// Nothing in the page took the focus over, and either the editor still holds it or the
// window itself has lost it — which is what opening a native picker's dialog does. The
// row has not been left, and committing here would close the editor under the dialog.
@@ -94,32 +109,45 @@ export default function AttributeValueEditor({ note, attribute, onEdit, onCommit
if (e.key === "Escape") {
e.stopPropagation();
onRevert();
- } else if (e.key === "Enter" && (typed.labelType !== "textarea" || e.ctrlKey || e.metaKey)) {
+ // Enter is the note search's own key — it is what picks — so only a label commits on
+ // it; a textarea keeps it for its newlines unless it is held down with the modifier.
+ } else if (e.key === "Enter" && typed && (typed.labelType !== "textarea" || e.ctrlKey || e.metaKey)) {
e.preventDefault();
e.stopPropagation();
onCommit();
}
}}
>
- <LabelValueInput
- labelType={typed.labelType}
- value={attribute.value ?? ""}
- onCommit={onEdit}
- commitOn="input"
- numberPrecision={typed.numberPrecision}
- selectOptions={typed.selectOptions}
- inputProps={{
- className: "form-control",
- // Names the dropdown's empty entry, as the detail form does for the same field.
- ...(typed.labelType === "select" && {
- placeholder: t("promoted_attributes.unset-field-placeholder")
- })
- }}
- />
+ {typed ? (
+ <LabelValueInput
+ labelType={typed.labelType}
+ value={attribute.value ?? ""}
+ onCommit={onEdit}
+ commitOn="input"
+ numberPrecision={typed.numberPrecision}
+ selectOptions={typed.selectOptions}
+ inputProps={{
+ className: "form-control",
+ // Names the dropdown's empty entry, as the detail form does for the same field.
+ ...(typed.labelType === "select" && {
+ placeholder: t("promoted_attributes.unset-field-placeholder")
+ })
+ }}
+ />
+ ) : (
+ <NoteAutocomplete
+ noteId={attribute.value || undefined}
+ opts={TARGET_NOTE_OPTS}
+ noteIdChanged={(noteId) => onEdit(noteId ?? "")}
+ />
+ )}
</span>
);
}
+/** Constant so it does not re-initialise the autocomplete on every render, as in the detail form. */
+const TARGET_NOTE_OPTS = { allowCreatingNotes: true };
+
/** The input types whose content `select()` applies to; the pickers select nothing and mind nothing. */
const SELECT_ALL_TYPES = new Set([ "text", "number", "url", "email", "tel" ]);