Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
test(text): cover the toolbar group menu's remaining branches
Elian Doran7bc2c8384dee
diff --git a/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.ts b/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.tsindex 11d7197ba3..ceb0acaa64 100644--- a/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.ts+++ b/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.ts@@ -100,6 +100,18 @@ class SampleDropdown extends Plugin {return view;});+ // A dropdown whose button carries an icon and no label.+ this.editor.ui.componentFactory.add("iconOnly", (locale: Locale) => {+ const dropdown = createDropdown(locale);+ dropdown.buttonView.set({ icon: "<svg />" });++ const items = new Collection<ListDropdownItemDefinition>();+ items.add({ type: "button", model: new UIModel({ label: "Row", withText: true }) });+ addListToDropdown(dropdown, items);++ return dropdown;+ });+// A dropdown with no list of its own, which the menu has no rows to draw for.this.editor.ui.componentFactory.add("panelOnly", (locale: Locale) => {const dropdown = createDropdown(locale);@@ -336,15 +348,19 @@ describe("ToolbarGroupMenu", () => {toolbar: {items: [{...MENU_GROUP,- items: ["plainView", "|", "bold", "-", "panelOnly", "|", "italic", "oddList", "|", "plainView"]+ items: [+ "plainView", "|", "bold", "-", "panelOnly", "|", "italic", "oddList",+ "iconOnly", "|", "plainView"+ ]}]}});groupDropdown(getToolbar(editor), "Insert").isOpen = true;- expect(labels(last().items)).toStrictEqual(["Bold", "—", "Italic", "Odd list"]);- // A list's groups are passed over, and its unlabeled button is named "".+ expect(labels(last().items)).toStrictEqual(["Bold", "—", "Italic", "Odd list", ""]);+ // A list's groups are passed over, and an unlabeled button or dropdown is named "".expect(labels(entry(last().items, "Odd list").children ?? [])).toStrictEqual(["First", "—", ""]);+ expect(labels(entry(last().items, "").children ?? [])).toStrictEqual(["Row"]);});it("converts a group of the block toolbar, filled after the plugin is set up", async () => {diff --git a/packages/ckeditor5/src/plugins/toolbar_group_menu.ts b/packages/ckeditor5/src/plugins/toolbar_group_menu.tsindex a9e88318f1..31f28e95df 100644--- a/packages/ckeditor5/src/plugins/toolbar_group_menu.ts+++ b/packages/ckeditor5/src/plugins/toolbar_group_menu.ts@@ -82,17 +82,19 @@ export default class ToolbarGroupMenu extends Plugin {editor.config.get("balloonToolbar"),editor.config.get("blockToolbar"));- if (!labels.size || !editor.config.get("toolbarGroupMenu")) {+ const menuConfig = editor.config.get("toolbarGroupMenu");+ if (!labels.size || !menuConfig) {return;}+ const createHost = () => menuConfig.host(editor);for (const toolbar of this.getToolbarViews()) {for (const item of toolbar.items) {- this.watchGroupDropdown(toolbar, item, labels);+ this.watchGroupDropdown(toolbar, item, labels, createHost);}this.listenTo(toolbar.items, "add", (evt, item: View) => {- this.watchGroupDropdown(toolbar, item, labels);+ this.watchGroupDropdown(toolbar, item, labels, createHost);});}}@@ -124,7 +126,9 @@ export default class ToolbarGroupMenu extends Plugin {return toolbars;}- private watchGroupDropdown(toolbar: ToolbarView, item: View, labels: Set<string>) {+ private watchGroupDropdown(+ toolbar: ToolbarView, item: View, labels: Set<string>, createHost: () => ToolbarGroupMenuHost+ ) {if (!(item instanceof DropdownView) || this.watchedDropdowns.has(item)) {return;}@@ -147,12 +151,14 @@ export default class ToolbarGroupMenu extends Plugin {if (this.shown?.dropdown === item) {this.hideMenu();} else {- this.showMenu(toolbar, item);+ this.showMenu(toolbar, item, createHost);}}, { priority: "high" });}- private showMenu(toolbar: ToolbarView, dropdown: DropdownView) {+ private showMenu(+ toolbar: ToolbarView, dropdown: DropdownView, createHost: () => ToolbarGroupMenuHost+ ) {const editor = this.editor;const anchor = dropdown.buttonView.element;/* v8 ignore next 3 -- a dropdown is opened from its rendered button */@@ -161,11 +167,7 @@ export default class ToolbarGroupMenu extends Plugin {}this.hideMenu();- const host = this.host ??= editor.config.get("toolbarGroupMenu")?.host(editor) ?? null;- /* v8 ignore next 3 -- `afterInit()` watches no dropdown without a host */- if (!host) {- return;- }+ const host = this.host ??= createHost();// The toolbar's "Show more items" dropdown closes as focus or a press leaves it, so it// counts the menu as its own too, while it holds the group.@@ -227,6 +229,7 @@ export default class ToolbarGroupMenu extends Plugin {this.building = false;}+ /* v8 ignore next -- opening the dropdown builds its `toolbarView` */return [...dropdown.toolbarView?.items ?? []];}