fix(webview): doesn't activate note context on click in browser

This commit is contained in:
Elian Doran 2026-02-17 22:53:48 +02:00
parent c139ff776c
commit 555b138a90
No known key found for this signature in database

View File

@ -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}) {