UNPKG

lichen-annotate-pdf

Version:

Annotation layer for pdf.js in vue fork of Aaron Leong pdf-annotate-vue

111 lines (100 loc) 3.56 kB
import renderScreenReaderHints from './renderScreenReaderHints'; import insertScreenReaderComment from './insertScreenReaderComment'; import renderScreenReaderComments from './renderScreenReaderComments'; import { addEventListener } from '../UI/event'; import PDFJSAnnotate from '../PDFJSAnnotate'; /** * Initialize the event handlers for keeping screen reader hints synced with data */ export default function initEventHandlers () { addEventListener('annotation:add', (documentId, pageNumber, annotation) => { // console.log('ajout annotation', annotation); reorderAnnotationsByType(documentId, pageNumber, annotation.type); }); addEventListener('annotation:edit', (documentId, annotationId, annotation) => { // console.log('edit annotation', annotation); reorderAnnotationsByType(documentId, annotation.page, annotation.type); }); addEventListener('annotation:delete', removeAnnotation); addEventListener('comment:add', insertComment); addEventListener('comment:delete', removeComment); } /** * Reorder the annotation numbers by annotation type * * @param {String} documentId The ID of the document * @param {Number} pageNumber The page number of the annotations * @param {Strig} type The annotation type */ function reorderAnnotationsByType(documentId, pageNumber, type) { PDFJSAnnotate.getStoreAdapter().getAnnotations(documentId, pageNumber) .then((annotations) => { return annotations.annotations.filter((a) => { return a.type === type; }); }) .then((annotations) => { annotations.forEach((a) => { removeAnnotation(documentId, a.uuid); }); return annotations; }) .then(renderScreenReaderHints); } /** * Remove the screen reader hint for an annotation * * @param {String} documentId The ID of the document * @param {String} annotationId The Id of the annotation */ function removeAnnotation(documentId, annotationId) { // console.log('remove annotation', annotationId) removeElementById(`pdf-annotate-screenreader-${annotationId}`); removeElementById(`pdf-annotate-screenreader-${annotationId}-end`); } /** * Insert a screen reader hint for a comment * * @param {String} documentId The ID of the document * @param {String} annotationId The ID of tha assocated annotation * @param {Object} comment The comment to insert a hint for */ function insertComment(documentId, annotationId, comment) { let list = document.querySelector(`pdf-annotate-screenreader-comment-list-${annotationId}`); let promise; // console.log('insert comment', comment) // Dispatch the event if (!list) { promise = renderScreenReaderComments(documentId, annotationId, []).then(() => { list = document.querySelector(`pdf-annotate-screenreader-comment-list-${annotationId}`); return true; }); } else { promise = Promise.resolve(true); } promise.then(() => { insertScreenReaderComment(comment); }); } /** * Remove a screen reader hint for a comment * * @param {String} documentId The ID of the document * @param {String} commentId The ID of the comment */ function removeComment(documentId, commentId) { // console.log('remove comment', commentId) removeElementById(`pdf-annotate-screenreader-comment-${commentId}`); } /** * Remove an element from the DOM by it's ID if it exists * * @param {String} elementId The ID of the element to be removed */ function removeElementById(elementId) { let el = document.getElementById(elementId); if (el) { el.parentNode.removeChild(el); } }