Skip to content

Build your personal knowledge base with Trilium Notes

fix(calendar): stand the popover beside a span that fills the grid

Elian Doran6f8c173be06f
diff --git a/apps/client/src/widgets/collections/calendar/EventPopover.tsx b/apps/client/src/widgets/collections/calendar/EventPopover.tsx
index 56c94a5e99..d8fa7ee0da 100644
--- a/apps/client/src/widgets/collections/calendar/EventPopover.tsx
+++ b/apps/client/src/widgets/collections/calendar/EventPopover.tsx
@@ -18,7 +18,7 @@ import EventDatesEditor from "./EventDatesEditor";
import { EventFieldList } from "./EventField";
import { useEventLabelOmissions } from "./hooks";
import RecurrenceEditor from "./RecurrenceEditor";
-import { AnchorPoint } from "./selection";
+import { AnchorPoint, narrowAnchorRect } from "./selection";
/**
* The whole of an event: the note's title, its own fields — when it happens, how it repeats — its
@@ -290,6 +290,9 @@ function EventDetailsBody({ note, parentNote, isEditable, onClose }: {
* row, so the one under the click is the one pointed at, falling back to the first drawn, and to
* the bare click point where no chip is on the grid at all. Read afresh on every reposition (see
* Popover), so the popover follows the chip through scrolls and redraws.
+ *
+ * A chip too wide to be stood beside — an event spanning the whole week, which is drawn the width
+ * of the grid — is narrowed to the click within it (see {@link narrowAnchorRect}).
*/
function eventAnchorRect(container: HTMLElement | null, noteId: string, point: AnchorPoint | null): DOMRect {
const chips = container?.querySelectorAll<HTMLElement>(`[data-event-note-id="${CSS.escape(noteId)}"]`);
@@ -305,7 +308,7 @@ function eventAnchorRect(container: HTMLElement | null, noteId: string, point: A
}
}
}
- return (pick ?? chips[0]).getBoundingClientRect();
+ return narrowAnchorRect((pick ?? chips[0]).getBoundingClientRect(), point);
}
return new DOMRect(point?.x ?? 0, point?.y ?? 0, 0, 0);
diff --git a/apps/client/src/widgets/collections/calendar/GhostPopover.tsx b/apps/client/src/widgets/collections/calendar/GhostPopover.tsx
index c438fbdcae..c86dda7d18 100644
--- a/apps/client/src/widgets/collections/calendar/GhostPopover.tsx
+++ b/apps/client/src/widgets/collections/calendar/GhostPopover.tsx
@@ -9,7 +9,7 @@ import Button from "../../react/Button";
import FormTextBox from "../../react/FormTextBox";
import Modal from "../../react/Modal";
import Popover from "../../react/Popover";
-import { AnchorPoint, EventDraft } from "./selection";
+import { AnchorPoint, EventDraft, narrowAnchorRect } from "./selection";
/**
* The ghost: the form an event is decided in before its note exists. It asks for the one thing the
@@ -170,6 +170,9 @@ export default function GhostPopover({ draft, anchor, container, onCommit, onCan
* to the bare point where no shading is on the grid at all. Read afresh on every reposition (see
* Popover), so the ghost follows the shading through scrolls and redraws.
*
+ * A piece too wide to be stood beside — a range dragged across a whole week — is narrowed to the
+ * end of the drag within it (see {@link narrowAnchorRect}).
+ *
* The pieces answer to the name the calendar gives them through `highlightClass`, FullCalendar
* having no class of its own left to go by (see the calendar view).
*/
@@ -187,7 +190,7 @@ function ghostAnchorRect(container: HTMLElement | null, point: AnchorPoint | nul
}
}
}
- return (pick ?? highlights[highlights.length - 1]).getBoundingClientRect();
+ return narrowAnchorRect((pick ?? highlights[highlights.length - 1]).getBoundingClientRect(), point);
}
return new DOMRect(point?.x ?? 0, point?.y ?? 0, 0, 0);
diff --git a/apps/client/src/widgets/collections/calendar/selection.spec.ts b/apps/client/src/widgets/collections/calendar/selection.spec.ts
new file mode 100644
index 0000000000..47f1692ed0
--- /dev/null
+++ b/apps/client/src/widgets/collections/calendar/selection.spec.ts
@@ -0,0 +1,40 @@
+import { afterEach, describe, expect, it } from "vitest";
+
+import { narrowAnchorRect } from "./selection";
+
+describe("narrowAnchorRect", () => {
+ afterEach(() => {
+ window.glob.isRtl = false;
+ });
+
+ /** A chip of a day or two: narrow enough for a card to stand beside it as it is. */
+ const chip = new DOMRect(700, 200, 150, 20);
+ /** An event running the length of the week, drawn the width of a grid that fills the window. */
+ const span = new DOMRect(20, 200, 1560, 20);
+
+ it("leaves a piece that can be stood beside as it stands", () => {
+ expect(narrowAnchorRect(chip, { x: 720, y: 205 })).toBe(chip);
+ // Right up to the width of the widest card, which still leaves the grid's own room.
+ expect(narrowAnchorRect(new DOMRect(700, 200, 380, 20), { x: 720, y: 205 })).toEqual(new DOMRect(700, 200, 380, 20));
+ });
+
+ it("narrows a piece too wide to be stood beside to the press within it", () => {
+ // The card lands beside the press rather than at the far end of the month, and has the
+ // grid's own room on either side of it — which is what keeps it on the screen at all.
+ expect(narrowAnchorRect(span, { x: 540, y: 205 })).toEqual(new DOMRect(540, 200, 0, 20));
+ });
+
+ it("keeps the press within the piece it belongs to", () => {
+ // A press that named a piece other than the one drawn — the popover re-anchored to the
+ // first chip of an event crossing rows, say — would otherwise carry the card off it.
+ expect(narrowAnchorRect(span, { x: 5, y: 205 })).toEqual(new DOMRect(20, 200, 0, 20));
+ expect(narrowAnchorRect(span, { x: 3000, y: 205 })).toEqual(new DOMRect(1580, 200, 0, 20));
+ });
+
+ it("stands at the start of a span nothing pressed on, whichever way the app reads", () => {
+ expect(narrowAnchorRect(span, null)).toEqual(new DOMRect(20, 200, 0, 20));
+
+ window.glob.isRtl = true;
+ expect(narrowAnchorRect(span, null)).toEqual(new DOMRect(1580, 200, 0, 20));
+ });
+});
diff --git a/apps/client/src/widgets/collections/calendar/selection.ts b/apps/client/src/widgets/collections/calendar/selection.ts
index 9e58f1cd9e..149f84496f 100644
--- a/apps/client/src/widgets/collections/calendar/selection.ts
+++ b/apps/client/src/widgets/collections/calendar/selection.ts
@@ -29,3 +29,38 @@ export interface AnchorPoint {
export type CalendarSelection =
| { noteId: string; anchor: AnchorPoint | null }
| { draft: EventDraft; anchor: AnchorPoint | null };
+
+/**
+ * The piece of the grid a popover is stood beside, narrowed to what can be stood beside at all.
+ *
+ * A popover stands to one side of its anchor, which asks that there be a side. An event running the
+ * length of a week — or a range dragged across one — is drawn as a piece as wide as the grid, and
+ * the far edge of that is nowhere near the press that opened the popover: the card lands at the
+ * other end of the month, which is how a popover comes to appear over the note tree. Where the grid
+ * is as wide as the window, it lands nowhere at all — neither side has the room for a card, and
+ * Popper keeps a popover within the viewport only along the axis it was placed on (the vertical
+ * one, for a card standing to the left or the right; see Popover), so the card is put outside the
+ * screen entirely and only its shadow is ever seen.
+ *
+ * So a piece wider than the card that stands beside it is narrowed to the press within it: the
+ * popover appears where the eye already is, with the grid's own room on either side of it. Where
+ * nothing named a point — a popover re-anchored rather than pressed for (see followLink) — the
+ * piece is narrowed to the edge the card stands away from, which is the start of the span.
+ */
+export function narrowAnchorRect(rect: DOMRect, point: AnchorPoint | null): DOMRect {
+ if (rect.width <= MAX_ANCHOR_WIDTH) {
+ return rect;
+ }
+
+ const x = point
+ ? Math.min(Math.max(point.x, rect.left), rect.right)
+ : (glob.isRtl ? rect.right : rect.left);
+
+ // The row's own height is kept: the card is aligned with the piece it belongs to rather than
+ // with the height of the press, which is where the eye is looking either way.
+ return new DOMRect(x, rect.top, 0, rect.height);
+}
+
+/** How wide a piece may be and still be stood beside: the width of the widest card the calendar
+ * anchors — the event popover's 380px, the ghost's own being narrower (see their CSS). */
+const MAX_ANCHOR_WIDTH = 380;
diff --git a/apps/client/src/widgets/react/Popover.tsx b/apps/client/src/widgets/react/Popover.tsx
index 2d1eb030ac..56cd6600c0 100644
--- a/apps/client/src/widgets/react/Popover.tsx
+++ b/apps/client/src/widgets/react/Popover.tsx
@@ -67,7 +67,15 @@ export default function Popover({ getAnchorRect, placement, updateKey, className
modifiers: [
// Room for the arrow to stand in, and a little air past its tip.
{ name: "offset", options: { offset: [ 0, 10 ] } },
- { name: "preventOverflow", options: { padding: 8 } },
+ // Held within the viewport on both axes. Popper keeps a popover inside it along the
+ // axis it was placed on and no further — a card standing to the left or the right
+ // is kept from running off the top and the bottom, and is left wherever the sides
+ // put it however far outside the window that is. An anchor with the room for a card
+ // on neither side (a calendar chip as wide as the grid, say) has no side to be
+ // placed on, so without this the card is put off the screen entirely and only its
+ // shadow is ever seen. Placement is still the first say — this is what is left when
+ // no placement fits.
+ { name: "preventOverflow", options: { padding: 8, altAxis: true } },
// Kept clear of the panel's rounded corners, where an arrow would grow out of thin
// air; an anchor so near a corner that it cannot be pointed at squarely gets the
// arrow as close as the padding allows.