Skip to content

Build your personal knowledge base with Trilium Notes

fix(calendar): redraw events when an inherited attribute changes

Elian Doranb2b24aa353c1
diff --git a/apps/client/src/widgets/collections/calendar/index.tsx b/apps/client/src/widgets/collections/calendar/index.tsx
index 26464144f8..f5abdac265 100644
--- a/apps/client/src/widgets/collections/calendar/index.tsx
+++ b/apps/client/src/widgets/collections/calendar/index.tsx
@@ -37,7 +37,7 @@ import GhostPopover from "./GhostPopover";
import { openCalendarContextMenu } from "./context_menu";
import { CalendarSelection, EventDraft } from "./selection";
import { buildEvents, buildEventsForCalendar } from "./event_builder";
-import { formatDateToLocalISO, formatTimeToLocalISO, isValidDuration, parseDurationSeconds, parseStartEndDateFromEvent, parseStartEndTimeFromEvent } from "./utils";
+import { formatDateToLocalISO, formatTimeToLocalISO, isAttributeChangeAffecting, isValidDuration, parseDurationSeconds, parseStartEndDateFromEvent, parseStartEndTimeFromEvent } from "./utils";
interface CalendarViewData {
@@ -289,15 +289,27 @@ export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarVi
const api = calendarRef.current;
if (!api) return;
- // Subnote attribute change.
- if (loadResults.getAttributeRows(parentComponent?.componentId).some((a) => noteIds.includes(a.noteId ?? ""))) {
+ // Attribute change on a note the calendar draws — written on the note itself, or inherited
+ // from an ancestor or a template, which is just as much a change to the chip (see
+ // isAttributeChangeAffecting).
+ //
+ // The notes asked about are the collection's own plus whatever chips stand on the grid: a
+ // calendar root draws day notes and their children, which are in no `noteIds` at all, and
+ // so answered nothing here.
+ const drawnNoteIds = new Set(noteIds);
+ for (const event of api.getEvents()) {
+ const noteId = event.extendedProps.noteId;
+ if (noteId) drawnNoteIds.add(String(noteId));
+ }
+
+ if (isAttributeChangeAffecting(loadResults.getAttributeRows(parentComponent?.componentId), drawnNoteIds)) {
// Defer execution after the load results are processed so that the event builder has the updated data to work with.
setTimeout(() => api.refetchEvents(), 0);
return; // early return since we'll refresh the events anyway
}
// Title change.
- for (const noteId of loadResults.getNoteIds().filter(noteId => noteIds.includes(noteId))) {
+ for (const noteId of loadResults.getNoteIds().filter(noteId => drawnNoteIds.has(noteId))) {
const event = api.getEventById(noteId);
const note = froca.getNoteFromCache(noteId);
if (!event || !note) continue;
diff --git a/apps/client/src/widgets/collections/calendar/utils.spec.ts b/apps/client/src/widgets/collections/calendar/utils.spec.ts
index fabf98ee8d..9bac1764b7 100644
--- a/apps/client/src/widgets/collections/calendar/utils.spec.ts
+++ b/apps/client/src/widgets/collections/calendar/utils.spec.ts
@@ -1,6 +1,8 @@
import { describe, expect, it } from "vitest";
-import { formatDateToLocalISO, formatTimeToLocalISO, getMonthsInDateRange, offsetDate, parseStartEndDateFromEvent, parseStartEndTimeFromEvent } from "./utils";
+import { AttributeRow } from "../../../services/load_results";
+import { buildNote } from "../../../test/easy-froca";
+import { formatDateToLocalISO, formatTimeToLocalISO, getMonthsInDateRange, isAttributeChangeAffecting, offsetDate, parseStartEndDateFromEvent, parseStartEndTimeFromEvent } from "./utils";
/**
* A moment built in the machine's own zone, which is what the calendar hands these functions: a
@@ -124,3 +126,54 @@ describe("getMonthsInDateRange", () => {
expect(months[9]).toBe("2026-10");
});
});
+
+describe("isAttributeChangeAffecting", () => {
+ /** What a reload hands over for one changed attribute; only the owner and the inheritance flag are read. */
+ function changed(noteId: string, name: string, isInheritable = false): AttributeRow {
+ return { attributeId: `${noteId}_${name}`, componentId: "some-component", noteId, type: "label", name, isInheritable };
+ }
+
+ it("answers for a label written on a drawn note itself", () => {
+ const note = buildNote({ id: "own", title: "Event", "#color": "red" });
+
+ expect(isAttributeChangeAffecting([ changed(note.noteId, "color") ], [ note.noteId ])).toBe(true);
+ });
+
+ it("answers for an inheritable label on an ancestor, which is drawn on the chip all the same", () => {
+ const parent = buildNote({
+ id: "inh-parent",
+ title: "Collection",
+ "#color(inheritable)": "red",
+ children: [ { id: "inh-child", title: "Event", "#startDate": "2026-06-05" } ]
+ });
+
+ // The collection note is nowhere in the drawn notes — only its child is.
+ expect(isAttributeChangeAffecting([ changed(parent.noteId, "color", true) ], [ "inh-child" ])).toBe(true);
+ });
+
+ it("answers for a label on a template the drawn note points at", () => {
+ buildNote({ id: "tpl", title: "Template", "#color": "blue" });
+ buildNote({ id: "tpl-instance", title: "Event", "~template": "tpl" });
+
+ expect(isAttributeChangeAffecting([ changed("tpl", "color") ], [ "tpl-instance" ])).toBe(true);
+ });
+
+ it("keeps quiet for an ancestor's label that no child inherits", () => {
+ buildNote({
+ id: "own-parent",
+ title: "Collection",
+ "#color": "red",
+ children: [ { id: "own-child", title: "Event" } ]
+ });
+
+ expect(isAttributeChangeAffecting([ changed("own-parent", "color") ], [ "own-child" ])).toBe(false);
+ });
+
+ it("keeps quiet for a change with nothing to do with the calendar, and for no change at all", () => {
+ buildNote({ id: "elsewhere", title: "Somewhere else", "#color": "green" });
+ buildNote({ id: "drawn", title: "Event" });
+
+ expect(isAttributeChangeAffecting([ changed("elsewhere", "color", true) ], [ "drawn" ])).toBe(false);
+ expect(isAttributeChangeAffecting([], [ "drawn" ])).toBe(false);
+ });
+});
diff --git a/apps/client/src/widgets/collections/calendar/utils.ts b/apps/client/src/widgets/collections/calendar/utils.ts
index 2c58828ae7..fe251cee19 100644
--- a/apps/client/src/widgets/collections/calendar/utils.ts
+++ b/apps/client/src/widgets/collections/calendar/utils.ts
@@ -1,6 +1,9 @@
import { DateSelectInfo, EventApi } from "fullcalendar";
import FNote from "../../../entities/fnote";
+import attributes from "../../../services/attributes";
+import froca from "../../../services/froca";
+import { AttributeRow } from "../../../services/load_results";
export function parseStartEndDateFromEvent(e: DateSelectInfo | EventApi) {
const startDate = formatDateToLocalISO(e.start);
@@ -59,6 +62,38 @@ export function formatTimeToLocalISO(date: Date | null | undefined) {
.substring(0, 5);
}
+/**
+ * Whether an attribute change reaches any of the notes the calendar draws, and so asks for the
+ * events to be built again.
+ *
+ * A label written on the note itself is the plain case, and the only one this used to answer. But
+ * everything the event builder reads — the colour, the icon, the dates, the promoted attributes —
+ * it reads through inheritance (`getLabelValue`), so the same value can just as well come from an
+ * inheritable label on an ancestor or from a template, neither of which is one of the drawn notes.
+ * A `#color` moved onto the collection note itself changed every chip's colouring and refreshed
+ * nothing.
+ *
+ * Rows naming a drawn note outright are answered by a set lookup, and only the rest are put to
+ * {@link attributes.isAffecting}, which walks the very inheritance the builder reads through.
+ */
+export function isAttributeChangeAffecting(attributeRows: AttributeRow[], noteIds: Iterable<string>) {
+ const drawnNoteIds = new Set(noteIds);
+
+ return attributeRows.some((row) => {
+ if (row.noteId && drawnNoteIds.has(row.noteId)) {
+ return true;
+ }
+
+ for (const noteId of drawnNoteIds) {
+ if (attributes.isAffecting(row, froca.getNoteFromCache(noteId))) {
+ return true;
+ }
+ }
+
+ return false;
+ });
+}
+
/** The labels the calendar draws an event by, each of which a note may rename for itself via the
* matching `#calendar:` label (see {@link getCustomisableLabel}). */
export type EventLabelName = "startDate" | "endDate" | "startTime" | "endTime" | "recurrence";