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): hold the keyboard's place until the keyboard is back
Elian Doranbe9d2efe8d28
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 76bf12f6ff..c01e72a7c0 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@@ -1,7 +1,7 @@import type { CKTextEditor } from "@triliumnext/ckeditor5";import { render } from "preact";import { act } from "preact/test-utils";-import { describe, expect, it } from "vitest";+import { afterEach, describe, expect, it, vi } from "vitest";import {isKeyboardPanelOpen, useEditorsFocused, useKeyboardPanel, useShownToolbarElement@@ -43,7 +43,13 @@ describe("useEditorsFocused", () => {});describe("useKeyboardPanel", () => {- it("takes the keyboard's place while mounted, only for a menu opened over the keyboard", () => {+ afterEach(() => {+ vi.useRealTimers();+ document.getElementById("root-widget")?.remove();+ document.querySelector(".ck-editor__editable")?.remove();+ });++ it("takes the keyboard's place while mounted, for a menu opened over it", async () => {const root = document.createElement("div");root.id = "root-widget";document.body.append(root);@@ -76,12 +82,79 @@ describe("useKeyboardPanel", () => {expect(seen.at(-1)).toBe(true);expect(isMarked()).toBe(true);- act(() => render(null, host));+ await act(async () => render(null, host));expect(isMarked()).toBe(false);- root.remove();+ });++ it("keeps the place for a keyboard that comes back, until it is up", async () => {+ const { root, editable, host, Harness, seen } = buildPanelFixture();++ root.classList.add("virtual-keyboard-opened");+ act(() => render(<Harness />, host));+ root.classList.remove("virtual-keyboard-opened");++ // An entry that runs, or the menu's own button, focuses the text as the menu closes.+ await act(async () => {+ render(null, host);+ editable.focus();+ });+ expect(isKeyboardPanelOpen()).toBe(true);++ // A menu opened while the keyboard is on its way takes the place over.+ act(() => render(<Harness />, host));+ expect(seen.at(-1)).toBe(true);+ await act(async () => render(null, host));+ expect(isKeyboardPanelOpen()).toBe(true);++ root.classList.add("virtual-keyboard-opened");+ await act(async () => {+ await new Promise((resolve) => setTimeout(resolve));+ });+ expect(isKeyboardPanelOpen()).toBe(false);+ });++ it("gives the place up at once 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(false);++ // A keyboard that never comes back, as with a hardware keyboard.+ 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);+ editable.focus();+ });+ expect(isKeyboardPanelOpen()).toBe(true);+ act(() => {+ vi.advanceTimersByTime(1000);+ });+ expect(isKeyboardPanelOpen()).toBe(false);});});+function buildPanelFixture() {+ const root = document.createElement("div");+ root.id = "root-widget";+ const editable = document.createElement("div");+ editable.className = "ck-editor__editable";+ editable.tabIndex = 0;+ document.body.append(root, editable);+ const seen: boolean[] = [];+ const host = document.createElement("div");+ function Harness() {+ seen.push(useKeyboardPanel(true));+ return null;+ }+ return { root, editable, host, Harness, seen };+}+describe("useShownToolbarElement", () => {it("shows the editor's toolbar, and the text formatting one while that is active", () => {const main = document.createElement("div");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 121a415baf..543e75c901 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@@ -152,17 +152,16 @@ export function useShownToolbarElement(editor: CKTextEditor | undefined) {* the layout of an open keyboard.*/export function useKeyboardPanel(enabled: boolean) {- const [ isPanel ] = useState(() => enabled- && !!document.getElementById("root-widget")?.classList.contains("virtual-keyboard-opened"));+ const [ isPanel ] = useState(() => {+ const root = document.getElementById("root-widget");+ const isKeyboardUp = !!root?.classList.contains("virtual-keyboard-opened");+ return enabled && (isKeyboardUp || isKeyboardPanelOpen());+ });useLayoutEffect(() => {if (!isPanel) return;- document.body.classList.add("keyboard-panel-open");- holdVisibleBottom(true);- return () => {- document.body.classList.remove("keyboard-panel-open");- holdVisibleBottom(false);- };+ takeKeyboardPlace();+ return handBackKeyboardPlace;}, [ isPanel ]);return isPanel;@@ -173,6 +172,69 @@ export function isKeyboardPanelOpen() {return document.body.classList.contains("keyboard-panel-open");}+/** How long a closed panel keeps the keyboard's place for a keyboard that does not come back. */+const KEYBOARD_RETURN_TIMEOUT = 1000;++/** The hand-back of a closed panel's place, while it waits for the keyboard. */+let pendingHandBack: { cancel(): void } | undefined;++/**+ * Lays the page out as for an open keyboard, or keeps the layout a closed panel still holds for+ * the keyboard, with the visible bottom it was taken at.+ */+function takeKeyboardPlace() {+ if (pendingHandBack) {+ pendingHandBack.cancel();+ pendingHandBack = undefined;+ return;+ }+ document.body.classList.add("keyboard-panel-open");+ holdVisibleBottom(true);+}++/**+ * 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.+ */+function handBackKeyboardPlace() {+ let isCancelled = false;+ let stopWaiting = () => {};+ const handBack = {+ cancel() {+ isCancelled = true;+ stopWaiting();+ }+ };+ pendingHandBack = handBack;+ const release = () => {+ stopWaiting();+ if (pendingHandBack === handBack) pendingHandBack = undefined;+ document.body.classList.remove("keyboard-panel-open");+ holdVisibleBottom(false);+ };++ // The plugin focuses the text after the menu has unmounted, in the same task.+ queueMicrotask(() => {+ if (isCancelled) return;+ const root = document.getElementById("root-widget");+ if (!root || !document.activeElement?.closest(".ck-editor__editable")) {+ release();+ return;+ }+ const observer = new MutationObserver(() => {+ if (root.classList.contains("virtual-keyboard-opened")) release();+ });+ observer.observe(root, { attributeFilter: [ "class" ] });+ const timer = setTimeout(release, KEYBOARD_RETURN_TIMEOUT);+ stopWaiting = () => {+ observer.disconnect();+ clearTimeout(timer);+ };+ });+}+/** The editors whose toolbar dropdowns open upwards already. */const repositionedEditors = new WeakSet<CKTextEditor>();