pdfTextLayer.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /* eslint-disable
  2. max-len,
  3. no-return-assign,
  4. no-undef,
  5. no-unused-vars,
  6. */
  7. // TODO: This file was created by bulk-decaffeinate.
  8. // Fix any style issues and re-enable lint.
  9. /*
  10. * decaffeinate suggestions:
  11. * DS102: Remove unnecessary code created because of implicit returns
  12. * DS207: Consider shorter variations of null checks
  13. * Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
  14. */
  15. define(['base'], App =>
  16. // uses the PDFJS text layer renderer to provide invisible overlayed
  17. // text for searching
  18. App.factory('pdfTextLayer', function() {
  19. let pdfTextLayer
  20. return (pdfTextLayer = class pdfTextLayer {
  21. constructor(options) {
  22. this.textLayerDiv = options.textLayerDiv
  23. this.divContentDone = false
  24. this.viewport = options.viewport
  25. this.textDivs = []
  26. this.renderer = options.renderer
  27. this.renderingDone = false
  28. }
  29. render(timeout) {
  30. if (this.renderingDone || !this.divContentDone) {
  31. return
  32. }
  33. if (this.textLayerRenderTask != null) {
  34. this.textLayerRenderTask.cancel()
  35. this.textLayerRenderTask = null
  36. }
  37. this.textDivs = []
  38. const textLayerFrag = document.createDocumentFragment()
  39. this.textLayerRenderTask = this.renderer({
  40. textContent: this.textContent,
  41. container: textLayerFrag,
  42. viewport: this.viewport,
  43. textDivs: this.textDivs,
  44. timeout,
  45. enhanceTextSelection: this.enhanceTextSelection
  46. })
  47. const textLayerSuccess = () => {
  48. this.textLayerDiv.appendChild(textLayerFrag)
  49. return (this.renderingDone = true)
  50. }
  51. const textLayerFailure = function() {
  52. // canceled or failed to render text layer -- skipping errors
  53. }
  54. return this.textLayerRenderTask.promise.then(
  55. textLayerSuccess,
  56. textLayerFailure
  57. )
  58. }
  59. setTextContent(textContent) {
  60. if (this.textLayerRenderTask) {
  61. this.textLayerRenderTask.cancel()
  62. this.textLayerRenderTask = null
  63. }
  64. this.textContent = textContent
  65. return (this.divContentDone = true)
  66. }
  67. })
  68. }))