fix(pdf): active context not changed when clicking preview

This commit is contained in:
Elian Doran 2025-12-30 09:47:02 +02:00
parent 52292cb5a5
commit 01f05ac6fd
No known key found for this signature in database
2 changed files with 25 additions and 3 deletions

View File

@ -10,13 +10,13 @@ import { TypeWidgetProps } from "./type_widget";
const TEXT_MAX_NUM_CHARS = 5000;
export default function FileTypeWidget({ note, parentComponent }: TypeWidgetProps) {
export default function FileTypeWidget({ note, parentComponent, noteContext }: TypeWidgetProps) {
const blob = useNoteBlob(note, parentComponent?.componentId);
if (blob?.content) {
return <TextPreview content={blob.content} />;
} else if (note.mime === "application/pdf") {
return <PdfPreview blob={blob} note={note} componentId={parentComponent?.componentId} />;
return <PdfPreview blob={blob} note={note} ntxId={noteContext?.ntxId} componentId={parentComponent?.componentId} />;
} else if (note.mime.startsWith("video/")) {
return <VideoPreview note={note} />;
} else if (note.mime.startsWith("audio/")) {

View File

@ -1,6 +1,7 @@
import { RefObject } from "preact";
import { useCallback, useEffect, useRef } from "preact/hooks";
import appContext from "../../../components/app_context";
import FBlob from "../../../entities/fblob";
import FNote from "../../../entities/fnote";
import server from "../../../services/server";
@ -14,10 +15,11 @@ const VARIABLE_WHITELIST = new Set([
"main-text-color"
]);
export default function PdfPreview({ note, blob, componentId }: {
export default function PdfPreview({ note, blob, componentId, ntxId }: {
note: FNote,
blob: FBlob | null | undefined,
componentId: string | undefined;
ntxId: string | null | undefined;
}) {
const iframeRef = useRef<HTMLIFrameElement>(null);
const { onLoad } = useStyleInjection(iframeRef);
@ -49,8 +51,28 @@ export default function PdfPreview({ note, blob, componentId }: {
}
}, [ blob ]);
// Trigger focus when iframe content is clicked (iframe focus doesn't bubble)
useEffect(() => {
const iframe = iframeRef.current;
if (!iframe) return;
const handleIframeClick = () => {
if (ntxId) {
appContext.tabManager.activateNoteContext(ntxId);
}
};
// Listen for clicks on the iframe's content window
const iframeDoc = iframe.contentWindow?.document;
if (iframeDoc) {
iframeDoc.addEventListener('click', handleIframeClick);
return () => iframeDoc.removeEventListener('click', handleIframeClick);
}
}, [ iframeRef.current?.contentWindow, ntxId ]);
return (historyConfig &&
<iframe
tabIndex={300}
ref={iframeRef}
class="pdf-preview"
src={`pdfjs/web/viewer.html?file=../../api/notes/${note.noteId}/open&lang=${locale}`}