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(text): keep the mention popup at the caret as the note scrolls
Elian Dorand552dc68648f
diff --git a/apps/client/src/widgets/type_widgets/text/mention_list_view.spec.tsx b/apps/client/src/widgets/type_widgets/text/mention_list_view.spec.tsxindex 5691337f72..ba625dc241 100644--- a/apps/client/src/widgets/type_widgets/text/mention_list_view.spec.tsx+++ b/apps/client/src/widgets/type_widgets/text/mention_list_view.spec.tsx@@ -27,6 +27,7 @@ describe("createMentionListView", () => {className: "ck-mention-list",selectedIndex: 0,caretRect: () => new DOMRect(10, 10, 1, 16),+ editable: null,select: vi.fn(),pick: vi.fn(),...overrides@@ -81,6 +82,29 @@ describe("createMentionListView", () => {expect(scroll.mock.contexts[0]).toBe(rows()[2]);});+ it("follows the caret as a container around the editor scrolls", async () => {+ const scroller = document.createElement("div");+ scroller.style.overflow = "auto";+ const editable = document.createElement("div");+ scroller.append(editable);+ document.body.append(scroller);+ let caretTop = 100;+ // Floating UI reads the viewport and the popup's size, which happy-dom does not lay out.+ vi.spyOn(document.documentElement, "clientWidth", "get").mockReturnValue(1000);+ vi.spyOn(document.documentElement, "clientHeight", "get").mockReturnValue(800);+ vi.spyOn(HTMLElement.prototype, "offsetWidth", "get").mockReturnValue(200);+ vi.spyOn(HTMLElement.prototype, "offsetHeight", "get").mockReturnValue(100);++ await show(stateWith({ editable, caretRect: () => new DOMRect(10, caretTop, 1, 16) }));+ const menu = document.querySelector<HTMLElement>(".mention-list-menu");+ await vi.waitFor(() => expect(menu?.style.top).toBe("116px"));++ caretTop = 40;+ scroller.dispatchEvent(new Event("scroll"));+ await vi.waitFor(() => expect(menu?.style.top).toBe("56px"));+ scroller.remove();+ });+it("reports the row the pointer moves onto and the one clicked, not a row under a still pointer", async () => {const state = stateWith();await show(state);diff --git a/apps/client/src/widgets/type_widgets/text/mention_list_view.tsx b/apps/client/src/widgets/type_widgets/text/mention_list_view.tsxindex 265b80b149..84545759b1 100644--- a/apps/client/src/widgets/type_widgets/text/mention_list_view.tsx+++ b/apps/client/src/widgets/type_widgets/text/mention_list_view.tsx@@ -74,7 +74,8 @@ function MentionMenu({ state, elementRef, scrollsToSelection, onPointerSelect }:return (<Popup// A new anchor each time the list changes, so it is placed again at the caret as it moves.- anchor={{ getBoundingClientRect: state.caretRect }}+ // Its `contextElement` places it again as the containers around the editor scroll.+ anchor={{ getBoundingClientRect: state.caretRect, contextElement: state.editable ?? undefined }}elementRef={elementRef}// The pointer moves the highlighted row, so `:hover` marks no second one.className={clsx("dropdown-menu show tn-dropdown-menu tn-menu-keyboard note-autocomplete-menu mention-list-menu", state.className)}diff --git a/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.spec.ts b/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.spec.tsindex db643f3600..a4e2b8f5bb 100644--- a/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.spec.ts+++ b/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.spec.ts@@ -613,6 +613,7 @@ describe("TriliumMentionUI", () => {expect(shown?.entries[0].marker).toBe("#");expect(shown?.entries[0].render()).toBe("rendered #alpha");expect(shown?.caretRect().height).toBeGreaterThan(0);+ expect(shown?.editable).toBe(editor.editing.view.getDomRoot());});it("moves the selection with the arrow keys, wrapping, and commits it on Enter", async () => {diff --git a/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.ts b/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.tsindex 29ba40efa4..3920e62efa 100644--- a/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.ts+++ b/packages/ckeditor5/src/plugins/mention/trilium_mention_ui.ts@@ -481,6 +481,7 @@ export default class TriliumMentionUI extends Plugin {const { left, top, width, height } = this._caretRect(marker);return new DOMRect(left, top, width, height);},+ editable: this.editor.editing.view.getDomRoot() ?? null,select: (index) => {this._selectedIndex = index;this._drawList(marker);diff --git a/packages/ckeditor5/src/plugins/mention/types.ts b/packages/ckeditor5/src/plugins/mention/types.tsindex 3d8d46e30b..0941cd7639 100644--- a/packages/ckeditor5/src/plugins/mention/types.ts+++ b/packages/ckeditor5/src/plugins/mention/types.ts@@ -91,6 +91,11 @@ export interface MentionListState {selectedIndex: number;/** Where the caret ends the query, in viewport coordinates. */caretRect(): DOMRect;+ /**+ * The editable element the caret is in. A view anchored at {@link caretRect} watches the+ * containers around it, so the list follows the caret as they scroll.+ */+ editable: HTMLElement | null;/** Highlights an entry, as the pointer moving over it does. */select( index: number ): void;/** Commits an entry, as a click on it does. */