Skip to content

Build your personal knowledge base with Trilium Notes

chore(pdfjs): log event bus

Elian Doran406232c47836
diff --git a/packages/pdfjs-viewer/src/custom.ts b/packages/pdfjs-viewer/src/custom.ts
index e69143379b..d3d9477886 100644
--- a/packages/pdfjs-viewer/src/custom.ts
+++ b/packages/pdfjs-viewer/src/custom.ts
@@ -1,3 +1,5 @@
+const LOG_EVENT_BUS = true;
+
async function main() {
// Wait for the PDF viewer application to be available.
while (!window.PDFViewerApplication) {
@@ -5,40 +7,56 @@ async function main() {
}
const app = window.PDFViewerApplication;
+ if (LOG_EVENT_BUS) {
+ patchEventBus();
+ }
+ app.eventBus.on("documentloaded", () => {
+ manageSave(app);
+ });
await app.initializedPromise;
+};
- app.eventBus.on("documentloaded", () => {
- const storage = app.pdfDocument.annotationStorage;
- let timeout = null;
-
- function debouncedSave() {
- if (timeout) {
- clearTimeout(timeout);
- }
- timeout = setTimeout(async () => {
- if (!storage) return;
- const data = await app.pdfDocument.saveDocument();
- window.parent.postMessage({
- type: "pdfjs-viewer-document-modified",
- data: data
- }, "*");
- storage.resetModified();
- timeout = null;
- }, 2_000);
+function manageSave(app: typeof window.PDFViewerApplication) {
+ const storage = app.pdfDocument.annotationStorage;
+ let timeout = null;
+
+ function debouncedSave() {
+ if (timeout) {
+ clearTimeout(timeout);
}
+ timeout = setTimeout(async () => {
+ if (!storage) return;
+ const data = await app.pdfDocument.saveDocument();
+ window.parent.postMessage({
+ type: "pdfjs-viewer-document-modified",
+ data: data
+ }, "*");
+ storage.resetModified();
+ timeout = null;
+ }, 2_000);
+ }
- app.eventBus.on("annotationeditorcommit", debouncedSave);
- app.eventBus.on("annotationeditorparamschanged", debouncedSave);
- app.eventBus.on("annotationeditorstateschanged", evt => {
- const { activeEditorId } = evt;
+ app.eventBus.on("annotationeditorcommit", debouncedSave);
+ app.eventBus.on("annotationeditorparamschanged", debouncedSave);
+ app.eventBus.on("annotationeditorstateschanged", evt => {
+ const { activeEditorId } = evt;
- // When activeEditorId becomes null, an editor was just committed
- if (activeEditorId === null) {
- debouncedSave();
- }
- });
+ // When activeEditorId becomes null, an editor was just committed
+ if (activeEditorId === null) {
+ debouncedSave();
+ }
});
-};
+}
+
+function patchEventBus() {
+ const eventBus = window.PDFViewerApplication.eventBus;
+ const originalDispatch = eventBus.dispatch.bind(eventBus);
+
+ eventBus.dispatch = (type: string, data?: any) => {
+ console.log("PDF.js event:", type, data);
+ return originalDispatch(type, data);
+ };
+}
main();
console.log("Custom script loaded");