Skip to content

Build your personal knowledge base with Trilium Notes

fix(calendar): keep the draft's dates over a template's

Elian Doran3b90691cc402
diff --git a/apps/client/src/widgets/collections/calendar/api.spec.ts b/apps/client/src/widgets/collections/calendar/api.spec.ts
index 554693ab7c..ffc2904f2c 100644
--- a/apps/client/src/widgets/collections/calendar/api.spec.ts
+++ b/apps/client/src/widgets/collections/calendar/api.spec.ts
@@ -161,6 +161,33 @@ describe("newEvent", () => {
expect(creationRequest().opts.type).toBe("text");
expect(creationRequest().opts.templateNoteId).toBeUndefined();
});
+
+ it("overrides the dates and times a template carries with the draft's", async () => {
+ vi.mocked(setAttribute).mockClear();
+ const calendar = buildNote({ title: "Calendar" });
+ // The labels stand in for what the note inherits from its template.
+ vi.mocked(note_create.createNote).mockResolvedValue({
+ note: buildNote({
+ title: "Fair", "#startDate": "2026-06-05",
+ "#calendar:startDate": "due", "#due": "2026-01-01",
+ "#startTime": "09:00", "#endTime": "10:00"
+ })
+ } as never);
+
+ await newEvent(calendar, {
+ startDate: "2026-06-05", endTime: "11:00",
+ template: {
+ id: "template:meeting", title: "Meeting", icon: "bx bx-group", group: "user",
+ options: { type: "text", templateNoteId: "meeting" }
+ }
+ });
+
+ expect(writtenLabels()).toEqual([
+ [ "due", "2026-06-05" ],
+ [ "startTime", "" ],
+ [ "endTime", "11:00" ]
+ ]);
+ });
});
describe("removeFromCalendar", () => {
diff --git a/apps/client/src/widgets/collections/calendar/api.ts b/apps/client/src/widgets/collections/calendar/api.ts
index d33c7ca98e..3ee619f53b 100644
--- a/apps/client/src/widgets/collections/calendar/api.ts
+++ b/apps/client/src/widgets/collections/calendar/api.ts
@@ -9,7 +9,7 @@ import { deleteNoteOrBranch } from "../../../services/note_deletion";
import { type NoteTypeOption } from "../../../services/note_types";
import { type TemplateChoiceConfig } from "../../react/TemplateDropdown";
import { type PromotedAttributeSetting } from "../promoted_attributes";
-import { EventLabelName } from "./utils";
+import { EventLabelName, getCustomisableLabel } from "./utils";
/** What the calendar stores in its view config. */
export interface CalendarViewData extends TemplateChoiceConfig {
@@ -83,9 +83,39 @@ export async function newEvent(parentNote: FNote, {
activate: false
}, componentId);
+ if (template?.options.templateNoteId && note) {
+ await overrideTemplateDates(note, { startDate, endDate, startTime, endTime }, componentId);
+ }
+
return note;
}
+/**
+ * Writes the draft over the dates and times `note` inherits from its template, including under a
+ * label the template renames (`#calendar:startDate=due`). An empty draft field writes an empty
+ * label, since an inherited label cannot be removed from the note.
+ */
+async function overrideTemplateDates(
+ note: FNote,
+ draft: Record<"startDate" | "endDate" | "startTime" | "endTime", string | null | undefined>,
+ componentId?: string
+) {
+ for (const [ name, value ] of Object.entries(draft)) {
+ if ((getCustomisableLabel(note, name, `calendar:${name}`) ?? "") === (value ?? "")) {
+ continue;
+ }
+
+ const customName = note.getLabelValue(`calendar:${name}`);
+ if (value) {
+ await setAttribute(note, "label", customName || name, value, componentId);
+ } else {
+ for (const label of customName ? [ customName, name ] : [ name ]) {
+ await setAttribute(note, "label", label, "", componentId);
+ }
+ }
+ }
+}
+
/**
* Takes an event off the calendar, having asked first, and offers to delete its note while it is at
* it — the bargain the geo map strikes for a marker (see removeFromMap there): taking an event off
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Calendar.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Calendar.html
index 0f8506871f..4444d952e3 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Calendar.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Collections/Calendar.html
@@ -42,7 +42,9 @@
date and time shows the note type or template the event's note is made
from. To make the event from another one, press the field and select it
from the list; the calendar remembers the choice for the next event. Select <em>More templates…</em> to
- change which templates are listed (see <a href="#event-templates">Event templates</a>).</p>
+ change which templates are listed (see <a href="#event-templates">Event templates</a>).
+ The date and time entered for the event replace any dates and times the
+ template carries.</p>
<p>To give the event a time before creating it (for example, after clicking
a day in the month view), press <em>Add time</em> and set the start and end
time; the duration is shown to the right of them. To make the event all-day
diff --git a/docs/User Guide/User Guide/Collections/Calendar.md b/docs/User Guide/User Guide/Collections/Calendar.md
index 32bd155f3b..ff696ee7ca 100644
--- a/docs/User Guide/User Guide/Collections/Calendar.md
+++ b/docs/User Guide/User Guide/Collections/Calendar.md
@@ -28,7 +28,7 @@ In either cases, a small popup will show with a prompt for a title and the date
At this point, the event hasn't been created yet to protect against accidental clicks. Add a title optionally and press the _Create_ button. If no title is provided, the event will follow the <a class="reference-link" href="../Advanced%20Usage/Default%20Note%20Title.md">Default Note Title</a> rules.
-The <span class="tn-icon bx bx-detail"></span> template field below the date and time shows the note type or template the event's note is made from. To make the event from another one, press the field and select it from the list; the calendar remembers the choice for the next event. Select _More templates…_ to change which templates are listed (see [Event templates](#event-templates)).
+The <span class="tn-icon bx bx-detail"></span> template field below the date and time shows the note type or template the event's note is made from. To make the event from another one, press the field and select it from the list; the calendar remembers the choice for the next event. Select _More templates…_ to change which templates are listed (see [Event templates](#event-templates)). The date and time entered for the event replace any dates and times the template carries.
To give the event a time before creating it (for example, after clicking a day in the month view), press _Add time_ and set the start and end time; the duration is shown to the right of them. To make the event all-day again, press the <span class="tn-icon bx bx-x"></span> button at the end of the row. Adding or removing a time keeps the dates, so an event can start at a time on one day and end at a time on another (for example, a night shift). Changing the start date or time moves the end by the same amount, keeping the duration. An empty end date means the event ends on the day it starts; setting the end time before the start time moves the end date to the next day.