Skip to content

Build your personal knowledge base with Trilium Notes

fix(ckeditor): grey out an AI submenu whose actions all need content

Elian Doranfe3c6c05ecbd
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 26da7c1888..dc03d0219d 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
@@ -20,6 +20,7 @@ interface MenuButtonView {
/** A submenu entry: a group that opens rather than listing its actions inline. */
interface NestedMenuView {
id: string;
+ isEnabled: boolean;
buttonView: { label?: string; element?: HTMLElement | null };
listView: { items: Array<{ childView: MenuButtonView }> };
}
@@ -33,6 +34,7 @@ interface QuickActionsDropdown {
/** A separator is a plain view: no `childView`, which is how the walker tells it apart. */
items: Array<{ childView?: MenuButtonView | NestedMenuView }>;
buttons: MenuButtonView[];
+ menus: NestedMenuView[];
};
}
@@ -220,6 +222,16 @@ describe("AiAssistantUI toolbar entry", () => {
expect(friendly?.element?.querySelector(".ck-ai-action-icon")).toBeNull();
});
+ it("gates a submenu on the content its actions need", () => {
+ const [tone] = openMenu(dropdown).menus;
+
+ setModelData(editor.model, "<paragraph>[]</paragraph>");
+ expect(tone.isEnabled).toBe(false);
+
+ setModelData(editor.model, "<paragraph>[foo]</paragraph>");
+ expect(tone.isEnabled).toBe(true);
+ });
+
it("gates the content-requiring actions on there being something to work on", () => {
const [fixTypos, , write, direct] = quickActionButtons(dropdown);
@@ -238,6 +250,22 @@ describe("AiAssistantUI toolbar entry", () => {
expect(fixTypos.isEnabled).toBe(true);
});
+ it("leaves a submenu alone when one of its actions runs without content", async () => {
+ const editorWithFreeAction = await createEditor([{
+ id: "generate",
+ label: "Generate",
+ submenu: true,
+ actions: [
+ { id: "write", label: "Write something", prompt: "Write.", requiresContent: false },
+ { id: "rewrite", label: "Rewrite", prompt: "Rewrite." }
+ ]
+ }], createStreamStub().stream);
+ const [generate] = openMenu(createComponent(editorWithFreeAction)).menus;
+
+ setModelData(editorWithFreeAction.model, "<paragraph>[]</paragraph>");
+ expect(generate.isEnabled).toBe(true);
+ });
+
it("runs an action picked from inside a submenu", async () => {
// A submenu button delegates its `execute` up through its menu to the root rather than
// firing on the dropdown itself, so this is a different path from an inlined action.
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 1af2a16f1b..e43cab4f52 100644
--- a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.ts
+++ b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.ts
@@ -194,7 +194,14 @@ export default class AiAssistantUI extends Plugin {
addIcon(button, action?.iconClass);
}
for (const menu of dropdownView.menuView?.menus ?? []) {
- addIcon(menu.buttonView, groupsById.get(menu.id)?.iconClass);
+ const group = groupsById.get(menu.id);
+ 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)) {
+ menu.bind("isEnabled").to(this, "hasContext");
+ }
}
// Back to front: each insertion shifts everything after it, and the recorded indices
// are into the list as the definition built it.