pdfPage.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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('defaultPageSize', function(
  61. defaultPageSize
  62. ) {
  63. if (defaultPageSize == null) {
  64. return
  65. }
  66. updatePageSize(defaultPageSize)
  67. return handler()
  68. })
  69. }
  70. }
  71. if (scope.page.current) {
  72. // console.log 'we must scroll to this page', scope.page.pageNum, 'at position', scope.page.position
  73. // this is the current page, we want to scroll it into view
  74. // and render it immediately
  75. scope.document.renderPage(scope.page)
  76. ctrl.setPdfPosition(scope.page, scope.page.position)
  77. }
  78. element.on('dblclick', function(e) {
  79. const offset = $(element)
  80. .find('.pdf-canvas')
  81. .offset()
  82. const dx = e.pageX - offset.left
  83. const dy = e.pageY - offset.top
  84. return scope.document
  85. .getPdfViewport(scope.page.pageNum)
  86. .then(function(viewport) {
  87. const pdfPoint = viewport.convertToPdfPoint(dx, dy)
  88. const event = {
  89. page: scope.page.pageNum,
  90. x: pdfPoint[0],
  91. y: viewport.viewBox[3] - pdfPoint[1]
  92. }
  93. return scope.$emit('pdfDoubleClick', event)
  94. })
  95. })
  96. const highlightsLayer = new pdfHighlights({
  97. highlights: highlightsElement
  98. })
  99. scope.$on('pdf:highlights', function(event, highlights) {
  100. let h
  101. if (highlights == null) {
  102. return
  103. }
  104. if (!(highlights.length > 0)) {
  105. return
  106. }
  107. if (scope.timeoutHandler) {
  108. $timeout.cancel(scope.timeoutHandler)
  109. highlightsLayer.clearHighlights()
  110. scope.timeoutHandler = null
  111. }
  112. // console.log 'got highlight watch in pdfPage', scope.page
  113. const pageHighlights = (() => {
  114. const result = []
  115. for (h of Array.from(highlights)) {
  116. if (h.page === scope.page.pageNum) {
  117. result.push(h)
  118. }
  119. }
  120. return result
  121. })()
  122. if (!pageHighlights.length) {
  123. return
  124. }
  125. scope.document.getPdfViewport(scope.page.pageNum).then(viewport =>
  126. (() => {
  127. const result1 = []
  128. for (let hl of Array.from(pageHighlights)) {
  129. // console.log 'adding highlight', h, viewport
  130. const top = viewport.viewBox[3] - hl.v
  131. result1.push(
  132. highlightsLayer.addHighlight(
  133. viewport,
  134. hl.h,
  135. top,
  136. hl.width,
  137. hl.height
  138. )
  139. )
  140. }
  141. return result1
  142. })()
  143. )
  144. return (scope.timeoutHandler = $timeout(function() {
  145. highlightsLayer.clearHighlights()
  146. return (scope.timeoutHandler = null)
  147. }, 1000))
  148. })
  149. return scope.$on('$destroy', function() {
  150. if (scope.timeoutHandler != null) {
  151. $timeout.cancel(scope.timeoutHandler)
  152. return highlightsLayer.clearHighlights()
  153. }
  154. })
  155. }
  156. })
  157. )