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(webview): doesn't activate note context on click in browser
Elian Doran555b138a906e
diff --git a/apps/client/src/widgets/type_widgets/WebView.tsx b/apps/client/src/widgets/type_widgets/WebView.tsxindex dd3ab43c9e..b624a62800 100644--- a/apps/client/src/widgets/type_widgets/WebView.tsx+++ b/apps/client/src/widgets/type_widgets/WebView.tsx@@ -1,7 +1,8 @@import "./WebView.css";-import { useCallback, useState } from "preact/hooks";+import { useCallback, useEffect, useRef, useState } from "preact/hooks";+import appContext from "../../components/app_context";import FNote from "../../entities/fnote";import attributes from "../../services/attributes";import { t } from "../../services/i18n";@@ -17,7 +18,7 @@ import { TypeWidgetProps } from "./type_widget";const isElectron = utils.isElectron();const HELP_PAGE = "1vHRoWCEjj0L";-export default function WebView({ note }: TypeWidgetProps) {+export default function WebView({ note, ntxId }: TypeWidgetProps) {const [ webViewSrc ] = useNoteLabel(note, "webViewSrc");const [ disabledWebViewSrc ] = useNoteLabel(note, "disabled:webViewSrc");@@ -29,15 +30,36 @@ export default function WebView({ note }: TypeWidgetProps) {return <SetupWebView note={note} />;}- return <WebViewContent src={webViewSrc} />;+ return isElectron+ ? <DesktopWebView src={webViewSrc} />+ : <BrowserWebView src={webViewSrc} ntxId={ntxId} />;}-function WebViewContent({ src }: { src: string }) {- if (!isElectron) {- return <iframe src={src} class="note-detail-web-view-content" sandbox="allow-same-origin allow-scripts allow-popups" />;- }+function DesktopWebView({ src }: { src: string }) {return <webview src={src} class="note-detail-web-view-content" />;+}++function BrowserWebView({ src, ntxId }: { src: string, ntxId: string | null | undefined }) {+ const iframeRef = useRef<HTMLIFrameElement>(null);++ useEffect(() => {+ function onBlur() {+ if (document.activeElement === iframeRef.current && ntxId) {+ appContext.tabManager.activateNoteContext(ntxId);+ }+ }+ window.addEventListener("blur", onBlur);+ return () => {+ window.removeEventListener("blur", onBlur);+ };+ }, [ ntxId ]);++ return <iframe+ ref={iframeRef}+ src={src}+ class="note-detail-web-view-content"+ sandbox="allow-same-origin allow-scripts allow-popups" />;}function SetupWebView({note}: {note: FNote}) {