2
0

annotation_layer_builder.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * Javascript code in this page
  4. *
  5. * Copyright 2021 Mozilla Foundation
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * @licend The above is the entire license notice for the
  20. * Javascript code in this page
  21. */
  22. "use strict";
  23. Object.defineProperty(exports, "__esModule", {
  24. value: true
  25. });
  26. exports.DefaultAnnotationLayerFactory = exports.AnnotationLayerBuilder = void 0;
  27. var _pdf = require("../pdf");
  28. var _l10n_utils = require("./l10n_utils.js");
  29. var _pdf_link_service = require("./pdf_link_service.js");
  30. class AnnotationLayerBuilder {
  31. constructor({
  32. pageDiv,
  33. pdfPage,
  34. linkService,
  35. downloadManager,
  36. annotationStorage = null,
  37. imageResourcesPath = "",
  38. renderForms = true,
  39. l10n = _l10n_utils.NullL10n,
  40. enableScripting = false,
  41. hasJSActionsPromise = null,
  42. fieldObjectsPromise = null,
  43. mouseState = null
  44. }) {
  45. this.pageDiv = pageDiv;
  46. this.pdfPage = pdfPage;
  47. this.linkService = linkService;
  48. this.downloadManager = downloadManager;
  49. this.imageResourcesPath = imageResourcesPath;
  50. this.renderForms = renderForms;
  51. this.l10n = l10n;
  52. this.annotationStorage = annotationStorage;
  53. this.enableScripting = enableScripting;
  54. this._hasJSActionsPromise = hasJSActionsPromise;
  55. this._fieldObjectsPromise = fieldObjectsPromise;
  56. this._mouseState = mouseState;
  57. this.div = null;
  58. this._cancelled = false;
  59. }
  60. async render(viewport, intent = "display") {
  61. const [annotations, hasJSActions = false, fieldObjects = null] = await Promise.all([this.pdfPage.getAnnotations({
  62. intent
  63. }), this._hasJSActionsPromise, this._fieldObjectsPromise]);
  64. if (this._cancelled || annotations.length === 0) {
  65. return;
  66. }
  67. const parameters = {
  68. viewport: viewport.clone({
  69. dontFlip: true
  70. }),
  71. div: this.div,
  72. annotations,
  73. page: this.pdfPage,
  74. imageResourcesPath: this.imageResourcesPath,
  75. renderForms: this.renderForms,
  76. linkService: this.linkService,
  77. downloadManager: this.downloadManager,
  78. annotationStorage: this.annotationStorage,
  79. enableScripting: this.enableScripting,
  80. hasJSActions,
  81. fieldObjects,
  82. mouseState: this._mouseState
  83. };
  84. if (this.div) {
  85. _pdf.AnnotationLayer.update(parameters);
  86. } else {
  87. this.div = document.createElement("div");
  88. this.div.className = "annotationLayer";
  89. this.pageDiv.appendChild(this.div);
  90. parameters.div = this.div;
  91. _pdf.AnnotationLayer.render(parameters);
  92. this.l10n.translate(this.div);
  93. }
  94. }
  95. cancel() {
  96. this._cancelled = true;
  97. }
  98. hide() {
  99. if (!this.div) {
  100. return;
  101. }
  102. this.div.hidden = true;
  103. }
  104. }
  105. exports.AnnotationLayerBuilder = AnnotationLayerBuilder;
  106. class DefaultAnnotationLayerFactory {
  107. createAnnotationLayerBuilder(pageDiv, pdfPage, annotationStorage = null, imageResourcesPath = "", renderForms = true, l10n = _l10n_utils.NullL10n, enableScripting = false, hasJSActionsPromise = null, mouseState = null, fieldObjectsPromise = null) {
  108. return new AnnotationLayerBuilder({
  109. pageDiv,
  110. pdfPage,
  111. imageResourcesPath,
  112. renderForms,
  113. linkService: new _pdf_link_service.SimpleLinkService(),
  114. l10n,
  115. annotationStorage,
  116. enableScripting,
  117. hasJSActionsPromise,
  118. fieldObjectsPromise,
  119. mouseState
  120. });
  121. }
  122. }
  123. exports.DefaultAnnotationLayerFactory = DefaultAnnotationLayerFactory;