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 the range a single click selected
Elian Doran9997ccbcd835
diff --git a/apps/client/src/widgets/collections/calendar/GhostPopover.tsx b/apps/client/src/widgets/collections/calendar/GhostPopover.tsxindex a651cd0ed2..917beca956 100644--- a/apps/client/src/widgets/collections/calendar/GhostPopover.tsx+++ b/apps/client/src/widgets/collections/calendar/GhostPopover.tsx@@ -24,7 +24,7 @@ import { AnchorPoint, EventDraft } 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 }: {+export default function GhostPopover({ draft, 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. */@@ -32,7 +32,14 @@ export default function GhostPopover({ draft, anchor, container, onCommit, onCan/** The calendar the range's shading is read out of (see {@link ghostAnchorRect}). */container: HTMLElement | null;onCommit(title: string): void;+ /** The draft given up on: Escape, or the close button. */onCancel(): void;+ /**+ * The draft pressed away from, which the host answers differently — the press itself decides+ * what becomes of the range's shading, and the ghost is in no position to say (see+ * dismissDraft in index.tsx).+ */+ onDismiss(): void;}) {const [ title, setTitle ] = useState("");// Committing is asked for once, however many times Enter falls before the note arrives and@@ -58,7 +65,7 @@ export default function GhostPopover({ draft, anchor, container, onCommit, onCanclassName="calendar-ghost-popover"placement={glob.isRtl ? "left-start" : "right-start"}getAnchorRect={() => ghostAnchorRect(container, anchor)}- onDismiss={onCancel}+ onDismiss={onDismiss}><div className="calendar-ghost-header"><span className="calendar-ghost-heading">{t("calendar_view.new_event")}</span>diff --git a/apps/client/src/widgets/collections/calendar/index.tsx b/apps/client/src/widgets/collections/calendar/index.tsxindex 0a2cbec4c2..72f734c1cd 100644--- a/apps/client/src/widgets/collections/calendar/index.tsx+++ b/apps/client/src/widgets/collections/calendar/index.tsx@@ -161,18 +161,33 @@ export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarVicomponentId: parentComponent?.componentId});if (created) {+ // The range has become an event and its shading has nothing left to stand for; the+ // chip takes its place. Said here because no press said it: committing is a key or a+ // button within the ghost, and the ghost is exempt from clearing the shading.+ calendarRef.current?.unselect();setSelection({ noteId: created.noteId, anchor: selection.anchor });}}, [ selection, note, parentComponent?.componentId ]);- // The dragged range keeps its shading for as long as the ghost stands for it — unselectAuto is- // off, so pressing into the ghost's own form does not clear it — and is let go the moment the- // selection is anything else: committed into a note, moved on from, or gone.- useEffect(() => {- if (!selection || !("draft" in selection)) {- calendarRef.current?.unselect();- }- }, [ selection ]);+ /**+ * The ghost given up on rather than pressed away from — Escape, or its own close button.+ * Neither is a press on anything that would settle the shading, so it is let go by hand.+ */+ const cancelDraft = useCallback(() => {+ calendarRef.current?.unselect();+ setSelection(null);+ }, []);++ /**+ * The ghost pressed away from, which is a different thing: the press itself settles what+ * becomes of the shading, and saying anything here would be saying it too early. A press on the+ * grid has already begun the next selection by the time this runs — the drag starts on the+ * press, `selectMinDistance` being 0 — so clearing here would wipe the very range just started,+ * which is why a click used to open a ghost over no shading at all while a drag kept its own+ * (the drag's later moves made the selection again). A press anywhere else clears it through+ * `unselectAuto`, and a press within the ghost is spared by `unselectCancel`.+ */+ const dismissDraft = useCallback(() => setSelection(null), []);// An event taken off the calendar takes the dock with it. Not in calendar-root mode, whose// events (day notes and their children) are not in the collection's noteIds at all.@@ -253,7 +268,12 @@ export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarViinitialDate={initialDate || undefined}locale={locale}{...editingProps}- unselectAuto={false}+ // The shading of a dragged range is FullCalendar's to keep and to clear, which it+ // does on the press that follows — except a press within the ghost standing for+ // that very range, which is what the exemption names. Letting it decide is what+ // spares a press on the grid: the next selection has already begun by then, and+ // FullCalendar knows not to clear what the same press has just made.+ unselectCancel=".calendar-ghost-popover"eventClick={onEventClick}// The event the popover stands for is marked as such, and asks for no hover// preview while it does: the popover beside it already says everything the@@ -287,7 +307,8 @@ export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarVianchor={selection.anchor}container={containerRef.current}onCommit={(title) => void commitDraft(title)}- onCancel={() => setSelection(null)}+ onCancel={cancelDraft}+ onDismiss={dismissDraft}/>)}</div>@@ -432,9 +453,8 @@ function useEditing(note: FNote, isEditable: boolean, isCalendarRoot: boolean, cconst { startTime, endTime } = parseStartEndTimeFromEvent(e);// On a phone the ghost does not open (see the event click), so the title is still asked- // for up front, in the dialog this flow always led with. Unselected by hand either way- // round: unselectAuto is off for the ghost's sake, so nothing else clears the drag's- // shading.+ // for up front, in the dialog this flow always led with. Unselected as the dialog closes,+ // rather than leaving the range shaded behind it until the next press lands.if (isMobile()) {const title = await dialog.prompt({ message: t("relation_map.enter_title_of_new_note"), defaultValue: t("relation_map.default_new_note_title") });if (title?.trim()) {