Skip to content

Build your personal knowledge base with Trilium Notes

feat(pdf): make searchable using PDF.js builtin mechanism

Elian Doranf24d81ffa2b5
diff --git a/apps/client/src/types-pdfjs.d.ts b/apps/client/src/types-pdfjs.d.ts
index 3a4b3f16d9..cdc0927050 100644
--- a/apps/client/src/types-pdfjs.d.ts
+++ b/apps/client/src/types-pdfjs.d.ts
@@ -107,6 +107,22 @@ interface PdfViewerLayersMessage {
toggleLayer?: (layerId: string, visible: boolean) => void;
}
+interface PdfAnnotationInfo {
+ id: string;
+ type: string;
+ contents: string;
+ author: string;
+ pageNumber: number;
+ color: string | null;
+ creationDate: string | null;
+ modificationDate: string | null;
+}
+
+interface PdfViewerAnnotationsMessage {
+ type: "pdfjs-viewer-annotations";
+ annotations: PdfAnnotationInfo[];
+}
+
type PdfMessageEvent = MessageEvent<
PdfDocumentModifiedMessage
| PdfSaveViewHistoryMessage
@@ -117,5 +133,6 @@ type PdfMessageEvent = MessageEvent<
| PdfViewerThumbnailMessage
| PdfViewerAttachmentsMessage
| PdfViewerLayersMessage
+ | PdfViewerAnnotationsMessage
| PdfDocumentBlobResultMessage
>;
diff --git a/apps/client/src/widgets/ribbon/NoteActions.tsx b/apps/client/src/widgets/ribbon/NoteActions.tsx
index c518176cb6..94548d3194 100644
--- a/apps/client/src/widgets/ribbon/NoteActions.tsx
+++ b/apps/client/src/widgets/ribbon/NoteActions.tsx
@@ -76,7 +76,8 @@ export function NoteContextMenu({ note, noteContext, itemsAtStart, itemsNearNote
const [viewType] = useNoteLabel(note, "viewType");
const canBeConvertedToAttachment = note?.isEligibleForConversionToAttachment();
const isSourceView = noteContext?.viewScope?.viewMode === "source";
- const isSearchable = isSourceView || ["text", "code", "book", "mindMap", "doc", "spreadsheet"].includes(noteType);
+ const isSearchable = isSourceView || ["text", "code", "book", "mindMap", "doc", "spreadsheet"].includes(noteType)
+ || (noteType === "file" && note.mime === "application/pdf");
const isInOptionsOrHelp = note?.noteId.startsWith("_options") || note?.noteId.startsWith("_help");
const isExportableToImage = ["mermaid", "mindMap"].includes(noteType);
const isContentAvailable = note.isContentAvailable();
diff --git a/apps/client/src/widgets/type_widgets/file/Pdf.tsx b/apps/client/src/widgets/type_widgets/file/Pdf.tsx
index 311074ccdc..21d47a5df3 100644
--- a/apps/client/src/widgets/type_widgets/file/Pdf.tsx
+++ b/apps/client/src/widgets/type_widgets/file/Pdf.tsx
@@ -188,6 +188,13 @@ export default function PdfPreview({ note, blob, componentId, noteContext }: {
}, window.location.origin);
});
+ useTriliumEvent("findInText", () => {
+ if (!noteContext.isActive()) return;
+ iframeRef.current?.contentWindow?.postMessage({
+ type: "trilium-find"
+ }, window.location.origin);
+ });
+
return (historyConfig &&
<PdfViewer
iframeRef={iframeRef}
diff --git a/packages/pdfjs-viewer/src/custom.ts b/packages/pdfjs-viewer/src/custom.ts
index b65e05125c..a0d1e6e771 100644
--- a/packages/pdfjs-viewer/src/custom.ts
+++ b/packages/pdfjs-viewer/src/custom.ts
@@ -28,7 +28,7 @@ async function main() {
}
const app = window.PDFViewerApplication;
- managePrint();
+ manageParentCommands();
if (isEditable) {
app.eventBus.on("documentloaded", () => {
@@ -129,13 +129,17 @@ function manageDownload() {
});
}
-function managePrint() {
+function manageParentCommands() {
window.addEventListener("message", event => {
if (event.origin !== window.location.origin) return;
if (event.data?.type === "trilium-print") {
window.print();
}
+
+ if (event.data?.type === "trilium-find") {
+ window.PDFViewerApplication?.findBar?.open();
+ }
});
}
diff --git a/packages/pdfjs-viewer/src/typings.d.ts b/packages/pdfjs-viewer/src/typings.d.ts
index db12b178d0..88797a5ce4 100644
--- a/packages/pdfjs-viewer/src/typings.d.ts
+++ b/packages/pdfjs-viewer/src/typings.d.ts
@@ -44,6 +44,11 @@ declare global {
on(event: string, listener: (...args: any[]) => void): void;
dispatch(event: string, data?: any): void;
};
+ findBar?: {
+ open(): void;
+ close(): void;
+ toggle(): void;
+ };
store: ViewHistory;
};
PDFViewerApplicationOptions?: { set(name: string, value: any): void; }