mirror of
https://github.com/zadam/trilium.git
synced 2026-01-04 13:44:25 +01:00
44 lines
1.4 KiB
JavaScript
44 lines
1.4 KiB
JavaScript
async function main() {
|
|
// Wait for the PDF viewer application to be available.
|
|
while (!window.PDFViewerApplication) {
|
|
await new Promise(r => setTimeout(r, 50));
|
|
}
|
|
|
|
const app = PDFViewerApplication;
|
|
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(storage);
|
|
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;
|
|
|
|
// When activeEditorId becomes null, an editor was just committed
|
|
if (activeEditorId === null) {
|
|
debouncedSave();
|
|
}
|
|
});
|
|
});
|
|
};
|
|
|
|
main();
|
|
console.log("Custom script loaded"); |