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): keep the keyboard down when opening a note from the tree
Elian Doranadbcab655321
diff --git a/apps/client/src/widgets/NoteDetail.spec.ts b/apps/client/src/widgets/NoteDetail.spec.tsindex e5cf41140c..c3284a6b81 100644--- a/apps/client/src/widgets/NoteDetail.spec.ts+++ b/apps/client/src/widgets/NoteDetail.spec.ts@@ -14,7 +14,8 @@ import froca from "../services/froca";vi.mock("./note_tree", () => ({ default: class {} }));import {- getExtendedWidgetType, NoteDetailWrapper, useDeferredDetailFocus, useNoteInfo+ getExtendedWidgetType, NoteDetailWrapper, shouldFocusOnNoteChange, useDeferredDetailFocus,+ useNoteInfo} from "./NoteDetail";import { isContextInActiveTab, useLegacyImperativeHandlers, useTriliumEvent } from "./react/hooks";import {@@ -312,3 +313,19 @@ describe("NoteDetailWrapper", () => {expect(addLinkToText).toHaveBeenCalledOnce();});});++describe("shouldFocusOnNoteChange", () => {+ it("focuses the editor of a note opened elsewhere, but not on a phone, nor while the tree or the split has focus", () => {+ const split = document.createElement("div");+ const title = document.createElement("input");+ split.append(title);+ const treeTitle = document.createElement("span");+ treeTitle.className = "fancytree-title";+ const elsewhere = document.createElement("button");++ expect(shouldFocusOnNoteChange(elsewhere, split, false)).toBe(true);+ expect(shouldFocusOnNoteChange(elsewhere, split, true)).toBe(false);+ expect(shouldFocusOnNoteChange(treeTitle, split, false)).toBe(false);+ expect(shouldFocusOnNoteChange(title, split, false)).toBe(false);+ });+});diff --git a/apps/client/src/widgets/NoteDetail.tsx b/apps/client/src/widgets/NoteDetail.tsxindex ff952007f1..23e826b362 100644--- a/apps/client/src/widgets/NoteDetail.tsx+++ b/apps/client/src/widgets/NoteDetail.tsx@@ -20,7 +20,7 @@ import { endLivePrint, getLivePrintTarget, printLiveInBrowser } from "../serviceimport { stopBackgroundMedia } from "../services/media_playback";import protected_session_holder from "../services/protected_session_holder";import toast from "../services/toast.js";-import { isElectron } from "../services/utils";+import { isElectron, isMobile } from "../services/utils";import { ExtendedNoteType, TYPE_MAPPINGS, TypeWidget } from "./note_types";import Button from "./react/Button";import {@@ -144,9 +144,8 @@ export default function NoteDetail() {// Automatically focus the editor.useTriliumEvent("activeNoteChanged", ({ ntxId: eventNtxId }) => {if (eventNtxId != ntxId) return;- // Restore focus to the editor when switching tabs,- // but only if the note tree and the note panel (e.g., note title or note detail) are not focused.- if (!document.activeElement?.classList.contains("fancytree-title") && !parentComponent.$widget[0].closest(".note-split")?.contains(document.activeElement)) {+ const noteSplit = parentComponent.$widget[0].closest(".note-split");+ if (shouldFocusOnNoteChange(document.activeElement, noteSplit, isMobile())) {parentComponent.triggerCommand("focusOnDetail", { ntxId });}});@@ -254,6 +253,18 @@ export default function NoteDetail() {);}+/**+ * Whether a note opened in a split focuses its editor, as when switching tabs: not while the note+ * tree or the split itself (its title or detail) has focus, and never on a phone, where focusing+ * the editor brings up the keyboard over the note just opened from the tree.+ */+export function shouldFocusOnNoteChange(+ focused: Element | null, noteSplit: Element | null | undefined, mobile: boolean+) {+ if (mobile) return false;+ return !focused?.classList.contains("fancytree-title") && !noteSplit?.contains(focused);+}+/*** Holds a `focusOnDetail` that arrives while `isReady` is false and calls `replay` with it once* `isReady` turns true. A request made while ready is left to the widgets that already received it.