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(text): close the toolbar menu as the keyboard returns
Elian Doranf2bba0d39838
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 ee76977e67..5a3437844d 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,8 +4,8 @@ import { act } from "preact/test-utils";import { afterEach, describe, expect, it, vi } from "vitest";import {- isKeyboardPanelOpen, useEditorsFocused, useIsKeyboardPanelOpen, useKeyboardPanel,- useShownToolbarElement+ isKeyboardPanelOpen, useClosedByToolbarTap, useEditorsFocused, useIsKeyboardPanelOpen,+ useKeyboardPanel, useShownToolbarElement} from "./mobile_editor_toolbar";describe("useEditorsFocused", () => {@@ -267,6 +267,43 @@ describe("useKeyboardPanel", () => {});expect(isKeyboardPanelOpen()).toBe(false);});++ it("gives the place to the keyboard a tap on another toolbar button brings up", async () => {+ const { root, editable, host } = buildPanelFixture();+ const row = document.createElement("div");+ row.className = "classic-toolbar-island";+ row.innerHTML = `<div class="classic-toolbar-widget"><button class="anchor"></button><button class="bold"></button></div>`;+ document.body.append(row);+ const anchor = row.querySelector<HTMLElement>(".anchor");+ const bold = row.querySelector<HTMLElement>(".bold");+ if (!anchor || !bold) throw new Error("toolbar fixture is missing a button");+ function Menu() {+ useKeyboardPanel(true);+ useClosedByToolbarTap(anchor as HTMLElement, () => render(null, host));+ return null;+ }++ vi.useFakeTimers({ toFake: [ "setTimeout", "clearTimeout" ] });+ root.classList.add("virtual-keyboard-opened");+ act(() => render(<Menu />, host));+ root.classList.remove("virtual-keyboard-opened");++ // The tap's click closes the menu in a capture listener. A browser drains the microtasks+ // after each listener of a user's click, so the button's own listener, which focuses the+ // text, runs after them.+ await act(async () => bold.click());+ expect(isKeyboardPanelOpen()).toBe(true);+ editable.focus();++ // The keyboard comes up well within `PRESS_TIMEOUT`, and the place goes with it.+ act(() => {+ vi.advanceTimersByTime(1);+ });+ root.classList.add("virtual-keyboard-opened");+ await act(async () => {});+ expect(isKeyboardPanelOpen()).toBe(false);+ row.remove();+ });});/** Ends the press that closed a menu with its click, and lets what the click runs settle. */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 dab5fd6bec..273b6aa502 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@@ -220,7 +220,13 @@ export function useClosedByToolbarTap(anchor: HTMLElement, close: () => void) {const onClick = (event: MouseEvent) => {const target = event.target;if (!(target instanceof Element) || anchor.contains(target)) return;- if (target.closest(MOBILE_TOOLBAR_ROW)) closeRef.current();+ if (!target.closest(MOBILE_TOOLBAR_ROW)) return;+ isClosingInClick = true;+ try {+ closeRef.current();+ } finally {+ isClosingInClick = false;+ }};window.addEventListener("click", onClick, true);return () => window.removeEventListener("click", onClick, true);@@ -259,6 +265,8 @@ let pendingHandBack: { cancel(): void } | undefined;let hasOwnHeightPlace = false;/** The `--tn-keyboard-height` of the keyboard, which a place of the menu's own height covers. */let keyboardHeight = "";+/** Whether `useClosedByToolbarTap` is closing a menu from within the click of its press. */+let isClosingInClick = false;/** Called as `keyboard-panel-open` is set on the body or cleared. */const keyboardPanelListeners = new Set<() => void>();@@ -291,6 +299,7 @@ function takeKeyboardPlace(isOwnHeight: boolean) {*/function handBackKeyboardPlace() {let isCancelled = false;+ const isClosedInClick = isClosingInClick;let stopWaiting = () => {};const handBack = {cancel() {@@ -352,6 +361,12 @@ function handBackKeyboardPlace() {stopWaiting();setTimeout(settle);};+ // The browser drains microtasks between the listeners of a click, so a menu closed in+ // one has its click still in progress, and that click reaches no listener added now.+ if (isClosedInClick) {+ onClick();+ return;+ }window.addEventListener("click", onClick, true);const timer = setTimeout(() => {stopWaiting();