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
client/media player: stop playback when players opened in a modal are closed
Adorian Dorane8bc3125f08f
diff --git a/apps/client/src/widgets/NoteDetail.tsx b/apps/client/src/widgets/NoteDetail.tsxindex 6c41d8a1e9..5399011923 100644--- a/apps/client/src/widgets/NoteDetail.tsx+++ b/apps/client/src/widgets/NoteDetail.tsx@@ -2,7 +2,7 @@ import "./NoteDetail.css";import clsx from "clsx";import { isValidElement, VNode } from "preact";-import { useEffect, useRef, useState } from "preact/hooks";+import { useContext, useEffect, useRef, useState } from "preact/hooks";import appContext from "../components/app_context";import NoteContext from "../components/note_context";@@ -20,6 +20,7 @@ import { useDelayedVisibility, useGetContextDataFrom, useNoteContext, useTriliumimport Icon from "./react/Icon";import NoItems from "./react/NoItems";import { NoteListWithLinks } from "./react/NoteList";+import { ContainerVisibilityContext } from "./react/react_utils";import { TypeWidgetProps } from "./type_widgets/type_widget";/**@@ -257,6 +258,10 @@ export function isContextInActiveTab(noteContext: NoteContext | undefined, activ* while the widget is visible, to avoid rendering in the background. When not visible, the DOM element is simply hidden.*/function NoteDetailWrapper({ Element, type, isVisible, isFullHeight, props }: { Element: (props: TypeWidgetProps) => VNode, type: ExtendedNoteType, isVisible: boolean, isFullHeight: boolean, props: TypeWidgetProps }) {+ // False when an enclosing dialog (e.g. the quick-edit popup) is hidden but kept in the DOM, so a widget+ // there is told it isn't displayed even though it's the context's current type — letting a media player+ // inside a closed popup stop, just like one in a navigated-away tab.+ const containerVisible = useContext(ContainerVisibilityContext);const [ cachedProps, setCachedProps ] = useState(props);useEffect(() => {@@ -274,7 +279,7 @@ function NoteDetailWrapper({ Element, type, isVisible, isFullHeight, props }: {height: isFullHeight ? "100%" : ""}}>- <Element {...cachedProps} isVisible={isVisible} />+ <Element {...cachedProps} isVisible={isVisible && containerVisible} /></div>);}diff --git a/apps/client/src/widgets/react/Modal.tsx b/apps/client/src/widgets/react/Modal.tsxindex 831354cc1f..651b0e9efa 100644--- a/apps/client/src/widgets/react/Modal.tsx+++ b/apps/client/src/widgets/react/Modal.tsx@@ -8,6 +8,7 @@ import { openDialog } from "../../services/dialog";import { t } from "../../services/i18n";import { openInAppHelpFromUrl } from "../../services/utils";import { useSyncedRef } from "./hooks";+import { ContainerVisibilityContext } from "./react_utils";interface CustomTitleBarButton {title: string;@@ -161,7 +162,7 @@ export default function Modal({ children, className, size, title, customTitleBarreturn (<div className={`modal fade mx-auto ${className}`} tabIndex={-1} style={dialogStyle} role="dialog" ref={modalRef}>- {(show || keepInDom) && <div className={clsx("modal-dialog", `modal-${size}`, {"modal-dialog-scrollable": scrollable, "modal-dialog-full-page-on-mobile": isFullPageOnMobile, "modal-content-with-sidebar": sidebar})} style={documentStyle} role="document">+ {(show || keepInDom) && <ContainerVisibilityContext.Provider value={show}><div className={clsx("modal-dialog", `modal-${size}`, {"modal-dialog-scrollable": scrollable, "modal-dialog-full-page-on-mobile": isFullPageOnMobile, "modal-content-with-sidebar": sidebar})} style={documentStyle} role="document"><div className={clsx("modal-content", sidebar && "modal-content-with-sidebar")}>{sidebar && <div className="modal-sidebar">{title && !hideSidebarHeader && <div className="modal-sidebar-header">@@ -211,7 +212,7 @@ export default function Modal({ children, className, size, title, customTitleBar)}</ModalMain></div>- </div>}+ </div></ContainerVisibilityContext.Provider>}</div>);}diff --git a/apps/client/src/widgets/react/react_utils.tsx b/apps/client/src/widgets/react/react_utils.tsxindex 3bbce9066f..5c2c56122f 100644--- a/apps/client/src/widgets/react/react_utils.tsx+++ b/apps/client/src/widgets/react/react_utils.tsx@@ -6,6 +6,13 @@ export const ParentComponent = createContext<Component | null>(null);export const NoteContextContext = createContext<NoteContext | null>(null);+/**+ * Whether the container (e.g. a dialog) holding the current note view is actually shown. False inside a dialog+ * that's hidden but kept mounted in the DOM (e.g. the quick-edit popup with `keepInDom`), so descendants like+ * media players can stop instead of playing on invisibly. Defaults to true — no enclosing dialog means shown.+ */+export const ContainerVisibilityContext = createContext(true);+/*** Takes in a React ref and returns a corresponding JQuery selector.*