Skip to content

Build your personal knowledge base with Trilium Notes

4.8 KB
Raw Blame
type HistoryData = {
    files: {
        fingerprint: string;
        page: number;
        zoom: string;
        scrollLeft: number;
        scrollTop: number;
        rotation: number;
        sidebarView: number;
    }[];
};

/**
 * A single reusable signature as stored by pdf.js' `SignatureStorage` (`web/signature_storage.js`):
 * a human-readable description plus the compressed outline data produced by `SignatureExtractor`.
 */
type PdfSignatureEntry = {
    description: string;
    signatureData: unknown;
};

/** The pdf.js reusable signature library, keyed by signature UUID. Mirrors `localStorage["pdfjs.signature"]`. */
type PdfSignatureStore = Record<string, PdfSignatureEntry>;

interface Window {
    /**
     * By default, pdf.js will try to store information about the opened PDFs such as zoom and scroll position in local storage.
     * The Trilium alternative is to use attachments stored at note level.
     * 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?: HistoryData;

    /**
     * The reusable signature library used by the pdf.js signature tool. pdf.js keeps this in
     * per-browser `localStorage`; Trilium instead injects it here (from a synced option) so signatures
     * follow the user across devices. Reads/writes of `localStorage["pdfjs.signature"]` are intercepted
     * in the viewer and routed through this global + a `pdfjs-viewer-save-signatures` message.
     * Must be set early at startup, before pdf.js fully initializes.
     */
    TRILIUM_SIGNATURES?: PdfSignatureStore;

    /**
     * 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 WithContext {
    ntxId: string;
    noteId: string | null | undefined;
}

interface PdfDocumentModifiedMessage extends WithContext {
    type: "pdfjs-viewer-document-modified";
}

interface PdfDocumentBlobResultMessage extends WithContext {
    type: "pdfjs-viewer-blob";
    data: Uint8Array<ArrayBufferLike>;
}

interface PdfSaveViewHistoryMessage extends WithContext {
    type: "pdfjs-viewer-save-view-history";
    data: string;
}

interface PdfSaveSignaturesMessage extends WithContext {
    type: "pdfjs-viewer-save-signatures";
    /** JSON string of the full signature library, as written by pdf.js to `localStorage["pdfjs.signature"]`. */
    data: string;
}

interface PdfViewerTocMessage extends WithContext {
    type: "pdfjs-viewer-toc";
    data: PdfOutlineItem[];
}

interface PdfViewerActiveHeadingMessage extends WithContext {
    type: "pdfjs-viewer-active-heading";
    headingId: string;
}

interface PdfViewerPageInfoMessage extends WithContext {
    type: "pdfjs-viewer-page-info";
    totalPages: number;
    currentPage: number;
}

interface PdfViewerCurrentPageMessage extends WithContext {
    type: "pdfjs-viewer-current-page";
    currentPage: number;
}

interface PdfViewerThumbnailMessage extends WithContext {
    type: "pdfjs-viewer-thumbnail";
    pageNumber: number;
    dataUrl: string;
}

interface PdfAttachment {
    /** Identifies the attachment within the document; filenames are not necessarily unique. */
    id: string;
    filename: string;
    size: number;
}

interface PdfViewerAttachmentsMessage extends WithContext {
    type: "pdfjs-viewer-attachments";
    attachments: PdfAttachment[];
    downloadAttachment?: (id: string) => void;
}

interface PdfLayer {
    id: string;
    name: string;
    visible: boolean;
}

interface PdfViewerLayersMessage extends WithContext {
    type: "pdfjs-viewer-layers";
    layers: PdfLayer[];
    toggleLayer?: (layerId: string, visible: boolean) => void;
}

interface PdfAnnotationInfo {
    id: string;
    type: string;
    contents: string;
    highlightedText: string;
    author: string;
    pageNumber: number;
    color: string | null;
    creationDate: string | null;
    modificationDate: string | null;
}

interface PdfViewerAnnotationsMessage extends WithContext {
    type: "pdfjs-viewer-annotations";
    annotations: PdfAnnotationInfo[];
}

type PdfMessageEvent = MessageEvent<
    PdfDocumentModifiedMessage
    | PdfSaveViewHistoryMessage
    | PdfSaveSignaturesMessage
    | PdfViewerTocMessage
    | PdfViewerActiveHeadingMessage
    | PdfViewerPageInfoMessage
    | PdfViewerCurrentPageMessage
    | PdfViewerThumbnailMessage
    | PdfViewerAttachmentsMessage
    | PdfViewerLayersMessage
    | PdfViewerAnnotationsMessage
    | PdfDocumentBlobResultMessage
>;