pdfAnnotations.js 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. import App from '../../../base'
  2. import PDFJS from './pdfJsLoader'
  3. const EXTERNAL_LINK_TARGET = '_blank'
  4. const REL_NOOPENER = 'noreferrer noopener'
  5. App.factory('pdfAnnotations', function() {
  6. class pdfAnnotations {
  7. constructor(options) {
  8. this.annotationsLayerDiv = options.annotations
  9. this.viewport = options.viewport
  10. this.navigateFn = options.navigateFn
  11. }
  12. setAnnotations(annotations) {
  13. const result = []
  14. for (let annotation of annotations) {
  15. switch (annotation.subtype) {
  16. case 'Link':
  17. result.push(this.addLink(annotation))
  18. break
  19. case 'Text':
  20. continue
  21. default:
  22. result.push(undefined)
  23. }
  24. }
  25. }
  26. addLink(link) {
  27. const element = this.buildLinkElementFromRect(link.rect)
  28. this.setLinkTarget(element, link)
  29. this.annotationsLayerDiv.appendChild(element)
  30. }
  31. buildLinkElementFromRect(rect) {
  32. rect = this.viewport.convertToViewportRectangle(rect)
  33. rect = PDFJS.Util.normalizeRect(rect)
  34. const element = document.createElement('a')
  35. element.style.left = Math.floor(rect[0]) + 'px'
  36. element.style.top = Math.floor(rect[1]) + 'px'
  37. element.style.width = Math.ceil(rect[2] - rect[0]) + 'px'
  38. element.style.height = Math.ceil(rect[3] - rect[1]) + 'px'
  39. return element
  40. }
  41. setLinkTarget(element, link) {
  42. if (link.url) {
  43. element.href = link.url
  44. element.target = EXTERNAL_LINK_TARGET
  45. element.rel = REL_NOOPENER
  46. } else if (link.dest) {
  47. element.href = `#${link.dest}`
  48. element.onclick = () => {
  49. this.navigateFn(link)
  50. return false
  51. }
  52. }
  53. }
  54. }
  55. return pdfAnnotations
  56. })