annotation_editor_layer_builder.js 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /* Copyright 2022 Mozilla Foundation
  2. *
  3. * Licensed under the Apache License, Version 2.0 (the "License");
  4. * you may not use this file except in compliance with the License.
  5. * You may obtain a copy of the License at
  6. *
  7. * http://www.apache.org/licenses/LICENSE-2.0
  8. *
  9. * Unless required by applicable law or agreed to in writing, software
  10. * distributed under the License is distributed on an "AS IS" BASIS,
  11. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. * See the License for the specific language governing permissions and
  13. * limitations under the License.
  14. */
  15. /** @typedef {import("../src/display/api").PDFPageProxy} PDFPageProxy */
  16. // eslint-disable-next-line max-len
  17. /** @typedef {import("../src/display/display_utils").PageViewport} PageViewport */
  18. // eslint-disable-next-line max-len
  19. /** @typedef {import("../src/display/editor/tools.js").AnnotationEditorUIManager} AnnotationEditorUIManager */
  20. // eslint-disable-next-line max-len
  21. /** @typedef {import("./text_accessibility.js").TextAccessibilityManager} TextAccessibilityManager */
  22. /** @typedef {import("./interfaces").IL10n} IL10n */
  23. import { AnnotationEditorLayer } from "pdfjs-lib";
  24. import { NullL10n } from "./l10n_utils.js";
  25. /**
  26. * @typedef {Object} AnnotationEditorLayerBuilderOptions
  27. * @property {AnnotationEditorUIManager} [uiManager]
  28. * @property {HTMLDivElement} pageDiv
  29. * @property {PDFPageProxy} pdfPage
  30. * @property {IL10n} [l10n]
  31. * @property {TextAccessibilityManager} [accessibilityManager]
  32. */
  33. class AnnotationEditorLayerBuilder {
  34. #uiManager;
  35. /**
  36. * @param {AnnotationEditorLayerBuilderOptions} options
  37. */
  38. constructor(options) {
  39. this.pageDiv = options.pageDiv;
  40. this.pdfPage = options.pdfPage;
  41. this.accessibilityManager = options.accessibilityManager;
  42. this.l10n = options.l10n || NullL10n;
  43. this.annotationEditorLayer = null;
  44. this.div = null;
  45. this._cancelled = false;
  46. this.#uiManager = options.uiManager;
  47. }
  48. /**
  49. * @param {PageViewport} viewport
  50. * @param {string} intent (default value is 'display')
  51. */
  52. async render(viewport, intent = "display") {
  53. if (intent !== "display") {
  54. return;
  55. }
  56. if (this._cancelled) {
  57. return;
  58. }
  59. const clonedViewport = viewport.clone({ dontFlip: true });
  60. if (this.div) {
  61. this.annotationEditorLayer.update({ viewport: clonedViewport });
  62. this.show();
  63. return;
  64. }
  65. // Create an AnnotationEditor layer div
  66. const div = (this.div = document.createElement("div"));
  67. div.className = "annotationEditorLayer";
  68. div.tabIndex = 0;
  69. this.pageDiv.append(div);
  70. this.annotationEditorLayer = new AnnotationEditorLayer({
  71. uiManager: this.#uiManager,
  72. div,
  73. accessibilityManager: this.accessibilityManager,
  74. pageIndex: this.pdfPage.pageNumber - 1,
  75. l10n: this.l10n,
  76. viewport: clonedViewport,
  77. });
  78. const parameters = {
  79. viewport: clonedViewport,
  80. div,
  81. annotations: null,
  82. intent,
  83. };
  84. this.annotationEditorLayer.render(parameters);
  85. }
  86. cancel() {
  87. this._cancelled = true;
  88. if (!this.div) {
  89. return;
  90. }
  91. this.pageDiv = null;
  92. this.annotationEditorLayer.destroy();
  93. this.div.remove();
  94. }
  95. hide() {
  96. if (!this.div) {
  97. return;
  98. }
  99. this.div.hidden = true;
  100. }
  101. show() {
  102. if (!this.div) {
  103. return;
  104. }
  105. this.div.hidden = false;
  106. }
  107. }
  108. export { AnnotationEditorLayerBuilder };