Skip to content

Build your personal knowledge base with Trilium Notes

fix(attributes): don't offer prefixed names when completing a definition

Elian Doran08d74c706a65
diff --git a/apps/client/src/entities/fattribute.ts b/apps/client/src/entities/fattribute.ts
index df5dd9a52c..7d1a31e126 100644
--- a/apps/client/src/entities/fattribute.ts
+++ b/apps/client/src/entities/fattribute.ts
@@ -102,7 +102,7 @@ class FAttribute {
}
}
-const DEFINITION_PREFIXES = [ "label:", "relation:" ];
+export const DEFINITION_PREFIXES = [ "label:", "relation:" ];
/**
* A definition needs an attribute name after its prefix: `#label:` on its own defines nothing, so it
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.ts b/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.ts
index 6bcae0467c..dee669572e 100644
--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.ts
+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.spec.ts
@@ -125,6 +125,29 @@ describe("attribute detail popup naming", () => {
expect(addDefinitionPrefix("priority", "relation")).toBe("priority");
});
+ it("completes a definition against bare names, so that picking one does not prefix it twice", async () => {
+ vi.resetModules();
+ const server = (await import("../../services/server")).default;
+ const { fetchDefinitionNames } = await import("./attribute_detail");
+ // The label list holds the definitions of both kinds, definitions being labels themselves.
+ const labelNames = [ "priority", "label:priority", "label:due", "relation:author", "label:" ];
+ const get = vi.fn(async (url: string) => url.includes("type=label") ? labelNames : [ "template" ]);
+ server.get = get as unknown as typeof server.get;
+
+ // A label's definitions come out of the one list it shares with them.
+ expect(await fetchDefinitionNames("label", "")).toEqual([ "priority", "due" ]);
+ expect(get).toHaveBeenCalledExactlyOnceWith("attribute-names/?type=label&query=");
+
+ // A relation's are only in the label list, which is asked for on top of the relation names —
+ // whose plain labels are no names for a relation, and stay out.
+ get.mockClear();
+ expect(await fetchDefinitionNames("relation", "a b")).toEqual([ "template", "author" ]);
+ expect(get.mock.calls.map(([ url ]) => url)).toEqual([
+ "attribute-names/?type=relation&query=a%20b",
+ "attribute-names/?type=label&query=a%20b"
+ ]);
+ });
+
it("searches for every note carrying the attribute by name alone", async () => {
const { formatAttributeForSearch } = await import("./attribute_detail");
diff --git a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
index 991457fcf3..6abcc36126 100644
--- a/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
+++ b/apps/client/src/widgets/attribute_widgets/attribute_detail.tsx
@@ -6,7 +6,7 @@ import { ComponentChildren, ComponentProps } from "preact";
import { useCallback, useContext, useEffect, useLayoutEffect, useMemo, useRef, useState } from "preact/hooks";
import appContext from "../../components/app_context.js";
-import { isDefinitionName } from "../../entities/fattribute.js";
+import { DEFINITION_PREFIXES, isDefinitionName } from "../../entities/fattribute.js";
import contextMenu, { MenuItem } from "../../menus/context_menu.js";
import type { Attribute } from "../../services/attribute_parser.js";
import { getBuiltinLabelSelectOptions, getBuiltinLabelValueType, isBuiltinAttribute } from "../../services/attributes.js";
@@ -361,7 +361,11 @@ export function AttributeForm({ opts, attrType: initialAttrType, currentNoteId,
const [ attrType, setAttrType ] = useState(initialAttrType);
// Definitions describe the attribute they define, so they complete against its own type.
const nameType = attrType === "relation" || attrType === "relation-definition" ? "relation" : "label";
- const suggestAttributeNames = useCallback((query: string) => fetchAttributeNames(nameType, query), [ nameType ]);
+ // ...and under its bare name, which is what the box holds for them (see fetchDefinitionNames).
+ const isDefinitionType = isDefinition(attrType);
+ const suggestAttributeNames = useCallback((query: string) => isDefinitionType
+ ? fetchDefinitionNames(nameType, query)
+ : fetchAttributeNames(nameType, query), [ nameType, isDefinitionType ]);
const renderNameSuggestion = useCallback(
(suggestion: string) => <AttributeNameSuggestion type={nameType} name={suggestion} />, [ nameType ]);
// Whatever a relation is the inverse of is itself a relation.
@@ -851,6 +855,47 @@ export function fetchAttributeNames(type: "label" | "relation", query: string) {
return server.get<string[]>(`attribute-names/?type=${type}&query=${encodeURIComponent(query)}`);
}
+/**
+ * The names a definition's name box completes against, which are bare as the box is: a definition is
+ * stored under the name of what it defines (`label:foo`), so offering that name as it stands would
+ * prefix it a second time, into `label:label:foo`.
+ *
+ * What is offered is therefore the names of the kind being defined — the labels a definition would
+ * promote, the relations it would type — together with whatever is already defined the same way
+ * elsewhere, stripped of its prefix. Definitions of the other kind are left out: their names belong
+ * to attributes this one cannot define.
+ */
+export async function fetchDefinitionNames(kind: "label" | "relation", query: string) {
+ const [ ownNames, definitionNames ] = await Promise.all([
+ fetchAttributeNames(kind, query),
+ // Whatever it defines, a definition is itself a label — so a relation's definitions are found
+ // in the label list alone, which for that kind has to be asked for on top of its own names.
+ kind === "relation" ? fetchAttributeNames("label", query) : undefined
+ ]);
+
+ const prefix = `${kind}:`;
+ // A name reached both ways — `priority` in use as a label and defined as `label:priority` — is
+ // offered once.
+ const names = new Set<string>();
+
+ for (const name of ownNames) {
+ // A prefixed name here is a definition, which the loop below takes bare; the bare prefix on
+ // its own is an ordinary label by name, but naming a definition after it would only produce
+ // another bare prefix, `label:label:`.
+ if (!DEFINITION_PREFIXES.some((definitionPrefix) => name.startsWith(definitionPrefix))) {
+ names.add(name);
+ }
+ }
+
+ for (const name of definitionNames ?? ownNames) {
+ if (isDefinitionName(name) && name.startsWith(prefix)) {
+ names.add(name.substring(prefix.length));
+ }
+ }
+
+ return [ ...names ];
+}
+
function isDefinition(attrType: AttrType) {
return attrType === "label-definition" || attrType === "relation-definition";
}