Skip to content

Build your personal knowledge base with Trilium Notes

fix(codemirror): measure the caret before a hosted list is shown

Elian Doran928e8ea76dd3
diff --git a/packages/codemirror/src/extensions/hosted_completion.spec.ts b/packages/codemirror/src/extensions/hosted_completion.spec.ts
index 82be7dcfab..2db82922d1 100644
--- a/packages/codemirror/src/extensions/hosted_completion.spec.ts
+++ b/packages/codemirror/src/extensions/hosted_completion.spec.ts
@@ -190,9 +190,11 @@ describe("hostedCompletion", () => {
});
it("places the list at the caret, or at the editor where the caret has no place on screen", async () => {
- const { shown } = build();
+ const { shown, placed } = build();
type("@");
await vi.waitFor(() => expect(shown()).toBeDefined());
+ // Read as CodeMirror shows the list, when it allows no layout reads.
+ expect(placed()).toBeInstanceOf(DOMRect);
const view = editor;
if (!view) throw new Error("expected an editor");
@@ -208,8 +210,17 @@ describe("hostedCompletion", () => {
function build(config: Partial<FieldEditorConfig> = {}) {
let element: HTMLElement | null = null;
let state: HostedCompletionState<TestMatch> | undefined;
+ let placed: unknown;
const list = {
- show: vi.fn((shown: HostedCompletionState<TestMatch>) => { state = shown; }),
+ // Places itself as it is shown, as a popup does.
+ show: vi.fn((shown: HostedCompletionState<TestMatch>) => {
+ state = shown;
+ try {
+ placed = shown.caretRect();
+ } catch (e) {
+ placed = e;
+ }
+ }),
hide: vi.fn(),
handleKeyDown: vi.fn(() => false),
get element() { return element; },
@@ -229,6 +240,7 @@ function build(config: Partial<FieldEditorConfig> = {}) {
return {
list,
shown: () => state,
+ placed: () => placed,
setElement: (el: HTMLElement | null) => { element = el; }
};
}
diff --git a/packages/codemirror/src/extensions/hosted_completion.ts b/packages/codemirror/src/extensions/hosted_completion.ts
index 3c7d8bec72..5dfe819132 100644
--- a/packages/codemirror/src/extensions/hosted_completion.ts
+++ b/packages/codemirror/src/extensions/hosted_completion.ts
@@ -131,20 +131,22 @@ export function hostedCompletion<M extends HostedCompletionMatch>({ match, list
this.from = found.from;
this.before = found.before;
- // Shown once the update is over: the list reads the caret's place, which CodeMirror does
- // not allow during one.
+ // Shown in a measure, as the list reads the caret's place, which CodeMirror allows only in
+ // `read`. While `show` runs, `caretRect()` answers what `read` measured.
this.view.requestMeasure({
- read: () => null,
- write: () => {
+ read: () => caretRect(this.view),
+ write: (measured) => {
// A later update has replaced or closed the list meanwhile.
if (this.from !== found.from || this.before !== found.before) return;
+ let showing = true;
this.list.show({
match: found.match,
- caretRect: () => caretRect(this.view),
+ caretRect: () => (showing ? measured : caretRect(this.view)),
editable: this.view.dom,
commit: (text) => this.commit(found.from, text)
});
+ showing = false;
}
});
}