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
feat(mobile/text): open toolbar menus in the place of the keyboard
Elian Doran99f60a3051d0
diff --git a/apps/client/src/widgets/containers/root_container.spec.ts b/apps/client/src/widgets/containers/root_container.spec.tsindex de59ab1573..8e1f8b370c 100644--- a/apps/client/src/widgets/containers/root_container.spec.ts+++ b/apps/client/src/widgets/containers/root_container.spec.ts@@ -71,6 +71,19 @@ describe("RootContainer", () => {expect($widget.hasClass("virtual-keyboard-opened")).toBe(true);});+ it("keeps the height of the last keyboard for a menu to take its place", () => {+ renderInPortrait();+ const keyboardHeight = () => document.body.style.getPropertyValue("--tn-keyboard-height");++ resizeTo({ width: 400, height: 800, viewportHeight: 450 });+ expect(keyboardHeight()).toBe("350px");++ // Closing the keyboard leaves the height for a menu that replaces it as it closes.+ resizeTo({ width: 400, height: 800 });+ expect(keyboardHeight()).toBe("350px");+ document.body.style.removeProperty("--tn-keyboard-height");+ });+it("marks the body while sticky table headers are off, and follows a change", () => {const isMarked = () => document.body.classList.contains("sticky-table-headers-disabled");options.set("textNoteStickyTableHeadersEnabled", "true");diff --git a/apps/client/src/widgets/containers/root_container.ts b/apps/client/src/widgets/containers/root_container.tsindex 128c57a36c..a41b98a541 100644--- a/apps/client/src/widgets/containers/root_container.ts+++ b/apps/client/src/widgets/containers/root_container.ts@@ -137,6 +137,11 @@ export default class RootContainer extends FlexContainer<BasicWidget> {const isKeyboardOpened = windowHeight - viewportHeight > 150;this.$widget.toggleClass("virtual-keyboard-opened", isKeyboardOpened);+ // Kept once the keyboard closes, for a menu that takes its place (see `useKeyboardPanel`).+ if (isKeyboardOpened) {+ const keyboardHeight = Math.round(windowHeight - viewportHeight);+ document.body.style.setProperty("--tn-keyboard-height", `${keyboardHeight}px`);+ }}#setMaxContentWidth() {diff --git a/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.css b/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.cssindex de7ad0f4ea..e667604513 100644--- a/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.css+++ b/apps/client/src/widgets/mobile_widgets/hide_keyboard_button.css@@ -27,7 +27,8 @@ body.mobile :is(.hide-keyboard-button, .hide-keyboard-action) {}}-body.mobile .virtual-keyboard-opened :is(.hide-keyboard-button, .hide-keyboard-action) {+body.mobile .virtual-keyboard-opened :is(.hide-keyboard-button, .hide-keyboard-action),+body.mobile.keyboard-panel-open .hide-keyboard-action {display: flex;}diff --git a/apps/client/src/widgets/type_widgets/text/color_menu.tsx b/apps/client/src/widgets/type_widgets/text/color_menu.tsxindex 2de7bdfd60..cad961a104 100644--- a/apps/client/src/widgets/type_widgets/text/color_menu.tsx+++ b/apps/client/src/widgets/type_widgets/text/color_menu.tsx@@ -7,6 +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";/** The gap between the button and the menu, as a `Dropdown` leaves under its toggle. */const ANCHOR_GAP = 2;@@ -43,13 +44,16 @@ function ColorMenu({ request, onClose }: {request: ColorMenuRequest;onClose(returnFocus: boolean): void;}) {+ const bottomSheet = isMobile() && isNarrowLayout();+ const keyboardPanel = useKeyboardPanel(bottomSheet);+return (<Menuanchor={request.anchor}offset={ANCHOR_GAP}portalClassName="tn-dropdown-portal"- className="tn-dropdown-menu"- bottomSheet={isMobile() && isNarrowLayout()}+ className={keyboardPanel ? "tn-dropdown-menu keyboard-panel" : "tn-dropdown-menu"}+ bottomSheet={bottomSheet}elementRef={request.setElement}onDismiss={() => onClose(false)}onClose={() => onClose(true)}diff --git a/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.css b/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.cssindex 27ffaa3d16..06cd5a42b7 100644--- a/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.css+++ b/apps/client/src/widgets/type_widgets/text/mobile_editor_toolbar.css@@ -80,6 +80,28 @@ body.mobile {border-start-end-radius: 0;}+ /* A menu of the toolbar in the place of the keyboard (see `useKeyboardPanel`). */+ &.keyboard-panel-open {+ --mobile-toolbar-clearance: calc(+ var(--mobile-toolbar-height) + 2 * var(--mobile-toolbar-gap)+ + var(--tn-keyboard-height, 0px)+ );++ #mobile-bottom-bar {+ display: none !important;+ }++ .classic-toolbar-island {+ bottom: var(--tn-keyboard-height, 0px);+ }+ }++ .dropdown-menu.mobile-bottom-menu.keyboard-panel.show {+ height: var(--tn-keyboard-height);+ max-height: var(--tn-keyboard-height);+ }++ &.keyboard-panel-open .classic-toolbar-island,.virtual-keyboard-opened .classic-toolbar-island {--mobile-toolbar-fade: 24px;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 4af900ed65..05fa89f1ea 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@@ -3,7 +3,9 @@ import { render } from "preact";import { act } from "preact/test-utils";import { describe, expect, it } from "vitest";-import { useEditorsFocused, useShownToolbarElement } from "./mobile_editor_toolbar";+import {+ useEditorsFocused, useKeyboardPanel, useShownToolbarElement+} from "./mobile_editor_toolbar";describe("useEditorsFocused", () => {it("follows the focus trackers of both editors, and stops once unmounted", () => {@@ -40,6 +42,46 @@ describe("useEditorsFocused", () => {});});+describe("useKeyboardPanel", () => {+ it("takes the keyboard's place while mounted, only for a menu opened over the keyboard", () => {+ const root = document.createElement("div");+ root.id = "root-widget";+ document.body.append(root);+ const isMarked = () => document.body.classList.contains("keyboard-panel-open");+ const seen: boolean[] = [];+ const host = document.createElement("div");+ function Harness({ enabled }: { enabled: boolean }) {+ seen.push(useKeyboardPanel(enabled));+ return null;+ }++ act(() => render(<Harness enabled />, host));+ expect(seen.at(-1)).toBe(false);+ expect(isMarked()).toBe(false);+ act(() => render(null, host));++ root.classList.add("virtual-keyboard-opened");+ act(() => render(<Harness enabled={false} />, host));+ expect(seen.at(-1)).toBe(false);+ expect(isMarked()).toBe(false);+ act(() => render(null, host));++ act(() => render(<Harness enabled />, host));+ expect(seen.at(-1)).toBe(true);+ expect(isMarked()).toBe(true);++ // The keyboard closes as the menu takes focus, and the menu keeps its place.+ root.classList.remove("virtual-keyboard-opened");+ act(() => render(<Harness enabled />, host));+ expect(seen.at(-1)).toBe(true);+ expect(isMarked()).toBe(true);++ act(() => render(null, host));+ expect(isMarked()).toBe(false);+ root.remove();+ });+});+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 c959d86255..2fd3781850 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@@ -2,7 +2,7 @@ import "./mobile_editor_toolbar.css";import type { CKTextEditor, ClassicEditor } from "@triliumnext/ckeditor5";import clsx from "clsx";-import { useEffect, useRef, useState } from "preact/hooks";+import { useEffect, useLayoutEffect, useRef, useState } from "preact/hooks";import { isIOS } from "../../../services/utils";import { HideKeyboardAction, usePositioningOniOS } from "../../mobile_widgets/hide_keyboard_button";@@ -143,6 +143,25 @@ export function useShownToolbarElement(editor: CKTextEditor | undefined) {return element ?? undefined;}+/**+ * Whether a menu of the formatting toolbar opens in the place of the keyboard, as it does when the+ * keyboard is up as it opens: the keyboard closes as the menu takes focus, and the menu fills its+ * `--tn-keyboard-height`. While such a menu is mounted, `keyboard-panel-open` on the body keeps+ * the layout of an open keyboard.+ */+export function useKeyboardPanel(enabled: boolean) {+ const [ isPanel ] = useState(() => enabled+ && !!document.getElementById("root-widget")?.classList.contains("virtual-keyboard-opened"));++ useLayoutEffect(() => {+ if (!isPanel) return;+ document.body.classList.add("keyboard-panel-open");+ return () => document.body.classList.remove("keyboard-panel-open");+ }, [ isPanel ]);++ return isPanel;+}+/** 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.tsxindex c41bdd9cb5..bc8926baca 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,6 +8,7 @@ import { render } from "preact";import { isMobile, isNarrowLayout } from "../../../services/utils";import { FormDropdownDivider, FormDropdownSubmenu, FormListItem } from "../../react/FormList";import Menu from "../../react/Menu";+import { 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;@@ -44,13 +45,16 @@ function ToolbarGroupMenu({ request, onClose }: {request: ToolbarGroupMenuRequest;onClose(returnFocus: boolean): void;}) {+ const bottomSheet = isMobile() && isNarrowLayout();+ const keyboardPanel = useKeyboardPanel(bottomSheet);+return (<Menuanchor={request.anchor}offset={ANCHOR_GAP}portalClassName="tn-dropdown-portal"- className="tn-dropdown-menu"- bottomSheet={isMobile() && isNarrowLayout()}+ className={keyboardPanel ? "tn-dropdown-menu keyboard-panel" : "tn-dropdown-menu"}+ bottomSheet={bottomSheet}aria-labelledby={request.anchor.getAttribute("aria-labelledby") ?? undefined}elementRef={request.setElement}onDismiss={() => onClose(false)}