Skip to content

Build your personal knowledge base with Trilium Notes

feat(attributes): let a note's own definition win over the one it inherits

Elian Doranf8b9c5920679
diff --git a/apps/client/src/entities/fnote.spec.ts b/apps/client/src/entities/fnote.spec.ts
index de4f602cfe..6ec00cc6d3 100644
--- a/apps/client/src/entities/fnote.spec.ts
+++ b/apps/client/src/entities/fnote.spec.ts
@@ -605,6 +605,39 @@ describe("FNote attributes & inheritance", () => {
// definitions originate from three different notes -> the cross-note sort branch runs both ways
expect(noteIds.size).toBeGreaterThanOrEqual(3);
});
+
+ it("getAttributeDefinitions lets a note's own definition win over the one it inherits", () => {
+ const parent = buildNote({
+ id: "shadowParent",
+ title: "shadowParent",
+ "#label:status(inheritable)": "promoted,single,text"
+ });
+ const child = buildNote({
+ id: "shadowChild",
+ title: "shadowChild",
+ "#label:status(inheritable)": "promoted,single,select,options=Todo;Done"
+ });
+ registerBranch("br-shadow", "shadowChild", "shadowParent", 0);
+ parent.addChild("shadowChild", "br-shadow", false);
+ child.addParent("shadowParent", "br-shadow", false);
+
+ delete noteAttributeCache.attributes["shadowChild"];
+ delete noteAttributeCache.attributes["shadowParent"];
+
+ // Both definitions reach the child, but only the nearest describes the field — otherwise the
+ // child would show two Status inputs for the same label.
+ expect(child.getAttributes().filter((attr) => attr.name === "label:status")).toHaveLength(2);
+
+ const definitions = child.getAttributeDefinitions().filter((attr) => attr.name === "label:status");
+ expect(definitions).toHaveLength(1);
+ expect(definitions[0].noteId).toBe("shadowChild");
+ expect(definitions[0].getDefinition().selectOptions).toEqual([ "Todo", "Done" ]);
+
+ // The parent still sees its own, unchanged.
+ expect(parent.getAttributeDefinitions()
+ .filter((attr) => attr.name === "label:status")
+ .map((attr) => attr.noteId)).toEqual([ "shadowParent" ]);
+ });
});
describe("FNote paths & hierarchy", () => {
diff --git a/apps/client/src/entities/fnote.ts b/apps/client/src/entities/fnote.ts
index 5e05617846..7546b02a5c 100644
--- a/apps/client/src/entities/fnote.ts
+++ b/apps/client/src/entities/fnote.ts
@@ -880,9 +880,29 @@ export default class FNote {
return promotedAttrs;
}
+ /**
+ * The attribute definitions that apply to this note, at most one per defined name.
+ *
+ * A definition can reach a note from several places at once — its own, an ancestor's, a
+ * template's — and the nearest one wins: a note redefining `label:status` describes it for
+ * itself rather than gaining a second field beside the one it inherited. `getAttributes()`
+ * already lists owned attributes before inherited ones and inherited before templated ones, at
+ * every level, so keeping the first of each name is keeping the nearest.
+ */
getAttributeDefinitions() {
- return this.getAttributes()
- .filter((attr) => attr.isDefinition());
+ const definitions: FAttribute[] = [];
+ const seenNames = new Set<string>();
+
+ for (const attr of this.getAttributes()) {
+ if (!attr.isDefinition() || seenNames.has(attr.name)) {
+ continue;
+ }
+
+ seenNames.add(attr.name);
+ definitions.push(attr);
+ }
+
+ return definitions;
}
hasAncestor(ancestorNoteId: string, followTemplates = false, visitedNoteIds: Set<string> | null = null) {
diff --git a/apps/client/src/widgets/collections/table/rows.ts b/apps/client/src/widgets/collections/table/rows.ts
index e6b7c83bd9..8e2de80ab6 100644
--- a/apps/client/src/widgets/collections/table/rows.ts
+++ b/apps/client/src/widgets/collections/table/rows.ts
@@ -71,8 +71,9 @@ export async function buildRowDefinitions(parentNote: FNote, infos: AttributeDef
export default function getAttributeDefinitionInformation(parentNote: FNote) {
const info: AttributeDefinitionInformation[] = [];
- const attrDefs = parentNote.getAttributes()
- .filter(attr => attr.isDefinition());
+ // Nearest-wins, so a collection redefining an inherited field is described by its own definition
+ // rather than by whichever of the two the scan happened to reach first.
+ const attrDefs = parentNote.getAttributeDefinitions();
for (const attrDef of attrDefs) {
const def = attrDef.getDefinition();
if (def.multiplicity !== "single") {