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): close the note panel on a heading or PDF page
Elian Doran3917a3cc4e11
diff --git a/apps/client/src/widgets/mobile_widgets/MobileNotePanel.tsx b/apps/client/src/widgets/mobile_widgets/MobileNotePanel.tsxindex 6489061d85..8326feb319 100644--- a/apps/client/src/widgets/mobile_widgets/MobileNotePanel.tsx+++ b/apps/client/src/widgets/mobile_widgets/MobileNotePanel.tsx@@ -10,6 +10,7 @@ import { useNoteContext } from "../react/hooks";import Modal from "../react/Modal";import TabStrip from "../react/TabStrip";import { RightPaneTabBodies, useRightPaneTabs } from "../sidebar/RightPanelContainer";+import { NavigatedInNote } from "../sidebar/RightPanelWidget";import { RightPaneTabId } from "../sidebar/RightPaneTabs";/** The mobile layout loads no script widgets for the right pane. */@@ -52,7 +53,9 @@ function MobileNotePanelModal({ shown, onHidden }: { shown: boolean; onHidden: ({/* The widgets' card, TOC and highlight styles are scoped to `#right-pane`, which themobile layout has no other element for. */}<div id="right-pane">- <RightPaneTabBodies tabs={tabs} activeTabId={activeTab?.id} />+ <NavigatedInNote.Provider value={onHidden}>+ <RightPaneTabBodies tabs={tabs} activeTabId={activeTab?.id} />+ </NavigatedInNote.Provider></div></Modal>);diff --git a/apps/client/src/widgets/sidebar/RightPanelWidget.tsx b/apps/client/src/widgets/sidebar/RightPanelWidget.tsxindex 6dfc00fa6d..298585ee6f 100644--- a/apps/client/src/widgets/sidebar/RightPanelWidget.tsx+++ b/apps/client/src/widgets/sidebar/RightPanelWidget.tsx@@ -26,6 +26,12 @@ export interface ExpandWidgetRequest {/** The pending {@link ExpandWidgetRequest}, set by whoever lays the widgets out. */export const ExpandWidgetRequests = createContext<ExpandWidgetRequest | null>(null);+/**+ * Called after a widget scrolls the note to a place in it (a heading, a PDF page), set by a host that+ * covers the note, such as the mobile note panel, so that it can close and show that place.+ */+export const NavigatedInNote = createContext<(() => void) | null>(null);+interface RightPanelWidgetProps {id: string;title: ComponentChildren;diff --git a/apps/client/src/widgets/sidebar/TableOfContents.spec.tsx b/apps/client/src/widgets/sidebar/TableOfContents.spec.tsxindex 450f590671..049ff843f6 100644--- a/apps/client/src/widgets/sidebar/TableOfContents.spec.tsx+++ b/apps/client/src/widgets/sidebar/TableOfContents.spec.tsx@@ -11,6 +11,7 @@ import { buildNote } from "../../test/easy-froca";import { renderInto } from "../../test/render";import { setEditorNoteId } from "../react/NoteStore";import { ParentComponent } from "../react/react_utils";+import { NavigatedInNote } from "./RightPanelWidget";import TableOfContents, { useActiveHeading } from "./TableOfContents";// `TableOfContents` loads `attributeChangeAffectsHeading` through a lazy `import()`. Pulling the@@ -190,7 +191,20 @@ describe("TableOfContents while an auto-read-only note is temporarily editable",});});-async function setupUnlockScenario() {+describe("TableOfContents in a host that covers the note", () => {+ it("tells the host once a heading is pressed, so that it can close over the heading", async () => {+ const onNavigated = vi.fn();+ const toc = await setupUnlockScenario({ onNavigated });+ const heading = toc.container.querySelector<HTMLElement>(".toc .item-content");+ expect(heading?.textContent).toBe("Current");++ await act(async () => heading?.click());++ expect(onNavigated).toHaveBeenCalledOnce();+ });+});++async function setupUnlockScenario({ onNavigated }: { onNavigated?: () => void } = {}) {options.load({ rightPaneCollapsedItems: "[]" } as Parameters<typeof options.load>[0]);const note = buildNote({ id: "bignote", title: "Big note", type: "text", content: "<h2>Current</h2>" });@@ -222,7 +236,9 @@ async function setupUnlockScenario() {await act(async () => {container = renderInto(<ParentComponent.Provider value={parent}>- <TableOfContents />+ <NavigatedInNote.Provider value={onNavigated ?? null}>+ <TableOfContents />+ </NavigatedInNote.Provider></ParentComponent.Provider>);});@@ -231,6 +247,7 @@ async function setupUnlockScenario() {return {noteId: note.noteId,editor,+ container,headings: () => [ ...container.querySelectorAll(".toc .item-content") ].map((el) => el.textContent),rows: () => [ ...container.querySelectorAll(".toc li") ],async unlock({ rounds }: { rounds?: number } = {}) {diff --git a/apps/client/src/widgets/sidebar/TableOfContents.tsx b/apps/client/src/widgets/sidebar/TableOfContents.tsxindex b7c84b6d29..04548ed869 100644--- a/apps/client/src/widgets/sidebar/TableOfContents.tsx+++ b/apps/client/src/widgets/sidebar/TableOfContents.tsx@@ -2,7 +2,7 @@ import "./TableOfContents.css";import type { CKTextEditor, ModelElement, ModelNode } from "@triliumnext/ckeditor5";import clsx from "clsx";-import { useCallback, useEffect, useRef, useState } from "preact/hooks";+import { useCallback, useContext, useEffect, useRef, useState } from "preact/hooks";import { revealElement } from "../../services/collapsible";import { t } from "../../services/i18n";@@ -11,7 +11,7 @@ import { useActiveNoteContext, useContentElement, useGetContextData, useIsNoteReimport Icon from "../react/Icon";import { getEditorNoteId } from "../react/NoteStore";import RawHtml from "../react/RawHtml";-import RightPanelWidget from "./RightPanelWidget";+import RightPanelWidget, { NavigatedInNote } from "./RightPanelWidget";//#region Generic impl.interface RawHeading {@@ -93,6 +93,7 @@ function TableOfContentsHeading({ heading, scrollToHeading, activeHeadingId }: {activeHeadingId?: string | null;}) {const [ collapsed, setCollapsed ] = useState(false);+ const onNavigated = useContext(NavigatedInNote);const isActive = heading.id === activeHeadingId;const contentRef = useRef<HTMLElement>(null);@@ -111,7 +112,10 @@ function TableOfContentsHeading({ heading, scrollToHeading, activeHeadingId }: {<RawHtmlcontainerRef={contentRef}className="item-content"- onClick={() => scrollToHeading(heading)}+ onClick={() => {+ scrollToHeading(heading);+ onNavigated?.();+ }}html={heading.text}/></li>diff --git a/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.tsx b/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.tsxindex 9b6af21b82..c3fd948306 100644--- a/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.tsx+++ b/apps/client/src/widgets/sidebar/pdf/PdfAnnotations.tsx@@ -1,12 +1,13 @@import "./PdfAnnotations.css";import clsx from "clsx";+import { useContext } from "preact/hooks";import contextMenu from "../../../menus/context_menu";import { t } from "../../../services/i18n";import { useActiveNoteContext, useGetContextData, useNoteProperty } from "../../react/hooks";import Icon from "../../react/Icon";-import RightPanelWidget from "../RightPanelWidget";+import RightPanelWidget, { NavigatedInNote } from "../RightPanelWidget";const TYPE_ICONS: Record<string, string> = {text: "bx bxs-comment-detail",@@ -62,6 +63,7 @@ function PdfAnnotationItem({// A drawing has no text of its own, and a highlight only gets one where the page has// extractable glyphs under it — name those by what they are so the row stays clickable.const hasText = !!(annotation.highlightedText || annotation.contents);+ const onNavigated = useContext(NavigatedInNote);return (<div@@ -70,7 +72,10 @@ function PdfAnnotationItem({annotation.color && "tinted",isDark(annotation.color) && "tinted-dark")}- onClick={() => onNavigate(annotation.id, annotation.pageNumber)}+ onClick={() => {+ onNavigate(annotation.id, annotation.pageNumber);+ onNavigated?.();+ }}onContextMenu={(e) => showCopyReferenceMenu(e, () => onCopyReference(annotation.id, annotation.pageNumber))}style={annotation.color ? { backgroundColor: annotation.color } : undefined}>diff --git a/apps/client/src/widgets/sidebar/pdf/PdfPages.tsx b/apps/client/src/widgets/sidebar/pdf/PdfPages.tsxindex 622014dd0b..39c76b0b35 100644--- a/apps/client/src/widgets/sidebar/pdf/PdfPages.tsx+++ b/apps/client/src/widgets/sidebar/pdf/PdfPages.tsx@@ -1,14 +1,14 @@import "./PdfPages.css";import type { CSSProperties } from "preact";-import { useCallback, useEffect, useRef, useState } from "preact/hooks";+import { useCallback, useContext, useEffect, useRef, useState } from "preact/hooks";import type React from "react";import { List, type RowComponentProps } from "react-window";import { NoteContextDataMap } from "../../../components/note_context";import { t } from "../../../services/i18n";import { useActiveNoteContext, useGetContextData, useNoteProperty } from "../../react/hooks";-import RightPanelWidget from "../RightPanelWidget";+import RightPanelWidget, { NavigatedInNote } from "../RightPanelWidget";import { showCopyReferenceMenu } from "./PdfAnnotations";const ROW_HEIGHT = 180;@@ -135,6 +135,7 @@ function PdfPageCell({ pageNumber, isActive, thumbnail, requestThumbnail, scrollcopyReference: (page: number) => void;}) {const hasRequested = useRef(false);+ const onNavigated = useContext(NavigatedInNote);useEffect(() => {if (!thumbnail && !hasRequested.current) {@@ -146,7 +147,10 @@ function PdfPageCell({ pageNumber, isActive, thumbnail, requestThumbnail, scrollreturn (<divclassName={`pdf-page-item ${isActive ? 'active' : ''}`}- onClick={() => scrollToPage(pageNumber)}+ onClick={() => {+ scrollToPage(pageNumber);+ onNavigated?.();+ }}onContextMenu={(e) => showCopyReferenceMenu(e, () => copyReference(pageNumber))}><div className="pdf-page-thumbnail">