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): let a new event be given hours before it is created
Elian Doran1095f5d2c620
diff --git a/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx b/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsxindex 93f3c2bba2..1000ffde14 100644--- a/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx+++ b/apps/client/src/widgets/collections/calendar/EventDatesEditor.tsx@@ -10,6 +10,7 @@ import FormToggle from "../../react/FormToggle";import { useUniqueName } from "../../react/hooks";import { EventField, EventFieldControls, EventFieldRow } from "./EventField";import { useEventLabel } from "./hooks";+import { DEFAULT_END_TIME, DEFAULT_START_TIME } from "./selection";/*** Edits when the event happens, through the date and time labels the calendar draws it by (see@@ -126,9 +127,3 @@ export default function EventDatesEditor({ note, repeats }: {</>);}--/** 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/GhostPopover.css b/apps/client/src/widgets/collections/calendar/GhostPopover.cssindex c8a6640d26..9c9350c89d 100644--- a/apps/client/src/widgets/collections/calendar/GhostPopover.css+++ b/apps/client/src/widgets/collections/calendar/GhostPopover.css@@ -18,6 +18,26 @@gap: 6px;color: var(--muted-text-color);font-size: 0.9em;++ .calendar-ghost-dates {+ flex: 1;+ min-width: 0;+ }+ }++ .calendar-ghost-time {+ display: flex;+ align-items: center;+ gap: 6px;++ > input {+ flex: 1 1 0;+ min-width: 0;+ }++ .calendar-ghost-time-separator {+ opacity: 0.75;+ }}.calendar-ghost-actions {@@ -31,7 +51,7 @@* room around what it holds — all of which a dialog brings for the sheet.*/.calendar-ghost-popover {- width: 300px;+ width: 340px;.calendar-ghost-header {display: flex;diff --git a/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx b/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsxindex d42ecdc8ff..3e2096ab62 100644--- a/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx+++ b/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx@@ -37,6 +37,7 @@ describe("GhostPopover", () => {/** The form's only button, which is the one that asks for the note. */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]") ];it("asks for the note once, however many times the ask is made before it arrives", async () => {// A request still out, which is the window each further Enter would make its own note in.@@ -70,7 +71,7 @@ describe("GhostPopover", () => {// And a second try really does ask again.await act(async () => { createButton()?.click(); });expect(onCommit).toHaveBeenCalledTimes(2);- expect(onCommit).toHaveBeenLastCalledWith("Fair");+ expect(onCommit).toHaveBeenLastCalledWith("Fair", draft);});it("focuses the title field once the popover is shown", async () => {@@ -98,6 +99,31 @@ describe("GhostPopover", () => {await act(async () => { createButton()?.click(); });// Committing blank is meaningful: the note is then named by the calendar's #titleTemplate.- expect(onCommit).toHaveBeenCalledWith("");+ expect(onCommit).toHaveBeenCalledWith("", draft);+ });++ it("lets a whole-day draft be given hours before it is created", async () => {+ const onCommit = vi.fn().mockResolvedValue(undefined);+ mount(onCommit);++ const allDay = document.querySelector<HTMLInputElement>(".calendar-ghost-when .switch-toggle");+ if (!allDay) throw new Error("The ghost has no all-day switch.");+ expect(allDay.checked).toBe(true);+ expect(timeInputs()).toHaveLength(0);++ act(() => { allDay.dispatchEvent(new Event("input", { bubbles: true })); });+ const [ start, end ] = timeInputs();+ expect(start.value).toBe("09:00");+ expect(end.value).toBe("10:00");++ start.value = "14:00";+ act(() => { start.dispatchEvent(new Event("input", { bubbles: true })); });+ end.value = "15:30";+ act(() => { end.dispatchEvent(new Event("input", { bubbles: true })); });+ await act(async () => { createButton()?.click(); });++ expect(onCommit).toHaveBeenCalledWith("", {+ startDate: "2026-06-05", endDate: null, startTime: "14:00", endTime: "15:30"+ });});});diff --git a/apps/client/src/widgets/collections/calendar/GhostPopover.tsx b/apps/client/src/widgets/collections/calendar/GhostPopover.tsxindex cc1e0b63a5..93ad5729bc 100644--- a/apps/client/src/widgets/collections/calendar/GhostPopover.tsx+++ b/apps/client/src/widgets/collections/calendar/GhostPopover.tsx@@ -7,15 +7,17 @@ import { isMobile } from "../../../services/utils";import ActionButton from "../../react/ActionButton";import Button from "../../react/Button";import FormTextBox from "../../react/FormTextBox";+import FormToggle from "../../react/FormToggle";import Modal from "../../react/Modal";import Popover from "../../react/Popover";-import { AnchorPoint, EventDraft, narrowAnchorRect } from "./selection";+import { AnchorPoint, EventDraft, narrowAnchorRect, withAllDay } from "./selection";/**- * The ghost: the form an event is decided in before its note exists. It asks for the one thing the- * drag did not say — the name — and writes out the one thing it did; 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 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 switch all-day off to give the event hours, 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.** 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.@@ -27,7 +29,7 @@ import { AnchorPoint, EventDraft, narrowAnchorRect } from "./selection";* calendar's own `#titleTemplate` where one is set (see getNewNoteTitle in trilium-core), which* the old title prompt used to override with whatever stood in its box.*/-export default function GhostPopover({ draft, anchor, container, onCommit, onCancel, onDismiss }: {+export default function GhostPopover({ draft: initialDraft, anchor, container, onCommit, onCancel, onDismiss }: {draft: EventDraft;/** Where the drag ended, in viewport coordinates — which of the shading's pieces to stand by,* and the anchor of last resort when the shading cannot be found at all. */@@ -36,7 +38,7 @@ export default function GhostPopover({ draft, anchor, container, onCommit, onCancontainer: HTMLElement | null;/** Answers when the note has been made, or rejects having failed to make one — the ghost stays* up for a second try either way it is told (see {@link commit}). */- onCommit(title: string): Promise<void> | void;+ onCommit(title: string, draft: EventDraft): Promise<void> | void;/** The draft given up on: Escape, or the close button. */onCancel(): void;/**@@ -47,6 +49,10 @@ export default function GhostPopover({ draft, anchor, container, onCommit, onCanonDismiss(): void;}) {const [ title, setTitle ] = useState("");+ // The range as dragged, until the user changes whether it is all-day or edits its hours.+ const [ draft, setDraft ] = useState(initialDraft);+ useEffect(() => setDraft(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);@@ -75,7 +81,7 @@ export default function GhostPopover({ draft, anchor, container, onCommit, onCanif (committing) return;setCommitting(true);try {- await onCommit(title);+ await onCommit(title, draft);} catch {setCommitting(false);}@@ -106,9 +112,33 @@ export default function GhostPopover({ draft, anchor, container, onCommit, onCan<div className="calendar-ghost-when"><span className="bx bx-calendar" />- {describeDraft(draft)}+ <span className="calendar-ghost-dates">{describeDates(draft)}</span>+ <FormToggle+ switchOnName={t("calendar.dates.all_day")}+ switchOffName={t("calendar.dates.all_day")}+ currentValue={allDay}+ onChange={(on) => setDraft((d) => withAllDay(d, on))}+ /></div>+ {!allDay && (+ <div className="calendar-ghost-time">+ <FormTextBox+ type="time"+ currentValue={draft.startTime ?? ""}+ aria-label={t("calendar.dates.start_time")}+ onChange={(startTime) => startTime && setDraft((d) => ({ ...d, startTime }))}+ />+ <span className="calendar-ghost-time-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 }))}+ />+ </div>+ )}+<div className="calendar-ghost-actions"><Buttonkind="primary"@@ -195,24 +225,17 @@ function ghostAnchorRect(container: HTMLElement | null, point: AnchorPoint | nul}/**- * When the draft happens, written out for the form: the one thing the drag already decided.- * Decorative and short-lived — the note's real date fields take over the moment the draft is- * committed — so the browser's own formatter is enough.+ * 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 describeDraft({ startDate, endDate, startTime, endTime }: EventDraft) {- // Taken apart by hand: `new Date("2026-08-05")` reads as UTC midnight, which is the evening- // before in half the world's timezones.+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" });};- if (startTime) {- return endDate && endDate !== startDate- ? `${date(startDate)}, ${startTime} – ${date(endDate)}, ${endTime ?? ""}`- : `${date(startDate)}, ${startTime}${endTime ? ` – ${endTime}` : ""}`;- }-return endDate && endDate !== startDate ? `${date(startDate)} – ${date(endDate)}` : date(startDate);}diff --git a/apps/client/src/widgets/collections/calendar/index.tsx b/apps/client/src/widgets/collections/calendar/index.tsxindex 4f78987221..2a928735ff 100644--- a/apps/client/src/widgets/collections/calendar/index.tsx+++ b/apps/client/src/widgets/collections/calendar/index.tsx@@ -187,12 +187,12 @@ export default function CalendarView({ note, noteIds, onOpenNote }: ViewModeProp* could be dragged out beside it. Whatever else the note may hold is a click on its chip away* — which is where such a surface would have opened anyway.*/- const commitDraft = useCallback(async (title: string) => {+ const commitDraft = useCallback(async (title: string, draft: EventDraft) => {if (!selection || !("draft" in selection)) return;await newEvent(note, {title: title.trim() || undefined,- ...selection.draft,+ ...draft,componentId: parentComponent?.componentId});diff --git a/apps/client/src/widgets/collections/calendar/selection.spec.ts b/apps/client/src/widgets/collections/calendar/selection.spec.tsindex b01df94b91..f129f91888 100644--- a/apps/client/src/widgets/collections/calendar/selection.spec.ts+++ b/apps/client/src/widgets/collections/calendar/selection.spec.ts@@ -1,6 +1,6 @@import { afterEach, describe, expect, it } from "vitest";-import { narrowAnchorRect } from "./selection";+import { narrowAnchorRect, withAllDay } from "./selection";describe("narrowAnchorRect", () => {afterEach(() => {@@ -40,3 +40,15 @@ describe("narrowAnchorRect", () => {expect(narrowAnchorRect(span, null)).toEqual(new DOMRect(1580, 200, 0, 20));});});++describe("withAllDay", () => {+ it("drops the hours of a timed draft and keeps its days", () => {+ expect(withAllDay({ startDate: "2026-06-05", endDate: "2026-06-06", startTime: "23:00", endTime: "01:00" }, true))+ .toEqual({ startDate: "2026-06-05", endDate: "2026-06-06", startTime: null, endTime: null });+ });++ it("gives a whole-day draft default hours within its first day", () => {+ expect(withAllDay({ startDate: "2026-06-05", endDate: "2026-06-07" }, false))+ .toEqual({ startDate: "2026-06-05", endDate: null, startTime: "09:00", endTime: "10:00" });+ });+});diff --git a/apps/client/src/widgets/collections/calendar/selection.ts b/apps/client/src/widgets/collections/calendar/selection.tsindex 4f354614f9..6eb6ef3f46 100644--- a/apps/client/src/widgets/collections/calendar/selection.ts+++ b/apps/client/src/widgets/collections/calendar/selection.ts@@ -10,6 +10,22 @@ export interface EventDraft {endTime?: string | null;}+/** 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";++/**+ * 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.+ */+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 };+}+/** A point in viewport coordinates — where a click or a drag's end fell, anchoring a popover* beside it where nothing better can be found on the grid (see the anchor helpers). */export interface AnchorPoint {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.htmlindex d9a42d39dc..72f53a323d 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@@ -36,6 +36,10 @@clicks. Add a title optionally and press the <em>Create</em> button. If notitle is provided, the event will follow the <a class="reference-link"href="#root/_help_47ZrP6FNuoG8">Default Note Title</a> rules.</p>+<p>To give the event a time before creating it (for example, after clicking+ a day in the month view), turn off the <em>All day</em> toggle and set the+ start and end time. A timed event spans a single day, so turning off <em>All day</em> for+ a range of days keeps only the first one.</p><p>After creating the event, it will show up on the calendar. To edit thecontent of the event (including recurrence), click on it to bring it intothe popup view.</p>diff --git a/docs/User Guide/User Guide/Collections/Calendar.md b/docs/User Guide/User Guide/Collections/Calendar.mdindex 076bd0af95..5daceb619d 100644--- a/docs/User Guide/User Guide/Collections/Calendar.md+++ b/docs/User Guide/User Guide/Collections/Calendar.md@@ -28,6 +28,8 @@ In either cases, a small popup will show with a prompt for a title and the dateAt 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.+To give the event a time before creating it (for example, after clicking a day in the month view), turn off the _All day_ toggle and set the start and end time. A timed event spans a single day, so turning off _All day_ for a range of days keeps only the first one.+After creating the event, it will show up on the calendar. To edit the content of the event (including recurrence), click on it to bring it into the popup view.> [!NOTE]