Skip to content

Build your personal knowledge base with Trilium Notes

fix(text): keep the mention panel within a phone's screen

Elian Dorana7eb53673925
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.css
index 996d6538ff..74c91cdf37 100644
--- a/apps/client/src/stylesheets/style.css
+++ b/apps/client/src/stylesheets/style.css
@@ -1894,6 +1894,15 @@ body.desktop .dropdown-submenu > .dropdown-menu {
white-space: normal !important;
}
+/*
+ * The panel stays within the viewport: twice `VIEWPORT_MARGIN` in `trilium_mention_ui.ts` narrower
+ * than it, and no taller than the space `TriliumMentionUI` measures beside the caret.
+ */
+.ck.ck-mention-balloon .ck-mentions {
+ max-width: calc(100vw - 2 * 8px);
+ max-height: min(var(--ck-mention-list-max-height), var(--tn-mention-visible-height, 100vh));
+}
+
/*
* A note suggestion is an icon beside a note path. Laid out as a row so the two are centred against
* each other: left inline, the icon aligns on the text baseline, and `vertical-align: middle` only
diff --git a/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.spec.ts b/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.spec.ts
index 0e32186981..7cadba3dd7 100644
--- a/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.spec.ts
+++ b/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.spec.ts
@@ -7,6 +7,7 @@ import {
MentionEditing,
type MentionFeedObjectItem,
Paragraph,
+ Rect,
_setModelData as setModelData,
View
} from "ckeditor5";
@@ -14,7 +15,7 @@ import { beforeEach, describe, expect, it, vi } from "vitest";
import { createTestEditor } from "../../../test/editor-kit.js";
import type { TriliumMentionFeed } from "./types.js";
-import TriliumMentionUI from "./trilium_mention_ui.js";
+import TriliumMentionUI, { balloonPositions } from "./trilium_mention_ui.js";
/** Longer than the plugin's 100 ms feed debounce. */
const AFTER_DEBOUNCE = 160;
@@ -167,6 +168,17 @@ describe("TriliumMentionUI", () => {
expect(isPanelVisible()).toBe(true);
});
+ it("caps the list's height to the room the visible viewport leaves beside the caret", async () => {
+ type("#al");
+ await settle();
+
+ const list = document.querySelector<HTMLElement>(".ck-mention-balloon .ck-mentions");
+ expect(list).not.toBe(null);
+ const height = parseFloat(list?.style.getPropertyValue("--tn-mention-visible-height") ?? "");
+ expect(height).toBeGreaterThan(0);
+ expect(height).toBeLessThan(window.visualViewport?.height ?? window.innerHeight);
+ });
+
it("positions the panel from the right in a right-to-left UI", async () => {
await createEditor({}, { language: { ui: "ar" } });
setModelData(editor.model, "<paragraph>[]</paragraph>");
@@ -563,3 +575,35 @@ describe("TriliumMentionUI", () => {
expect(editor.plugins.get(ContextualBalloon).visibleView?.element?.textContent).toContain("label:#alpha");
});
});
+
+describe("balloonPositions", () => {
+ const rect = (left: number, top: number, width: number, height: number) =>
+ new Rect({ left, top, width, height, right: left + width, bottom: top + height });
+ const viewport = rect(0, 0, 400, 800);
+ const balloon = rect(0, 0, 300, 200);
+
+ function place(caret: Rect, ...args: Parameters<typeof balloonPositions>) {
+ return balloonPositions(...args).map((position) => position(caret, balloon, viewport));
+ }
+
+ it("slides each corner sideways to keep the panel within the viewport", () => {
+ // A caret near the right edge: opening rightward from it would run off the screen.
+ const [ se, sw ] = place(rect(350, 100, 1, 20), undefined, "ltr");
+ expect(se).toMatchObject({ name: "caret_se", top: 123, left: 400 - 8 - 300 });
+ expect(sw).toMatchObject({ name: "caret_sw", left: 51 });
+
+ // Near the left edge, opening leftward stops at the margin.
+ const [ , leftward ] = place(rect(20, 100, 1, 20), undefined, "ltr");
+ expect(leftward).toMatchObject({ name: "caret_sw", left: 8 });
+ });
+
+ it("offers the corners above first where asked, and the matched one first of all, the others after it", () => {
+ const caret = rect(100, 400, 1, 20);
+ const names = (...args: Parameters<typeof balloonPositions>) => place(caret, ...args).map((position) => position?.name);
+
+ expect(names(undefined, "ltr")).toEqual([ "caret_se", "caret_sw", "caret_ne", "caret_nw" ]);
+ expect(names(undefined, "rtl")).toEqual([ "caret_sw", "caret_se", "caret_nw", "caret_ne" ]);
+ expect(names(undefined, "ltr", true)).toEqual([ "caret_ne", "caret_nw", "caret_se", "caret_sw" ]);
+ expect(names("caret_nw", "ltr")).toEqual([ "caret_nw", "caret_se", "caret_sw", "caret_ne" ]);
+ });
+});
diff --git a/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.ts b/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.ts
index b80d1ee039..69bfec26ff 100644
--- a/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.ts
+++ b/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.ts
@@ -23,6 +23,10 @@ import { createMarkerPattern, findMarkerMatch, type MarkerMatch } from "./marker
import type { TriliumMentionFeed } from "./types.js";
const VERTICAL_SPACING = 3;
+/** The least gap kept between the panel and an edge of the visible viewport, which `style.css` matches. */
+const VIEWPORT_MARGIN = 8;
+/** Below this the space under the caret is unusable, and the panel opens above it where there is more room. */
+const MIN_LIST_HEIGHT = 150;
const MARKER_NAME = "mention";
const FEED_DEBOUNCE_MS = 100;
const DEFAULT_DROPDOWN_LIMIT = 10;
@@ -314,15 +318,22 @@ export default class TriliumMentionUI extends Plugin {
// throws `contextualballoon-add-view-exist` for a view that is already registered in any
// stack, visible or not. Upstream's `MentionUI` branches on visibility here and so crashes
// as soon as another plugin's balloon (added with `singleViewMode`) buries the panel.
+ // Rendered ahead of `ContextualBalloon.add()`, which would otherwise render it, so the height
+ // is capped before the first placement measures the panel.
+ if (!this._view.isRendered) {
+ this._view.render();
+ }
+ const preferAbove = this._fitToVisibleViewport(marker);
+
if (!this._balloon.hasView(this._view)) {
this._balloon.add({
view: this._view,
- position: this._positionData(marker),
+ position: this._positionData(marker, preferAbove),
singleViewMode: true,
balloonClassName: "ck-mention-balloon"
});
} else if (this._isVisible) {
- this._balloon.updatePosition(this._positionData(marker));
+ this._balloon.updatePosition(this._positionData(marker, preferAbove));
}
this._view.position = this._balloon.view.position;
@@ -425,32 +436,55 @@ export default class TriliumMentionUI extends Plugin {
return button;
}
- private _positionData(marker: Marker): Partial<DomOptimalPositionOptions> {
+ /**
+ * Caps the list's height to the larger of the spaces above and below the caret in the visible
+ * viewport, which on a phone excludes the on-screen keyboard. Returns whether the panel opens above
+ * the caret.
+ */
+ private _fitToVisibleViewport(marker: Marker): boolean {
+ const caret = this._caretRect(marker);
+ const viewport = window.visualViewport;
+ const visibleTop = viewport?.offsetTop ?? 0;
+ const visibleBottom = viewport ? viewport.offsetTop + viewport.height : window.innerHeight;
+
+ const below = visibleBottom - caret.bottom - VERTICAL_SPACING - VIEWPORT_MARGIN;
+ const above = caret.top - visibleTop - VERTICAL_SPACING - VIEWPORT_MARGIN;
+ const preferAbove = below < MIN_LIST_HEIGHT && above > below;
+
+ this._view.element?.style.setProperty("--tn-mention-visible-height", `${Math.max(preferAbove ? above : below, 0)}px`);
+ return preferAbove;
+ }
+
+ /** The rect the panel is placed against: the end of the marker, or of the selection once it is gone. */
+ private _caretRect(marker: Marker): Rect {
const editing = this.editor.editing;
+ let range = marker.getRange();
- return {
- target: () => {
- let range = marker.getRange();
+ // The marker may already be gone; fall back to the selection so ContextualBalloon
+ // can still place a panel here.
+ if (range.start.root.rootName === "$graveyard") {
+ /* v8 ignore next -- the document selection always holds at least one range, so the `?? range` arm never runs */
+ range = this.editor.model.document.selection.getFirstRange() ?? range;
+ }
- // The marker may already be gone; fall back to the selection so ContextualBalloon
- // can still place a panel here.
- if (range.start.root.rootName === "$graveyard") {
- /* v8 ignore next -- the document selection always holds at least one range, so the `?? range` arm never runs */
- range = this.editor.model.document.selection.getFirstRange() ?? range;
- }
+ const viewRange = editing.mapper.toViewRange(range);
+ const rects = Rect.getDomRangeRects(editing.view.domConverter.viewRangeToDom(viewRange));
- const viewRange = editing.mapper.toViewRange(range);
- const rects = Rect.getDomRangeRects(editing.view.domConverter.viewRangeToDom(viewRange));
+ return rects[rects.length - 1];
+ }
- return rects[rects.length - 1];
- },
+ private _positionData(marker: Marker, preferAbove: boolean): Partial<DomOptimalPositionOptions> {
+ const editing = this.editor.editing;
+
+ return {
+ target: () => this._caretRect(marker),
limiter: () => {
const editable = editing.view.document.selection.editableElement;
/* v8 ignore next -- the editing view selection is inside the root editable whenever the panel is positioned, so the `null` arm only satisfies the limiter's return type */
return editable ? editing.view.domConverter.mapViewToDom(editable.root) as HTMLElement : null;
},
- positions: balloonPositions(this._view.position, this.editor.locale.uiLanguageDirection)
+ positions: balloonPositions(this._view.position, this.editor.locale.uiLanguageDirection, preferAbove)
};
}
}
@@ -478,42 +512,41 @@ function isBeforeExistingMention(markerPosition: ModelPosition): boolean {
/**
* Balloon placement callbacks, anchored to the caret. Lifted from upstream's
- * `getBalloonPanelPositions()`, which is module-private.
+ * `getBalloonPanelPositions()`, which is module-private, with each corner slid sideways to stay
+ * within the viewport.
+ *
+ * The position that matched last comes first, so the panel does not jump as the list grows, but the
+ * others follow it, so a panel that no longer fits there can move. `preferAbove` puts the corners
+ * above the caret first.
+ *
+ * Exported for its own tests.
*/
-function balloonPositions(preferred: string | undefined, uiLanguageDirection: string): DomOptimalPositionOptions["positions"] {
+export function balloonPositions(preferred: string | undefined, uiLanguageDirection: string, preferAbove = false): DomOptimalPositionOptions["positions"] {
+ const below = (target: Rect) => target.bottom + VERTICAL_SPACING;
+ const above = (target: Rect, balloon: Rect) => target.top - balloon.height - VERTICAL_SPACING;
+ const fromCaret = (target: Rect) => target.right;
+ const toCaret = (target: Rect, balloon: Rect) => target.right - balloon.width;
+
const positions: Record<string, DomOptimalPositionOptions["positions"][0]> = {
- caret_se: (target) => ({
- top: target.bottom + VERTICAL_SPACING,
- left: target.right,
- name: "caret_se",
- config: { withArrow: false }
- }),
- caret_ne: (target, balloon) => ({
- top: target.top - balloon.height - VERTICAL_SPACING,
- left: target.right,
- name: "caret_ne",
- config: { withArrow: false }
- }),
- caret_sw: (target, balloon) => ({
- top: target.bottom + VERTICAL_SPACING,
- left: target.right - balloon.width,
- name: "caret_sw",
- config: { withArrow: false }
- }),
- caret_nw: (target, balloon) => ({
- top: target.top - balloon.height - VERTICAL_SPACING,
- left: target.right - balloon.width,
- name: "caret_nw",
- config: { withArrow: false }
- })
+ caret_se: (target, balloon, viewport) => corner("caret_se", below(target), fromCaret(target), balloon, viewport),
+ caret_ne: (target, balloon, viewport) => corner("caret_ne", above(target, balloon), fromCaret(target), balloon, viewport),
+ caret_sw: (target, balloon, viewport) => corner("caret_sw", below(target), toCaret(target, balloon), balloon, viewport),
+ caret_nw: (target, balloon, viewport) => corner("caret_nw", above(target, balloon), toCaret(target, balloon), balloon, viewport)
};
- // Stick to the position that already matched, so the panel does not jump as the list grows.
- if (preferred && preferred in positions) {
- return [ positions[preferred] ];
+ const south = uiLanguageDirection !== "rtl" ? [ "caret_se", "caret_sw" ] : [ "caret_sw", "caret_se" ];
+ const north = uiLanguageDirection !== "rtl" ? [ "caret_ne", "caret_nw" ] : [ "caret_nw", "caret_ne" ];
+ const order = preferAbove ? [ ...north, ...south ] : [ ...south, ...north ];
+ if (preferred && order.includes(preferred)) {
+ order.splice(order.indexOf(preferred), 1);
+ order.unshift(preferred);
}
- return uiLanguageDirection !== "rtl"
- ? [ positions.caret_se, positions.caret_sw, positions.caret_ne, positions.caret_nw ]
- : [ positions.caret_sw, positions.caret_se, positions.caret_nw, positions.caret_ne ];
+ return order.map((name) => positions[name]);
+}
+
+function corner(name: string, top: number, left: number, balloon: Rect, viewport: Rect) {
+ const fittedLeft = Math.max(viewport.left + VIEWPORT_MARGIN, Math.min(left, viewport.right - VIEWPORT_MARGIN - balloon.width));
+
+ return { top, left: fittedLeft, name, config: { withArrow: false } };
}