Skip to content

Build your personal knowledge base with Trilium Notes

test(calendar): labels as promoted attributes

Elian Dorane7d06fcebafc
diff --git a/src/public/app/test/easy-froca.ts b/src/public/app/test/easy-froca.ts
index dfc02f1100..0ccb768fd4 100644
--- a/src/public/app/test/easy-froca.ts
+++ b/src/public/app/test/easy-froca.ts
@@ -31,63 +31,67 @@ export function buildNotes(notes: NoteDefinition[]) {
const ids = [];
for (const noteDef of notes) {
- const note = new FNote(froca, {
- noteId: noteDef.id ?? utils.randomString(12),
- title: noteDef.title,
- type: "text",
- mime: "text/html",
- isProtected: false,
- blobId: ""
- });
- froca.notes[note.noteId] = note;
- ids.push(note.noteId);
+ ids.push(buildNote(noteDef).noteId);
+ }
+
+ return ids;
+}
+
+export function buildNote(noteDef: NoteDefinition) {
+ const note = new FNote(froca, {
+ noteId: noteDef.id ?? utils.randomString(12),
+ title: noteDef.title,
+ type: "text",
+ mime: "text/html",
+ isProtected: false,
+ blobId: ""
+ });
+ froca.notes[note.noteId] = note;
- let position = 0;
- for (const [ key, value ] of Object.entries(noteDef)) {
- const attributeId = utils.randomString(12);
- const name = key.substring(1);
+ let position = 0;
+ for (const [ key, value ] of Object.entries(noteDef)) {
+ const attributeId = utils.randomString(12);
+ const name = key.substring(1);
- let attribute: FAttribute | null = null;
- if (key.startsWith("#")) {
- attribute = new FAttribute(froca, {
- noteId: note.noteId,
- attributeId,
- type: "label",
- name,
- value,
- position,
- isInheritable: false
- });
- }
+ let attribute: FAttribute | null = null;
+ if (key.startsWith("#")) {
+ attribute = new FAttribute(froca, {
+ noteId: note.noteId,
+ attributeId,
+ type: "label",
+ name,
+ value,
+ position,
+ isInheritable: false
+ });
+ }
- if (key.startsWith("~")) {
- attribute = new FAttribute(froca, {
- noteId: note.noteId,
- attributeId,
- type: "relation",
- name,
- value,
- position,
- isInheritable: false
- });
- }
+ if (key.startsWith("~")) {
+ attribute = new FAttribute(froca, {
+ noteId: note.noteId,
+ attributeId,
+ type: "relation",
+ name,
+ value,
+ position,
+ isInheritable: false
+ });
+ }
- if (!attribute) {
- continue;
- }
+ if (!attribute) {
+ continue;
+ }
- froca.attributes[attributeId] = attribute;
- note.attributes.push(attributeId);
- position++;
+ froca.attributes[attributeId] = attribute;
+ note.attributes.push(attributeId);
+ position++;
- // Inject the attribute into the note attribute cache, since FNote.getAttribute() doesn't always work.
- // If we add support for templates into froca, this might cause issues.
- if (!noteAttributeCache.attributes[note.noteId]) {
- noteAttributeCache.attributes[note.noteId] = [];
- }
- noteAttributeCache.attributes[note.noteId].push(attribute);
+ // Inject the attribute into the note attribute cache, since FNote.getAttribute() doesn't always work.
+ // If we add support for templates into froca, this might cause issues.
+ if (!noteAttributeCache.attributes[note.noteId]) {
+ noteAttributeCache.attributes[note.noteId] = [];
}
+ noteAttributeCache.attributes[note.noteId].push(attribute);
}
-
- return ids;
+ return note;
}
diff --git a/src/public/app/widgets/view_widgets/calendar_view.spec.ts b/src/public/app/widgets/view_widgets/calendar_view.spec.ts
index 4e6446c3f2..c512a4d567 100644
--- a/src/public/app/widgets/view_widgets/calendar_view.spec.ts
+++ b/src/public/app/widgets/view_widgets/calendar_view.spec.ts
@@ -1,5 +1,5 @@
import { describe, expect, it } from "vitest";
-import { buildNotes } from "../../test/easy-froca.js";
+import { buildNote, buildNotes } from "../../test/easy-froca.js";
import CalendarView from "./calendar_view.js";
describe("Building events", () => {
@@ -126,3 +126,23 @@ describe("Building events", () => {
});
});
+
+describe("Promoted attributes", () => {
+ it("supports labels", async () => {
+ const note = buildNote({
+ "title": "Hello",
+ "#weight": "75",
+ "#mood": "happy",
+ "#label:weight": "promoted,number,single,precision=1",
+ "#label:mood": "promoted,alias=Mood,single,text",
+ "#calendar:promotedAttributes": "label:weight,label:mood"
+ });
+
+ const event = await CalendarView.buildEvent(note, "2025-04-04");
+ expect(event).toHaveLength(1);
+ expect(event[0]?.promotedAttributes).toMatchObject({
+ weight: "75",
+ Mood: "happy"
+ })
+ });
+});
diff --git a/src/public/app/widgets/view_widgets/calendar_view.ts b/src/public/app/widgets/view_widgets/calendar_view.ts
index c970f559d7..512d5a650a 100644
--- a/src/public/app/widgets/view_widgets/calendar_view.ts
+++ b/src/public/app/widgets/view_widgets/calendar_view.ts
@@ -272,7 +272,7 @@ export default class CalendarView extends ViewMode {
continue;
}
- events.push(await CalendarView.#buildEvent(dateNote, startDate));
+ events.push(await CalendarView.buildEvent(dateNote, startDate));
if (dateNote.hasChildren()) {
const childNoteIds = dateNote.getChildNoteIds();
@@ -287,7 +287,7 @@ export default class CalendarView extends ViewMode {
const childNotes = await froca.getNotes(childNoteIds);
for (const childNote of childNotes) {
const startDate = childNoteToDateMapping[childNote.noteId];
- const event = await CalendarView.#buildEvent(childNote, startDate);
+ const event = await CalendarView.buildEvent(childNote, startDate);
events.push(event);
}
@@ -313,7 +313,7 @@ export default class CalendarView extends ViewMode {
}
const endDate = CalendarView.#getCustomisableLabel(note, "endDate", "calendar:endDate");
- events.push(await CalendarView.#buildEvent(note, startDate, endDate));
+ events.push(await CalendarView.buildEvent(note, startDate, endDate));
}
return events.flat();
@@ -341,7 +341,7 @@ export default class CalendarView extends ViewMode {
return note.getLabelValue(defaultLabelName);
}
- static async #buildEvent(note: FNote, startDate: string, endDate?: string | null) {
+ static async buildEvent(note: FNote, startDate: string, endDate?: string | null) {
const customTitle = note.getLabelValue("calendar:title");
const titles = await CalendarView.#parseCustomTitle(customTitle, note);
const color = note.getLabelValue("calendar:color") ?? note.getLabelValue("color");
@@ -378,6 +378,8 @@ export default class CalendarView extends ViewMode {
const filteredPromotedAttributes = note.getPromotedDefinitionAttributes().filter((attr) => promotedAttributeNames.includes(attr.name));
const result: Record<string, string> = {};
+ console.log("Got promoted attributes ", promotedAttributeNames, filteredPromotedAttributes);
+
for (const promotedAttribute of filteredPromotedAttributes) {
const [ type, name ] = promotedAttribute.name.split(":", 2);
const definition = promotedAttribute.getDefinition();