Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor: use a dropdown menu for the "Insert" formatting toolbar button

Adorian Doran6632dc0ac690
diff --git a/apps/client/src/widgets/type_widgets/text/toolbar.ts b/apps/client/src/widgets/type_widgets/text/toolbar.ts
index fe17b6546f..0dc2dc64ec 100644
--- a/apps/client/src/widgets/type_widgets/text/toolbar.ts
+++ b/apps/client/src/widgets/type_widgets/text/toolbar.ts
@@ -204,7 +204,10 @@ function buildTextFormattingGroup() {
function buildInsertGroup() {
return {
label: t("text-editor.toolbar-groups.insert"),
- icon: "plus"
+ icon: "plus",
+ // The group holds enough entries that a strip of icons reads as a puzzle; `asMenu` (see
+ // the `ToolbarGroupMenu` plugin) opens it as a menu of icon-and-label rows instead.
+ asMenu: true
};
}
diff --git a/packages/ckeditor5/src/plugins.ts b/packages/ckeditor5/src/plugins.ts
index 2fd43a575d..15819d469d 100644
--- a/packages/ckeditor5/src/plugins.ts
+++ b/packages/ckeditor5/src/plugins.ts
@@ -57,6 +57,7 @@ import TodoListMultistate from "./plugins/todo_list_multistate/todo_list_multist
import TodoListUncheckOnEnter from "./plugins/todo_list_uncheck_on_enter.js";
import CollapsibleListItems from "./plugins/collapsible_list_items.js";
import TableIndent from "./plugins/table_indent.js";
+import ToolbarGroupMenu from "./plugins/toolbar_group_menu.js";
/**
* Plugins that are specific to Trilium and not part of the CKEditor 5 core, included in both text editors but not in the attribute editor.
@@ -92,6 +93,7 @@ const TRILIUM_PLUGINS: typeof Plugin[] = [
TodoListMultistate,
CollapsibleListItems,
TableIndent,
+ ToolbarGroupMenu,
CopyAnchorLinkButton,
CopyLinkUrlButton,
ImageActions,
diff --git a/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.ts b/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.ts
new file mode 100644
index 0000000000..84be833f7c
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.ts
@@ -0,0 +1,296 @@
+import {
+ addListToDropdown, Bold, ButtonView, type ClassicEditor, Collection, createDropdown,
+ type DropdownMenuNestedMenuView, DropdownView, Essentials, Italic,
+ type ListDropdownItemDefinition, ListSeparatorView, type Locale, Paragraph, Plugin,
+ SplitButtonView, type ToolbarView, UIModel
+} from "ckeditor5";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+
+import { createTestEditor } from "../../test/editor-kit.js";
+import ToolbarGroupMenu from "./toolbar_group_menu.js";
+
+const insertedSamples: string[] = [];
+const blankInserts = vi.fn();
+/** What the `clock` entry below writes into its list, standing in for `dateTime`'s previews. */
+let now = "";
+
+beforeEach(() => {
+ insertedSamples.length = 0;
+ blankInserts.mockClear();
+ now = "noon";
+});
+
+/**
+ * Stands in for the group's dropdown entries — `mermaid` and `dateTime` are split buttons over a
+ * list, with an action of their own on the button half.
+ */
+class SampleDropdown extends Plugin {
+
+ init() {
+ this.editor.ui.componentFactory.add("samples", (locale: Locale) => {
+ const dropdown = createDropdown(locale, SplitButtonView);
+ dropdown.buttonView.set({ label: "Insert sample", icon: "<svg />", tooltip: true });
+ dropdown.buttonView.on("execute", () => blankInserts());
+
+ const items = new Collection<ListDropdownItemDefinition>();
+ for (const name of ["Flowchart", "Sequence"]) {
+ items.add({ type: "button", model: new UIModel({ label: name, withText: true }) });
+ }
+ addListToDropdown(dropdown, items);
+ dropdown.on("execute", (evt) => {
+ insertedSamples.push((evt.source as ButtonView).label ?? "");
+ });
+
+ return dropdown;
+ });
+
+ // Like `dateTime`, which shows the moment its list was opened at.
+ this.editor.ui.componentFactory.add("clock", (locale: Locale) => {
+ const dropdown = createDropdown(locale);
+ dropdown.buttonView.set({ label: "Clock", icon: "<svg />" });
+
+ const items = new Collection<ListDropdownItemDefinition>();
+ addListToDropdown(dropdown, items);
+ dropdown.on("change:isOpen", (evt, name, isOpen) => {
+ if (isOpen) {
+ items.clear();
+ const model = new UIModel({ label: now, withText: true });
+ items.add({ type: "button", model });
+ }
+ });
+
+ return dropdown;
+ });
+
+ // A dropdown with no list of its own: nothing to open onto, so it stays a plain row.
+ this.editor.ui.componentFactory.add("panelOnly", (locale: Locale) => {
+ const dropdown = createDropdown(locale);
+ dropdown.buttonView.set({ label: "Panel only", icon: "<svg />" });
+ return dropdown;
+ });
+ }
+
+}
+
+const MENU_GROUP = {
+ label: "Insert",
+ icon: "plus",
+ asMenu: true,
+ items: ["bold", "|", "italic", "samples"]
+};
+const PLAIN_GROUP = { label: "Plain", icon: "text", items: ["bold", "italic"] };
+const PLUGINS = [Essentials, Paragraph, Bold, Italic, SampleDropdown, ToolbarGroupMenu];
+
+describe("ToolbarGroupMenu", () => {
+ it("opens a group marked with asMenu as a menu of rows, ruled as the group was", async () => {
+ const editor = await createTestEditor(PLUGINS, {
+ toolbar: { items: ["bold", MENU_GROUP, PLAIN_GROUP] }
+ });
+
+ const menu = openGroup(getToolbar(editor), "Insert");
+ expect(rowLabels(menu)).toStrictEqual(["Bold", "—", "Italic", "Insert sample"]);
+
+ // Each row carries the icon of the toolbar item it stands for, and follows its enabled
+ // state: `bold` is disabled while the editor is read-only, so its row is too.
+ const [bold] = menu.buttons;
+ expect(bold.icon).toBeTruthy();
+ expect(bold.isEnabled).toBe(true);
+ editor.enableReadOnlyMode("spec");
+ expect(bold.isEnabled).toBe(false);
+ });
+
+ it("opens an entry carrying a list of its own as a submenu over that list", async () => {
+ const editor = await createTestEditor(PLUGINS, {
+ toolbar: { items: [MENU_GROUP] }
+ });
+
+ const dropdown = openDropdown(getToolbar(editor), "Insert");
+ const submenu = openSubmenu(dropdown);
+ expect(submenu.buttonView.label).toBe("Insert sample");
+ expect(submenu.buttonView.icon).toBe("<svg />");
+ expect(itemLabels(submenu.listView.items)).toStrictEqual(["Flowchart", "Sequence"]);
+ });
+
+ it("runs the action of a split button when its submenu's opener is pressed", async () => {
+ const editor = await createTestEditor(PLUGINS, {
+ toolbar: { items: [MENU_GROUP] }
+ });
+
+ const dropdown = openDropdown(getToolbar(editor), "Insert");
+ submenuOf(dropdown).buttonView.fire("execute");
+
+ expect(blankInserts).toHaveBeenCalled();
+ expect(dropdown.isOpen).toBe(false);
+ });
+
+ it("runs the toolbar item a row stands for, in the menu and in a submenu alike", async () => {
+ const editor = await createTestEditor(PLUGINS, {
+ toolbar: { items: [MENU_GROUP] }
+ });
+
+ const dropdown = openDropdown(getToolbar(editor), "Insert");
+ const menuView = openGroup(getToolbar(editor), "Insert");
+
+ const bold = menuView.buttons.find((button) => button.label === "Bold");
+ expect(bold).toBeTruthy();
+ bold?.fire("execute");
+ expect(editor.commands.get("bold")?.value).toBe(true);
+
+ submenuRow(openSubmenu(dropdown), "Sequence").fire("execute");
+ expect(insertedSamples).toStrictEqual(["Sequence"]);
+ });
+
+ it("draws a submenu on each open, so an entry filling its own list stays fresh", async () => {
+ const editor = await createTestEditor(PLUGINS, {
+ toolbar: { items: [{ ...MENU_GROUP, items: ["clock"] }] }
+ });
+
+ const dropdown = openDropdown(getToolbar(editor), "Insert");
+ expect(itemLabels(openSubmenu(dropdown).listView.items)).toStrictEqual(["noon"]);
+
+ // Reopening the submenu alone is enough: it is what the reader waits in front of.
+ now = "half past";
+ submenuOf(dropdown).isOpen = false;
+ expect(itemLabels(openSubmenu(dropdown).listView.items)).toStrictEqual(["half past"]);
+
+ now = "one";
+ dropdown.isOpen = false;
+ dropdown.isOpen = true;
+ expect(itemLabels(openSubmenu(dropdown).listView.items)).toStrictEqual(["one"]);
+ });
+
+ it("only opens the submenu of an entry that has no action of its own", async () => {
+ const editor = await createTestEditor(PLUGINS, {
+ toolbar: { items: [{ ...MENU_GROUP, items: ["clock"] }] }
+ });
+
+ const dropdown = openDropdown(getToolbar(editor), "Insert");
+ const submenu = submenuOf(dropdown);
+ submenu.buttonView.fire("execute");
+
+ expect(submenu.isOpen).toBe(true);
+ expect(dropdown.isOpen).toBe(true);
+ });
+
+ it("leaves a dropdown with no list of its own as a plain row", async () => {
+ const editor = await createTestEditor(PLUGINS, {
+ toolbar: { items: [{ ...MENU_GROUP, items: ["bold", "panelOnly"] }] }
+ });
+
+ const menu = openGroup(getToolbar(editor), "Insert");
+ expect(menu.menus).toStrictEqual([]);
+ expect(rowLabels(menu)).toStrictEqual(["Bold", "Panel only"]);
+ });
+
+ it("converts a group of the block toolbar, filled after the plugin is set up", async () => {
+ const { BlockToolbar } = await import("ckeditor5");
+ const editor = await createTestEditor([BlockToolbar, ...PLUGINS], {
+ toolbar: { items: ["bold"] },
+ blockToolbar: [MENU_GROUP]
+ });
+
+ const menu = openGroup(editor.plugins.get(BlockToolbar).toolbarView, "Insert");
+ expect(rowLabels(menu)).toStrictEqual(["Bold", "—", "Italic", "Insert sample"]);
+ });
+
+ it("converts a group of the selection balloon", async () => {
+ const { BalloonToolbar } = await import("ckeditor5");
+ const editor = await createTestEditor([BalloonToolbar, ...PLUGINS], {
+ toolbar: { items: ["bold"] },
+ balloonToolbar: [MENU_GROUP]
+ });
+
+ const menu = openGroup(editor.plugins.get(BalloonToolbar).toolbarView, "Insert");
+ expect(rowLabels(menu)).toStrictEqual(["Bold", "—", "Italic", "Insert sample"]);
+ });
+
+ it("leaves every group alone when none is marked", async () => {
+ const editor = await createTestEditor(PLUGINS, {
+ toolbar: { items: ["bold", PLAIN_GROUP] }
+ });
+
+ expect(editor.plugins.has(ToolbarGroupMenu)).toBe(true);
+ expect(openDropdown(getToolbar(editor), "Plain").menuView).toBeUndefined();
+ });
+});
+
+function getToolbar(editor: ClassicEditor) {
+ return (editor.ui.view as { toolbar: ToolbarView }).toolbar;
+}
+
+function openDropdown(toolbar: ToolbarView, label: string) {
+ const dropdown = [...toolbar.items].find((item): item is DropdownView =>
+ item instanceof DropdownView && item.buttonView.label === label);
+ if (!dropdown) {
+ throw new Error(`No "${label}" group dropdown in the toolbar.`);
+ }
+
+ dropdown.isOpen = true;
+ return dropdown;
+}
+
+/** Open the group dropdown carrying `label` and return the menu built in its panel. */
+function openGroup(toolbar: ToolbarView, label: string) {
+ const menuView = openDropdown(toolbar, label).menuView;
+ if (!menuView) {
+ throw new Error(`The "${label}" group built no menu.`);
+ }
+
+ return menuView;
+}
+
+function submenuOf(dropdown: DropdownView) {
+ const [submenu] = dropdown.menuView?.menus ?? [];
+ if (!submenu) {
+ throw new Error("The menu holds no submenu.");
+ }
+
+ return submenu;
+}
+
+/** Open the menu's one submenu, which is what draws its rows. */
+function openSubmenu(dropdown: DropdownView) {
+ const submenu = submenuOf(dropdown);
+ submenu.isOpen = true;
+ return submenu;
+}
+
+function submenuRow(submenu: DropdownMenuNestedMenuView, label: string) {
+ for (const item of submenu.listView.items) {
+ const child = (item as { children?: { first?: unknown } }).children?.first;
+ if (child instanceof ButtonView && child.label === label) {
+ return child;
+ }
+ }
+
+ throw new Error(`No "${label}" row in the submenu.`);
+}
+
+/** The labels the menu shows, top level only, with a rule written as an em dash. */
+function rowLabels(menuView: { items: Iterable<unknown> }) {
+ return itemLabels(menuView.items);
+}
+
+function itemLabels(items: Iterable<unknown>) {
+ const labels: string[] = [];
+
+ for (const item of items) {
+ if (item instanceof ListSeparatorView) {
+ labels.push("—");
+ continue;
+ }
+
+ const child = (item as { children?: { first?: unknown } }).children?.first;
+ if (child instanceof ButtonView) {
+ labels.push(child.label ?? "");
+ } else if (isNestedMenu(child)) {
+ labels.push(child.buttonView.label ?? "");
+ }
+ }
+
+ return labels;
+}
+
+function isNestedMenu(view: unknown): view is DropdownMenuNestedMenuView {
+ return !!view && typeof view === "object" && "buttonView" in view && "listView" in view;
+}
diff --git a/packages/ckeditor5/src/plugins/toolbar_group_menu.ts b/packages/ckeditor5/src/plugins/toolbar_group_menu.ts
new file mode 100644
index 0000000000..f8c0348509
--- /dev/null
+++ b/packages/ckeditor5/src/plugins/toolbar_group_menu.ts
@@ -0,0 +1,369 @@
+import {
+ addMenuToDropdown, BalloonToolbar, BlockToolbar, type Button, ButtonView,
+ type DropdownMenuDefinition, DropdownMenuListItemButtonView, DropdownMenuListItemView,
+ type DropdownMenuNestedMenuView, DropdownView, ListItemView, ListSeparatorView, Plugin,
+ SplitButtonView, type ToolbarConfig, type ToolbarConfigItem,
+ ToolbarSeparatorView, type ToolbarView, type View
+} from "ckeditor5";
+import "../theme/toolbar_group_menu.css";
+
+/**
+ * Opens a toolbar group as a dropdown menu — a row of icon and label per entry, and a submenu
+ * for an entry that carries a list of its own — rather than as a strip of icons.
+ *
+ * A group opts in with `asMenu: true` beside its `label` in the toolbar configuration.
+ * `ToolbarView#_createNestedToolbarDropdown()` builds those dropdowns and keeps no reference to the
+ * definition each came from, so the labels of the opted-in groups are read from the configuration
+ * and matched against the dropdown buttons. The views CKEditor lays out in the group are what the
+ * menu is built from: a row mirrors the label, icon and enabled state of one of them, and runs it.
+ */
+export default class ToolbarGroupMenu extends Plugin {
+
+ private watchedDropdowns = new WeakSet<DropdownView>();
+ private plans = new WeakMap<DropdownView, MenuPlan>();
+ private sourceToolbars = new Set<ToolbarView>();
+
+ static get pluginName() {
+ return "ToolbarGroupMenu" as const;
+ }
+
+ afterInit() {
+ const editor = this.editor;
+ const labels = collectMenuGroupLabels(
+ editor.config.get("toolbar"),
+ editor.config.get("balloonToolbar"),
+ editor.config.get("blockToolbar")
+ );
+ if (!labels.size) {
+ return;
+ }
+
+ for (const toolbar of this.getToolbarViews()) {
+ for (const item of toolbar.items) {
+ this.watchGroupDropdown(item, labels);
+ }
+
+ this.listenTo(toolbar.items, "add", (evt, item: View) => {
+ this.watchGroupDropdown(item, labels);
+ });
+ }
+ }
+
+ destroy() {
+ super.destroy();
+
+ for (const toolbar of this.sourceToolbars) {
+ toolbar.destroy();
+ }
+ }
+
+ /**
+ * The toolbars an editor can hold: the fixed bar of a `DecoupledEditor`, the selection balloon
+ * of a `BalloonEditor`, and the block toolbar either of them can carry.
+ */
+ private getToolbarViews() {
+ const editor = this.editor;
+ const toolbars: ToolbarView[] = [];
+ const { toolbar } = editor.ui.view as { toolbar?: ToolbarView };
+
+ if (toolbar) {
+ toolbars.push(toolbar);
+ }
+ if (editor.plugins.has(BalloonToolbar)) {
+ toolbars.push(editor.plugins.get(BalloonToolbar).toolbarView);
+ }
+ if (editor.plugins.has(BlockToolbar)) {
+ toolbars.push(editor.plugins.get(BlockToolbar).toolbarView);
+ }
+
+ return toolbars;
+ }
+
+ private watchGroupDropdown(item: View, labels: Set<string>) {
+ if (!(item instanceof DropdownView) || this.watchedDropdowns.has(item)) {
+ return;
+ }
+
+ const label = item.buttonView.label;
+ if (!label || !labels.has(label)) {
+ return;
+ }
+
+ this.watchedDropdowns.add(item);
+
+ // The menu is drawn afresh on every open: an entry such as `dateTime` fills its own list
+ // with the time it is opened at, so rows drawn once would show the time they were drawn at.
+ // `addToolbarToDropdown()` builds the group's views on the first open, from a listener it
+ // registers at `highest` priority, so `high` runs once those views are in place.
+ this.listenTo(item, "change:isOpen", () => {
+ if (item.isOpen && item.toolbarView) {
+ this.drawMenu(item, item.toolbarView);
+ }
+ }, { priority: "high" });
+
+ // Every row reaches this, submenu entries included: the root list delegates `menu:execute`
+ // to the dropdown, and delegation keeps the button that was pressed as the event's source.
+ this.listenTo(item, "execute", (evt) => {
+ const id = (evt.source as DropdownMenuListItemButtonView).id;
+ this.plans.get(item)?.rows.get(id)?.run();
+ });
+ }
+
+ private drawMenu(dropdown: DropdownView, toolbar: ToolbarView) {
+ if (this.sourceToolbars.has(toolbar)) {
+ discardMenu(dropdown);
+ } else {
+ // The group's views go on serving the menu, so only the strip they were laid out in
+ // leaves the panel. It is destroyed with the plugin, since nothing else holds it now.
+ dropdown.panelView.children.remove(toolbar);
+ dropdown.focusTracker.remove(toolbar);
+ this.sourceToolbars.add(toolbar);
+ }
+
+ const locale = this.editor.locale;
+ const plan = planGroupMenu([...toolbar.items]);
+ addMenuToDropdown(dropdown, this.editor.ui.view.body, plan.definition, {
+ ariaLabel: dropdown.buttonView.label
+ });
+
+ this.plans.set(dropdown, plan);
+
+ const menuView = dropdown.menuView;
+ if (!menuView) {
+ return;
+ }
+
+ // A submenu's panel is appended to the editor's body, outside the dropdown, so it needs the
+ // stylesheet's hook of its own.
+ menuView.menuPanelClass = "ck-toolbar-group-menu__panel";
+
+ for (const button of menuView.buttons) {
+ const row = plan.rows.get(button.id);
+ if (row) {
+ button.icon = row.view.icon;
+ button.bind("isEnabled").to(row.view, "isEnabled");
+ }
+ }
+
+ for (const menu of menuView.menus) {
+ const opener = plan.openers.get(menu.id);
+ if (!opener) {
+ continue;
+ }
+
+ menu.buttonView.icon = opener.view.icon;
+ menu.bind("isEnabled").to(opener.view, "isEnabled");
+ bindOpenerAction(menu, opener, dropdown);
+
+ // Filled on the way open rather than once: `dateTime` renders its list in the time it
+ // is opened at, so rows filled in earlier would show the time of that earlier opening.
+ menu.on("change:isOpen", (evt, name, isOpen) => {
+ if (isOpen) {
+ this.fillSubmenu(menu, opener, plan);
+ }
+ });
+ }
+
+ for (const index of [...plan.rules].reverse()) {
+ menuView.items.add(new ListSeparatorView(locale), index);
+ }
+ }
+
+ /** Draws a submenu over the rows the entry's own list holds at this moment. */
+ private fillSubmenu(menu: DropdownMenuNestedMenuView, opener: MenuOpener, plan: MenuPlan) {
+ const locale = this.editor.locale;
+ const items = menu.listView.items;
+
+ for (const id of opener.rowIds) {
+ plan.rows.delete(id);
+ }
+ opener.rowIds = [];
+ for (const drawn of [...items]) {
+ items.remove(drawn);
+ drawn.destroy();
+ }
+
+ for (const entry of listEntriesOf(opener.source)) {
+ if (entry instanceof ListSeparatorView) {
+ if (items.length) {
+ items.add(new ListSeparatorView(locale));
+ }
+ continue;
+ }
+
+ const id = `${menu.id}:${opener.rowIds.length}`;
+ plan.rows.set(id, { view: entry, run: () => entry.fire("execute") });
+ opener.rowIds.push(id);
+
+ const button = new DropdownMenuListItemButtonView(locale, id, entry.label ?? "");
+ button.icon = entry.icon;
+ button.bind("isEnabled").to(entry, "isEnabled");
+ items.add(new DropdownMenuListItemView(locale, menu, button));
+ }
+ }
+
+}
+
+/**
+ * Runs the action an opener stands for when its row is pressed, and dismisses the menu — hovering
+ * still opens the submenu. An opener with no action of its own only opens, as CKEditor has it.
+ */
+function bindOpenerAction(
+ menu: DropdownMenuNestedMenuView, opener: MenuOpener, dropdown: DropdownView
+) {
+ const run = opener.run;
+ if (!run) {
+ return;
+ }
+
+ // On the button, which the next draw destroys along with the menu holding it.
+ menu.buttonView.on("execute", () => {
+ run();
+ dropdown.isOpen = false;
+ });
+}
+
+/** Lets go of the menu the previous draw left behind, which nothing else takes out of the panel. */
+function discardMenu(dropdown: DropdownView) {
+ const previous = dropdown.menuView;
+ /* v8 ignore next 3 -- a menu is drawn before this runs, and only a redraw discards one */
+ if (!previous) {
+ return;
+ }
+
+ for (const menu of previous.menus) {
+ dropdown.focusTracker.remove(menu);
+ }
+ dropdown.focusTracker.remove(previous);
+ dropdown.panelView.children.remove(previous);
+ previous.destroy();
+}
+
+/** The view a row stands for: a split button where the entry also runs an action of its own. */
+type SourceButton = View & Button;
+
+interface MenuRow {
+ /** The view the row takes its label, icon and enabled state from. */
+ view: SourceButton;
+ /** Runs what the row stands for. */
+ run: () => void;
+}
+
+interface MenuOpener {
+ /** The view the submenu's opener takes its label, icon and enabled state from. */
+ view: SourceButton;
+ /** The dropdown of the group the submenu is drawn over, read afresh each time it opens. */
+ source: DropdownView;
+ /** The ids the rows drawn from that list are registered under. */
+ rowIds: string[];
+ /** What pressing the opener runs, where the entry carries an action of its own. */
+ run?: () => void;
+}
+
+interface MenuPlan {
+ definition: DropdownMenuDefinition;
+ rows: Map<string, MenuRow>;
+ /** What a submenu's opener stands for, by the submenu's id. */
+ openers: Map<string, MenuOpener>;
+ /** Where a rule goes in the root list, as an index into the list the definition builds. */
+ rules: number[];
+}
+
+function collectMenuGroupLabels(...configs: (ToolbarConfig | undefined)[]) {
+ const labels = new Set<string>();
+
+ for (const config of configs) {
+ collectFromItems(Array.isArray(config) ? config : config?.items, labels);
+ }
+
+ return labels;
+}
+
+function collectFromItems(items: ToolbarConfigItem[] | undefined, labels: Set<string>) {
+ for (const item of items ?? []) {
+ if (typeof item === "string") {
+ continue;
+ }
+
+ if ((item as { asMenu?: boolean }).asMenu) {
+ labels.add(item.label);
+ }
+
+ collectFromItems(item.items, labels);
+ }
+}
+
+function planGroupMenu(items: View[]) {
+ const plan: MenuPlan = {
+ definition: [],
+ rows: new Map(),
+ openers: new Map(),
+ rules: []
+ };
+ let nextId = 0;
+ const takeId = () => `row${nextId++}`;
+
+ for (const item of items) {
+ if (item instanceof ToolbarSeparatorView) {
+ if (plan.definition.length) {
+ plan.rules.push(plan.definition.length);
+ }
+ } else if (item instanceof ButtonView) {
+ plan.definition.push(describeRow(plan, takeId(), item));
+ } else if (item instanceof DropdownView) {
+ planSubmenu(plan, takeId, item);
+ }
+ }
+
+ return plan;
+}
+
+/** Records a row against the view it stands for, and describes it to the menu. */
+function describeRow(plan: MenuPlan, id: string, view: SourceButton) {
+ plan.rows.set(id, { view, run: () => view.fire("execute") });
+ return { id, label: view.label ?? "" };
+}
+
+/**
+ * Describes a dropdown of the group as a submenu over the rows of its own list. Where the dropdown
+ * is a split button, pressing the opener runs the action of its button half and the submenu stays
+ * the way to the rest, as a row of the note tree's context menu does.
+ *
+ * The submenu is described empty and drawn on the way open, by `ToolbarGroupMenu#fillSubmenu`.
+ */
+function planSubmenu(plan: MenuPlan, takeId: () => string, dropdown: DropdownView) {
+ const opener = dropdown.buttonView;
+
+ // A dropdown offering no rows of its own has nothing to open onto, so it stays a plain row.
+ if (!listEntriesOf(dropdown).length) {
+ plan.definition.push(describeRow(plan, takeId(), opener));
+ return;
+ }
+
+ const id = takeId();
+ plan.openers.set(id, {
+ view: opener,
+ source: dropdown,
+ rowIds: [],
+ run: opener instanceof SplitButtonView ? () => opener.fire("execute") : undefined
+ });
+ plan.definition.push({ id, menu: opener.label ?? "", children: [] });
+}
+
+/** The rows a dropdown's own list holds, which it builds the first time it is opened. */
+function listEntriesOf(dropdown: DropdownView) {
+ const wasOpen = dropdown.isOpen;
+ dropdown.isOpen = true;
+ dropdown.isOpen = wasOpen;
+
+ const entries: Array<ButtonView | ListSeparatorView> = [];
+ for (const item of dropdown.listView?.items ?? []) {
+ if (item instanceof ListSeparatorView) {
+ entries.push(item);
+ } else if (item instanceof ListItemView && item.children.first instanceof ButtonView) {
+ entries.push(item.children.first);
+ }
+ }
+
+ return entries;
+}
diff --git a/packages/ckeditor5/src/theme/toolbar_group_menu.css b/packages/ckeditor5/src/theme/toolbar_group_menu.css
new file mode 100644
index 0000000000..d92726c1d7
--- /dev/null
+++ b/packages/ckeditor5/src/theme/toolbar_group_menu.css
@@ -0,0 +1,5 @@
+/* Every row is named, so the icons are sized against the label rather than against the toolbar. */
+.ck.ck-toolbar__nested-toolbar-dropdown > .ck-dropdown__panel > .ck-dropdown-menu-list,
+.ck.ck-dropdown-menu__nested-menu__panel.ck-toolbar-group-menu__panel {
+ --ck-icon-size: 1.1em;
+}