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(calendar): give calendar events an automatic icon
Elian Doranf07c4851423f
diff --git a/apps/client/src/widgets/note_tree.ts b/apps/client/src/widgets/note_tree.tsindex d2b3485b06..db3754d96f 100644--- a/apps/client/src/widgets/note_tree.ts+++ b/apps/client/src/widgets/note_tree.ts@@ -1295,10 +1295,10 @@ export default class NoteTreeWidget extends NoteContextAwareWidget {#processAttributeRows(attributeRows: AttributeRow[], refreshCtx: RefreshContext) {for (const attrRow of attributeRows) {- // `geolocation` and `mapPosition` decide the icon of a note that has none of its own- // (see getNoteIcon).+ // `geolocation`, `mapPosition` and `startDate` decide the icon of a note that has none+ // of its own (see getNoteIcon).const dirtyingLabels = ["iconClass", "cssClass", "workspace", "workspaceIconClass",- "color", GEO_LOCATION_ATTRIBUTE, MAP_POSITION_ATTRIBUTE];+ "color", GEO_LOCATION_ATTRIBUTE, MAP_POSITION_ATTRIBUTE, "startDate"];if (attrRow.type === "label" && dirtyingLabels.includes(attrRow.name ?? "") && attrRow.noteId) {if (attrRow.isInheritable) {diff --git a/packages/commons/src/lib/notes.spec.ts b/packages/commons/src/lib/notes.spec.tsindex 82cf0d1d1c..6fc02cd59d 100644--- a/packages/commons/src/lib/notes.spec.ts+++ b/packages/commons/src/lib/notes.spec.ts@@ -1,7 +1,7 @@import { describe, expect, it } from "vitest";import {- CANVAS_ATTACHMENT_MIME, GEO_LOCATION_ATTRIBUTE, GEO_SHAPE_ATTRIBUTE, getContentRendererGroup,+ CALENDAR_EVENT_ICON, CANVAS_ATTACHMENT_MIME, GEO_LOCATION_ATTRIBUTE, GEO_SHAPE_ATTRIBUTE, getContentRendererGroup,getImageAttachmentTitle, getMimeIcon, getNoteContentType, getNoteIcon, isFullWidthNoteType,MAP_POSITION_ATTRIBUTE, MAP_SHAPE_ATTRIBUTE, NOTE_TYPE_ICONS, NOTE_TYPE_IMAGE_ATTACHMENTS,parseMindMapNoteLink@@ -126,6 +126,27 @@ describe("getNoteIcon", () => {}});+ it("draws a calendar event by its start date, under its own name where it renames it", () => {+ const labelled = (labels: Record<string, string>) => (name: string) => labels[name] ?? null;++ expect(getNoteIcon(buildArgs({ getLabelValue: labelled({ startDate: "2026-10-12" }) })))+ .toBe(CALENDAR_EVENT_ICON);+ expect(getNoteIcon(buildArgs({+ getLabelValue: labelled({ startDate: "2026-10-12" }), isFolder: () => true+ }))).toBe(CALENDAR_EVENT_ICON);+ expect(getNoteIcon(buildArgs({+ getLabelValue: labelled({ "calendar:startDate": "due", due: "2026-10-12" })+ }))).toBe(CALENDAR_EVENT_ICON);+ expect(getNoteIcon(buildArgs({+ getLabelValue: labelled({ startDate: "2026-10-12" }), iconClass: "bx bx-cake"+ }))).toBe("bx bx-cake");+ expect(getNoteIcon(buildArgs({ getLabelValue: labelled({ startDate: "" }) })))+ .toBe("bx bx-note");+ expect(getNoteIcon(buildArgs({+ type: "file", mime: "application/pdf", getLabelValue: labelled({ startDate: "2026-10-12" })+ }))).toBe("bx bxs-file-pdf");+ });+it("draws a shape note as the shape its label names, by the same rules as a pin", () => {const drawn = (value: string) =>(name: string) => name === GEO_SHAPE_ATTRIBUTE ? value : null;diff --git a/packages/commons/src/lib/notes.ts b/packages/commons/src/lib/notes.tsindex 77d8e2b696..b3a708b1cf 100644--- a/packages/commons/src/lib/notes.ts+++ b/packages/commons/src/lib/notes.ts@@ -142,6 +142,9 @@ export const CLIP_TYPE_ICONS: ReadonlyMap<string, string> = new Map([[ "tabs", "bx bx-window-alt" ]]);+/** The icon of a calendar event, a text note with a start date (see {@link getEventStartDate}). */+export const CALENDAR_EVENT_ICON = "bx bx-calendar-event";+/*** The icon a note is drawn under: its own `#iconClass` where it has one, and a default read off* what the note is otherwise.@@ -151,7 +154,8 @@ export const CLIP_TYPE_ICONS: ReadonlyMap<string, string> = new Map([* The geo map therefore writes no `#iconClass` onto a marker or a shape it creates, so an icon it* hands down through* `#child:iconClass` or a template still applies, and redrawing a shape as another kind changes the- * icon with it. A note the web clipper made is drawn by its `#clipType` ({@link CLIP_TYPE_ICONS}).+ * icon with it. A note the web clipper made is drawn by its `#clipType` ({@link CLIP_TYPE_ICONS}),+ * and a calendar event by {@link CALENDAR_EVENT_ICON}.* `iconClass` stays an argument rather than being read here because the share tree* narrows it to the prefixes an icon pack supplies.*/@@ -190,6 +194,9 @@ export function getNoteIcon({if (clipIcon) {return clipIcon;}+ if (getEventStartDate(getLabelValue)) {+ return CALENDAR_EVENT_ICON;+ }if (isFolder()) {return "bx bx-folder";}@@ -205,6 +212,15 @@ export function getNoteIcon({return NOTE_TYPE_ICONS[type];}+/**+ * The start date of a calendar event: the value of the label `#calendar:startDate` names, falling+ * back to `#startDate`, as the calendar collection reads it.+ */+export function getEventStartDate(getLabelValue: (name: string) => string | null) {+ const customLabelName = getLabelValue("calendar:startDate");+ return (customLabelName && getLabelValue(customLabelName)) || getLabelValue("startDate");+}+/*** The icon for something known only by its media type — an attachment, or anything else the app holds* that is not a note and so has no type to ask about.