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
fix(mobile/text): let a toolbar press open its menu over a panel
Elian Dorand19e268f8887
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.tsxindex c01e72a7c0..c8f38a4d4a 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@@ -83,6 +83,7 @@ describe("useKeyboardPanel", () => {expect(isMarked()).toBe(true);await act(async () => render(null, host));+ await endPress();expect(isMarked()).toBe(false);});@@ -113,13 +114,51 @@ describe("useKeyboardPanel", () => {expect(isKeyboardPanelOpen()).toBe(false);});- it("gives the place up at once when focus goes elsewhere, or after 1s", async () => {+ it("keeps the place through the press that closes a menu, for the menu it opens", async () => {+ const { root, host, Harness, seen } = buildPanelFixture();++ root.classList.add("virtual-keyboard-opened");+ act(() => render(<Harness />, host));+ root.classList.remove("virtual-keyboard-opened");++ // A press on another button of the toolbar closes the menu as it starts, and its click+ // opens the button's own menu; the button must not move from under it in between.+ await act(async () => render(null, host));+ expect(isKeyboardPanelOpen()).toBe(true);+ await act(async () => {+ window.dispatchEvent(new MouseEvent("click"));+ render(<Harness />, host);+ await new Promise((resolve) => setTimeout(resolve));+ });+ expect(seen.at(-1)).toBe(true);+ expect(isKeyboardPanelOpen()).toBe(true);++ await act(async () => render(null, host));+ await endPress();+ expect(isKeyboardPanelOpen()).toBe(false);+ });++ it("gives the place up after its press when focus goes elsewhere, or after 1s", async () => {const { root, editable, host, Harness } = buildPanelFixture();root.classList.add("virtual-keyboard-opened");act(() => render(<Harness />, host));root.classList.remove("virtual-keyboard-opened");await act(async () => render(null, host));+ expect(isKeyboardPanelOpen()).toBe(true);+ await endPress();+ expect(isKeyboardPanelOpen()).toBe(false);++ // A press with no click, as one that scrolls.+ vi.useFakeTimers({ toFake: [ "setTimeout", "clearTimeout" ] });+ root.classList.add("virtual-keyboard-opened");+ act(() => render(<Harness />, host));+ root.classList.remove("virtual-keyboard-opened");+ await act(async () => render(null, host));+ expect(isKeyboardPanelOpen()).toBe(true);+ act(() => {+ vi.advanceTimersByTime(500);+ });expect(isKeyboardPanelOpen()).toBe(false);// A keyboard that never comes back, as with a hardware keyboard.@@ -139,6 +178,14 @@ describe("useKeyboardPanel", () => {});});+/** Ends the press that closed a menu with its click, and lets what the click runs settle. */+async function endPress() {+ await act(async () => {+ window.dispatchEvent(new MouseEvent("click"));+ await new Promise((resolve) => setTimeout(resolve));+ });+}+function buildPanelFixture() {const root = document.createElement("div");root.id = "root-widget";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.tsxindex 543e75c901..cb7682d307 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@@ -174,6 +174,8 @@ export function isKeyboardPanelOpen() {/** How long a closed panel keeps the keyboard's place for a keyboard that does not come back. */const KEYBOARD_RETURN_TIMEOUT = 1000;+/** How long a closed panel waits for the click of the press that closed it, which can scroll. */+const PRESS_TIMEOUT = 500;/** The hand-back of a closed panel's place, while it waits for the keyboard. */let pendingHandBack: { cancel(): void } | undefined;@@ -196,7 +198,10 @@ function takeKeyboardPlace() {* Gives a closed panel's place back. When the text has the focus once the menu has closed, as* after an entry runs or the menu's own button closes it, the keyboard is on its way, so the* layout stays until `RootContainer` reports it up; laying out for no keyboard in between drops- * the toolbar and brings the launcher bar back for some 250ms.+ * the toolbar and brings the launcher bar back for some 250ms. A menu that a press closes, which+ * `Popup` does as the press starts, keeps the layout until the press's click: that click can open+ * the menu of another button of the toolbar, or focus the text, and the button must not move from+ * under it.*/function handBackKeyboardPlace() {let isCancelled = false;@@ -215,11 +220,9 @@ function handBackKeyboardPlace() {holdVisibleBottom(false);};- // The plugin focuses the text after the menu has unmounted, in the same task.- queueMicrotask(() => {- if (isCancelled) return;+ const waitForKeyboard = () => {const root = document.getElementById("root-widget");- if (!root || !document.activeElement?.closest(".ck-editor__editable")) {+ if (!root || root.classList.contains("virtual-keyboard-opened")) {release();return;}@@ -232,9 +235,44 @@ function handBackKeyboardPlace() {observer.disconnect();clearTimeout(timer);};+ };+ const settle = () => {+ if (isCancelled) return;+ if (isTextFocused()) {+ waitForKeyboard();+ } else {+ release();+ }+ };++ // The plugin focuses the text after the menu has unmounted, in the same task.+ queueMicrotask(() => {+ if (isCancelled) return;+ if (isTextFocused()) {+ waitForKeyboard();+ return;+ }+ // What the click runs, such as opening another menu, runs before `settle`.+ const onClick = () => {+ stopWaiting();+ setTimeout(settle);+ };+ window.addEventListener("click", onClick, true);+ const timer = setTimeout(() => {+ stopWaiting();+ settle();+ }, PRESS_TIMEOUT);+ stopWaiting = () => {+ window.removeEventListener("click", onClick, true);+ clearTimeout(timer);+ };});}+function isTextFocused() {+ return !!document.activeElement?.closest(".ck-editor__editable");+}+/** The editors whose toolbar dropdowns open upwards already. */const repositionedEditors = new WeakSet<CKTextEditor>();