2
0

annotation_layer_builder.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /**
  2. * @licstart The following is the entire license notice for the
  3. * JavaScript code in this page
  4. *
  5. * Copyright 2022 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.AnnotationLayerBuilder = void 0;
  27. var _pdf = require("../pdf");
  28. var _l10n_utils = require("./l10n_utils.js");
  29. var _ui_utils = require("./ui_utils.js");
  30. class AnnotationLayerBuilder {
  31. #numAnnotations = 0;
  32. #onPresentationModeChanged = null;
  33. constructor({
  34. pageDiv,
  35. pdfPage,
  36. linkService,
  37. downloadManager,
  38. annotationStorage = null,
  39. imageResourcesPath = "",
  40. renderForms = true,
  41. l10n = _l10n_utils.NullL10n,
  42. enableScripting = false,
  43. hasJSActionsPromise = null,
  44. fieldObjectsPromise = null,
  45. annotationCanvasMap = null,
  46. accessibilityManager = null
  47. }) {
  48. this.pageDiv = pageDiv;
  49. this.pdfPage = pdfPage;
  50. this.linkService = linkService;
  51. this.downloadManager = downloadManager;
  52. this.imageResourcesPath = imageResourcesPath;
  53. this.renderForms = renderForms;
  54. this.l10n = l10n;
  55. this.annotationStorage = annotationStorage;
  56. this.enableScripting = enableScripting;
  57. this._hasJSActionsPromise = hasJSActionsPromise || Promise.resolve(false);
  58. this._fieldObjectsPromise = fieldObjectsPromise || Promise.resolve(null);
  59. this._annotationCanvasMap = annotationCanvasMap;
  60. this._accessibilityManager = accessibilityManager;
  61. this.div = null;
  62. this._cancelled = false;
  63. this._eventBus = linkService.eventBus;
  64. }
  65. async render(viewport, intent = "display") {
  66. if (this.div) {
  67. if (this._cancelled || this.#numAnnotations === 0) {
  68. return;
  69. }
  70. _pdf.AnnotationLayer.update({
  71. viewport: viewport.clone({
  72. dontFlip: true
  73. }),
  74. div: this.div,
  75. annotationCanvasMap: this._annotationCanvasMap
  76. });
  77. return;
  78. }
  79. const [annotations, hasJSActions, fieldObjects] = await Promise.all([this.pdfPage.getAnnotations({
  80. intent
  81. }), this._hasJSActionsPromise, this._fieldObjectsPromise]);
  82. if (this._cancelled) {
  83. return;
  84. }
  85. this.#numAnnotations = annotations.length;
  86. this.div = document.createElement("div");
  87. this.div.className = "annotationLayer";
  88. this.pageDiv.append(this.div);
  89. if (this.#numAnnotations === 0) {
  90. this.hide();
  91. return;
  92. }
  93. _pdf.AnnotationLayer.render({
  94. viewport: viewport.clone({
  95. dontFlip: true
  96. }),
  97. div: this.div,
  98. annotations,
  99. page: this.pdfPage,
  100. imageResourcesPath: this.imageResourcesPath,
  101. renderForms: this.renderForms,
  102. linkService: this.linkService,
  103. downloadManager: this.downloadManager,
  104. annotationStorage: this.annotationStorage,
  105. enableScripting: this.enableScripting,
  106. hasJSActions,
  107. fieldObjects,
  108. annotationCanvasMap: this._annotationCanvasMap,
  109. accessibilityManager: this._accessibilityManager
  110. });
  111. this.l10n.translate(this.div);
  112. if (this.linkService.isInPresentationMode) {
  113. this.#updatePresentationModeState(_ui_utils.PresentationModeState.FULLSCREEN);
  114. }
  115. if (!this.#onPresentationModeChanged) {
  116. this.#onPresentationModeChanged = evt => {
  117. this.#updatePresentationModeState(evt.state);
  118. };
  119. this._eventBus?._on("presentationmodechanged", this.#onPresentationModeChanged);
  120. }
  121. }
  122. cancel() {
  123. this._cancelled = true;
  124. if (this.#onPresentationModeChanged) {
  125. this._eventBus?._off("presentationmodechanged", this.#onPresentationModeChanged);
  126. this.#onPresentationModeChanged = null;
  127. }
  128. }
  129. hide() {
  130. if (!this.div) {
  131. return;
  132. }
  133. this.div.hidden = true;
  134. }
  135. #updatePresentationModeState(state) {
  136. if (!this.div) {
  137. return;
  138. }
  139. let disableFormElements = false;
  140. switch (state) {
  141. case _ui_utils.PresentationModeState.FULLSCREEN:
  142. disableFormElements = true;
  143. break;
  144. case _ui_utils.PresentationModeState.NORMAL:
  145. break;
  146. default:
  147. return;
  148. }
  149. for (const section of this.div.childNodes) {
  150. if (section.hasAttribute("data-internal-link")) {
  151. continue;
  152. }
  153. section.inert = disableFormElements;
  154. }
  155. }
  156. }
  157. exports.AnnotationLayerBuilder = AnnotationLayerBuilder;