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): say when an event happens from its dock
Elian Doran7e4cd01ecd3c
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex fcf3a15f04..83aba4ef76 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -1007,6 +1007,14 @@"pin_date": "Pin the current date so the calendar always opens on this interval instead of today","unpin_date": "Unpin the date so the calendar opens on today's date again","close_details": "Close event details",+ "dates": {+ "all_day": "All day",+ "start_date": "Start date",+ "end_date": "End date",+ "time": "Time",+ "start_time": "Start time",+ "end_time": "End time"+ },"recurrence": {"repeats": "Repeats","does_not_repeat": "Does not repeat",diff --git a/apps/client/src/widgets/collections/calendar/DetailDock.tsx b/apps/client/src/widgets/collections/calendar/DetailDock.tsxindex 44a417e034..d1f384bad2 100644--- a/apps/client/src/widgets/collections/calendar/DetailDock.tsx+++ b/apps/client/src/widgets/collections/calendar/DetailDock.tsx@@ -12,6 +12,7 @@ import PromotedAttributes from "../../PromotedAttributes";import ActionButton from "../../react/ActionButton";import { useLegacyComponentElement, useNote } from "../../react/hooks";import { removeFromCalendar } from "./api";+import EventDatesEditor from "./EventDatesEditor";import { EventFieldList } from "./EventField";import RecurrenceEditor from "./RecurrenceEditor";@@ -135,10 +136,12 @@ function DockContent({ note, parentNote, isEditable, onClose }: {</>}</EmbeddedNoteActions>- {/* How the event repeats — a way of changing it, so it goes where the calendar may- be edited: a calendar root's day notes are days, and days do not recur. */}+ {/* The event's own fields — when it happens, how it repeats — which are ways of+ changing it, so they go where the calendar may be edited: a calendar root's day+ notes are days, and a day neither moves nor recurs. */}{isEditable && (<EventFieldList>+ <EventDatesEditor note={note} /><RecurrenceEditor note={note} /></EventFieldList>)}diff --git a/apps/client/src/widgets/collections/calendar/EventDatesEditor.css b/apps/client/src/widgets/collections/calendar/EventDatesEditor.cssnew file mode 100644index 0000000000..4028009131--- /dev/null+++ b/apps/client/src/widgets/collections/calendar/EventDatesEditor.css@@ -0,0 +1,11 @@+.calendar-event-time-range {+ /* The two hours share the row evenly, the dash between them taking only its own width. */+ > input {+ flex: 1 1 0;+ min-width: 0;+ }++ .calendar-event-time-range-separator {+ opacity: 0.75;+ }+}diff --git a/apps/client/src/widgets/collections/calendar/EventDatesEditor.spec.tsx b/apps/client/src/widgets/collections/calendar/EventDatesEditor.spec.tsxnew file mode 100644index 0000000000..170a4cb825--- /dev/null+++ b/apps/client/src/widgets/collections/calendar/EventDatesEditor.spec.tsx@@ -0,0 +1,109 @@+import { render } from "preact";+import { act } from "preact/test-utils";+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";++import Component from "../../../components/component";+import type FNote from "../../../entities/fnote";+import attributes from "../../../services/attributes";+import { buildNote } from "../../../test/easy-froca";+import { ParentComponent } from "../../react/react_utils";+import EventDatesEditor from "./EventDatesEditor";++describe("EventDatesEditor", () => {+ let container: HTMLElement;+ const setLabel = vi.spyOn(attributes, "setLabel").mockResolvedValue(undefined);+ const removeLabel = vi.spyOn(attributes, "removeOwnedLabelByName").mockResolvedValue(true);++ beforeEach(() => {+ container = document.createElement("div");+ document.body.appendChild(container);+ setLabel.mockClear();+ removeLabel.mockClear();+ });++ afterEach(() => {+ render(null, container);+ container.remove();+ });++ function mount(note: FNote) {+ act(() => render(+ <ParentComponent.Provider value={new Component()}>+ <EventDatesEditor note={note} />+ </ParentComponent.Provider>,+ container));+ }++ const allDayBox = () => container.querySelector<HTMLInputElement>("input[type='checkbox']");+ const dateInputs = () => container.querySelectorAll<HTMLInputElement>("input[type='date']");+ const timeInputs = () => container.querySelectorAll<HTMLInputElement>("input[type='time']");++ /** Commits a field as leaving it does — the inputs commit on blur, bound as focusout (see+ * the preact/compat note in label_value_input.tsx). */+ function commitValue(input: HTMLInputElement, value: string) {+ input.value = value;+ act(() => { input.dispatchEvent(new Event("focusout")); });+ }++ it("shows a whole-day event as a span of days, and a timed one as a day and its hours", () => {+ mount(buildNote({ title: "Fair", "#startDate": "2026-06-05", "#endDate": "2026-06-07" }));+ expect(allDayBox()?.checked).toBe(true);+ expect(dateInputs()).toHaveLength(2);+ expect(timeInputs()).toHaveLength(0);+ expect(dateInputs()[0].value).toBe("2026-06-05");+ expect(dateInputs()[1].value).toBe("2026-06-07");++ mount(buildNote({ title: "Meeting", "#startDate": "2026-06-05", "#startTime": "13:00", "#endTime": "14:30" }));+ expect(allDayBox()?.checked).toBe(false);+ expect(dateInputs()).toHaveLength(1);+ expect(timeInputs()).toHaveLength(2);+ expect(timeInputs()[0].value).toBe("13:00");+ expect(timeInputs()[1].value).toBe("14:30");+ });++ it("switching all-day off gives the day hours to edit and lets a span of days go", () => {+ const note = buildNote({ title: "Fair", "#startDate": "2026-06-05", "#endDate": "2026-06-07" });+ mount(note);++ act(() => allDayBox()?.click());++ expect(setLabel).toHaveBeenCalledWith(note.noteId, "startTime", "09:00");+ expect(setLabel).toHaveBeenCalledWith(note.noteId, "endTime", "10:00");+ expect(removeLabel).toHaveBeenCalledWith(note, "endDate");+ // The fields answer the press at once, ahead of the labels echoing back.+ expect(timeInputs()).toHaveLength(2);+ expect(dateInputs()).toHaveLength(1);+ });++ it("switching all-day on takes the hours off and keeps the dates", () => {+ const note = buildNote({ title: "Meeting", "#startDate": "2026-06-05", "#startTime": "13:00", "#endTime": "14:30" });+ mount(note);++ act(() => allDayBox()?.click());++ expect(removeLabel).toHaveBeenCalledWith(note, "startTime");+ expect(removeLabel).toHaveBeenCalledWith(note, "endTime");+ expect(setLabel).not.toHaveBeenCalled();+ expect(dateInputs()).toHaveLength(2);+ });++ it("commits an end date as the field is left, and emptying it makes the event a single day", () => {+ const note = buildNote({ title: "Fair", "#startDate": "2026-06-05", "#endDate": "2026-06-07" });+ mount(note);++ commitValue(dateInputs()[1], "2026-06-09");+ expect(setLabel).toHaveBeenCalledWith(note.noteId, "endDate", "2026-06-09");++ commitValue(dateInputs()[1], "");+ expect(removeLabel).toHaveBeenCalledWith(note, "endDate");+ });++ it("never commits an emptied start date, without which the note would stop being an event", () => {+ const note = buildNote({ title: "Fair", "#startDate": "2026-06-05" });+ mount(note);++ commitValue(dateInputs()[0], "");+ expect(setLabel).not.toHaveBeenCalled();+ expect(removeLabel).not.toHaveBeenCalled();+ });+});diff --git a/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx b/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsxnew file mode 100644index 0000000000..0c3ada6c6b--- /dev/null+++ b/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx@@ -0,0 +1,114 @@+import "./EventDatesEditor.css";++import { useEffect, useState } from "preact/hooks";++import FNote from "../../../entities/fnote";+import { t } from "../../../services/i18n";+import LabelValueInput from "../../attribute_widgets/label_value_input";+import FormCheckbox from "../../react/FormCheckbox";+import { useNoteLabel, useUniqueName } from "../../react/hooks";+import { EventField, EventFieldControls } from "./EventField";++/**+ * Edits when the event happens, through the date and time labels the calendar draws it by (see+ * event_builder.ts). Two shapes, told apart by the all-day switch: 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 EventField} rows, standing in the dock's field column+ * beside the recurrence.+ *+ * Only the stock label names, as the dock reads only those (see EVENT_LABELS in DetailDock.tsx).+ */+export default function EventDatesEditor({ note }: { note: FNote }) {+ const [ startDate, setStartDate ] = useNoteLabel(note, "startDate");+ const [ endDate, setEndDate ] = useNoteLabel(note, "endDate");+ const [ startTime, setStartTime ] = useNoteLabel(note, "startTime");+ const [ endTime, setEndTime ] = useNoteLabel(note, "endTime");+ const startDateId = useUniqueName("event-start-date");+ const endDateId = useUniqueName("event-end-date");++ // All day is the absence of a start time, which is how the event builder reads it too. Held+ // locally so the switch 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 ]);++ const toggleAllDay = (on: boolean) => {+ setAllDay(on);+ if (on) {+ // A whole day has no hours; the dates stay as they were.+ setStartTime(null);+ setEndTime(null);+ } else {+ // 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);+ }+ };++ return (+ <>+ <FormCheckbox+ label={t("calendar.dates.all_day")}+ currentValue={allDay}+ onChange={toggleAllDay}+ />++ <EventField name={t("calendar.dates.start_date")} htmlFor={startDateId}>+ <LabelValueInput+ labelType="date"+ 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 instead.+ onCommit={(value) => value && setStartDate(value)}+ inputProps={{ id: startDateId, className: "form-control" }}+ />+ </EventField>++ {allDay ? (+ <EventField name={t("calendar.dates.end_date")} htmlFor={endDateId}>+ <LabelValueInput+ labelType="date"+ value={endDate ?? ""}+ 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)}+ inputProps={{ id: endDateId, className: "form-control" }}+ />+ </EventField>+ ) : (+ <EventField name={t("calendar.dates.time")}>+ <EventFieldControls className="calendar-event-time-range">+ <LabelValueInput+ labelType="time"+ value={startTime ?? ""}+ commitOn="blur"+ // Never emptied either: a timed event without a start time is the+ // all-day shape, and the switch above is the way to say that.+ onCommit={(value) => value && setStartTime(value)}+ inputProps={{ className: "form-control", "aria-label": t("calendar.dates.start_time") }}+ />+ <span className="calendar-event-time-range-separator" aria-hidden="true">–</span>+ <LabelValueInput+ labelType="time"+ value={endTime ?? ""}+ commitOn="blur"+ onCommit={(value) => value && setEndTime(value)}+ inputProps={{ className: "form-control", "aria-label": t("calendar.dates.end_time") }}+ />+ </EventFieldControls>+ </EventField>+ )}+ </>+ );+}++/** The hours a day gets when it stops being whole: a meeting-sized block in mid-morning, put there+ * to be edited rather than kept. An end time the note already had (a half-said timed event) is+ * honoured instead. */+const DEFAULT_START_TIME = "09:00";+const DEFAULT_END_TIME = "10:00";diff --git a/apps/client/src/widgets/collections/calendar/EventField.tsx b/apps/client/src/widgets/collections/calendar/EventField.tsxindex 0930f31ba2..c1d4f89ab4 100644--- a/apps/client/src/widgets/collections/calendar/EventField.tsx+++ b/apps/client/src/widgets/collections/calendar/EventField.tsx@@ -1,5 +1,6 @@import "./EventField.css";+import clsx from "clsx";import { ComponentChildren } from "preact";/**@@ -35,6 +36,9 @@ export function EventField({ name, htmlFor, children }: {* Controls standing in one line, for a field said as a sentence — "every [2] weeks", "after [10]* times" — rather than as a single box.*/-export function EventFieldControls({ children }: { children: ComponentChildren }) {- return <div className="calendar-event-field-controls">{children}</div>;+export function EventFieldControls({ className, children }: {+ className?: string;+ children: ComponentChildren;+}) {+ return <div className={clsx("calendar-event-field-controls", className)}>{children}</div>;}