Skip to content

Build your personal knowledge base with Trilium Notes

fix(ai-assistant): give the selection back when the assistant is dismissed

Elian Doran19aa41c4358f
diff --git a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.spec.ts b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.spec.ts
index d98ba91446..4a29063d28 100644
--- a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.spec.ts
+++ b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.spec.ts
@@ -1,4 +1,4 @@
-import { _setModelData as setModelData, ButtonView, ClassicEditor, Dialog, Essentials, Paragraph, SplitButtonView } from "ckeditor5";
+import { _getModelData as getModelData, _setModelData as setModelData, ButtonView, ClassicEditor, Dialog, Essentials, Paragraph, SplitButtonView } from "ckeditor5";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createTestEditor } from "../../../test/editor-kit.js";
@@ -22,7 +22,8 @@ interface NestedMenuView {
id: string;
isEnabled: boolean;
buttonView: { label?: string; element?: HTMLElement | null };
- listView: { items: Array<{ childView: MenuButtonView }> };
+ /** A separator is a plain view: no `childView`, the same as at the top level. */
+ listView: { items: Array<{ childView?: MenuButtonView }> };
}
interface QuickActionsDropdown {
@@ -119,7 +120,7 @@ function menuEntries(dropdown: QuickActionsDropdown): Array<string | { menu: str
return "listView" in childView
? {
menu: childView.buttonView.label ?? "",
- actions: childView.listView.items.map((item) => item.childView.label ?? "")
+ actions: childView.listView.items.map((item) => item.childView?.label ?? "---")
}
: childView.label ?? "";
});
@@ -318,6 +319,42 @@ describe("AiAssistantUI toolbar entry", () => {
expect(dialog.id).toBe("aiAssistant");
});
+ it("hands the pinned range back as the selection when dismissed", async () => {
+ setModelData(editor.model, "<paragraph>[foo]</paragraph>");
+ editor.execute("aiAssistant");
+
+ // The marker, not the selection, is what shows the target while the dialog stands.
+ expect(editor.model.markers.has("triliumAiTarget")).toBe(true);
+
+ // Whatever becomes of the selection meanwhile — the editable is blurred for the
+ // dialog's whole life, so nothing is holding it in place.
+ const start = editor.model.document.selection.getFirstPosition();
+ if (start) {
+ editor.model.change((writer) => writer.setSelection(start));
+ }
+ expect(getModelData(editor.model)).toBe("<paragraph>[]foo</paragraph>");
+
+ editor.plugins.get(Dialog).hide();
+
+ expect(editor.model.markers.has("triliumAiTarget")).toBe(false);
+ expect(getModelData(editor.model)).toBe("<paragraph>[foo]</paragraph>");
+ });
+
+ it("leaves the selection to the insertion when the response is committed", async () => {
+ setModelData(editor.model, "<paragraph>[foo]</paragraph>");
+ const [fixTypos] = quickActionButtons(dropdown);
+
+ fixTypos.fire("execute");
+ await vi.waitFor(() => expect(requests).toHaveLength(1));
+
+ openForm(editor).fire("replace");
+
+ // A commit places the selection itself; the pinned range it wrote over must not be
+ // restored over the top of that, which would leave the insertion selected.
+ expect(editor.model.markers.has("triliumAiTarget")).toBe(false);
+ expect(getModelData(editor.model)).toBe("<paragraph>[]done</paragraph>");
+ });
+
it("titles the dialog with the action it is running, as the palette names it", async () => {
setModelData(editor.model, "<paragraph>[foo]</paragraph>");
const [fixTypos, , , direct] = quickActionButtons(dropdown);
diff --git a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.ts b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.ts
index ec1f9a6b2a..2359ae4a78 100644
--- a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.ts
+++ b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.ts
@@ -17,7 +17,7 @@ import {
type ModelRange
} from "ckeditor5";
-import type { AiCompletionUsage, AiDiffResult, AiQuickAction, AiQuickActionGroup, AiReviewView } from "./ai_assistant_config.js";
+import type { AiCompletionUsage, AiDiffResult, AiQuickAction, AiQuickActionFooter, AiQuickActionGroup, AiReviewView } from "./ai_assistant_config.js";
import AiAssistantEditing, { AI_TARGET_MARKER } from "./ai_assistant_editing.js";
import AiAssistantFormView from "./ai_assistant_form.js";
import aiIcon from "./theme/icons/ai.svg?raw";
@@ -165,6 +165,7 @@ export default class AiAssistantUI extends Plugin {
// The menu carries only ids and labels, so the action each button stands for is looked up
// on execute rather than carried by the view.
const actionsById = new Map<string, AiQuickAction>();
+ const footersById = new Map<string, AiQuickActionFooter>();
const groupsById = new Map<string, AiQuickActionGroup>();
const definition: DropdownMenuDefinition = [];
// Where the menu wants a rule drawn, as indices into the finished item list. An inlined
@@ -179,6 +180,11 @@ export default class AiAssistantUI extends Plugin {
return { id: action.id, label: action.label };
});
if (group.submenu) {
+ if (group.footer) {
+ const id = `${group.id}:footer`;
+ footersById.set(id, group.footer);
+ children.push({ id, label: group.footer.label });
+ }
definition.push({ id: group.id, menu: group.label, children });
} else {
definition.push(...children);
@@ -200,6 +206,14 @@ export default class AiAssistantUI extends Plugin {
// and they are built exactly once, so this unsubscribes itself.
const decorateMenu = () => {
for (const button of dropdownView.menuView?.buttons ?? []) {
+ const footer = footersById.get(button.id);
+ if (footer) {
+ // A footer configures the group rather than running against the document, so it
+ // stays reachable when everything above it is closed off.
+ addIcon(button, footer.iconClass);
+ continue;
+ }
+
const action = actionsById.get(button.id);
if (action?.requiresContent !== false) {
button.bind("isEnabled").to(this, "hasContext");
@@ -211,10 +225,16 @@ export default class AiAssistantUI extends Plugin {
addIcon(menu.buttonView, group?.iconClass);
// A submenu with nothing runnable in it should not invite the pointer: gate the
// opener on the same context its actions are gated on, unless one of them can run
- // without any. The nested menu binds its own button to this.
- if (group?.actions.every((action) => action.requiresContent !== false)) {
+ // without any — a footer counts, since it runs whatever the selection is. The
+ // nested menu binds its own button to this.
+ if (!group?.footer && group?.actions.every((action) => action.requiresContent !== false)) {
menu.bind("isEnabled").to(this, "hasContext");
}
+ // The footer was appended as the last child; the rule telling it from the actions
+ // above goes immediately before it.
+ if (group?.footer) {
+ menu.listView.items.add(new ListSeparatorView(locale), menu.listView.items.length - 1);
+ }
}
// Back to front: each insertion shifts everything after it, and the recorded indices
// are into the list as the definition built it.
@@ -230,7 +250,14 @@ export default class AiAssistantUI extends Plugin {
// delegates up through its menu to the root — and delegation preserves the original
// source, so the button (and its id) is the same either way.
dropdownView.on("execute", (evt) => {
- const action = actionsById.get((evt.source as DropdownMenuListItemButtonView).id);
+ const id = (evt.source as DropdownMenuListItemButtonView).id;
+ const footer = footersById.get(id);
+ if (footer) {
+ footer.run();
+ return;
+ }
+
+ const action = actionsById.get(id);
/* v8 ignore next -- every button in the menu was built from an action in this map */
if (action) {
this.runQuickAction(action);
@@ -542,6 +569,13 @@ export default class AiAssistantUI extends Plugin {
if (!target) {
return;
}
+ // Retired here rather than left to the teardown, which hands the pinned range back as
+ // the selection: `insertContent` decides where the selection lands after a commit, and
+ // restoring the range it just wrote over would overrule it. Same change, so the marker
+ // goes with the insertion in a single undo step.
+ if (model.markers.has(AI_TARGET_MARKER)) {
+ writer.removeMarker(AI_TARGET_MARKER);
+ }
if (mode === "replace") {
model.insertContent(modelFragment, target);
} else {
@@ -595,8 +629,17 @@ export default class AiAssistantUI extends Plugin {
this._reviewView = null;
this._updateHasContext();
- if (editor.model.markers.has(AI_TARGET_MARKER)) {
- editor.model.change((writer) => writer.removeMarker(AI_TARGET_MARKER));
+ const marker = editor.model.markers.get(AI_TARGET_MARKER);
+ if (marker) {
+ // Hand the pinned range back as the selection. The marker is what showed the target
+ // for as long as the dialog stood — the editable was blurred throughout, so the
+ // selection was neither on show nor under anyone's control — and dropping it without
+ // this leaves whatever the selection decayed to meanwhile.
+ const range = marker.getRange();
+ editor.model.change((writer) => {
+ writer.removeMarker(AI_TARGET_MARKER);
+ writer.setSelection(range);
+ });
}
this._formView?.reset();
}