Skip to content

Build your personal knowledge base with Trilium Notes

refactor(calendar): share the date and time fields between both popups

Elian Doran9c9fb3f92546
diff --git a/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx b/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx
index 4a966600f1..126074872f 100644
--- a/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx
+++ b/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx
@@ -6,15 +6,13 @@ import { t } from "../../../services/i18n";
import LabelValueInput from "../../attribute_widgets/label_value_input";
import { EventDuration, EventRow, EventTimeRow } from "./EventField";
import { useEventLabel } from "./hooks";
-import { DEFAULT_END_TIME, DEFAULT_START_TIME } from "./selection";
+import { EventDraft, withAllDay } from "./selection";
/**
- * Edits when the event happens, through the date and time labels the calendar draws it by (see
- * event_builder.ts) — under whatever names the note draws them by, a `#calendar:startDate`
- * renaming included (see useEventLabel). Two shapes, told apart by whether it has hours: a whole-day
- * event is a span of days — a start date and, where it runs longer, an end date — while a timed
- * one is a single day and the hours within it. Rendered as {@link EventRow} lines: the date, the
- * hours with their duration (or the button to add them), then the repeats field.
+ * Edits when an existing event happens, through the date and time labels the calendar draws it by
+ * (see event_builder.ts) — under whatever names the note draws them by, a `#calendar:startDate`
+ * renaming included (see useEventLabel). The fields are {@link EventDatesFields}, shared with the
+ * new-event popup; this writes back only the labels an edit changed, then adds the repeats row.
*/
export default function EventDatesEditor({ note, repeats }: {
note: FNote;
@@ -33,32 +31,54 @@ export default function EventDatesEditor({ note, repeats }: {
const [ allDay, setAllDay ] = useState(storedAllDay);
useEffect(() => setAllDay(storedAllDay), [ storedAllDay ]);
- const addTime = () => {
- setAllDay(false);
- // A timed event is said within one day, so a span of days is let go rather than kept
- // where the fields no longer show it. The hours are a foothold to edit, not a guess.
- setStartTime(DEFAULT_START_TIME);
- setEndTime(endTime ?? DEFAULT_END_TIME);
- setEndDate(null);
+ const value: EventDraft = { startDate: startDate ?? "", endDate, startTime, endTime };
+ const onChange = (next: EventDraft) => {
+ setAllDay(!next.startTime);
+ if (next.startDate !== value.startDate) setStartDate(next.startDate);
+ if ((next.endDate ?? null) !== endDate) setEndDate(next.endDate ?? null);
+ if ((next.startTime ?? null) !== startTime) setStartTime(next.startTime ?? null);
+ if ((next.endTime ?? null) !== endTime) setEndTime(next.endTime ?? null);
};
- const removeTime = () => {
- // A whole day has no hours; the dates stay as they were.
- setAllDay(true);
- setStartTime(null);
- setEndTime(null);
- };
+ return (
+ <>
+ <EventDatesFields value={value} allDay={allDay} onChange={onChange} />
+
+ {repeats && (
+ <EventRow icon="bx-repeat" title={t("calendar.recurrence.repeats")}>
+ {repeats}
+ </EventRow>
+ )}
+ </>
+ );
+}
+
+/**
+ * The date and time rows of an event, shared by the event popover and the new-event popup. Two
+ * shapes, told apart by whether the event has hours: a whole-day event is a span of days — a start
+ * date and, where it runs longer, an end date — while a timed one is a single day and the hours
+ * within it, with their duration. Fields commit as they are left, and hand the whole edited event
+ * to `onChange`.
+ */
+export function EventDatesFields({ value, allDay, onChange }: {
+ value: EventDraft;
+ /** Whether the time row offers to add hours. Given separately from `value` so a host can
+ * answer a press ahead of the value it writes coming back. */
+ allDay: boolean;
+ onChange(next: EventDraft): void;
+}) {
+ const { startDate, endDate, startTime, endTime } = value;
return (
<>
<EventRow icon="bx-calendar" title={t("calendar.dates.date")}>
<LabelValueInput
labelType="date"
- value={startDate ?? ""}
+ value={startDate}
commitOn="blur"
- // Never emptied: without a start date the note stops being an event at all,
- // and the dock is no place to do that by accident. The field snaps back.
- onCommit={(value) => value && setStartDate(value)}
+ // Never emptied: without a start date the note stops being an event at all.
+ // The field snaps back.
+ onCommit={(date) => date && onChange({ ...value, startDate: date })}
inputProps={{ className: "form-control", "aria-label": t("calendar.dates.start_date") }}
/>
{allDay && <>
@@ -69,20 +89,24 @@ export default function EventDatesEditor({ note, repeats }: {
commitOn="blur"
// Emptied, the event is a single day: the label goes rather than
// staying blank, which is how the builder reads a day-long event.
- onCommit={(value) => setEndDate(value || null)}
+ onCommit={(date) => onChange({ ...value, endDate: date || null })}
inputProps={{ className: "form-control", "aria-label": t("calendar.dates.end_date") }}
/>
</>}
</EventRow>
- <EventTimeRow hasTime={!allDay} onAdd={addTime} onRemove={removeTime}>
+ <EventTimeRow
+ hasTime={!allDay}
+ onAdd={() => onChange(withAllDay(value, false))}
+ onRemove={() => onChange(withAllDay(value, true))}
+ >
<LabelValueInput
labelType="time"
value={startTime ?? ""}
commitOn="blur"
// Never emptied either: a timed event without a start time is the all-day
// shape, and the button beside the hours is the way to say that.
- onCommit={(value) => value && setStartTime(value)}
+ onCommit={(time) => time && onChange({ ...value, startTime: time })}
inputProps={{ className: "form-control", "aria-label": t("calendar.dates.start_time") }}
/>
<span className="calendar-event-row-separator" aria-hidden="true">–</span>
@@ -90,17 +114,11 @@ export default function EventDatesEditor({ note, repeats }: {
labelType="time"
value={endTime ?? ""}
commitOn="blur"
- onCommit={(value) => value && setEndTime(value)}
+ onCommit={(time) => time && onChange({ ...value, endTime: time })}
inputProps={{ className: "form-control", "aria-label": t("calendar.dates.end_time") }}
/>
- {startDate && <EventDuration event={{ startDate, endDate, startTime, endTime }} />}
+ {startDate && <EventDuration event={value} />}
</EventTimeRow>
-
- {repeats && (
- <EventRow icon="bx-repeat" title={t("calendar.recurrence.repeats")}>
- {repeats}
- </EventRow>
- )}
</>
);
}
diff --git a/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx b/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx
index aa63056ed1..e1f721c69f 100644
--- a/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx
+++ b/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx
@@ -44,6 +44,13 @@ describe("GhostPopover", () => {
const createButton = () => document.querySelector<HTMLButtonElement>(".calendar-ghost-actions button");
const titleInput = () => document.querySelector<HTMLInputElement>(".calendar-ghost-body input");
const timeInputs = () => [ ...document.querySelectorAll<HTMLInputElement>(".calendar-ghost-body input[type=time]") ];
+ const dateInputs = () => [ ...document.querySelectorAll<HTMLInputElement>(".calendar-ghost-body input[type=date]") ];
+
+ /** Commits a field as leaving it does, as the event popover's fields do (see EventDatesFields). */
+ function commitValue(input: HTMLInputElement, value: string) {
+ input.value = value;
+ act(() => { input.dispatchEvent(new Event("focusout")); });
+ }
const duration = () => document.querySelector(".calendar-event-duration")?.textContent;
it("asks for the note once, however many times the ask is made before it arrives", async () => {
@@ -116,6 +123,8 @@ describe("GhostPopover", () => {
const addTime = document.querySelector<HTMLButtonElement>(".calendar-event-add-time");
if (!addTime) throw new Error("The ghost has no button to add a time.");
expect(timeInputs()).toHaveLength(0);
+ // The same date fields as the event popover: a start and an end day for a whole-day draft.
+ expect(dateInputs().map((input) => input.value)).toEqual([ "2026-06-05", "" ]);
expect(addTime.closest(".calendar-event-row")?.querySelector(".bx-time-five")).not.toBeNull();
expect(duration()).toBeUndefined();
@@ -126,10 +135,9 @@ describe("GhostPopover", () => {
expect(end.value).toBe("10:00");
expect(duration()).toBe("calendar_view.duration_hours|1");
- start.value = "14:00";
- act(() => { start.dispatchEvent(new Event("input", { bubbles: true })); });
- end.value = "15:30";
- act(() => { end.dispatchEvent(new Event("input", { bubbles: true })); });
+ expect(dateInputs()).toHaveLength(1);
+ commitValue(start, "14:00");
+ commitValue(timeInputs()[1], "15:30");
expect(duration()).toBe("calendar_view.duration_hours_minutes|1|30");
expect(timeInputs()[0].closest(".calendar-event-row")?.querySelector(".calendar-event-duration")).not.toBeNull();
await act(async () => { createButton()?.click(); });
@@ -139,6 +147,17 @@ describe("GhostPopover", () => {
});
});
+ it("lets the days of a draft be changed before it is created", async () => {
+ const onCommit = vi.fn().mockResolvedValue(undefined);
+ mount(onCommit);
+
+ commitValue(dateInputs()[0], "2026-06-08");
+ commitValue(dateInputs()[1], "2026-06-10");
+ await act(async () => { createButton()?.click(); });
+
+ expect(onCommit).toHaveBeenCalledWith("", { startDate: "2026-06-08", endDate: "2026-06-10" });
+ });
+
it("lets the hours be removed again, making the draft whole-day", async () => {
const onCommit = vi.fn().mockResolvedValue(undefined);
mount(onCommit);
diff --git a/apps/client/src/widgets/collections/calendar/GhostPopover.tsx b/apps/client/src/widgets/collections/calendar/GhostPopover.tsx
index 1347de8570..6b571f740d 100644
--- a/apps/client/src/widgets/collections/calendar/GhostPopover.tsx
+++ b/apps/client/src/widgets/collections/calendar/GhostPopover.tsx
@@ -9,8 +9,8 @@ import Button from "../../react/Button";
import FormTextBox from "../../react/FormTextBox";
import Modal from "../../react/Modal";
import Popover from "../../react/Popover";
-import { EventDuration, EventRow, EventTimeRow } from "./EventField";
-import { AnchorPoint, EventDraft, narrowAnchorRect, withAllDay } from "./selection";
+import { EventDatesFields } from "./EventDatesEditor";
+import { AnchorPoint, EventDraft, narrowAnchorRect } from "./selection";
/**
* The ghost: the form an event is decided in before its note exists. It asks for the name, shows
@@ -111,30 +111,7 @@ export default function GhostPopover({ draft: initialDraft, anchor, container, o
/>
<div className="calendar-ghost-when">
- <EventRow icon="bx-calendar" title={t("calendar.dates.date")}>
- <span>{describeDates(draft)}</span>
- </EventRow>
-
- <EventTimeRow
- hasTime={!allDay}
- onAdd={() => setDraft((d) => withAllDay(d, false))}
- onRemove={() => setDraft((d) => withAllDay(d, true))}
- >
- <FormTextBox
- type="time"
- currentValue={draft.startTime ?? ""}
- aria-label={t("calendar.dates.start_time")}
- onChange={(startTime) => startTime && setDraft((d) => ({ ...d, startTime }))}
- />
- <span className="calendar-event-row-separator" aria-hidden="true">–</span>
- <FormTextBox
- type="time"
- currentValue={draft.endTime ?? ""}
- aria-label={t("calendar.dates.end_time")}
- onChange={(endTime) => endTime && setDraft((d) => ({ ...d, endTime }))}
- />
- <EventDuration event={draft} />
- </EventTimeRow>
+ <EventDatesFields value={draft} allDay={allDay} onChange={setDraft} />
</div>
<div className="calendar-ghost-actions">
@@ -221,19 +198,3 @@ function ghostAnchorRect(container: HTMLElement | null, point: AnchorPoint | nul
return new DOMRect(point?.x ?? 0, point?.y ?? 0, 0, 0);
}
-
-/**
- * The days the draft spans, written out for the form. The hours have fields of their own. The
- * browser's formatter is enough, since the note's date labels take over once the draft is created.
- */
-function describeDates({ startDate, endDate }: EventDraft) {
- // Parsed by hand: `new Date("2026-08-05")` reads as UTC midnight, which is the evening before
- // in timezones west of UTC.
- const date = (iso: string) => {
- const [ year, month, day ] = iso.split("-").map(Number);
- return new Date(year, month - 1, day)
- .toLocaleDateString(undefined, { weekday: "short", day: "numeric", month: "short", year: "numeric" });
- };
-
- return endDate && endDate !== startDate ? `${date(startDate)} – ${date(endDate)}` : date(startDate);
-}
diff --git a/apps/client/src/widgets/collections/calendar/selection.spec.ts b/apps/client/src/widgets/collections/calendar/selection.spec.ts
index f129f91888..674d69a952 100644
--- a/apps/client/src/widgets/collections/calendar/selection.spec.ts
+++ b/apps/client/src/widgets/collections/calendar/selection.spec.ts
@@ -51,4 +51,9 @@ describe("withAllDay", () => {
expect(withAllDay({ startDate: "2026-06-05", endDate: "2026-06-07" }, false))
.toEqual({ startDate: "2026-06-05", endDate: null, startTime: "09:00", endTime: "10:00" });
});
+
+ it("keeps an end time the event already holds when hours are added", () => {
+ expect(withAllDay({ startDate: "2026-06-05", endTime: "11:30" }, false))
+ .toEqual({ startDate: "2026-06-05", endDate: null, startTime: "09:00", endTime: "11:30" });
+ });
});
diff --git a/apps/client/src/widgets/collections/calendar/selection.ts b/apps/client/src/widgets/collections/calendar/selection.ts
index 6eb6ef3f46..7dd80af6c6 100644
--- a/apps/client/src/widgets/collections/calendar/selection.ts
+++ b/apps/client/src/widgets/collections/calendar/selection.ts
@@ -11,19 +11,20 @@ export interface EventDraft {
}
/** The hours an event gets when it stops being all-day, for the user to edit. */
-export const DEFAULT_START_TIME = "09:00";
-export const DEFAULT_END_TIME = "10:00";
+const DEFAULT_START_TIME = "09:00";
+const DEFAULT_END_TIME = "10:00";
/**
- * Switches a draft between all-day and timed. A timed event spans a single day, so turning off
- * all-day drops the end date, as `EventDatesEditor` does for an existing event.
+ * Switches an event between all-day and timed, for the new-event popup and the event popover
+ * alike (see EventDatesFields). A timed event spans a single day, so adding hours drops the end
+ * date. An end time the event already holds is kept.
*/
export function withAllDay(draft: EventDraft, allDay: boolean): EventDraft {
if (allDay) {
return { ...draft, startTime: null, endTime: null };
}
- return { ...draft, endDate: null, startTime: DEFAULT_START_TIME, endTime: DEFAULT_END_TIME };
+ return { ...draft, endDate: null, startTime: DEFAULT_START_TIME, endTime: draft.endTime ?? DEFAULT_END_TIME };
}
/** A point in viewport coordinates — where a click or a drag's end fell, anchoring a popover
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 f39f736ff7..fc68e415a4 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
@@ -31,7 +31,9 @@
start and end date.</li>
</ul>
<p>In either cases, a small popup will show with a prompt for a title and
- the date &amp; time of the event that will be created.</p>
+ the date &amp; time of the event that will be created. The date and time
+ can be changed there the same way as in the popup view of an existing event
+ (see below).</p>
<p>At this point, the event hasn't been created yet to protect against accidental
clicks. Add a title optionally and press the <em>Create</em> button. If no
title is provided, the event will follow the&nbsp;<a class="reference-link"
diff --git a/docs/User Guide/User Guide/Collections/Calendar.md b/docs/User Guide/User Guide/Collections/Calendar.md
index 5a2e9d9b1e..0aa41dab9c 100644
--- a/docs/User Guide/User Guide/Collections/Calendar.md
+++ b/docs/User Guide/User Guide/Collections/Calendar.md
@@ -24,7 +24,7 @@ To create a new event:
* First, click the desired day (month & year views) or the desired timeslot (day view).
* Alternatively, drag across multiple days (or time slots) to set both the start and end date.
-In either cases, a small popup will show with a prompt for a title and the date & time of the event that will be created.
+In either cases, a small popup will show with a prompt for a title and the date & time of the event that will be created. The date and time can be changed there the same way as in the popup view of an existing event (see below).
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.