@@ -4,9 +4,8 @@ import appContext from "../../../components/app_context";
44import type NoteContext from "../../../components/note_context" ;
55import FBlob from "../../../entities/fblob" ;
66import FNote from "../../../entities/fnote" ;
7- import server from "../../../services/server" ;
87import { useViewModeConfig } from "../../collections/NoteList" ;
9- import { useTriliumEvent } from "../../react/hooks" ;
8+ import { useBlobEditorSpacedUpdate , useTriliumEvent } from "../../react/hooks" ;
109import PdfViewer from "./PdfViewer" ;
1110
1211export default function PdfPreview ( { note, blob, componentId, noteContext } : {
@@ -18,12 +17,48 @@ export default function PdfPreview({ note, blob, componentId, noteContext }: {
1817 const iframeRef = useRef < HTMLIFrameElement > ( null ) ;
1918 const historyConfig = useViewModeConfig < HistoryData > ( note , "pdfHistory" ) ;
2019
20+ const spacedUpdate = useBlobEditorSpacedUpdate ( {
21+ note,
22+ noteType : "file" ,
23+ noteContext,
24+ getData ( ) {
25+ if ( ! iframeRef . current ?. contentWindow ) return undefined ;
26+
27+ return new Promise < Blob > ( ( resolve , reject ) => {
28+ const timeout = setTimeout ( ( ) => {
29+ reject ( new Error ( "Timeout while waiting for blob response" ) ) ;
30+ } , 10_000 ) ;
31+
32+ const onMessageReceived = ( event : PdfMessageEvent ) => {
33+ if ( event . data . type !== "pdfjs-viewer-blob" ) return ;
34+ if ( event . data . noteId !== note . noteId || event . data . ntxId !== noteContext . ntxId ) return ;
35+ const blob = new Blob ( [ event . data . data as Uint8Array < ArrayBuffer > ] , { type : note . mime } ) ;
36+
37+ clearTimeout ( timeout ) ;
38+ window . removeEventListener ( "message" , onMessageReceived ) ;
39+ resolve ( blob ) ;
40+ } ;
41+
42+ window . addEventListener ( "message" , onMessageReceived ) ;
43+ iframeRef . current ?. contentWindow ?. postMessage ( {
44+ type : "trilium-request-blob" ,
45+ } , window . location . origin ) ;
46+ } ) ;
47+ } ,
48+ onContentChange ( ) {
49+ if ( iframeRef . current ?. contentWindow ) {
50+ iframeRef . current . contentWindow . location . reload ( ) ;
51+ }
52+ } ,
53+ replaceWithoutRevision : true
54+ } ) ;
55+
2156 useEffect ( ( ) => {
2257 function handleMessage ( event : PdfMessageEvent ) {
2358 if ( event . data ?. type === "pdfjs-viewer-document-modified" ) {
24- const blob = new Blob ( [ event . data . data as Uint8Array < ArrayBuffer > ] , { type : note . mime } ) ;
2559 if ( event . data . noteId === note . noteId && event . data . ntxId === noteContext . ntxId ) {
26- server . upload ( `notes/${ note . noteId } /file` , new File ( [ blob ] , note . title , { type : note . mime } ) , componentId ) ;
60+ spacedUpdate . resetUpdateTimer ( ) ;
61+ spacedUpdate . scheduleUpdate ( ) ;
2762 }
2863 }
2964
@@ -138,13 +173,6 @@ export default function PdfPreview({ note, blob, componentId, noteContext }: {
138173 } ;
139174 } , [ note , historyConfig , componentId , blob , noteContext ] ) ;
140175
141- // Refresh when blob changes.
142- useEffect ( ( ) => {
143- if ( iframeRef . current ?. contentWindow ) {
144- iframeRef . current . contentWindow . location . reload ( ) ;
145- }
146- } , [ blob ] ) ;
147-
148176 useTriliumEvent ( "customDownload" , ( { ntxId } ) => {
149177 if ( ntxId !== noteContext . ntxId ) return ;
150178 iframeRef . current ?. contentWindow ?. postMessage ( {
@@ -171,6 +199,7 @@ export default function PdfPreview({ note, blob, componentId, noteContext }: {
171199 } ) ;
172200 }
173201 } }
202+ editable
174203 />
175204 ) ;
176205}
0 commit comments