Skip to content

Build your personal knowledge base with Trilium Notes

client/text editor: fix an issue

Adorian Doranc06302b5ef1e
diff --git a/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.ts b/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.ts
index 84be833f7c..82625e0bd6 100644
--- a/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.ts
+++ b/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.ts
@@ -172,14 +172,30 @@ describe("ToolbarGroupMenu", () => {
expect(dropdown.isOpen).toBe(true);
});
- it("leaves a dropdown with no list of its own as a plain row", async () => {
+ it("holds a dropdown that carries a panel of its own in the row, panel and all", 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"]);
+ const dropdown = openDropdown(getToolbar(editor), "Insert");
+ expect(openGroup(getToolbar(editor), "Insert").menus).toStrictEqual([]);
+
+ const mounted = rowChildren(dropdown).at(-1);
+ expect(mounted).toBeInstanceOf(DropdownView);
+ if (!(mounted instanceof DropdownView)) {
+ return;
+ }
+
+ // Its panel opens from inside the menu, where firing the detached dropdown could only have
+ // opened it on the strip the menu replaced.
+ expect(mounted.buttonView.withText).toBe(true);
+ mounted.isOpen = true;
+ expect(mounted.panelView.isVisible).toBe(true);
+
+ // A redraw leaves it alive: it belongs to the group, not to the menu drawn over it.
+ dropdown.isOpen = false;
+ dropdown.isOpen = true;
+ expect(rowChildren(dropdown).at(-1)).toBe(mounted);
});
it("converts a group of the block toolbar, filled after the plugin is set up", async () => {
@@ -266,6 +282,12 @@ function submenuRow(submenu: DropdownMenuNestedMenuView, label: string) {
throw new Error(`No "${label}" row in the submenu.`);
}
+/** What each row of the menu holds: a button, a submenu, or a control mounted into the row. */
+function rowChildren(dropdown: DropdownView) {
+ return [...dropdown.menuView?.items ?? []]
+ .map((item) => (item as { children?: { first?: unknown } }).children?.first);
+}
+
/** 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);
diff --git a/packages/ckeditor5/src/plugins/toolbar_group_menu.ts b/packages/ckeditor5/src/plugins/toolbar_group_menu.ts
index f8c0348509..30c8f37f04 100644
--- a/packages/ckeditor5/src/plugins/toolbar_group_menu.ts
+++ b/packages/ckeditor5/src/plugins/toolbar_group_menu.ts
@@ -1,7 +1,8 @@
import {
addMenuToDropdown, BalloonToolbar, BlockToolbar, type Button, ButtonView,
type DropdownMenuDefinition, DropdownMenuListItemButtonView, DropdownMenuListItemView,
- type DropdownMenuNestedMenuView, DropdownView, ListItemView, ListSeparatorView, Plugin,
+ type DropdownMenuNestedMenuView, type DropdownMenuRootListView, DropdownView, ListItemView,
+ ListSeparatorView, Plugin,
SplitButtonView, type ToolbarConfig, type ToolbarConfigItem,
ToolbarSeparatorView, type ToolbarView, type View
} from "ckeditor5";
@@ -111,7 +112,7 @@ export default class ToolbarGroupMenu extends Plugin {
private drawMenu(dropdown: DropdownView, toolbar: ToolbarView) {
if (this.sourceToolbars.has(toolbar)) {
- discardMenu(dropdown);
+ discardMenu(dropdown, this.plans.get(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.
@@ -145,6 +146,10 @@ export default class ToolbarGroupMenu extends Plugin {
}
}
+ for (const [id, source] of plan.mounts) {
+ mountControl(menuView, plan, id, source);
+ }
+
for (const menu of menuView.menus) {
const opener = plan.openers.get(menu.id);
if (!opener) {
@@ -204,6 +209,29 @@ export default class ToolbarGroupMenu extends Plugin {
}
+/**
+ * Puts a control that carries a panel of its own into the row held for it, in place of the button
+ * standing there. The control keeps its panel and opens it from inside the menu; a row merely
+ * firing it would open that panel on the strip the menu replaced, where nothing can reach it.
+ */
+function mountControl(
+ menuView: DropdownMenuRootListView, plan: MenuPlan, id: string, source: DropdownView
+) {
+ const placeholder = menuView.buttons.find((button) => button.id === id);
+ const row = [...menuView.items].find((item): item is DropdownMenuListItemView =>
+ item instanceof DropdownMenuListItemView && item.childView === placeholder);
+ /* v8 ignore next 3 -- the row was described to the menu under this very id */
+ if (!placeholder || !row) {
+ return;
+ }
+
+ row.children.remove(placeholder);
+ placeholder.destroy();
+ source.buttonView.withText = true;
+ row.children.add(source);
+ plan.mounted.push({ row, source });
+}
+
/**
* 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.
@@ -224,13 +252,19 @@ function bindOpenerAction(
}
/** Lets go of the menu the previous draw left behind, which nothing else takes out of the panel. */
-function discardMenu(dropdown: DropdownView) {
+function discardMenu(dropdown: DropdownView, plan: MenuPlan | undefined) {
const previous = dropdown.menuView;
/* v8 ignore next 3 -- a menu is drawn before this runs, and only a redraw discards one */
if (!previous) {
return;
}
+ // The controls mounted into it belong to the group, so they leave before the menu holding them
+ // is destroyed; the next draw mounts them again.
+ for (const { row, source } of plan?.mounted ?? []) {
+ row.children.remove(source);
+ }
+
for (const menu of previous.menus) {
dropdown.focusTracker.remove(menu);
}
@@ -267,6 +301,10 @@ interface MenuPlan {
openers: Map<string, MenuOpener>;
/** Where a rule goes in the root list, as an index into the list the definition builds. */
rules: number[];
+ /** The controls whose row holds the control itself, by the id of the row held for it. */
+ mounts: Map<string, DropdownView>;
+ /** Where each of those controls ended up, so a redraw can take it out before destroying it. */
+ mounted: Array<{ row: DropdownMenuListItemView; source: DropdownView }>;
}
function collectMenuGroupLabels(...configs: (ToolbarConfig | undefined)[]) {
@@ -298,7 +336,9 @@ function planGroupMenu(items: View[]) {
definition: [],
rows: new Map(),
openers: new Map(),
- rules: []
+ rules: [],
+ mounts: new Map(),
+ mounted: []
};
let nextId = 0;
const takeId = () => `row${nextId++}`;
@@ -334,9 +374,12 @@ function describeRow(plan: MenuPlan, id: string, view: SourceButton) {
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.
+ // A dropdown offering no rows of its own carries a panel instead, so the row holds the dropdown
+ // itself and it opens that panel from the menu.
if (!listEntriesOf(dropdown).length) {
- plan.definition.push(describeRow(plan, takeId(), opener));
+ const mountId = takeId();
+ plan.mounts.set(mountId, dropdown);
+ plan.definition.push({ id: mountId, label: opener.label ?? "" });
return;
}
diff --git a/packages/ckeditor5/src/theme/toolbar_group_menu.css b/packages/ckeditor5/src/theme/toolbar_group_menu.css
index d92726c1d7..564cc98313 100644
--- a/packages/ckeditor5/src/theme/toolbar_group_menu.css
+++ b/packages/ckeditor5/src/theme/toolbar_group_menu.css
@@ -3,3 +3,8 @@
.ck.ck-dropdown-menu__nested-menu__panel.ck-toolbar-group-menu__panel {
--ck-icon-size: 1.1em;
}
+
+/* A control mounted into a row, panel and all, fills the row as the rows beside it do. */
+.ck.ck-dropdown-menu-list__nested-menu__item > .ck.ck-dropdown > .ck-button.ck-dropdown__button {
+ width: 100%;
+}