Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(attributes): read the values from the trailing edge, a ledger
Elian Doran5e87304b8d73
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 35fbfa8b17..7b6ef24ba6 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -821,10 +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"+ "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/AttributeList.css b/apps/client/src/widgets/sidebar/AttributeList.cssindex c019a5838e..4e7f844f63 100644--- a/apps/client/src/widgets/sidebar/AttributeList.css+++ b/apps/client/src/widgets/sidebar/AttributeList.css@@ -109,6 +109,15 @@font-size: 0.85em;}+/* Set against the row's trailing edge rather than trailing the name, so the values line up in a+ column of their own: name reads from the one edge, value from the other, and the gap between them+ says how much room is left rather than being dead space after every short value. Where the card+ asks for it — the two lists of plain attributes, owned and inherited (see AttributeList.tsx);+ the definitions' summaries keep prose order. */+.attribute-list-panel.align-values-end .attribute-value {+ text-align: end;+}+.attribute-list-panel .attribute-value.empty {font-style: italic;opacity: 0.7;@@ -156,6 +165,11 @@width: fit-content;}+/* Being a box rather than text, the chip follows the card's alignment by margin instead. */+.attribute-list-panel.align-values-end .attribute-value > .label-color-chip {+ margin-inline-start: auto;+}+/* The settings of a definition, which are a summary and not a value — worn at the size every valuepreview now shares. Lowercased, the settings being sentence-cased words of Trilium's own read as alist where they read as a sentence otherwise. */diff --git a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx b/apps/client/src/widgets/sidebar/AttributeList.spec.tsxindex 5faec9082a..f957bfe8c9 100644--- a/apps/client/src/widgets/sidebar/AttributeList.spec.tsx+++ b/apps/client/src/widgets/sidebar/AttributeList.spec.tsx@@ -53,13 +53,16 @@ vi.mock("../react/NoteAutocomplete", async () => {});import appContext from "../../components/app_context";+import type Component from "../../components/component";import FAttribute, { FAttributeRow } from "../../entities/fattribute";import type { Attribute } from "../../services/attribute_parser";import froca from "../../services/froca";+import type LoadResults from "../../services/load_results";import noteAttributeCache from "../../services/note_attribute_cache";import options from "../../services/options";import server from "../../services/server";import { buildNote } from "../../test/easy-froca";+import { ParentComponent } from "../react/react_utils";import AttributeList, { getAttributeKind, getDisplayName, listInherited, listInternal, listOwned, splitIntoSections } from "./AttributeList";describe("listOwned", () => {@@ -642,6 +645,103 @@ describe("AttributeList", () => {expect(picker?.value).toBe("#8000ff");});+ it("commits the in-place edit on enter for a label, a textarea keeping it for its lines", async () => {+ renderPanel(noteWithAttributes());+ act(() => firstRow().querySelector<HTMLElement>(".attribute-value")?.click());++ const input = container.querySelector<HTMLInputElement>(".attribute-value-editor input");+ act(() => {+ if (input) {+ input.value = "typed";+ input.dispatchEvent(new Event("input", { bubbles: true }));+ }+ });+ await act(async () => {+ input?.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));+ });++ expect(container.querySelector(".attribute-value-editor")).toBeNull();+ expect(put).toHaveBeenCalledOnce();+ const [ , saved ] = put.mock.calls[0] as [ string, { name: string; value: string }[] ];+ expect(saved.find((attribute) => attribute.name === "author")?.value).toBe("typed");++ // A textarea's enter is its own — it makes lines — so only held down with the modifier+ // does it stand for "done here".+ render(null, container);+ renderPanel(buildNote({+ id: "noted", title: "Noted",+ "#memo": "line one",+ "#label:memo": "promoted,single,textarea"+ }));+ act(() => firstRow().querySelector<HTMLElement>(".attribute-value")?.click());++ const textarea = container.querySelector<HTMLTextAreaElement>(".attribute-value-editor textarea");+ expect(textarea).not.toBeNull();+ act(() => {+ textarea?.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));+ });+ expect(container.querySelector(".attribute-value-editor")).not.toBeNull();++ await act(async () => {+ textarea?.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", ctrlKey: true, bubbles: true }));+ });+ expect(container.querySelector(".attribute-value-editor")).toBeNull();+ });++ it("wraps up the popup when an in-place edit starts, the new editor taking over from it", () => {+ renderPanel(noteWithAttributes());++ act(() => firstRow().click());+ expect(document.querySelector(".attr-detail")).not.toBeNull();++ act(() => firstRow().querySelector<HTMLElement>(".attribute-value")?.click());++ // The popup is committed — for a list of rows, saved — rather than left standing behind.+ expect(document.querySelector(".attr-detail")).toBeNull();+ expect(container.querySelector(".attribute-value-editor")).not.toBeNull();+ expect(put).toHaveBeenCalledOnce();+ });++ it("un-creates the creation row on escape, saving nothing", async () => {+ renderPanel(noteWithAttributes());+ act(() => container.querySelector<HTMLElement>(".attribute-add-row")?.click());++ const editor = container.querySelector<HTMLElement>(".attribute-creation-editor");+ const nameInput = editor?.querySelector<HTMLInputElement>(".attribute-creation-name input");+ act(() => {+ if (nameInput) {+ nameInput.value = "discarded";+ nameInput.dispatchEvent(new Event("input", { bubbles: true }));+ }+ });+ act(() => {+ nameInput?.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));+ });++ expect(container.querySelector(".attribute-creation-editor")).toBeNull();+ expect(put).not.toHaveBeenCalled();+ expect(namesIn(container)).not.toContain("discarded");+ });++ it("skips the reloads it caused itself while an editor is open, and follows a foreign one", () => {+ const note = noteWithAttributes();+ renderPanel(note, true);++ act(() => firstRow().querySelector<HTMLElement>(".attribute-value")?.click());+ expect(container.querySelector(".attribute-value-editor")).not.toBeNull();++ // The widget's own save coming back: nothing to reload over, the edits being the freshest.+ act(() => fireEntitiesReloaded((componentId) => componentId === panelComponentId() ? [] : affectingRows(note)));+ expect(container.querySelector(".attribute-value-editor")).not.toBeNull();++ // A change made elsewhere rebuilds the rows; the editor's row is gone with them, so the+ // edit is dropped rather than left dangling over the fresher state.+ holdLabel(note, "elsewhere", "x");+ act(() => fireEntitiesReloaded(() => affectingRows(note)));+ expect(container.querySelector(".attribute-value-editor")).toBeNull();+ expect(namesIn(container)).toContain("elsewhere");+ });+it("discards what the popup was told when it is closed rather than pressed away from", async () => {renderPanel(noteWithAttributes());@@ -665,9 +765,49 @@ describe("AttributeList", () => {expect(namesIn(container)).not.toContain("author");});- function renderPanel(note: FNote) {+ /** What the panel subscribed to, when rendered under a parent component that can tell it. */+ let eventHandlers: Map<string, (data: unknown) => void>;++ function renderPanel(note: FNote, withEvents = false) {shownNote.current = note;- act(() => render(<AttributeList />, container));+ if (!withEvents) {+ act(() => render(<AttributeList />, container));+ return;+ }++ eventHandlers = new Map();+ const parent = {+ componentId: panelComponentId(),+ registerHandler: (name: string, callback: (data: unknown) => void) => eventHandlers.set(name, callback),+ removeHandler: () => {}+ } as unknown as Component;+ act(() => render(+ <ParentComponent.Provider value={parent}>+ <AttributeList />+ </ParentComponent.Provider>,+ container+ ));+ }++ function panelComponentId() {+ return "panel-cid";+ }++ /** Hands the panel a reload, answering the row lookup as the given function does. */+ function fireEntitiesReloaded(getAttributeRows: (componentId?: string) => Partial<FAttributeRow>[]) {+ eventHandlers.get("entitiesReloaded")?.({ loadResults: { getAttributeRows } as unknown as LoadResults });+ }++ /** The reload's rows for a change touching the note, whoever made it. */+ function affectingRows(note: FNote) {+ return [ { noteId: note.noteId, type: "label", name: "changed", value: "", isInheritable: false } as Partial<FAttributeRow> ];+ }++ /** Writes another label onto the note behind the panel's back, as a foreign change would. */+ function holdLabel(note: FNote, name: string, value: string) {+ const added = attribute({ noteId: note.noteId, name, value, position: 50 });+ froca.attributes[added.attributeId] = added;+ note.attributes.push(added.attributeId);}/** Reads another note in the panel already rendered, as navigating to one does. */diff --git a/apps/client/src/widgets/sidebar/AttributeList.tsx b/apps/client/src/widgets/sidebar/AttributeList.tsxindex f687623940..baaddd3386 100644--- a/apps/client/src/widgets/sidebar/AttributeList.tsx+++ b/apps/client/src/widgets/sidebar/AttributeList.tsx@@ -376,7 +376,10 @@ export default function AttributeList() {>{/* Presses inside the sections do not dismiss the popup (see `parent` above), whichleaves closing on a press next to a row up to this handler. */}- <div class="attribute-list-panel" ref={containerRef} onClick={commit}>+ {/* The values read from the trailing edge here and in the inherited card below,+ the two lists of plain attributes reading as one ledger. The definitions keep+ prose order: their "value" is a summary of settings, not a value. */}+ <div class="attribute-list-panel align-values-end" ref={containerRef} onClick={commit}>{sections.owned.length > 0 ? (<AttributeRowList rows={sections.owned} {...rowProps} />) : (@@ -395,7 +398,7 @@ export default function AttributeList() {id="attributes-inherited"title={t("attribute_list_panel.inherited", { count: sections.inherited.length })}>- <div class="attribute-list-panel" onClick={commit}>+ <div class="attribute-list-panel align-values-end" onClick={commit}><AttributeRowList rows={sections.inherited} {...rowProps} /></div></AttributeSection>@@ -611,8 +614,8 @@ function AttributeRow({ attribute, note, active, valueEditor, isSystem, showOwne<><span class="attribute-name">{getDisplayName(attribute, attrType)}</span>- {valueEditor ?? <AttributeValue attribute={attribute} note={note} attrType={attrType} onEdit={onEditValue} />}-+ {/* Beside the name it qualifies — being inheritable is the attribute's standing, not part+ of its value — which also leaves the values' trailing edge to the values alone. */}{attribute.isInheritable && (<IconclassName="attribute-marker"@@ -621,10 +624,14 @@ function AttributeRow({ attribute, note, active, valueEditor, isSystem, showOwne/>)}+ {/* Where the attribute reaches the note from, and so beside the marker of its being able+ to: the source is part of its standing too, and the trailing edge stays the values'. */}{showOwner && attribute.noteId && (<NoteLink containerClassName="attribute-owner" notePath={attribute.noteId} noPreview />)}+ {valueEditor ?? <AttributeValue attribute={attribute} note={note} attrType={attrType} onEdit={onEditValue} />}+{/* A relation's value is a link and stays one, so its edit has a way in of its own — putaway while the editor is open, whose field already is the edit. */}{onEditValue && attrType === "relation" && !valueEditor && (diff --git a/apps/client/src/widgets/sidebar/AttributeValueEditor.css b/apps/client/src/widgets/sidebar/AttributeValueEditor.cssindex 1ac53904f7..4428a59f03 100644--- a/apps/client/src/widgets/sidebar/AttributeValueEditor.css+++ b/apps/client/src/widgets/sidebar/AttributeValueEditor.css@@ -6,4 +6,12 @@default whichever stylesheet loads last. */.attribute-list-panel .attribute-editor-overlay.attribute-value-editor {inset-inline-start: min(var(--value-editor-name-end, 20%), 20%);++ /* Typed where the preview shows it: the value reads from the trailing edge in the row (see+ AttributeList.css), so the field keeps it there while it is edited, and closing the editor+ does not make the text jump sides. The colour swatch is a box, not text, and needs none. */+ & .form-control:not(.label-color-picker),+ & .form-select {+ text-align: end;+ }}