pdfPage.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. /* eslint-disable
  2. max-len,
  3. new-cap,
  4. no-return-assign,
  5. */
  6. // TODO: This file was created by bulk-decaffeinate.
  7. // Fix any style issues and re-enable lint.
  8. /*
  9. * decaffeinate suggestions:
  10. * DS101: Remove unnecessary use of Array.from
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS205: Consider reworking code to avoid use of IIFEs
  13. * DS207: Consider shorter variations of null checks
  14. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  15. */
  16. import App from '../../../base'
  17. export default App.directive(
  18. 'pdfPage',
  19. ($timeout, pdfHighlights, pdfSpinner) => ({
  20. require: '^pdfViewer',
  21. template: `\
  22. <div class="plv-page-view page-view">
  23. <div class="pdf-canvas pdfng-empty"></div>
  24. <div class="plv-text-layer text-layer"></div>
  25. <div class="plv-annotations-layer annotations-layer"></div>
  26. <div class="plv-highlights-layer highlights-layer"></div>
  27. </div>\
  28. `,
  29. link(scope, element, attrs, ctrl) {
  30. const canvasElement = $(element).find('.pdf-canvas')
  31. const textElement = $(element).find('.text-layer')
  32. const annotationsElement = $(element).find('.annotations-layer')
  33. const highlightsElement = $(element).find('.highlights-layer')
  34. const updatePageSize = function (size) {
  35. const h = Math.floor(size[0])
  36. const w = Math.floor(size[1])
  37. element.height(h)
  38. element.width(w)
  39. canvasElement.height(h)
  40. canvasElement.width(w)
  41. return (scope.page.sized = true)
  42. }
  43. // keep track of our page element, so we can access it in the
  44. // parent with scope.pages[i].element, and the contained
  45. // elements for each part
  46. scope.page.element = element
  47. scope.page.elementChildren = {
  48. canvas: canvasElement,
  49. text: textElement,
  50. annotations: annotationsElement,
  51. highlights: highlightsElement,
  52. container: element,
  53. }
  54. if (!scope.page.sized) {
  55. if (scope.defaultPageSize != null) {
  56. updatePageSize(scope.defaultPageSize)
  57. } else {
  58. // shouldn't get here - the default page size should now
  59. // always be set before redraw is called
  60. var handler = scope.$watch(
  61. 'defaultPageSize',
  62. function (defaultPageSize) {
  63. if (defaultPageSize == null) {
  64. return
  65. }
  66. updatePageSize(defaultPageSize)
  67. return handler()
  68. }
  69. )
  70. }
  71. }
  72. if (scope.page.current) {
  73. // console.log 'we must scroll to this page', scope.page.pageNum, 'at position', scope.page.position
  74. // this is the current page, we want to scroll it into view
  75. // and render it immediately
  76. scope.document.renderPage(scope.page)
  77. ctrl.setPdfPosition(scope.page, scope.page.position)
  78. }
  79. element.on('dblclick', function (e) {
  80. const offset = $(element).find('.pdf-canvas').offset()
  81. const dx = e.pageX - offset.left
  82. const dy = e.pageY - offset.top
  83. return scope.document
  84. .getPdfViewport(scope.page.pageNum)
  85. .then(function (viewport) {
  86. const pdfPoint = viewport.convertToPdfPoint(dx, dy)
  87. const event = {
  88. page: scope.page.pageNum,
  89. x: pdfPoint[0],
  90. y: viewport.viewBox[3] - pdfPoint[1],
  91. }
  92. return scope.$emit('pdfDoubleClick', event)
  93. })
  94. })
  95. const highlightsLayer = new pdfHighlights({
  96. highlights: highlightsElement,
  97. })
  98. scope.$on('pdf:highlights', function (event, highlights) {
  99. let h
  100. if (highlights == null) {
  101. return
  102. }
  103. if (!(highlights.length > 0)) {
  104. return
  105. }
  106. if (scope.timeoutHandler) {
  107. $timeout.cancel(scope.timeoutHandler)
  108. highlightsLayer.clearHighlights()
  109. scope.timeoutHandler = null
  110. }
  111. // console.log 'got highlight watch in pdfPage', scope.page
  112. const pageHighlights = (() => {
  113. const result = []
  114. for (h of Array.from(highlights)) {
  115. if (h.page === scope.page.pageNum) {
  116. result.push(h)
  117. }
  118. }
  119. return result
  120. })()
  121. if (!pageHighlights.length) {
  122. return
  123. }
  124. scope.document.getPdfViewport(scope.page.pageNum).then(viewport =>
  125. (() => {
  126. const result1 = []
  127. for (const hl of Array.from(pageHighlights)) {
  128. // console.log 'adding highlight', h, viewport
  129. const top = viewport.viewBox[3] - hl.v
  130. result1.push(
  131. highlightsLayer.addHighlight(
  132. viewport,
  133. hl.h,
  134. top,
  135. hl.width,
  136. hl.height
  137. )
  138. )
  139. }
  140. return result1
  141. })()
  142. )
  143. return (scope.timeoutHandler = $timeout(function () {
  144. highlightsLayer.clearHighlights()
  145. return (scope.timeoutHandler = null)
  146. }, 1000))
  147. })
  148. return scope.$on('$destroy', function () {
  149. if (scope.timeoutHandler != null) {
  150. $timeout.cancel(scope.timeoutHandler)
  151. return highlightsLayer.clearHighlights()
  152. }
  153. })
  154. },
  155. })
  156. )