Skip to content

Build your personal knowledge base with Trilium Notes

feat(ckeditor): rule off the blocks in the AI quick actions menu

Elian Doran15f5be34b81b
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 aa51ceaf32..26da7c1888 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
@@ -30,7 +30,8 @@ interface QuickActionsDropdown {
class?: string;
buttonView: SplitButtonView;
menuView: {
- items: Array<{ childView: MenuButtonView | NestedMenuView }>;
+ /** A separator is a plain view: no `childView`, which is how the walker tells it apart. */
+ items: Array<{ childView?: MenuButtonView | NestedMenuView }>;
buttons: MenuButtonView[];
};
}
@@ -60,6 +61,15 @@ const QUICK_ACTIONS: AiQuickActionGroup[] = [
{ id: "direct", label: "Direct", commandLabel: "Make it direct", prompt: "Make it direct.", iconClass: "bx bx-target-lock" },
{ id: "friendly", label: "Friendly", prompt: "Make it friendly." }
]
+ },
+ {
+ id: "translate",
+ label: "Translate",
+ submenu: true,
+ iconClass: "bx bx-globe",
+ actions: [
+ { id: "romanian", label: "Romanian", commandLabel: "Translate to Romanian", prompt: "Translate it." }
+ ]
}
];
@@ -100,12 +110,17 @@ function openMenu(dropdown: QuickActionsDropdown): QuickActionsDropdown["menuVie
* label plus the labels it holds.
*/
function menuEntries(dropdown: QuickActionsDropdown): Array<string | { menu: string; actions: string[] }> {
- return openMenu(dropdown).items.map(({ childView }) => ("listView" in childView
- ? {
- menu: childView.buttonView.label ?? "",
- actions: childView.listView.items.map((item) => item.childView.label ?? "")
+ return openMenu(dropdown).items.map(({ childView }) => {
+ if (!childView) {
+ return "---";
}
- : childView.label ?? ""));
+ return "listView" in childView
+ ? {
+ menu: childView.buttonView.label ?? "",
+ actions: childView.listView.items.map((item) => item.childView.label ?? "")
+ }
+ : childView.label ?? "";
+ });
}
/** Every action button, submenus included, in definition order. */
@@ -170,11 +185,22 @@ describe("AiAssistantUI toolbar entry", () => {
expect(menuEntries(dropdown)).toEqual([
"Fix typos",
"Make shorter",
+ "---",
"Write something",
- { menu: "Change tone", actions: ["Direct", "Friendly"] }
+ "---",
+ { menu: "Change tone", actions: ["Direct", "Friendly"] },
+ { menu: "Translate", actions: ["Romanian"] }
]);
});
+ // A rule between blocks, and none between two openers: without the headings the menu
+ // definition cannot carry, the separator is what marks where a set of actions ends.
+ it("rules off each block but not between adjacent submenus", () => {
+ const entries = menuEntries(dropdown);
+ expect(entries.filter((entry) => entry === "---")).toHaveLength(2);
+ expect(entries.at(-1)).toEqual({ menu: "Translate", actions: ["Romanian"] });
+ });
+
// The menu definition has no icon field, so the glyphs are hung on the views CKEditor
// built — on an item inside a submenu as much as on an inlined one.
it("renders an icon-pack glyph for the actions and submenus that have one", () => {
@@ -184,7 +210,7 @@ describe("AiAssistantUI toolbar entry", () => {
const [tone] = openMenu(dropdown).items
.map(({ childView }) => childView)
- .filter((childView): childView is NestedMenuView => "listView" in childView);
+ .filter((childView): childView is NestedMenuView => !!childView && "listView" in childView);
expect(tone.buttonView.element?.querySelector(".ck-ai-action-icon")?.className)
.toBe("ck-ai-action-icon bx bx-palette");
});
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 c30bc55c78..1af2a16f1b 100644
--- a/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.ts
+++ b/packages/ckeditor5/src/plugins/ai_assistant/ai_assistant_ui.ts
@@ -5,6 +5,7 @@ import {
createDropdown,
Dialog,
DialogViewPosition,
+ ListSeparatorView,
Plugin,
SplitButtonView,
View,
@@ -153,7 +154,12 @@ export default class AiAssistantUI extends Plugin {
const actionsById = new Map<string, AiQuickAction>();
const groupsById = new Map<string, AiQuickActionGroup>();
const definition: DropdownMenuDefinition = [];
- for (const group of groups) {
+ // Where the menu wants a rule drawn, as indices into the finished item list. An inlined
+ // group lost its heading to the menu definition, so a separator is what is left to say
+ // where one set of actions ends and the next begins. Consecutive submenus need none: they
+ // read as one block of "openers" already.
+ const separatorAt: number[] = [];
+ for (const [index, group] of groups.entries()) {
groupsById.set(group.id, group);
const children = group.actions.map((action) => {
actionsById.set(action.id, action);
@@ -164,6 +170,11 @@ export default class AiAssistantUI extends Plugin {
} else {
definition.push(...children);
}
+
+ const next = groups[index + 1];
+ if (next && !(group.submenu && next.submenu)) {
+ separatorAt.push(definition.length);
+ }
}
addMenuToDropdown(dropdownView, this.editor.ui.view.body, definition, {
ariaLabel: locale.t("AI assistant")
@@ -185,6 +196,11 @@ export default class AiAssistantUI extends Plugin {
for (const menu of dropdownView.menuView?.menus ?? []) {
addIcon(menu.buttonView, groupsById.get(menu.id)?.iconClass);
}
+ // Back to front: each insertion shifts everything after it, and the recorded indices
+ // are into the list as the definition built it.
+ for (const index of [...separatorAt].reverse()) {
+ dropdownView.menuView?.items.add(new ListSeparatorView(locale), index);
+ }
this.stopListening(dropdownView, "change:isOpen", decorateMenu);
};
this.listenTo(dropdownView, "change:isOpen", decorateMenu);