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
refactor(pdfjs): add type safety for messages
Elian Doranb7af99c67169
diff --git a/apps/client/src/components/note_context.ts b/apps/client/src/components/note_context.tsindex 7672061678..6c7b184ddf 100644--- a/apps/client/src/components/note_context.ts+++ b/apps/client/src/components/note_context.ts@@ -32,11 +32,11 @@ export interface NoteContextDataMap {requestThumbnail(page: number): void;};pdfAttachments: {- attachments: Array<{ filename: string; size: number }>;+ attachments: PdfAttachment[];downloadAttachment(filename: string): void;};pdfLayers: {- layers: Array<{ id: string; name: string; visible: boolean }>;+ layers: PdfLayer[];toggleLayer(layerId: string, visible: boolean): void;};}diff --git a/apps/client/src/types-pdfjs.d.ts b/apps/client/src/types-pdfjs.d.tsindex 13cb91f6e6..ada0729cc7 100644--- a/apps/client/src/types-pdfjs.d.ts+++ b/apps/client/src/types-pdfjs.d.ts@@ -1,3 +1,15 @@+type HistoryData = {+ files: {+ fingerprint: string;+ page: number;+ zoom: string;+ scrollLeft: number;+ scrollTop: number;+ rotation: number;+ sidebarView: number;+ }[];+};+interface Window {/*** By default, pdf.js will try to store information about the opened PDFs such as zoom and scroll position in local storage.@@ -5,11 +17,97 @@ interface Window {* This variable represents the direct content used by the pdf.js viewer in its local storage key, but in plain JS object format.* The variable must be set early at startup, before pdf.js fully initializes.*/- TRILIUM_VIEW_HISTORY_STORE?: object;+ TRILIUM_VIEW_HISTORY_STORE?: HistoryData;/*** If set to true, hides the pdf.js viewer default sidebar containing the outline, page navigation, etc.* This needs to be set early in the main method.*/TRILIUM_HIDE_SIDEBAR?: boolean;++ TRILIUM_NOTE_ID: string;++ TRILIUM_NTX_ID: string | null | undefined;+}++interface PdfOutlineItem {+ title: string;+ level: number;+ dest: unknown;+ id: string;+ items: PdfOutlineItem[];+}++interface PdfDocumentModifiedMessage {+ type: "pdfjs-viewer-document-modified";+ data: Uint8Array<ArrayBufferLike>;+ ntxId: string;+ noteId: string | null | undefined;+}++interface PdfSaveViewHistoryMessage {+ type: "pdfjs-viewer-save-view-history";+ data: string;+}++interface PdfViewerTocMessage {+ type: "pdfjs-viewer-toc";+ data: PdfOutlineItem[];+}++interface PdfViewerActiveHeadingMessage {+ type: "pdfjs-viewer-active-heading";+ headingId: string;+}++interface PdfViewerPageInfoMessage {+ type: "pdfjs-viewer-page-info";+ totalPages: number;+ currentPage: number;+}++interface PdfViewerCurrentPageMessage {+ type: "pdfjs-viewer-current-page";+ currentPage: number;+}++interface PdfViewerThumbnailMessage {+ type: "pdfjs-viewer-thumbnail";+ pageNumber: number;+ dataUrl: string;+}++interface PdfAttachment {+ filename: string;+ size: number;}++interface PdfViewerAttachmentsMessage {+ type: "pdfjs-viewer-attachments";+ attachments: PdfAttachment[];+ downloadAttachment?: (fileName: string) => void;+}++interface PdfLayer {+ id: string;+ name: string;+ visible: boolean;+}++interface PdfViewerLayersMessage {+ type: "pdfjs-viewer-layers";+ layers: PdfLayer[];+ toggleLayer?: (layerId: string, visible: boolean) => void;+}++type PdfMessageEvent = MessageEvent<+ PdfDocumentModifiedMessage+ | PdfSaveViewHistoryMessage+ | PdfViewerTocMessage+ | PdfViewerActiveHeadingMessage+ | PdfViewerPageInfoMessage+ | PdfViewerCurrentPageMessage+ | PdfViewerThumbnailMessage+ | PdfViewerAttachmentsMessage+ | PdfViewerLayersMessage+>;diff --git a/apps/client/src/widgets/type_widgets/file/Pdf.tsx b/apps/client/src/widgets/type_widgets/file/Pdf.tsxindex 78ec869722..a0114a8478 100644--- a/apps/client/src/widgets/type_widgets/file/Pdf.tsx+++ b/apps/client/src/widgets/type_widgets/file/Pdf.tsx@@ -29,9 +29,9 @@ export default function PdfPreview({ note, blob, componentId, noteContext }: {const [ newLayout ] = useTriliumOptionBool("newLayout");useEffect(() => {- function handleMessage(event: MessageEvent) {- if (event.data?.type === "pdfjs-viewer-document-modified" && event.data?.data) {- const blob = new Blob([event.data.data], { type: note.mime });+ function handleMessage(event: PdfMessageEvent) {+ if (event.data?.type === "pdfjs-viewer-document-modified") {+ const blob = new Blob([event.data.data as Uint8Array<ArrayBuffer>], { type: note.mime });server.upload(`notes/${note.noteId}/file`, new File([blob], note.title, { type: note.mime }), componentId);}@@ -246,14 +246,6 @@ function cssVarsToString(vars: Record<string, string>) {.join('\n')}\n}`;}-interface PdfOutlineItem {- title: string;- level: number;- dest: unknown;- id: string;- items: PdfOutlineItem[];-}-interface PdfHeading {level: number;text: string;diff --git a/packages/pdfjs-viewer/src/attachments.ts b/packages/pdfjs-viewer/src/attachments.tsindex bfaf2eb52c..e9a35c60a6 100644--- a/packages/pdfjs-viewer/src/attachments.ts+++ b/packages/pdfjs-viewer/src/attachments.ts@@ -24,7 +24,7 @@ async function extractAndSendAttachments() {window.parent.postMessage({type: "pdfjs-viewer-attachments",attachments: []- }, window.location.origin);+ } satisfies PdfViewerAttachmentsMessage, window.location.origin);return;}@@ -42,13 +42,13 @@ async function extractAndSendAttachments() {filename: att.filename,size: att.size}))- }, window.location.origin);+ } satisfies PdfViewerAttachmentsMessage, window.location.origin);} catch (error) {console.error("Error extracting attachments:", error);window.parent.postMessage({type: "pdfjs-viewer-attachments",attachments: []- }, window.location.origin);+ } satisfies PdfViewerAttachmentsMessage, window.location.origin);}}diff --git a/packages/pdfjs-viewer/src/custom.ts b/packages/pdfjs-viewer/src/custom.tsindex 6a77f586c0..c58e4104b8 100644--- a/packages/pdfjs-viewer/src/custom.ts+++ b/packages/pdfjs-viewer/src/custom.ts@@ -66,8 +66,10 @@ function manageSave() {const data = await app.pdfDocument.saveDocument();window.parent.postMessage({type: "pdfjs-viewer-document-modified",- data: data- }, window.location.origin);+ data,+ ntxId: window.TRILIUM_NTX_ID,+ noteId: window.TRILIUM_NOTE_ID+ } satisfies PdfDocumentModifiedMessage, window.location.origin);storage.resetModified();timeout = null;}, 2_000);diff --git a/packages/pdfjs-viewer/src/layers.ts b/packages/pdfjs-viewer/src/layers.tsindex 3097aa1ec8..3c80c089c8 100644--- a/packages/pdfjs-viewer/src/layers.ts+++ b/packages/pdfjs-viewer/src/layers.ts@@ -29,7 +29,7 @@ async function extractAndSendLayers() {window.parent.postMessage({type: "pdfjs-viewer-layers",layers: []- }, window.location.origin);+ } satisfies PdfViewerLayersMessage, window.location.origin);return;}@@ -39,7 +39,7 @@ async function extractAndSendLayers() {window.parent.postMessage({type: "pdfjs-viewer-layers",layers: []- }, window.location.origin);+ } satisfies PdfViewerLayersMessage, window.location.origin);return;}@@ -78,13 +78,13 @@ async function extractAndSendLayers() {window.parent.postMessage({type: "pdfjs-viewer-layers",layers- }, window.location.origin);+ } satisfies PdfViewerLayersMessage, window.location.origin);} catch (error) {console.error("Error extracting layers:", error);window.parent.postMessage({type: "pdfjs-viewer-layers",layers: []- }, window.location.origin);+ } satisfies PdfViewerLayersMessage, window.location.origin);}}diff --git a/packages/pdfjs-viewer/src/pages.ts b/packages/pdfjs-viewer/src/pages.tsindex 4a4e65a96c..866e996208 100644--- a/packages/pdfjs-viewer/src/pages.ts+++ b/packages/pdfjs-viewer/src/pages.ts@@ -16,7 +16,7 @@ export function setupPdfPages() {window.parent.postMessage({type: "pdfjs-viewer-current-page",currentPage: evt.pageNumber- }, window.location.origin);+ } satisfies PdfViewerCurrentPageMessage, window.location.origin);});window.addEventListener("message", async(event) => {@@ -42,7 +42,7 @@ function sendPageInfo() {type: "pdfjs-viewer-page-info",totalPages: app.pdfDocument.numPages,currentPage: app.pdfViewer.currentPageNumber- }, window.location.origin);+ } satisfies PdfViewerPageInfoMessage, window.location.origin);}async function generateThumbnail(pageNumber: number) {@@ -75,7 +75,7 @@ async function generateThumbnail(pageNumber: number) {type: "pdfjs-viewer-thumbnail",pageNumber,dataUrl- }, window.location.origin);+ } satisfies PdfViewerThumbnailMessage, window.location.origin);} catch (error) {console.error(`Error generating thumbnail for page %d:`, pageNumber, error);}diff --git a/packages/pdfjs-viewer/src/persistence.ts b/packages/pdfjs-viewer/src/persistence.tsindex 7f0110799d..c28298a449 100644--- a/packages/pdfjs-viewer/src/persistence.ts+++ b/packages/pdfjs-viewer/src/persistence.ts@@ -42,7 +42,7 @@ function saveHistory(value: string) {window.parent.postMessage({type: "pdfjs-viewer-save-view-history",data: JSON.stringify(history)- }, window.location.origin);+ } satisfies PdfSaveViewHistoryMessage, window.location.origin);saveTimeout = null;}, 2_000);}diff --git a/packages/pdfjs-viewer/src/toc.ts b/packages/pdfjs-viewer/src/toc.tsindex dca8a94b46..9ba8777098 100644--- a/packages/pdfjs-viewer/src/toc.ts+++ b/packages/pdfjs-viewer/src/toc.ts@@ -11,7 +11,7 @@ export async function extractAndSendToc() {window.parent.postMessage({type: "pdfjs-viewer-toc",data: null- }, window.location.origin);+ } satisfies PdfViewerTocMessage, window.location.origin);return;}@@ -26,12 +26,12 @@ export async function extractAndSendToc() {window.parent.postMessage({type: "pdfjs-viewer-toc",data: toc- }, window.location.origin);+ } satisfies PdfViewerTocMessage, window.location.origin);} catch (error) {window.parent.postMessage({type: "pdfjs-viewer-toc",data: null- }, window.location.origin);+ } satisfies PdfViewerTocMessage, window.location.origin);}}@@ -165,7 +165,7 @@ export function setupActiveHeadingTracking() {window.parent.postMessage({type: "pdfjs-viewer-active-heading",headingId: activeHeadingId- }, window.location.origin);+ } satisfies PdfViewerActiveHeadingMessage, window.location.origin);}}