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(revisions): add mobile master-detail flow
Elian Doran7c76a0758303
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex 2e2fb0f8a3..2579d1fcea 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -341,6 +341,7 @@},"revisions": {"note_revisions": "Note Revisions",+ "back": "Back","delete_all_revisions": "Delete all revisions of this note","delete_all_button": "Delete all revisions","help_title": "Help on Note Revisions",@@ -2456,9 +2457,6 @@"popup-editor": {"maximize": "Switch to full editor"},- "tree-popup": {- "title": "Configure"- },"server": {"unknown_http_error_title": "Communication error with the server","unknown_http_error_content": "Status code: {{statusCode}}\nURL: {{method}} {{url}}\nMessage: {{message}}",diff --git a/apps/client/src/widgets/dialogs/OptionsDialog.tsx b/apps/client/src/widgets/dialogs/OptionsDialog.tsxindex 4dca179be6..c6a2ab1014 100644--- a/apps/client/src/widgets/dialogs/OptionsDialog.tsx+++ b/apps/client/src/widgets/dialogs/OptionsDialog.tsx@@ -1,6 +1,6 @@import "./OptionsDialog.css";-import { useCallback, useContext, useEffect, useRef, useState } from "preact/hooks";+import { useCallback, useContext, useRef, useState } from "preact/hooks";import appContext from "../../components/app_context";import NoteContext from "../../components/note_context";@@ -9,7 +9,7 @@ import utils from "../../services/utils";import NoteDetail from "../NoteDetail";import ActionButton from "../react/ActionButton";import FormList, { FormListItem } from "../react/FormList";-import { useChildNotes, useContainedLinkNavigation, useNoteContext, useTriliumEvent, useWindowSize } from "../react/hooks";+import { useChildNotes, useContainedLinkNavigation, useMobileMasterDetail, useNoteContext, useTriliumEvent } from "../react/hooks";import Modal from "../react/Modal";import { NoteContextContext, ParentComponent } from "../react/react_utils";import SettingsNavigation from "../type_widgets/options/components/SettingsNavigation";@@ -17,10 +17,6 @@ import SettingsNavigation from "../type_widgets/options/components/SettingsNavig/** The settings page shown when no specific section was requested and none was viewed yet this session. */const DEFAULT_SECTION = "_optionsAppearance";-/** Mobile viewports at least this wide (tablets) keep the desktop-style side-by-side sidebar- * layout; narrower ones get the master-detail flow. */-const TABLET_MIN_WIDTH = 768;-/*** The settings dialog, opened via the `showOptions` command. Settings open in a dialog rather than* a hoisted tab (which confused users by seemingly hiding the note tree): the full-height sidebar@@ -36,29 +32,9 @@ export default function OptionsDialog() {// Remembers the page last viewed this session so reopening the dialog lands there instead of// always on Appearance. Kept in component state (resets on reload), not persisted.const [ lastSection, setLastSection ] = useState<string | null>(null);- // Which half of the mobile master-detail flow is visible; has no effect on desktop.- const [ mobileView, setMobileView ] = useState<"list" | "page">("list");- // Direction of the in-flight slide between the two mobile views, or null when at rest. While- // set, both panes stay rendered so the outgoing one can slide away as the incoming one slides- // in; cleared when the slide animation finishes.- const [ mobileTransition, setMobileTransition ] = useState<"to-list" | "to-page" | null>(null);const modalRef = useRef<HTMLDivElement>(null);const isMobile = utils.isMobile();- const { windowWidth } = useWindowSize();- // Only narrow mobile viewports get the master-detail flow; tablets keep the sidebar layout.- const isMasterDetail = isMobile && windowWidth < TABLET_MIN_WIDTH;-- // Switches between the mobile master/detail views with a slide. The initial view on open is- // set directly (without animating) by the showOptions handler instead.- const switchMobileView = useCallback((view: "list" | "page") => {- if (view === mobileView) return;- setMobileView(view);- // With animations globally disabled there is no animationend to clear the transition,- // so switch directly. Outside the master-detail flow there is nothing to animate.- if (isMasterDetail && !document.body.classList.contains("motion-disabled")) {- setMobileTransition(view === "page" ? "to-page" : "to-list");- }- }, [ mobileView, isMasterDetail ]);+ const { isMasterDetail, mobileView, switchMobileView, resetMobileView } = useMobileMasterDetail(modalRef, "options-slide");useTriliumEvent("showOptions", async ({ section }) => {const noteContext = new NoteContext("_options-dialog");@@ -68,35 +44,10 @@ export default function OptionsDialog() {noteContext.triggerEvent = (name, data) => parentComponent?.handleEventInChildren(name, data);setNoteContext(noteContext);// Requesting a specific section (e.g. "set up a password") skips the mobile master list.- setMobileView(section ? "page" : "list");- setMobileTransition(null);+ resetMobileView(section ? "page" : "list");setShown(true);});- // Bootstrap adds its own classes (e.g. `show`) to the modal element at runtime, so the- // className prop must stay static — rewriting it from a render would wipe them and visually- // dismiss the dialog. Toggle the mobile view classes directly on the element instead.- useEffect(() => {- modalRef.current?.classList.toggle("mobile-master-detail", isMasterDetail);- modalRef.current?.classList.toggle("mobile-view-list", mobileView === "list");- modalRef.current?.classList.toggle("mobile-view-page", mobileView === "page");- modalRef.current?.classList.toggle("mobile-transition-to-list", mobileTransition === "to-list");- modalRef.current?.classList.toggle("mobile-transition-to-page", mobileTransition === "to-page");- }, [ isMasterDetail, mobileView, mobileTransition ]);-- // End the view transition once the slide finishes (animationend bubbles up from the panes).- useEffect(() => {- const modalElement = modalRef.current;- if (!modalElement) return;- function onAnimationEnd(e: AnimationEvent) {- if (e.animationName.startsWith("options-slide")) {- setMobileTransition(null);- }- }- modalElement.addEventListener("animationend", onAnimationEnd);- return () => modalElement.removeEventListener("animationend", onAnimationEnd);- }, []);-// Keep navigation between settings pages (sidebar entries, "Related settings" links) inside the// dialog; links to regular notes open in the quick-edit popup instead.useContainedLinkNavigation(modalRef, useCallback((notePath, viewScope) => {diff --git a/apps/client/src/widgets/dialogs/revisions.css b/apps/client/src/widgets/dialogs/revisions.cssindex 3580ab1b46..a684e063c6 100644--- a/apps/client/src/widgets/dialogs/revisions.css+++ b/apps/client/src/widgets/dialogs/revisions.css@@ -42,6 +42,16 @@ body.desktop .revisions-dialog {flex-direction: column;}+ /* Wraps the toolbar + preview so it can fill the body (desktop) and act as the detail pane of+ the mobile master-detail flow. */+ .revision-detail {+ display: flex;+ flex-direction: column;+ flex: 1;+ min-width: 0;+ min-height: 0;+ }+.modal-sidebar {background-color: var(--card-background-color);}@@ -246,3 +256,147 @@ body.desktop .revisions-dialog {}}++/* On narrow mobile viewports the dialog becomes a master-detail flow instead of the bottom-strip+ sidebar layout (the `mobile-master-detail` class, toggled by useMobileMasterDetail, tracks this):+ the revision list is rendered as a pane inside the modal body (the Modal sidebar is unused) so the+ list and the selected revision can slide over each other when switching between them. */+body.mobile .revisions-dialog.mobile-master-detail {+ .modal-body {+ position: relative;+ padding: 0;+ /* The panes scroll themselves; this also keeps the sliding panes contained. */+ overflow: hidden;+ }++ .revision-mobile-nav {+ height: 100%;+ overflow: auto;+ }++ .revision-detail {+ height: 100%;+ overflow: hidden;+ }++ /* The back button + revision date in the custom header replace the static "Note revisions" title. */+ .modal-header > .modal-title {+ display: none;+ }++ /* At rest only the active pane is displayed; during a transition both stay visible so the+ outgoing pane can slide away as the incoming one slides in. */+ &.mobile-view-list:not(.mobile-transition-to-list) .revision-detail {+ display: none;+ }++ &.mobile-view-page:not(.mobile-transition-to-page) .revision-mobile-nav {+ display: none;+ }++ /* Directional slide: opening a revision, the list slides out towards the start side while the+ detail slides in from the end side; going back reverses the directions. The detail pane stacks+ above the list, and `forwards` holds the outgoing pane off-screen until the transition classes+ are removed after animationend. The global `motion-disabled` rule suppresses the animations;+ the dialog then switches views directly without entering a transition. */+ &.mobile-transition-to-page,+ &.mobile-transition-to-list {+ .revision-mobile-nav,+ .revision-detail {+ position: absolute;+ inset: 0;+ background-color: var(--modal-background-color);+ }++ .revision-detail {+ z-index: 1;+ }+ }++ &.mobile-transition-to-page {+ .revision-detail {+ animation: revision-slide-in-end 0.35s ease-in-out forwards;+ }++ .revision-mobile-nav {+ animation: revision-slide-out-start 0.35s ease-in-out forwards;+ }+ }++ &.mobile-transition-to-list {+ .revision-detail {+ animation: revision-slide-out-end 0.35s ease-in-out forwards;+ }++ .revision-mobile-nav {+ animation: revision-slide-in-start 0.35s ease-in-out forwards;+ }+ }+}++body[dir=rtl].mobile .revisions-dialog.mobile-master-detail.mobile-transition-to-page {+ .revision-detail {+ animation-name: revision-slide-in-start;+ }++ .revision-mobile-nav {+ animation-name: revision-slide-out-end;+ }+}++body[dir=rtl].mobile .revisions-dialog.mobile-master-detail.mobile-transition-to-list {+ .revision-detail {+ animation-name: revision-slide-out-start;+ }++ .revision-mobile-nav {+ animation-name: revision-slide-in-end;+ }+}++.revision-mobile-header {+ display: flex;+ align-items: center;+ gap: 4px;+ flex-grow: 1;+ min-width: 0;++ /* Decorative history icon shown in the list view. It borrows `icon-action` so it keeps exactly+ the back button's footprint (the title doesn't shift between views), with the interactive+ affordances neutralized. */+ .revision-header-icon {+ display: inline-flex;+ align-items: center;+ justify-content: center;+ pointer-events: none;+ cursor: default;+ }++ .revision-mobile-title {+ margin: 0;+ overflow: hidden;+ white-space: nowrap;+ text-overflow: ellipsis;+ flex-grow: 1;+ }+}++@keyframes revision-slide-in-end {+ from { transform: translateX(100%); opacity: 0; }+ to { transform: translateX(0); opacity: 1; }+}++@keyframes revision-slide-in-start {+ from { transform: translateX(-100%); opacity: 0; }+ to { transform: translateX(0); opacity: 1; }+}++@keyframes revision-slide-out-end {+ from { transform: translateX(0); opacity: 1; }+ to { transform: translateX(100%); opacity: 0; }+}++@keyframes revision-slide-out-start {+ from { transform: translateX(0); opacity: 1; }+ to { transform: translateX(-100%); opacity: 0; }+}diff --git a/apps/client/src/widgets/dialogs/revisions.tsx b/apps/client/src/widgets/dialogs/revisions.tsxindex c3d13fab25..d5c1e63b5f 100644--- a/apps/client/src/widgets/dialogs/revisions.tsx+++ b/apps/client/src/widgets/dialogs/revisions.tsx@@ -4,7 +4,7 @@ import { dayjs, type RevisionItem, type RevisionPojo } from "@triliumnext/commonimport clsx from "clsx";import { diffWords } from "diff";import HtmlDiff from "htmldiff-js";-import { Fragment } from "preact";+import { ComponentChildren, Fragment } from "preact";import type { CSSProperties } from "preact/compat";import { Dispatch, StateUpdater, useEffect, useRef, useState } from "preact/hooks";@@ -25,7 +25,7 @@ import Button from "../react/Button";import Dropdown from "../react/Dropdown";import FormList, { FormDropdownDivider, FormListItem } from "../react/FormList";import FormToggle from "../react/FormToggle";-import { useTriliumEvent } from "../react/hooks";+import { useMobileMasterDetail, useTriliumEvent } from "../react/hooks";import Modal from "../react/Modal";import NoItems from "../react/NoItems";import { RawHtmlBlock, SanitizedHtml } from "../react/RawHtml";@@ -41,11 +41,15 @@ export default function RevisionsDialog() {const [ shown, setShown ] = useState(false);const [ showDiff, setShowDiff ] = useState(true);const [ refreshCounter, setRefreshCounter ] = useState(0);+ const modalRef = useRef<HTMLDivElement>(null);+ const { isMasterDetail, mobileView, switchMobileView, resetMobileView } = useMobileMasterDetail(modalRef, "revision-slide");useTriliumEvent("showRevisions", async ({ noteId }) => {const note = await getNote(noteId);if (note) {setNote(note);+ // Mobile opens on the revision list; selecting one slides to its detail.+ resetMobileView("list");setShown(true);}});@@ -104,70 +108,124 @@ export default function RevisionsDialog() {);}+ const selectRevision = (revisionId: string) => {+ const correspondingRevision = (revisions ?? []).find((r) => r.revisionId === revisionId);+ if (correspondingRevision) {+ setCurrentRevision(correspondingRevision);+ }+ switchMobileView("page");+ };++ const revisionsList = (+ <RevisionsList+ revisions={revisions ?? []}+ onSelect={selectRevision}+ currentRevision={currentRevision}+ />+ );++ const menu = note && (+ <RevisionsMenu+ note={note}+ onRevisionSaved={() => {+ setRefreshCounter(c => c + 1);+ setCurrentRevision(undefined);+ }}+ onAllDeleted={() => {+ setRevisions([]);+ setCurrentRevision(undefined);+ }}+ hasRevisions={true}+ />+ );+return (<Modal+ modalRef={modalRef}className="revisions-dialog"size="xl"title={t("revisions.note_revisions")}helpPageId="vZWERwf8U3nx"- header={note && (- <RevisionsMenu- note={note}- onRevisionSaved={() => {- setRefreshCounter(c => c + 1);- setCurrentRevision(undefined);- }}- onAllDeleted={() => {- setRevisions([]);- setCurrentRevision(undefined);- }}- hasRevisions={true}- />- )}- sidebar={- <RevisionsList- revisions={revisions ?? []}- onSelect={(revisionId) => {- const correspondingRevision = (revisions ?? []).find((r) => r.revisionId === revisionId);- if (correspondingRevision) {- setCurrentRevision(correspondingRevision);- }- }}- currentRevision={currentRevision}- />- }+ header={isMasterDetail+ ? (+ <RevisionsMobileHeader+ inPage={mobileView === "page"}+ onBack={() => switchMobileView("list")}+ revisionItem={currentRevision}+ menu={menu}+ />+ )+ : menu}+ sidebar={isMasterDetail ? undefined : revisionsList}onHidden={onHidden}show={shown}>- <RevisionToolbar- revisionItem={currentRevision}- showDiff={showDiff}- setShowDiff={setShowDiff}- setShown={setShown}- onRevisionDeleted={() => {- setRefreshCounter(c => c + 1);- setCurrentRevision(undefined);- }}- onDescriptionUpdated={(revisionId, description) => {- setRevisions(prev => prev?.map(r =>- r.revisionId === revisionId ? { ...r, description } : r- ));- if (currentRevision?.revisionId === revisionId) {- setCurrentRevision({ ...currentRevision, description });- }- }}- />- <div className="revision-content-wrapper">- <RevisionPreview- noteContent={noteContent}+ {isMasterDetail && (+ <div className="revision-mobile-nav">+ {revisionsList}+ </div>+ )}+ <div className="revision-detail">+ <RevisionToolbarrevisionItem={currentRevision}showDiff={showDiff}+ setShowDiff={setShowDiff}+ setShown={setShown}+ onRevisionDeleted={() => {+ setRefreshCounter(c => c + 1);+ setCurrentRevision(undefined);+ // The detail is now empty; on mobile slide back to the list.+ switchMobileView("list");+ }}+ onDescriptionUpdated={(revisionId, description) => {+ setRevisions(prev => prev?.map(r =>+ r.revisionId === revisionId ? { ...r, description } : r+ ));+ if (currentRevision?.revisionId === revisionId) {+ setCurrentRevision({ ...currentRevision, description });+ }+ }}/>+ <div className="revision-content-wrapper">+ <RevisionPreview+ noteContent={noteContent}+ revisionItem={currentRevision}+ showDiff={showDiff}+ />+ </div></div></Modal>);}+/**+ * Replaces the static "Note revisions" title on narrow mobile (the master-detail flow). In the list+ * view a decorative history icon precedes the dialog title and the actions menu stays available; in+ * the page view a back button returns to the list, followed by the revision's date.+ */+function RevisionsMobileHeader({ inPage, onBack, revisionItem, menu }: {+ inPage: boolean,+ onBack: () => void,+ revisionItem?: RevisionItem,+ menu?: ComponentChildren+}) {+ const pageTitle = revisionItem?.dateCreated+ ? dayjs(revisionItem.dateCreated).format("MMM D, YYYY · HH:mm")+ : t("revisions.note_revisions");++ return (+ <div className="revision-mobile-header">+ {inPage ? (+ <ActionButton icon="bx bx-chevron-left" text={t("revisions.back")} onClick={onBack} />+ ) : (+ <span className="revision-header-icon icon-action bx bx-history" aria-hidden="true" />+ )}+ <h5 className="revision-mobile-title">{inPage ? pageTitle : t("revisions.note_revisions")}</h5>+ {!inPage && menu}+ </div>+ );+}+function RevisionsMenu({ note, onRevisionSaved, onAllDeleted, hasRevisions }: {note: FNote,onRevisionSaved: () => void,diff --git a/apps/client/src/widgets/react/hooks.tsx b/apps/client/src/widgets/react/hooks.tsxindex d0edb8b616..9c051fe29d 100644--- a/apps/client/src/widgets/react/hooks.tsx+++ b/apps/client/src/widgets/react/hooks.tsx@@ -944,6 +944,79 @@ export function useWindowSize() {return size;}+/** Mobile viewports at least this wide (tablets) keep a side-by-side layout; narrower ones get the+ * master-detail flow. */+export const MASTER_DETAIL_TABLET_MIN_WIDTH = 768;++export interface MobileMasterDetail {+ /** True on narrow mobile viewports where the list and detail collapse into a master-detail flow. */+ isMasterDetail: boolean;+ /** Which half of the master-detail flow is currently visible. */+ mobileView: "list" | "page";+ /** Switch between the two views with a slide animation. */+ switchMobileView: (view: "list" | "page") => void;+ /** Set the view directly without animating (e.g. when (re)opening the dialog). */+ resetMobileView: (view: "list" | "page") => void;+}++/**+ * Drives the mobile master-detail flow shared by dialogs that pair a list with a detail pane (e.g.+ * the settings and revisions dialogs): it tracks which pane is visible, animates the slide between+ * them, and toggles the corresponding classes on the modal element (`mobile-master-detail`,+ * `mobile-view-{list,page}`, `mobile-transition-to-{list,page}`). The dialog supplies the layout and+ * slide keyframes in CSS; the keyframe names must start with `slideAnimationPrefix` so the in-flight+ * transition can be cleared once the slide finishes.+ */+export function useMobileMasterDetail(modalRef: RefObject<HTMLElement>, slideAnimationPrefix: string): MobileMasterDetail {+ const [ mobileView, setMobileView ] = useState<"list" | "page">("list");+ // Direction of the in-flight slide between the two views, or null when at rest. While set, both+ // panes stay rendered so the outgoing one can slide away as the incoming one slides in.+ const [ mobileTransition, setMobileTransition ] = useState<"to-list" | "to-page" | null>(null);+ const isMobile = utils.isMobile();+ const { windowWidth } = useWindowSize();+ const isMasterDetail = isMobile && windowWidth < MASTER_DETAIL_TABLET_MIN_WIDTH;++ const switchMobileView = useCallback((view: "list" | "page") => {+ if (view === mobileView) return;+ setMobileView(view);+ // With animations globally disabled there is no animationend to clear the transition, so+ // switch directly. Outside the master-detail flow there is nothing to animate.+ if (isMasterDetail && !document.body.classList.contains("motion-disabled")) {+ setMobileTransition(view === "page" ? "to-page" : "to-list");+ }+ }, [ mobileView, isMasterDetail ]);++ const resetMobileView = useCallback((view: "list" | "page") => {+ setMobileView(view);+ setMobileTransition(null);+ }, []);++ // Bootstrap adds its own classes (e.g. `show`) to the modal element at runtime, so the className+ // prop must stay static; toggle the mobile view classes directly on the element instead.+ useEffect(() => {+ modalRef.current?.classList.toggle("mobile-master-detail", isMasterDetail);+ modalRef.current?.classList.toggle("mobile-view-list", mobileView === "list");+ modalRef.current?.classList.toggle("mobile-view-page", mobileView === "page");+ modalRef.current?.classList.toggle("mobile-transition-to-list", mobileTransition === "to-list");+ modalRef.current?.classList.toggle("mobile-transition-to-page", mobileTransition === "to-page");+ }, [ isMasterDetail, mobileView, mobileTransition ]);++ // End the view transition once the slide finishes (animationend bubbles up from the panes).+ useEffect(() => {+ const modalElement = modalRef.current;+ if (!modalElement) return;+ function onAnimationEnd(e: AnimationEvent) {+ if (e.animationName.startsWith(slideAnimationPrefix)) {+ setMobileTransition(null);+ }+ }+ modalElement.addEventListener("animationend", onAnimationEnd);+ return () => modalElement.removeEventListener("animationend", onAnimationEnd);+ }, [ slideAnimationPrefix ]);++ return { isMasterDetail, mobileView, switchMobileView, resetMobileView };+}+// Workaround for https://github.com/twbs/bootstrap/issues/37474// Bootstrap's dispose() sets ALL properties to null. But pending animation callbacks// (scheduled via setTimeout) can still fire and crash when accessing null properties.