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
fix(calendar): keep quick date and time edits from being lost
Elian Doran7d7f5cc5eb23
diff --git a/apps/client/src/widgets/collections/calendar/EventDatesEditor.spec.tsx b/apps/client/src/widgets/collections/calendar/EventDatesEditor.spec.tsxindex 32213e5fd7..ec954914b4 100644--- a/apps/client/src/widgets/collections/calendar/EventDatesEditor.spec.tsx+++ b/apps/client/src/widgets/collections/calendar/EventDatesEditor.spec.tsx@@ -91,6 +91,23 @@ describe("EventDatesEditor", () => {expect(timeInputs()).toHaveLength(2);expect(document.activeElement).toBe(timeInputs()[0]);expect(dateInputs()).toHaveLength(2);++ // Removed again before the labels echo back, the hours still come off.+ act(() => removeTimeButton()?.click());+ expect(removeLabel).toHaveBeenCalledWith(note, "startTime");+ expect(removeLabel).toHaveBeenCalledWith(note, "endTime");+ expect(addTimeButton()).not.toBeNull();+ });++ it("edits the hours as last entered while the earlier edit has not echoed back", () => {+ const note = buildNote({ title: "Meeting", "#startDate": "2026-06-05", "#startTime": "13:00", "#endTime": "14:30" });+ mount(note);++ commitValue(timeInputs()[1], "15:00");+ expect(timeInputs()[1].value).toBe("15:00");+ commitValue(timeInputs()[0], "16:00");++ expect(setLabel).toHaveBeenLastCalledWith(note.noteId, "endTime", "18:00");});it("shows the day an event actually ends on, and writes it once an edit is made", () => {diff --git a/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx b/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsxindex e4c4c66cc3..e6d84ff873 100644--- a/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx+++ b/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx@@ -9,10 +9,8 @@ import { useEventLabel } from "./hooks";import { EventDraft, normalizeEventDates, withAllDay, withStart } from "./selection";/**- * 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.+ * Edits the date and time labels of an existing event, including labels renamed through+ * `#calendar:startDate` and similar (see `useEventLabel()`). Writes only the labels an edit changes.*/export default function EventDatesEditor({ note, repeats }: {note: FNote;@@ -24,25 +22,23 @@ export default function EventDatesEditor({ note, repeats }: {const [ startTime, setStartTime ] = useEventLabel(note, "startTime");const [ endTime, setEndTime ] = useEventLabel(note, "endTime");- // All day is the absence of a start time, which is how the event builder reads it too. Held- // locally so the time row answers the press at once — the labels the press writes come back only- // with the server's echo — and follows the note's own state wherever it changes from.- const storedAllDay = !startTime;- const [ allDay, setAllDay ] = useState(storedAllDay);- useEffect(() => setAllDay(storedAllDay), [ storedAllDay ]);+ // The labels change only when the server echoes a write, so the last edit is kept here and the+ // next edit is compared against it. A label change from the server replaces it.+ const stored: EventDraft = { startDate: startDate ?? "", endDate, startTime, endTime };+ const [ value, setValue ] = useState(stored);+ useEffect(() => setValue(stored), [ startDate, endDate, startTime, endTime ]);- const value: EventDraft = { startDate: startDate ?? "", endDate, startTime, endTime };const onChange = (next: EventDraft) => {- setAllDay(!next.startTime);+ setValue(next);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);+ if ((next.endDate ?? null) !== (value.endDate ?? null)) setEndDate(next.endDate ?? null);+ if ((next.startTime ?? null) !== (value.startTime ?? null)) setStartTime(next.startTime ?? null);+ if ((next.endTime ?? null) !== (value.endTime ?? null)) setEndTime(next.endTime ?? null);};return (<>- <EventDatesFields value={value} allDay={allDay} onChange={onChange} />+ <EventDatesFields value={value} onChange={onChange} />{repeats && (<EventRow icon="bx-repeat" title={t("calendar.recurrence.repeats")}>@@ -54,19 +50,13 @@ export default function EventDatesEditor({ note, repeats }: {}/**- * The date and time rows of an event, shared by the event popover and the new-event popup. The- * date row holds a start date and, where the event runs longer than a day, an end date; the time- * row holds the hours and their duration, or the button to add them for an all-day event. Fields- * commit as they are left, and hand the whole edited event to `onChange`.+ * The date and time rows of an event, used by `EventPopover` and `GhostPopover`. Each field commits+ * on blur and passes the whole edited event to `onChange`.*/-export function EventDatesFields({ value, allDay, onChange }: {+export function EventDatesFields({ value, 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;}) {- // Shown and handed on with the end date it actually ends on, which an edit then writes back.const event = normalizeEventDates(value);const { startDate, endDate, startTime, endTime } = event;const change = (next: EventDraft) => onChange(normalizeEventDates(next));@@ -78,8 +68,7 @@ export function EventDatesFields({ value, allDay, onChange }: {labelType="date"value={startDate}commitOn="blur"- // Never emptied: without a start date the note stops being an event at all.- // The field snaps back.+ // An empty start date is ignored, since a note without one is not an event.onCommit={(date) => date && change(withStart(event, { startDate: date }))}inputProps={{ className: "form-control", "aria-label": t("calendar.dates.start_date") }}/>@@ -88,15 +77,14 @@ export function EventDatesFields({ value, allDay, onChange }: {labelType="date"value={endDate ?? ""}commitOn="blur"- // Emptied, the event ends on the day it starts: the label goes rather than- // staying blank, which is how the builder reads a single-day event.+ // An empty end date removes the label, which makes it a single-day event.onCommit={(date) => change({ ...event, endDate: date || null })}inputProps={{ className: "form-control", "aria-label": t("calendar.dates.end_date") }}/></EventRow><EventTimeRow- hasTime={!allDay}+ hasTime={!!startTime}onAdd={() => change(withAllDay(event, false))}onRemove={() => change(withAllDay(event, true))}>@@ -104,8 +92,7 @@ export function EventDatesFields({ value, allDay, onChange }: {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.+ // An empty start time is ignored; `EventTimeRow`'s remove button makes it all-day.onCommit={(time) => time && change(withStart(event, { startTime: time }))}inputProps={{ className: "form-control", "aria-label": t("calendar.dates.start_time") }}/>diff --git a/apps/client/src/widgets/collections/calendar/EventField.tsx b/apps/client/src/widgets/collections/calendar/EventField.tsxindex b50c25a34e..3c305d4681 100644--- a/apps/client/src/widgets/collections/calendar/EventField.tsx+++ b/apps/client/src/widgets/collections/calendar/EventField.tsx@@ -39,9 +39,8 @@ export function EventField({ name, htmlFor, children }: {}/**- * One line of when an event happens, led by an icon that names what the line holds, with the icon's- * name as its tooltip. A line without an icon is indented to the same column, so its controls line- * up with the ones above. Shared by the event popover and the new-event popup.+ * A row of event controls led by an icon, with `title` as the icon's tooltip. Without `icon`, the+ * icon column stays empty so the controls align with the rows above.*/export function EventRow({ icon, title, rowRef, children }: {/** A boxicons class such as `bx-calendar`. */@@ -70,9 +69,8 @@ export function EventFieldControls({ className, children }: {}/**- * The row of an event's hours. An all-day event has none, so the row offers to add them; a timed- * one shows its hours (`children`) with a button that removes them, which makes it all-day again.- * Either button hands the focus on to what replaces it: the start time, or the button to add one.+ * The time row: an "Add time" button when `hasTime` is false, otherwise `children` and a remove+ * button. After either button is pressed, the focus moves to the control that replaces it.*/export function EventTimeRow({ hasTime, onAdd, onRemove, children }: {hasTime: boolean;@@ -119,10 +117,7 @@ export function EventTimeRow({ hasTime, onAdd, onRemove, children }: {);}-/**- * How long a timed event lasts, at the end of its time row. Renders nothing for an all-day event. An end before the start reads as running into the next day (see- * {@link getTimedDurationMinutes}).- */+/** The duration of a timed event (see `getTimedDurationMinutes()`); nothing for an all-day event. */export function EventDuration({ event }: { event: Parameters<typeof getTimedDurationMinutes>[0] }) {const total = getTimedDurationMinutes(event);if (total === null) return null;diff --git a/apps/client/src/widgets/collections/calendar/EventPopover.tsx b/apps/client/src/widgets/collections/calendar/EventPopover.tsxindex 25ef2a6411..8a08ee1424 100644--- a/apps/client/src/widgets/collections/calendar/EventPopover.tsx+++ b/apps/client/src/widgets/collections/calendar/EventPopover.tsx@@ -268,10 +268,9 @@ function EventDetails({ note, parentNote, isEditable, maximized, setMaximized, o}/**- * The event itself, under whatever heads it: its own fields, the note's promoted attributes and- * the note. Shared by the two shells, which differ only in what they put around this and how they- * are dismissed. The popover puts {@link EventActions} in its header; the sheet passes them as- * `children`, above the fields.+ * The event's fields, the note's promoted attributes and the note, shared by the popover and the+ * phone sheet. The popover renders {@link EventActions} in its header; the sheet passes them as+ * `children`, which render above the fields.*/function EventDetailsBody({ note, isEditable, maximized, children }: {note: FNote;@@ -310,9 +309,8 @@ function EventDetailsBody({ note, isEditable, maximized, children }: {}/**- * The toolbar of an event: the ways of opening its note (see OpenNoteActions), then the ways of- * changing it, left out rather than disabled where the calendar can't be edited, as the geo pane- * leaves them out.+ * The event's toolbar: `OpenNoteActions`, then the editing actions, which are omitted when the+ * calendar is read-only.*/function EventActions({ note, parentNote, isEditable, onClose }: {note: FNote;diff --git a/apps/client/src/widgets/collections/calendar/GhostPopover.css b/apps/client/src/widgets/collections/calendar/GhostPopover.cssindex 3005f824a9..68616209b1 100644--- a/apps/client/src/widgets/collections/calendar/GhostPopover.css+++ b/apps/client/src/widgets/collections/calendar/GhostPopover.css@@ -29,7 +29,7 @@* room around what it holds — all of which a dialog brings for the sheet.*/.calendar-ghost-popover {- width: 460px;+ width: min(460px, calc(100vw - 4rem));.calendar-ghost-header {display: flex;diff --git a/apps/client/src/widgets/collections/calendar/GhostPopover.tsx b/apps/client/src/widgets/collections/calendar/GhostPopover.tsxindex 8e0e88d642..5861e3386d 100644--- a/apps/client/src/widgets/collections/calendar/GhostPopover.tsx+++ b/apps/client/src/widgets/collections/calendar/GhostPopover.tsx@@ -13,11 +13,8 @@ import { EventDatesFields } from "./EventDatesEditor";import { AnchorPoint, EventDraft, narrowAnchorRect, normalizeEventDates } from "./selection";/**- * The ghost: the form an event is decided in before its note exists. It asks for the name, shows- * the days the drag covered, and lets the user add hours to the event, as a- * click in the month view drafts a whole day. Everything else an event can hold waits for the- * note, a click on its chip away. Dismissed — Escape, the close button, a press anywhere else —- * the draft simply evaporates: nothing was created.+ * The form for a new event before its note exists: a title field and the date and time fields+ * (`EventDatesFields`). Dismissing it (Escape, the close button, a click outside) creates nothing.** A desktop stands it beside the range that called for it, a draft being born of a place: the form* appears where the eye already is, and the grid does not reflow under the very range just dragged.@@ -52,7 +49,6 @@ export default function GhostPopover({ draft: initialDraft, anchor, container, o// The range as dragged, until the user changes whether it is all-day or edits its hours.const [ draft, setDraft ] = useState(() => normalizeEventDates(initialDraft));useEffect(() => setDraft(normalizeEventDates(initialDraft)), [ initialDraft ]);- const allDay = !draft.startTime;// Committing is asked for once, however many times Enter falls before the note arrives and// the form goes: each ask would make its own note.const [ committing, setCommitting ] = useState(false);@@ -111,7 +107,7 @@ export default function GhostPopover({ draft: initialDraft, anchor, container, o/><div className="calendar-ghost-when">- <EventDatesFields value={draft} allDay={allDay} onChange={setDraft} />+ <EventDatesFields value={draft} onChange={setDraft} /></div><div className="calendar-ghost-actions">diff --git a/apps/client/src/widgets/collections/calendar/RecurrenceEditor.tsx b/apps/client/src/widgets/collections/calendar/RecurrenceEditor.tsxindex a4c4d4f70b..228a9d5de2 100644--- a/apps/client/src/widgets/collections/calendar/RecurrenceEditor.tsx+++ b/apps/client/src/widgets/collections/calendar/RecurrenceEditor.tsx@@ -29,8 +29,8 @@ import {* Edits how the event repeats, through the note's `#recurrence` label — or whatever label a* `#calendar:recurrence` renaming points at instead (see useEventLabel) — structured fields for* the everyday rules, the raw RRULE string for one saying more than they can (see recurrence.ts- * for where that line runs). Rendered as a dropdown whose button names the rule, shown beside the- * all-day switch (see EventDatesEditor); the rule's fields open inside it.+ * for where that line runs). Renders a dropdown whose button names the rule; the rule's fields open+ * inside it.*/export default function RecurrenceEditor({ note }: { note: FNote }) {const [ storedValue, setStoredValue ] = useEventLabel(note, "recurrence");diff --git a/apps/client/src/widgets/collections/calendar/selection.ts b/apps/client/src/widgets/collections/calendar/selection.tsindex 4df5253546..fd7b96ab0a 100644--- a/apps/client/src/widgets/collections/calendar/selection.ts+++ b/apps/client/src/widgets/collections/calendar/selection.ts@@ -17,9 +17,8 @@ const DEFAULT_START_TIME = "09:00";const DEFAULT_END_TIME = "10:00";/**- * Switches an event between all-day and timed, for the new-event popup and the event popover- * alike (see EventDatesFields). The days are kept either way, a timed event being able to span- * several of them, and so is an end time the event already holds.+ * Switches an event between all-day and timed. Keeps the dates, and keeps an existing end time when+ * adding a time.*/export function withAllDay(draft: EventDraft, allDay: boolean): EventDraft {if (allDay) {@@ -30,16 +29,8 @@ export function withAllDay(draft: EventDraft, allDay: boolean): EventDraft {}/**- * Makes the end date say the day an event actually ends on, so the date fields never contradict- * how the calendar draws it (see buildEvent in event_builder.ts):- *- * - A timed event whose end falls before its start ends on the first day the end time comes after- * the start: the start day itself, or the next day for an end time before the start time.- * - An all-day event whose end date is before its start date lasts a single day.- * - An end date equal to the start date is emptied, an empty end date meaning the event ends on the- * day it starts.- *- * Returns the event unchanged where its end date is already right.+ * Sets the end date to the day `buildEvent()` draws the event ending on. An end date equal to the+ * start date becomes `null`.*/export function normalizeEventDates(event: EventDraft): EventDraft {const { startDate, startTime, endTime } = event;@@ -61,11 +52,7 @@ export function normalizeEventDates(event: EventDraft): EventDraft {return endDate === (event.endDate || null) ? event : { ...event, endDate };}-/**- * Moves an event to a new start date or start time, keeping how long it lasts, as calendar apps do:- * the end moves by as much as the start, across midnight where it has to. Only an edit of the end- * itself changes the duration.- */+/** Moves an event to a new start date or start time and shifts the end by the same amount. */export function withStart(event: EventDraft, start: { startDate?: string; startTime?: string }): EventDraft {const current = normalizeEventDates(event);const next = { ...current, ...start };@@ -76,7 +63,7 @@ export function withStart(event: EventDraft, start: { startDate?: string; startTconst endMinute = hours * 60 + minutes + duration;const endOfDay = endMinute % (24 * 60);const endDay = Math.floor(endMinute / (24 * 60));- // Back on the start day: an end date the event had is cleared, a missing one stays missing.+ // A missing end date stays `undefined` rather than becoming `null`.next.endDate = endDay > 0 ? addDays(next.startDate, endDay) : (current.endDate ? null : current.endDate);next.endTime = `${pad(Math.floor(endOfDay / 60))}:${pad(endOfDay % 60)}`;} else if (current.endDate) {