Skip to content

Build your personal knowledge base with Trilium Notes

chore(pdfjs): address requested changes

Elian Doranfb4e912ed0cc
diff --git a/apps/client/src/widgets/type_widgets/file/Pdf.tsx b/apps/client/src/widgets/type_widgets/file/Pdf.tsx
index 7acbc6d6cf..ee731448a9 100644
--- a/apps/client/src/widgets/type_widgets/file/Pdf.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Pdf.tsx
@@ -24,11 +24,17 @@ export default function PdfPreview({ note, blob, componentId, noteContext }: {
getData() {
if (!iframeRef.current?.contentWindow) return undefined;
- return new Promise<Blob>((resolve) => {
+ return new Promise<Blob>((resolve, reject) => {
+ const timeout = setTimeout(() => {
+ reject(new Error("Timeout while waiting for blob response"));
+ }, 10_000);
+
const onMessageReceived = (event: PdfMessageEvent) => {
if (event.data.type !== "pdfjs-viewer-blob") return;
if (event.data.noteId !== note.noteId || event.data.ntxId !== noteContext.ntxId) return;
const blob = new Blob([event.data.data as Uint8Array<ArrayBuffer>], { type: note.mime });
+
+ clearTimeout(timeout);
window.removeEventListener("message", onMessageReceived);
resolve(blob);
};
@@ -36,7 +42,7 @@ export default function PdfPreview({ note, blob, componentId, noteContext }: {
window.addEventListener("message", onMessageReceived);
iframeRef.current?.contentWindow?.postMessage({
type: "trilium-request-blob",
- });
+ }, window.location.origin);
});
},
onContentChange() {
diff --git a/packages/pdfjs-viewer/scripts/build.ts b/packages/pdfjs-viewer/scripts/build.ts
index ff0cd98051..4a7e56880c 100644
--- a/packages/pdfjs-viewer/scripts/build.ts
+++ b/packages/pdfjs-viewer/scripts/build.ts
@@ -4,7 +4,7 @@ import { build as esbuild } from "esbuild";
import { LOCALES } from "@triliumnext/commons";
import { watch } from "chokidar";
import { readFileSync, writeFileSync } from "fs";
-import packageJson from "../package.json" with { type: "json "};
+import packageJson from "../package.json" with { type: "json " };
const build = new BuildHelper("packages/pdfjs-viewer");
const watchMode = process.argv.includes("--watch");
@@ -71,7 +71,7 @@ function patchCacheBuster(htmlFilePath: string) {
`<link rel="stylesheet" href="custom.css" />`,
`<link rel="stylesheet" href="custom.css?v=${version}" />`);
html = html.replace(
- `<script src="custom.mjs" type="module"></script>` ,
+ `<script src="custom.mjs" type="module"></script>`,
`<script src="custom.mjs?v=${version}" type="module"></script>`
);
diff --git a/packages/pdfjs-viewer/src/custom.ts b/packages/pdfjs-viewer/src/custom.ts
index 2532c8d767..95b56a3c2c 100644
--- a/packages/pdfjs-viewer/src/custom.ts
+++ b/packages/pdfjs-viewer/src/custom.ts
@@ -82,7 +82,7 @@ function manageSave() {
data,
ntxId: window.TRILIUM_NTX_ID,
noteId: window.TRILIUM_NOTE_ID
- } satisfies PdfDocumentBlobResultMessage, window.location.origin)
+ } satisfies PdfDocumentBlobResultMessage, window.location.origin);
}
});