Skip to content

Build your personal knowledge base with Trilium Notes

fix(mobile/text): keep the toolbar shown while menus swap places

Elian Doran29d14b2d6937
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 38484ceb8b..f28c7647cf 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
@@ -141,6 +141,42 @@ describe("useKeyboardPanel", () => {
expect(isKeyboardPanelOpen()).toBe(false);
});
+ it("keeps the editor counted as focused while a closed panel holds the place", async () => {
+ const { root, host, Harness } = buildPanelFixture();
+ const editor = buildEditor();
+ const focused: boolean[] = [];
+ const focusHost = document.createElement("div");
+ function Focus() {
+ focused.push(useEditorsFocused(editor.editor, null));
+ return null;
+ }
+ act(() => render(<Focus />, focusHost));
+ act(() => editor.setFocused(true));
+
+ root.classList.add("virtual-keyboard-opened");
+ act(() => render(<Harness />, host));
+ root.classList.remove("virtual-keyboard-opened");
+
+ // The press that closes the panel blurs the editor until its click focuses the toolbar.
+ await act(async () => {
+ render(null, host);
+ editor.setFocused(false);
+ });
+ expect(focused.at(-1)).toBe(true);
+
+ await endPress();
+ expect(isKeyboardPanelOpen()).toBe(false);
+ expect(focused.at(-1)).toBe(false);
+
+ // An editor that had no focus as the place was taken does not gain it.
+ root.classList.add("virtual-keyboard-opened");
+ act(() => render(<Harness />, host));
+ expect(focused.at(-1)).toBe(false);
+ await act(async () => render(null, host));
+ await endPress();
+ act(() => render(null, focusHost));
+ });
+
it("gives the place up after its press when focus goes elsewhere, or after 1s", async () => {
const { root, editable, host, Harness } = buildPanelFixture();
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 10199b4106..d9124c7fc1 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
@@ -92,7 +92,8 @@ export default function MobileEditorToolbar() {
/**
* Whether either editor has the focus, as its `focusTracker` reports it. The tracker of a classic
* editor covers its toolbar and the panels of the toolbar's dropdowns too, wherever the toolbar is
- * placed.
+ * placed. An editor focused as a closed panel keeps the keyboard's place counts as focused until
+ * the place is given back: the press that closes the panel blurs the editor until its click lands.
*/
export function useEditorsFocused(
noteEditor: CKTextEditor | null | undefined,
@@ -103,15 +104,18 @@ export function useEditorsFocused(
useEffect(() => {
const trackers = [ noteEditor, nestedEditor ]
.flatMap((editor) => editor ? [ editor.ui.focusTracker ] : []);
- const update = () => setFocused(trackers.some((tracker) => tracker.isFocused));
+ const update = () => setFocused((wasFocused) =>
+ trackers.some((tracker) => tracker.isFocused) || (wasFocused && isKeyboardPanelOpen()));
update();
for (const tracker of trackers) {
tracker.on("change:isFocused", update);
}
+ keyboardPanelListeners.add(update);
return () => {
for (const tracker of trackers) {
tracker.off("change:isFocused", update);
}
+ keyboardPanelListeners.delete(update);
};
}, [ noteEditor, nestedEditor ]);
@@ -181,6 +185,8 @@ const PRESS_TIMEOUT = 500;
/** The hand-back of a closed panel's place, while it waits for the keyboard. */
let pendingHandBack: { cancel(): void } | undefined;
+/** Called as `keyboard-panel-open` is set on the body or cleared. */
+const keyboardPanelListeners = new Set<() => void>();
/**
* Lays the page out as for an open keyboard, or keeps the layout a closed panel still holds for
@@ -192,7 +198,7 @@ function takeKeyboardPlace() {
pendingHandBack = undefined;
return;
}
- document.body.classList.add("keyboard-panel-open");
+ setKeyboardPanelOpen(true);
holdVisibleBottom(true);
}
@@ -218,7 +224,7 @@ function handBackKeyboardPlace() {
const release = () => {
stopWaiting();
if (pendingHandBack === handBack) pendingHandBack = undefined;
- document.body.classList.remove("keyboard-panel-open");
+ setKeyboardPanelOpen(false);
holdVisibleBottom(false);
};
@@ -275,6 +281,13 @@ function isTextFocused() {
return !!document.activeElement?.closest(".ck-editor__editable");
}
+function setKeyboardPanelOpen(isOpen: boolean) {
+ document.body.classList.toggle("keyboard-panel-open", isOpen);
+ for (const listener of keyboardPanelListeners) {
+ listener();
+ }
+}
+
/** The editors whose toolbar dropdowns open upwards already. */
const repositionedEditors = new WeakSet<CKTextEditor>();