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): make a tapped event as long as the slot it was tapped in
Elian Doran05b1b3457df2
diff --git a/apps/client/src/widgets/collections/calendar/event_builder.spec.ts b/apps/client/src/widgets/collections/calendar/event_builder.spec.tsindex e6d484dce9..853fcba612 100644--- a/apps/client/src/widgets/collections/calendar/event_builder.spec.ts+++ b/apps/client/src/widgets/collections/calendar/event_builder.spec.ts@@ -2,7 +2,7 @@ import { describe, expect, it, vi } from "vitest";import { buildNote, buildNotes } from "../../../test/easy-froca.js";import { buildEvent, buildEvents } from "./event_builder.js";import { LOCALE_MAPPINGS } from "./index.js";-import { isValidDuration } from "./utils.js";+import { isValidDuration, parseDurationSeconds } from "./utils.js";import { LOCALES } from "@triliumnext/commons";describe("Building events", () => {@@ -328,6 +328,29 @@ describe("isValidDuration", () => {});});+/**+ * What the length of a slot is read by, a tap on the grid making an event of exactly one (see+ * draftFromDateClick in index.tsx). Bounded lengths are {@link isValidDuration}'s business; this+ * only says how long one lasts.+ */+describe("parseDurationSeconds", () => {+ it("reads a duration written as hours, minutes and seconds", () => {+ expect(parseDurationSeconds("00:15:00")).toBe(15 * 60);+ expect(parseDurationSeconds("01:00:00")).toBe(60 * 60);+ expect(parseDurationSeconds("00:00:30")).toBe(30);+ expect(parseDurationSeconds("24:00:00")).toBe(24 * 60 * 60);+ });++ it("answers nothing for what is not written that way", () => {+ expect(parseDurationSeconds("1:0:0")).toBe(null);+ expect(parseDurationSeconds("00:60:00")).toBe(null);+ expect(parseDurationSeconds("abc")).toBe(null);+ expect(parseDurationSeconds("")).toBe(null);+ expect(parseDurationSeconds(null)).toBe(null);+ expect(parseDurationSeconds(undefined)).toBe(null);+ });+});+describe("Building locales", () => {it("every language has a locale defined", async () => {for (const { id, contentOnly } of LOCALES) {diff --git a/apps/client/src/widgets/collections/calendar/index.tsx b/apps/client/src/widgets/collections/calendar/index.tsxindex fe96ce5e01..3101192f4d 100644--- a/apps/client/src/widgets/collections/calendar/index.tsx+++ b/apps/client/src/widgets/collections/calendar/index.tsx@@ -29,7 +29,7 @@ import GhostPopover from "./GhostPopover";import { openCalendarContextMenu } from "./context_menu";import { CalendarSelection, EventDraft } from "./selection";import { buildEvents, buildEventsForCalendar } from "./event_builder";-import { formatDateToLocalISO, formatTimeToLocalISO, isValidDuration, parseStartEndDateFromEvent, parseStartEndTimeFromEvent } from "./utils";+import { formatDateToLocalISO, formatTimeToLocalISO, isValidDuration, parseDurationSeconds, parseStartEndDateFromEvent, parseStartEndTimeFromEvent } from "./utils";interface CalendarViewData {@@ -133,6 +133,9 @@ export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarViuseResizeObserver(containerRef, () => calendarRef.current?.updateSize());const isCalendarRoot = (calendarRoot || workspaceCalendarRoot);const isEditable = !isCalendarRoot;+ // Worked out once and handed to both the grid and the tap that makes an event of one of its+ // slots (see draftFromDateClick), so that the two cannot come to disagree on a slot's length.+ const effectiveSlotDuration = isValidDuration(slotDuration) ? slotDuration : DEFAULT_SLOT_DURATION;const eventBuilder = useMemo(() => {if (!isCalendarRoot) {return async () => await buildEvents(noteIds);@@ -145,7 +148,7 @@ export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarViconst { eventDidMount } = useEventDisplayCustomization(note, parentComponent?.componentId);const editingProps = useEditing(note, isEditable, isCalendarRoot, parentComponent?.componentId,- (draft, anchor) => setSelection({ draft, anchor }));+ (draft, anchor) => setSelection({ draft, anchor }), effectiveSlotDuration);/*** Turns the standing ghost into the note: created only now, at the commit, and — where the@@ -288,7 +291,7 @@ export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarVifirstDay={firstDayOfWeek ?? 0}weekends={!hideWeekends}weekNumbers={weekNumbers}- slotDuration={isValidDuration(slotDuration) ? slotDuration : DEFAULT_SLOT_DURATION}+ slotDuration={effectiveSlotDuration}slotLabelInterval={isValidDuration(slotLabelInterval) ? slotLabelInterval : DEFAULT_SLOT_LABEL_INTERVAL}height="100%"nowIndicator@@ -476,7 +479,9 @@ function useLocale() {}function useEditing(note: FNote, isEditable: boolean, isCalendarRoot: boolean, componentId: string | undefined,- onDraft: (draft: EventDraft, anchor: { x: number; y: number } | null) => void) {+ onDraft: (draft: EventDraft, anchor: { x: number; y: number } | null) => void,+ /** The length of one of the grid's slots, which is how long a tapped event lasts. */+ slotDuration: string) {const onCalendarSelection = useCallback((e: DateSelectArg) => {const { startDate, endDate } = parseStartEndDateFromEvent(e);if (!startDate) return;@@ -525,9 +530,9 @@ function useEditing(note: FNote, isEditable: boolean, isCalendarRoot: boolean, creturn;}- const draft = draftFromDateClick(e);+ const draft = draftFromDateClick(e, slotDuration);if (draft) onDraft(draft, null);- }, [ note, isCalendarRoot, onDraft ]);+ }, [ note, isCalendarRoot, onDraft, slotDuration ]);return {select: onCalendarSelection,@@ -542,10 +547,15 @@ function useEditing(note: FNote, isEditable: boolean, isCalendarRoot: boolean, c/*** The draft a tap stands for, a tap naming a moment where a drag names a range: the whole of a day- * where the view deals in days, and an hour from the slot where it deals in hours — the length- * FullCalendar itself gives an event whose end nobody said.+ * where the view deals in days, and the slot it landed in where the view deals in hours.+ *+ * One slot rather than any length of our own, because that is what a click makes of a moment on a+ * desktop — a click selects the slot it fell in, `selectMinDistance` being 0 — and a tap and a+ * click are the same wish. An hour, which this used to give, was an hour whichever way the grid was+ * divided, and a tap between two slots then made an event starting at the quarter or half hour and+ * running an hour from there.*/-function draftFromDateClick(e: DateClickArg): EventDraft | null {+function draftFromDateClick(e: DateClickArg, slotDuration: string): EventDraft | null {const startDate = formatDateToLocalISO(e.date);if (!startDate) return null;@@ -553,20 +563,18 @@ function draftFromDateClick(e: DateClickArg): EventDraft | null {return { startDate };}- const end = new Date(e.date.getTime() + TAPPED_EVENT_DURATION);+ const slotSeconds = parseDurationSeconds(slotDuration) ?? 0;+ const end = new Date(e.date.getTime() + slotSeconds * 1000);const endDate = formatDateToLocalISO(end);return {startDate,- // Only where the hour runs into the next day; a single day says itself with the start.+ // Only where the slot runs into the next day; a single day says itself with the start.endDate: endDate !== startDate ? endDate : undefined,startTime: formatTimeToLocalISO(e.date),endTime: formatTimeToLocalISO(end)};}-/** How long an event tapped into being lasts, FullCalendar's own default for one that says no end. */-const TAPPED_EVENT_DURATION = 60 * 60 * 1000;-function useEventDisplayCustomization(parentNote: FNote, componentId: string | undefined) {const eventDidMount = useCallback((e: EventMountArg) => {const { iconClass, promotedAttributes } = e.event.extendedProps;diff --git a/apps/client/src/widgets/collections/calendar/utils.ts b/apps/client/src/widgets/collections/calendar/utils.tsindex e441626b72..5b0f05b007 100644--- a/apps/client/src/widgets/collections/calendar/utils.ts+++ b/apps/client/src/widgets/collections/calendar/utils.ts@@ -104,7 +104,9 @@ export function parseDurationSeconds(str: string | null | undefined): number | nreturn Number(hours) * 3600 + Number(minutes) * 60 + Number(seconds);}-export function isValidDuration(str: string | null | undefined): boolean {+/** Whether a duration is written as `HH:MM:SS` and lasts a length a calendar will take. Answers+ * as a narrowing, a duration that passes being a string a caller can go on to use. */+export function isValidDuration(str: string | null | undefined): str is string {// The regex already constrains minutes/seconds to 00–59, so only the total needs bounding.const totalSeconds = parseDurationSeconds(str);if (totalSeconds === null) return false;