Skip to content

Build your personal knowledge base with Trilium Notes

fix(ai-assistant): put focus in the prompt when the assistant opens

Elian Doran4ad64f3715c5
diff --git a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_form.spec.ts b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_form.spec.ts
index 832a0cc5e3..9304d2e99b 100644
--- a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_form.spec.ts
+++ b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_form.spec.ts
@@ -1,5 +1,5 @@
import { Locale } from "ckeditor5";
-import { afterEach, describe, expect, it } from "vitest";
+import { afterEach, describe, expect, it, vi } from "vitest";
import AiAssistantFormView from "./ai_assistant_form.js";
@@ -167,6 +167,24 @@ describe("AiAssistantFormView", () => {
expect(previewHtml()).toBe("<p>diff</p>");
});
+ // `document.activeElement` cannot be asserted here — the headless window holds no focus, so
+ // even a direct `element.focus()` leaves it on `<body>`. Delegation is the testable part.
+ it("puts focus in the prompt whatever phase it is asked in", () => {
+ const form = makeForm();
+ const focusPrompt = vi.spyOn(form.promptInputView, "focus");
+
+ form.focus();
+ expect(focusPrompt).toHaveBeenCalledTimes(1);
+
+ // Not the first focusable in the list, and which rows are on show varies by phase, so the
+ // landing spot must not be arrived at by cycling.
+ form.beginStreaming();
+ form.setPreview("<p>new</p>");
+ form.enterReview({ hasContent: true });
+ form.focus();
+ expect(focusPrompt).toHaveBeenCalledTimes(2);
+ });
+
it("takes the prompt through a placeholder rather than a label, and sends with an icon", () => {
const form = makeForm();
diff --git a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_form.ts b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_form.ts
index 4105a20a35..b61c9e8c0a 100644
--- a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_form.ts
+++ b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_form.ts
@@ -80,7 +80,6 @@ export default class AiAssistantFormView extends View {
public readonly keystrokes = new KeystrokeHandler();
private readonly _focusables = new ViewCollection<FocusableView>();
- private readonly _focusCycler: FocusCycler;
constructor(locale: Locale) {
super(locale);
@@ -254,7 +253,9 @@ export default class AiAssistantFormView extends View {
]
});
- this._focusCycler = new FocusCycler({
+ // Kept for its wiring rather than its handle: constructing it is what binds Tab and
+ // Shift+Tab to the focusables, and nothing here drives the cycle by hand.
+ new FocusCycler({
focusables: this._focusables,
focusTracker: this.focusTracker,
keystrokeHandler: this.keystrokes,
@@ -353,8 +354,14 @@ export default class AiAssistantFormView extends View {
this._renderPreview();
}
+ /**
+ * Lands on the prompt, which is the one thing there is to do on arrival in every phase — the
+ * rest of the form is a response to read or an action to take on one. Straight to the field
+ * rather than cycling to the first focusable, so where focus goes does not depend on which
+ * rows the current phase happens to be showing.
+ */
public focus(): void {
- this._focusCycler.focusFirst();
+ this.promptInputView.focus();
}
/** Shows whichever stored content the current view mode selects. */