mirror of
https://github.com/zadam/trilium.git
synced 2025-12-30 03:04:29 +01:00
chore(pdfjs): first attempt at intercepting store
This commit is contained in:
parent
7836de3f08
commit
fc0ea36cf3
@ -1,4 +1,4 @@
|
||||
const LOG_EVENT_BUS = true;
|
||||
const LOG_EVENT_BUS = false;
|
||||
|
||||
async function main() {
|
||||
// Wait for the PDF viewer application to be available.
|
||||
@ -11,12 +11,14 @@ async function main() {
|
||||
patchEventBus();
|
||||
}
|
||||
app.eventBus.on("documentloaded", () => {
|
||||
manageSave(app);
|
||||
interceptViewHistory();
|
||||
manageSave();
|
||||
});
|
||||
await app.initializedPromise;
|
||||
};
|
||||
|
||||
function manageSave(app: typeof window.PDFViewerApplication) {
|
||||
function manageSave() {
|
||||
const app = window.PDFViewerApplication;
|
||||
const storage = app.pdfDocument.annotationStorage;
|
||||
let timeout = null;
|
||||
|
||||
@ -49,6 +51,22 @@ function manageSave(app: typeof window.PDFViewerApplication) {
|
||||
});
|
||||
}
|
||||
|
||||
function interceptViewHistory() {
|
||||
const app = window.PDFViewerApplication;
|
||||
let activeFingerprint: string = app.pdfDocument.fingerprints[0];
|
||||
|
||||
const store = app.store;
|
||||
store._writeToStorage = async function() {
|
||||
const fileEntry = store.database.files?.find(f => f.fingerprint === activeFingerprint);
|
||||
const databaseStr = JSON.stringify(fileEntry);
|
||||
console.log("Write attempt.", databaseStr);
|
||||
}
|
||||
store._readFromStorage = async function() {
|
||||
console.log("Read attempt", activeFingerprint);
|
||||
return "{}";
|
||||
}
|
||||
}
|
||||
|
||||
function patchEventBus() {
|
||||
const eventBus = window.PDFViewerApplication.eventBus;
|
||||
const originalDispatch = eventBus.dispatch.bind(eventBus);
|
||||
|
||||
14
packages/pdfjs-viewer/src/typings.d.ts
vendored
14
packages/pdfjs-viewer/src/typings.d.ts
vendored
@ -1,6 +1,19 @@
|
||||
import type { PDFDocumentProxy } from "pdfjs-dist";
|
||||
|
||||
declare global {
|
||||
/**
|
||||
* @source https://github.com/mozilla/pdf.js/blob/master/web/view_history.js
|
||||
*/
|
||||
interface ViewHistory {
|
||||
database: {
|
||||
files?: {
|
||||
fingerprint: string;
|
||||
}[];
|
||||
},
|
||||
_writeToStorage: () => Promise<void>;
|
||||
_readFromStorage: () => Promise<string>;
|
||||
}
|
||||
|
||||
interface Window {
|
||||
PDFViewerApplication?: {
|
||||
initializedPromise: Promise<void>;
|
||||
@ -9,6 +22,7 @@ declare global {
|
||||
on(event: string, listener: (...args: any[]) => void): void;
|
||||
dispatch(event: string, data?: any): void;
|
||||
};
|
||||
store: ViewHistory;
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user