Skip to content

Build your personal knowledge base with Trilium Notes

fix(mobile/text): bring the keyboard back from a menu's own button

Elian Doranf06b36c35859
diff --git a/apps/client/src/widgets/type_widgets/text/color_menu.tsx b/apps/client/src/widgets/type_widgets/text/color_menu.tsx
index cad961a104..e425e46e7b 100644
--- a/apps/client/src/widgets/type_widgets/text/color_menu.tsx
+++ b/apps/client/src/widgets/type_widgets/text/color_menu.tsx
@@ -7,7 +7,7 @@ import ColorPicker from "../../react/ColorPicker";
import { FormListCustomItem, FormListHeader } from "../../react/FormList";
import Menu from "../../react/Menu";
import { buildFontColorConfig } from "./color_palette";
-import { useKeyboardPanel } from "./mobile_editor_toolbar";
+import { isKeyboardPanelOpen, useKeyboardPanel } from "./mobile_editor_toolbar";
/** The gap between the button and the menu, as a `Dropdown` leaves under its toggle. */
const ANCHOR_GAP = 2;
@@ -36,7 +36,8 @@ export function createColorMenuHost(): ColorMenuHost {
render(<ColorMenu request={request} onClose={close} />, container);
},
hide: unmount,
- destroy: unmount
+ destroy: unmount,
+ isInPlaceOfKeyboard: isKeyboardPanelOpen
};
}
diff --git a/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.spec.tsx b/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.spec.tsx
index 05fa89f1ea..76bf12f6ff 100644
--- a/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.spec.tsx
+++ b/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.spec.tsx
@@ -4,7 +4,7 @@ import { act } from "preact/test-utils";
import { describe, expect, it } from "vitest";
import {
- useEditorsFocused, useKeyboardPanel, useShownToolbarElement
+ isKeyboardPanelOpen, useEditorsFocused, useKeyboardPanel, useShownToolbarElement
} from "./mobile_editor_toolbar";
describe("useEditorsFocused", () => {
@@ -47,7 +47,7 @@ describe("useKeyboardPanel", () => {
const root = document.createElement("div");
root.id = "root-widget";
document.body.append(root);
- const isMarked = () => document.body.classList.contains("keyboard-panel-open");
+ const isMarked = isKeyboardPanelOpen;
const seen: boolean[] = [];
const host = document.createElement("div");
function Harness({ enabled }: { enabled: boolean }) {
diff --git a/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx b/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx
index 132cb7abcf..121a415baf 100644
--- a/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx
+++ b/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.tsx
@@ -168,6 +168,11 @@ export function useKeyboardPanel(enabled: boolean) {
return isPanel;
}
+/** Whether a menu of the toolbar is in the keyboard's place, for its host to tell the editor. */
+export function isKeyboardPanelOpen() {
+ return document.body.classList.contains("keyboard-panel-open");
+}
+
/** The editors whose toolbar dropdowns open upwards already. */
const repositionedEditors = new WeakSet<CKTextEditor>();
diff --git a/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.tsx b/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.tsx
index 68a5d9e129..b72c1cb9b4 100644
--- a/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.tsx
+++ b/apps/client/src/widgets/type_widgets/text/toolbar_group_menu.tsx
@@ -8,7 +8,7 @@ import { render } from "preact";
import { escapeHtml, isMobile, isNarrowLayout } from "../../../services/utils";
import { FormDropdownDivider, FormDropdownSubmenu, FormListItem } from "../../react/FormList";
import Menu, { MenuTile } from "../../react/Menu";
-import { useKeyboardPanel } from "./mobile_editor_toolbar";
+import { isKeyboardPanelOpen, useKeyboardPanel } from "./mobile_editor_toolbar";
/** The gap between the group's button and the menu, as a `Dropdown` leaves under its toggle. */
const ANCHOR_GAP = 2;
@@ -37,7 +37,8 @@ export function createToolbarGroupMenuHost(): ToolbarGroupMenuHost {
render(<ToolbarGroupMenu request={request} onClose={close} />, container);
},
hide: unmount,
- destroy: unmount
+ destroy: unmount,
+ isInPlaceOfKeyboard: isKeyboardPanelOpen
};
}
diff --git a/packages/ckeditor5/src/plugins/color_menu.spec.ts b/packages/ckeditor5/src/plugins/color_menu.spec.ts
index 17c13e36ff..5e45e7c1ef 100644
--- a/packages/ckeditor5/src/plugins/color_menu.spec.ts
+++ b/packages/ckeditor5/src/plugins/color_menu.spec.ts
@@ -129,6 +129,25 @@ describe("ColorMenu", () => {
expect(host.show).toHaveBeenCalledTimes(4);
});
+ it("gives the text the focus as its button closes a menu in the keyboard's place", async () => {
+ const { editor, host, button } = await createEditor();
+ const inPlaceOfKeyboard = vi.fn(() => false);
+ Object.assign(host, { isInPlaceOfKeyboard: inPlaceOfKeyboard });
+ const focusText = vi.spyOn(editor.editing.view, "focus");
+
+ button.fire("execute");
+ button.fire("execute");
+ expect(host.hide).toHaveBeenCalledTimes(1);
+ expect(focusText).not.toHaveBeenCalled();
+
+ // The press that closes it brings the keyboard back, which a later focus would not.
+ inPlaceOfKeyboard.mockReturnValue(true);
+ button.fire("execute");
+ button.fire("execute");
+ expect(host.hide).toHaveBeenCalledTimes(2);
+ expect(focusText).toHaveBeenCalledOnce();
+ });
+
it("is enabled while either color can be set, and destroys the host with the editor", async () => {
const { editor, host, button } = await createEditor();
expect(button.isEnabled).toBe(true);
diff --git a/packages/ckeditor5/src/plugins/color_menu.ts b/packages/ckeditor5/src/plugins/color_menu.ts
index c1662e92af..31821c38b2 100644
--- a/packages/ckeditor5/src/plugins/color_menu.ts
+++ b/packages/ckeditor5/src/plugins/color_menu.ts
@@ -6,6 +6,12 @@ export interface ColorMenuHost {
show(request: ColorMenuRequest): void;
hide(): void;
destroy(): void;
+ /**
+ * Whether the shown menu takes the place of the on-screen keyboard. Its button then gives that
+ * place back as it closes the menu, focusing the text in the same press, as a mobile browser
+ * raises the keyboard only for a focus a press makes.
+ */
+ isInPlaceOfKeyboard?(): boolean;
}
/** The command a color of the menu runs: the color of the text, or its background. */
@@ -70,7 +76,11 @@ export default class ColorMenu extends Plugin {
);
button.on("execute", () => {
if (this.shown) {
+ const toKeyboard = this.shown.host.isInPlaceOfKeyboard?.() ?? false;
this.hideMenu();
+ if (toKeyboard) {
+ editor.editing.view.focus();
+ }
} else {
this.showMenu(button, () => config.host(editor));
}
diff --git a/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.ts b/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.ts
index 9a26e1279a..0df4dfb318 100644
--- a/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.ts
+++ b/packages/ckeditor5/src/plugins/toolbar_group_menu.spec.ts
@@ -299,6 +299,31 @@ describe("ToolbarGroupMenu", () => {
expect(host.hide).toHaveBeenCalledTimes(3);
});
+ it("gives the text the focus as its button closes a menu in the keyboard's place", async () => {
+ const { host } = createHost();
+ const inPlaceOfKeyboard = vi.fn(() => false);
+ const editor = await createTestEditor(PLUGINS, {
+ toolbarGroupMenu: {
+ host: () => Object.assign(host, { isInPlaceOfKeyboard: inPlaceOfKeyboard })
+ },
+ toolbar: { items: [MENU_GROUP] }
+ });
+ const focusText = vi.spyOn(editor.editing.view, "focus");
+ const dropdown = groupDropdown(getToolbar(editor), "Insert");
+
+ dropdown.buttonView.fire("open");
+ dropdown.buttonView.fire("open");
+ expect(host.hide).toHaveBeenCalledTimes(1);
+ expect(focusText).not.toHaveBeenCalled();
+
+ // The press that closes it brings the keyboard back, which a later focus would not.
+ inPlaceOfKeyboard.mockReturnValue(true);
+ dropdown.buttonView.fire("open");
+ dropdown.buttonView.fire("open");
+ expect(host.hide).toHaveBeenCalledTimes(2);
+ expect(focusText).toHaveBeenCalledOnce();
+ });
+
it("counts the menu's element as the editor's own while it is shown", async () => {
const { config, last } = createHost();
const editor = await createTestEditor(PLUGINS, {
diff --git a/packages/ckeditor5/src/plugins/toolbar_group_menu.ts b/packages/ckeditor5/src/plugins/toolbar_group_menu.ts
index 03a7845c0a..13ebe5dd37 100644
--- a/packages/ckeditor5/src/plugins/toolbar_group_menu.ts
+++ b/packages/ckeditor5/src/plugins/toolbar_group_menu.ts
@@ -20,6 +20,12 @@ export interface ToolbarGroupMenuHost {
show(request: ToolbarGroupMenuRequest): void;
hide(): void;
destroy(): void;
+ /**
+ * Whether the shown menu takes the place of the on-screen keyboard. Its button then gives that
+ * place back as it closes the menu, focusing the text in the same press, as a mobile browser
+ * raises the keyboard only for a focus a press makes.
+ */
+ isInPlaceOfKeyboard?(): boolean;
}
/** What a {@link ToolbarGroupMenuHost} shows a menu for. */
@@ -186,7 +192,11 @@ export default class ToolbarGroupMenu extends Plugin {
evt.return = false;
if (this.shown?.dropdown === item) {
+ const toKeyboard = this.host?.isInPlaceOfKeyboard?.() ?? false;
this.hideMenu();
+ if (toKeyboard) {
+ this.editor.editing.view.focus();
+ }
} else {
this.showMenu(toolbar, item, createHost);
}