Skip to content

Build your personal knowledge base with Trilium Notes

fix(calendar): keep the new event's draft while its templates are edited

Elian Doranb02b5e463595
diff --git a/apps/client/src/widgets/collections/calendar/CalendarProperties.spec.tsx b/apps/client/src/widgets/collections/calendar/CalendarProperties.spec.tsx
index 174d5fb378..a169cefd47 100644
--- a/apps/client/src/widgets/collections/calendar/CalendarProperties.spec.tsx
+++ b/apps/client/src/widgets/collections/calendar/CalendarProperties.spec.tsx
@@ -90,6 +90,22 @@ describe("CalendarProperties", () => {
expect(draw({ templates: [ "note:mine" ] })?.dataset.templates).toBe("note:mine");
});
+ /** On a phone the new-event form is itself a dialog, opened before this one from its template field. */
+ it("opens over the dialog already open rather than closing it", () => {
+ const sheet = document.createElement("div");
+ sheet.className = "modal";
+ document.body.appendChild(sheet);
+ glob.activeDialog = $(sheet);
+
+ try {
+ draw(undefined);
+ expect(glob.activeDialog?.[0]).toBe(sheet);
+ } finally {
+ glob.activeDialog = null;
+ sheet.remove();
+ }
+ });
+
it("stores what the card reports beside the rest of the config, and refuses an empty list", () => {
const card = draw({ templates: [ "type:text:text/html" ], template: "type:text:text/html" });
expect(card).not.toBeNull();
diff --git a/apps/client/src/widgets/collections/calendar/CalendarProperties.tsx b/apps/client/src/widgets/collections/calendar/CalendarProperties.tsx
index fea961a46f..99e7d8af74 100644
--- a/apps/client/src/widgets/collections/calendar/CalendarProperties.tsx
+++ b/apps/client/src/widgets/collections/calendar/CalendarProperties.tsx
@@ -31,6 +31,7 @@ export default function CalendarProperties({ note, viewConfig, saveConfig, onClo
size="lg"
scrollable
show
+ stackable
onHidden={onClose}
>
<TemplateSelectionCard
diff --git a/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx b/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx
index 7eb6db7bb5..576af70389 100644
--- a/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx
+++ b/apps/client/src/widgets/collections/calendar/GhostPopover.spec.tsx
@@ -116,6 +116,36 @@ describe("GhostPopover", () => {
expect(visibilityAtFocus.every((visibility) => visibility === "visible")).toBe(true);
});
+ it("is given up on by Escape, except an Escape meant for a dialog or menu opened over it", () => {
+ const onCancel = vi.fn();
+ act(() => render(
+ <GhostPopover
+ draft={draft}
+ anchor={{ x: 10, y: 10 }}
+ container={container}
+ templates={{ offered: [ TEXT ], current: TEXT, onSelect() {}, onMore() {} }}
+ onCommit={vi.fn()}
+ onCancel={onCancel}
+ onDismiss={() => {}}
+ />,
+ container));
+ const escape = (target: Element | null) => act(() => {
+ target?.dispatchEvent(new KeyboardEvent("keydown", { key: "Escape", bubbles: true }));
+ });
+
+ // The calendar's properties dialog, opened from the template field's menu.
+ const dialog = document.createElement("div");
+ dialog.className = "modal";
+ dialog.appendChild(document.createElement("input"));
+ document.body.appendChild(dialog);
+ escape(dialog.querySelector("input"));
+ expect(onCancel).not.toHaveBeenCalled();
+
+ expect(titleInput()).not.toBeNull();
+ escape(titleInput());
+ expect(onCancel).toHaveBeenCalledTimes(1);
+ });
+
it("hands over the title as typed, an empty one standing for the calendar's own naming", async () => {
const onCommit = vi.fn().mockResolvedValue(undefined);
mount(onCommit);
diff --git a/apps/client/src/widgets/collections/calendar/GhostPopover.tsx b/apps/client/src/widgets/collections/calendar/GhostPopover.tsx
index 2a6bbead33..c5ca919859 100644
--- a/apps/client/src/widgets/collections/calendar/GhostPopover.tsx
+++ b/apps/client/src/widgets/collections/calendar/GhostPopover.tsx
@@ -7,6 +7,7 @@ import { type NoteTypeOption } from "../../../services/note_types";
import { isMobile } from "../../../services/utils";
import ActionButton from "../../react/ActionButton";
import Button from "../../react/Button";
+import { isWithinFloatingLayer } from "../../react/floating_layers";
import FormTextBox from "../../react/FormTextBox";
import Modal from "../../react/Modal";
import Popover from "../../react/Popover";
@@ -95,7 +96,8 @@ export default function GhostPopover({
if (isMobile()) return;
const onKeyDown = (e: KeyboardEvent) => {
- if (e.key === "Escape") onCancel();
+ // An Escape in a dialog or menu opened over the popover closes only that.
+ if (e.key === "Escape" && !isWithinFloatingLayer(e.target)) onCancel();
};
window.addEventListener("keydown", onKeyDown);
return () => window.removeEventListener("keydown", onKeyDown);
diff --git a/apps/client/src/widgets/collections/calendar/index.tsx b/apps/client/src/widgets/collections/calendar/index.tsx
index 8269b89d7e..3435a450d3 100644
--- a/apps/client/src/widgets/collections/calendar/index.tsx
+++ b/apps/client/src/widgets/collections/calendar/index.tsx
@@ -28,6 +28,7 @@ import ActionButton from "../../react/ActionButton";
import Button, { ButtonGroup } from "../../react/Button";
import CollapseOnOverflow from "../../react/CollapseOnOverflow";
import Dropdown from "../../react/Dropdown";
+import { FLOATING_LAYER_SELECTOR } from "../../react/floating_layers";
import { FormListItem } from "../../react/FormList";
import { useEffectiveReadOnly, useNoteContext, useNoteLabel, useNoteLabelBoolean, useNoteTypeOptions, useSpacedUpdate, useTriliumEvent, useTriliumOption, useTriliumOptionInt } from "../../react/hooks";
import { ParentComponent } from "../../react/react_utils";
@@ -215,11 +216,7 @@ export default function CalendarView({
saveConfig({ ...viewConfig, template: template.id });
}
}, [ viewConfig, saveConfig ]);
- // The draft is let go first: on a phone it is a dialog itself, and Bootstrap does not stack two.
- const editEventTemplates = useCallback(() => {
- cancelDraft();
- setIsEditingProperties(true);
- }, [ cancelDraft ]);
+ const editEventTemplates = useCallback(() => setIsEditingProperties(true), []);
const eventTemplates = useTemplateChoice({
ids: offeredTemplateIds(viewConfig, DEFAULT_EVENT_TEMPLATES),
lastUsedId: viewConfig?.template,
@@ -381,8 +378,9 @@ export default function CalendarView({
// that very range, whichever shell it wears, 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, .calendar-ghost-sheet"
+ // has just made. A press in a dialog or menu over the ghost, such as the template
+ // menu or `CalendarProperties`, leaves the ghost standing, so it keeps the shading too.
+ unselectCancel={`.calendar-ghost-popover, .calendar-ghost-sheet, ${FLOATING_LAYER_SELECTOR}`}
// Named by us so the ghost can find the shading it stands beside, v7 drawing it
// under a hashed class otherwise (see ghostAnchorRect in GhostPopover). The look
// is Forma's own, which is one declaration off the palette.