diff --git a/static/reader/pdf.js b/static/reader/pdf.js index 8f47db0..82da720 100644 --- a/static/reader/pdf.js +++ b/static/reader/pdf.js @@ -30,9 +30,18 @@ window.addEventListener('keydown', function(event) { } }) -window.addEventListener('pagerendered', function (event) { - loadAnnotations(event.detail.pageNumber - 1) -}) +function bindEvents() { + if (!window.PDFViewerApplication || !window.PDFViewerApplication.eventBus) { + setTimeout(bindEvents, 10) + return + } + PDFViewerApplication.eventBus.on('pagerendered', function(event) { + var pageIndex = event.pageNumber - 1 + loadAnnotations(pageIndex) + }) +} + +bindEvents() function getHighlight() { var pageIndex = PDFViewerApplication.pdfViewer.currentPageNumber - 1; @@ -112,12 +121,12 @@ function saveAnnotations() { localStorage[id + '.annotations'] = JSON.stringify(annotations) } -function loadAnnotations(page) { - document.querySelectorAll('.oml-annotation.page' + page).forEach(function(e) { +function loadAnnotations(pageIndex) { + document.querySelectorAll('.oml-annotation.page' + pageIndex).forEach(function(e) { e.remove() }) annotations.filter(function(a) { - return a.page == page + return a.page == pageIndex }).forEach(function(annot) { renderAnnotation(annot) })