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 press put away what stands open before it acts
Elian Doran105f1f1f66ed
diff --git a/apps/client/src/menus/context_menu.ts b/apps/client/src/menus/context_menu.tsindex 94a4c650f5..b69ca4749f 100644--- a/apps/client/src/menus/context_menu.ts+++ b/apps/client/src/menus/context_menu.ts@@ -399,6 +399,15 @@ class ContextMenu {return $item;}+ /**+ * Whether a menu is up. For a host that answers a press itself and so keeps it from the+ * document, whose click is what would otherwise put the menu away (see the listener bound in+ * the constructor): such a host has to know a standing menu is what its press is really for.+ */+ isShown() {+ return this.$widget.hasClass("show");+ }+async hide() {this.options?.onHide?.();this.$widget.removeClass("show");diff --git a/apps/client/src/widgets/collections/calendar/index.tsx b/apps/client/src/widgets/collections/calendar/index.tsxindex 3101192f4d..8b3e49b62b 100644--- a/apps/client/src/widgets/collections/calendar/index.tsx+++ b/apps/client/src/widgets/collections/calendar/index.tsx@@ -9,6 +9,7 @@ import { useCallback, useContext, useEffect, useMemo, useRef, useState } from "pimport appContext from "../../../components/app_context";import FNote from "../../../entities/fnote";+import contextMenu from "../../../menus/context_menu";import date_notes from "../../../services/date_notes";import froca from "../../../services/froca";import { t } from "../../../services/i18n";@@ -146,7 +147,21 @@ export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarViconst plugins = usePlugins(isEditable, isCalendarRoot);const locale = useLocale();- const { eventDidMount } = useEventDisplayCustomization(note, parentComponent?.componentId);+ /**+ * Puts away whatever surface stands over the calendar, answering whether there was one.+ *+ * Read through a ref because what asks is bound to a chip as it is drawn (see eventDidMount),+ * once and for all events, and so cannot be given the selection as it changes.+ */+ const selectionRef = useRef(selection);+ selectionRef.current = selection;+ const dismissSurface = useCallback(() => {+ if (!selectionRef.current) return false;+ setSelection(null);+ return true;+ }, []);++ const { eventDidMount } = useEventDisplayCustomization(note, parentComponent?.componentId, dismissSurface);const editingProps = useEditing(note, isEditable, isCalendarRoot, parentComponent?.componentId,(draft, anchor) => setSelection({ draft, anchor }), effectiveSlotDuration);@@ -246,6 +261,14 @@ export default function CalendarView({ note, noteIds }: ViewModeProps<CalendarVi// chip both of them are drawn beside.note_tooltip.dismissAllTooltips();+ // A menu standing open is what the click is for: putting it away is what a click anywhere+ // means while one is up, and the stop above is what kept this one from saying so on its own+ // (see isShown in context_menu.ts). The event is not opened as well — one press, one thing.+ if (contextMenu.isShown()) {+ void contextMenu.hide();+ return;+ }+const noteId = e.event.extendedProps.noteId;if (noteId) {// The click names which of the event's chips to stand by (see eventAnchorRect).@@ -575,7 +598,9 @@ function draftFromDateClick(e: DateClickArg, slotDuration: string): EventDraft |};}-function useEventDisplayCustomization(parentNote: FNote, componentId: string | undefined) {+function useEventDisplayCustomization(parentNote: FNote, componentId: string | undefined,+ /** Puts away whatever surface stands over the calendar, answering whether there was one. */+ dismissSurface: () => boolean) {const eventDidMount = useCallback((e: EventMountArg) => {const { iconClass, promotedAttributes } = e.event.extendedProps;@@ -638,6 +663,19 @@ function useEventDisplayCustomization(parentNote: FNote, componentId: string | u}async function onContextMenu(contextMenuEvent: PointerEvent) {+ // A surface standing open is what the press is for. A menu raised over it would cover+ // the very event it belongs to — the surface is drawn beside that chip — and offer a+ // second, smaller way to do what the surface already offers in full. So the press puts+ // the surface away and stops there; a second one raises the menu over a clear grid.+ //+ // Said here rather than left to the popover's own outside-press dismissal, which spares+ // a press on a chip: that exemption is for a press that switches the surface to another+ // event (see keepOpenSelector), and a right-click asks for no such thing.+ if (dismissSurface()) {+ contextMenuEvent.preventDefault();+ return;+ }+const note = await froca.getNote(e.event.extendedProps.noteId);if (!note) return;@@ -647,7 +685,7 @@ function useEventDisplayCustomization(parentNote: FNote, componentId: string | u// A long press raises it on a phone, as a right-click does on a desktop; the tap itself now// belongs to the event sheet, which offers what the menu offers and more (see onEventClick).e.el.addEventListener("contextmenu", onContextMenu);- }, []);+ }, [ dismissSurface ]);return { eventDidMount };}