Skip to content

Build your personal knowledge base with Trilium Notes

feat(attributes): add from the foot of the list, the kind a keystroke

Elian Doran8793a065ebd9
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 82c6036aac..35fbfa8b17 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -821,7 +821,10 @@
"type_relation": "Relation",
"type_label_definition": "Label definition",
"type_relation_definition": "Relation definition",
- "system_hint": "System — this name has a built-in meaning"
+ "system_hint": "System — this name has a built-in meaning",
+ "add_attribute": "Add attribute",
+ "creation_kind_label": "Label — press here, or type ~, to make it a relation",
+ "creation_kind_relation": "Relation — press here, or type #, to make it a label"
},
"attribute_names": {
"system": "System",
diff --git a/apps/client/src/widgets/sidebar/AttributeCreationEditor.css b/apps/client/src/widgets/sidebar/AttributeCreationEditor.css
index a28d1906aa..17f915f807 100644
--- a/apps/client/src/widgets/sidebar/AttributeCreationEditor.css
+++ b/apps/client/src/widgets/sidebar/AttributeCreationEditor.css
@@ -2,6 +2,20 @@
name leading and the value taking the rest, weighted rather than measured — with only the two of
them, a fixed split would starve one side on a narrow pane and waste it on a wide one. */
.attribute-list-panel .attribute-creation-editor {
+ /* The kind, worn where a row wears its icon — and pressed to switch, so it says it can be. */
+ & .attribute-creation-kind {
+ flex: 0 0 auto;
+ display: flex;
+ align-items: center;
+ padding-inline: 2px;
+ color: var(--muted-text-color);
+ cursor: pointer;
+ }
+
+ & .attribute-creation-kind:hover {
+ color: var(--main-text-color);
+ }
+
& .attribute-creation-name,
& .attribute-creation-value {
display: flex;
diff --git a/apps/client/src/widgets/sidebar/AttributeCreationEditor.tsx b/apps/client/src/widgets/sidebar/AttributeCreationEditor.tsx
index 6fe3864d55..609bd51884 100644
--- a/apps/client/src/widgets/sidebar/AttributeCreationEditor.tsx
+++ b/apps/client/src/widgets/sidebar/AttributeCreationEditor.tsx
@@ -9,6 +9,7 @@ import utils from "../../services/utils";
import { AttributeNameSuggestion, fetchAttributeNames } from "../attribute_widgets/attribute_detail";
import LabelValueInput from "../attribute_widgets/label_value_input";
import FormAutocomplete from "../react/FormAutocomplete";
+import Icon from "../react/Icon";
import NoteAutocomplete from "../react/NoteAutocomplete";
import AttributeEditorOverlay from "./AttributeEditorOverlay";
import { resolveValueField, TARGET_NOTE_OPTS } from "./AttributeValueEditor";
@@ -33,22 +34,31 @@ interface AttributeCreationEditorProps {
*
* Stands on the {@link AttributeEditorOverlay} shell: leaving keeps the draft, escape drops it. In
* the name box, enter walks on to the value field once the completion has had its say.
+ *
+ * Which of the two kinds is being created is the draft's to start with, but not fixed: the `#`/`~`
+ * the attributes editor opens its syntax with switch it from the head of the name box, and the kind
+ * icon leading the row switches it by mouse — so one way in (the add row at the list's foot, which
+ * always opens a label) serves both kinds.
*/
export default function AttributeCreationEditor({ note, attribute, onCommit, onRevert }: AttributeCreationEditorProps) {
const containerRef = useRef<HTMLSpanElement>(null);
const nameRef = useRef<HTMLInputElement>(null);
- const isRelation = attribute.type === "relation";
- const nameType = isRelation ? "relation" : "label";
- // The one field state here: the value lives in the draft alone, but the name is what the value
- // field's kind hangs off, so typing it has to re-render.
+ // Held as state rather than read off the draft, because the draft can change kinds under the
+ // editor's hands: the attributes editor spells a kind as the `#`/`~` its syntax opens with, and
+ // typing either at the head of the name here switches the draft the same way (see commitName) —
+ // as does the kind icon leading the row, for the mouse.
+ const [ kind, setKind ] = useState<"label" | "relation">(attribute.type === "relation" ? "relation" : "label");
+ const isRelation = kind === "relation";
+ // The one other field state here: the value lives in the draft alone, but the name is what the
+ // value field's kind hangs off, so typing it has to re-render.
const [ name, setName ] = useState(attribute.name);
// Committing mid-composition would filter the characters being composed out from under the IME;
// the same guard the detail form keeps (https://github.com/zadam/trilium/pull/3812).
const isComposing = useRef(false);
- const suggestNames = useCallback((query: string) => fetchAttributeNames(nameType, query), [ nameType ]);
+ const suggestNames = useCallback((query: string) => fetchAttributeNames(kind, query), [ kind ]);
const renderNameSuggestion = useCallback(
- (suggestion: string) => <AttributeNameSuggestion type={nameType} name={suggestion} />, [ nameType ]);
+ (suggestion: string) => <AttributeNameSuggestion type={kind} name={suggestion} />, [ kind ]);
// What the name as it stands would be typed as, so a name with a definition behind it gets its
// field the moment the name settles — type `due`, and the value side becomes its date picker.
@@ -58,7 +68,28 @@ export default function AttributeCreationEditor({ note, attribute, onCommit, onR
useEffect(() => nameRef.current?.focus(), []);
+ function switchKind(newKind: "label" | "relation") {
+ if (newKind === kind) {
+ return;
+ }
+
+ setKind(newKind);
+ attribute.type = newKind;
+ // A value typed for the one kind means nothing to the other: a note is not named by the text
+ // a label held, nor a label by a noteId. The field is remounted empty by the kind switch.
+ attribute.value = "";
+ }
+
function commitName(newName: string) {
+ // The `#`/`~` the attributes editor opens its syntax with act as kind switches here, at the
+ // head of the name where the syntax puts them; the filter below then strips them, as it
+ // strips both characters wherever they turn up.
+ if (newName.startsWith("#")) {
+ switchKind("label");
+ } else if (newName.startsWith("~")) {
+ switchKind("relation");
+ }
+
// Invalid characters are simply ignored, as the detail form ignores them.
const filtered = utils.filterAttributeName(newName);
setName(filtered);
@@ -94,6 +125,17 @@ export default function AttributeCreationEditor({ note, attribute, onCommit, onR
}
}}
>
+ {/* The kind, where the row would carry its icon — and the mouse's way of switching it,
+ the mousedown guard above keeping the press from blurring the name box meanwhile. */}
+ <span
+ className="attribute-creation-kind"
+ title={isRelation
+ ? t("attribute_list_panel.creation_kind_relation")
+ : t("attribute_list_panel.creation_kind_label")}
+ onClick={() => switchKind(isRelation ? "label" : "relation")}
+ >
+ <Icon icon={isRelation ? "bx bx-transfer" : "bx bx-hash"} />
+ </span>
<span className="attribute-creation-name">
<FormAutocomplete
inputRef={nameRef}
diff --git a/apps/client/src/widgets/sidebar/AttributeList.css b/apps/client/src/widgets/sidebar/AttributeList.css
index 8accca0e7f..c019a5838e 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.css
+++ b/apps/client/src/widgets/sidebar/AttributeList.css
@@ -131,6 +131,25 @@
visibility: visible;
}
+/* The way in at the foot of the list: a ghost of a row, quiet until pointed at — the standing the
+ table's own add-row keeps. Sized as the values are: an invitation, not an entry. */
+.attribute-list-panel .attribute-add-row {
+ display: flex;
+ align-items: center;
+ gap: 6px;
+ padding: 3px 4px;
+ border-radius: var(--border-radius-size, 4px);
+ cursor: pointer;
+ color: var(--muted-text-color);
+ font-size: 0.85em;
+ opacity: 0.75;
+}
+
+.attribute-list-panel .attribute-add-row:hover {
+ background-color: var(--hover-item-background-color);
+ opacity: 1;
+}
+
/* A colour's chip keeps to its own size: a chip is laid out as a flex box, which alone in the value's
slot would run the width of it, a bar rather than a chip. */
.attribute-list-panel .attribute-value > .label-color-chip {
diff --git a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx b/apps/client/src/widgets/sidebar/AttributeList.spec.tsx
index 49e3297125..5faec9082a 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx
+++ b/apps/client/src/widgets/sidebar/AttributeList.spec.tsx
@@ -394,6 +394,51 @@ describe("AttributeList", () => {
expect(namesIn(container)).not.toContain("");
});
+ it("creates from the add row at the list's foot, the kind switched by prefix or by its icon", async () => {
+ renderPanel(noteWithAttributes());
+
+ const addRow = container.querySelector<HTMLElement>(".attribute-add-row");
+ expect(addRow).not.toBeNull();
+ act(() => addRow?.click());
+
+ // Opens as a label — the kind nearly always being added...
+ const editor = container.querySelector<HTMLElement>(".attribute-creation-editor");
+ expect(editor?.querySelector(".attribute-creation-value input")).not.toBeNull();
+ expect(editor?.querySelector(".note-autocomplete-stub")).toBeNull();
+
+ // ...until the `~` the attributes editor spells relations with is typed at the name's head:
+ // the prefix is spent on the switch, the name keeping only what follows it.
+ const nameInput = editor?.querySelector<HTMLInputElement>(".attribute-creation-name input");
+ act(() => {
+ if (nameInput) {
+ nameInput.value = "~depicts";
+ nameInput.dispatchEvent(new Event("input", { bubbles: true }));
+ }
+ });
+ expect(nameInput?.value).toBe("depicts");
+ expect(editor?.querySelector(".note-autocomplete-stub")).not.toBeNull();
+
+ // The kind icon presses it back to a label, and again to a relation.
+ act(() => editor?.querySelector<HTMLElement>(".attribute-creation-kind")?.click());
+ expect(editor?.querySelector(".note-autocomplete-stub")).toBeNull();
+ act(() => editor?.querySelector<HTMLElement>(".attribute-creation-kind")?.click());
+
+ const target = editor?.querySelector<HTMLInputElement>(".note-autocomplete-stub");
+ act(() => {
+ if (target) {
+ target.value = "tpl";
+ target.dispatchEvent(new Event("input", { bubbles: true }));
+ }
+ });
+ await act(async () => {
+ editor?.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.at(-1)).toMatchObject({ type: "relation", name: "depicts", value: "tpl" });
+ });
+
it("creates a relation in its row, its target picked in the note search", async () => {
buildNote({ id: "target-note", title: "Target" });
renderPanel(noteWithAttributes());
diff --git a/apps/client/src/widgets/sidebar/AttributeList.tsx b/apps/client/src/widgets/sidebar/AttributeList.tsx
index 4444a63609..f687623940 100644
--- a/apps/client/src/widgets/sidebar/AttributeList.tsx
+++ b/apps/client/src/widgets/sidebar/AttributeList.tsx
@@ -382,6 +382,11 @@ export default function AttributeList() {
) : (
<NoItems icon="bx bx-hash" text={t("attribute_list_panel.no_attributes")} />
)}
+
+ {/* Only on a desktop: a phone adds from the header, page flow and all. */}
+ {!IS_MOBILE && note && (
+ <AddAttributeRow onAdd={(e) => addAttribute("label", e)} />
+ )}
</div>
</AttributeSection>
@@ -915,6 +920,28 @@ function AddAttributeButton({ text, attrTypes, onSelect }: {
);
}
+/**
+ * The way in at the foot of the list: a ghost of a row that creates a label in place when pressed —
+ * a label because that is nearly always the kind being added, and the creation editor it opens can
+ * be switched to a relation from its own name box or kind icon (see AttributeCreationEditor). The
+ * card header's menu stays the way to every kind, definitions included.
+ */
+function AddAttributeRow({ onAdd }: { onAdd: (e: MouseEvent) => void }) {
+ return (
+ <div
+ class="attribute-add-row"
+ onClick={(e) => {
+ // The container's click handler would close the very editor this opens.
+ e.stopPropagation();
+ onAdd(e);
+ }}
+ >
+ <Icon icon="bx bx-plus" />
+ {t("attribute_list_panel.add_attribute")}
+ </div>
+ );
+}
+
/** What each card's add button offers, in the order the attributes editor's own menu offers it. */
const ADD_MENU_ENTRIES: { attrType: AttributeKind; title: string; icon: string }[] = [
{ attrType: "label", title: t("attribute_editor.add_new_label"), icon: "bx bx-hash" },