pdf_attachment_viewer.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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.PDFAttachmentViewer = void 0;
  27. var _pdf = require("../pdf");
  28. var _base_tree_viewer = require("./base_tree_viewer.js");
  29. var _event_utils = require("./event_utils.js");
  30. class PDFAttachmentViewer extends _base_tree_viewer.BaseTreeViewer {
  31. constructor(options) {
  32. super(options);
  33. this.downloadManager = options.downloadManager;
  34. this.eventBus._on("fileattachmentannotation", this.#appendAttachment.bind(this));
  35. }
  36. reset(keepRenderedCapability = false) {
  37. super.reset();
  38. this._attachments = null;
  39. if (!keepRenderedCapability) {
  40. this._renderedCapability = (0, _pdf.createPromiseCapability)();
  41. }
  42. this._pendingDispatchEvent = false;
  43. }
  44. async _dispatchEvent(attachmentsCount) {
  45. this._renderedCapability.resolve();
  46. if (attachmentsCount === 0 && !this._pendingDispatchEvent) {
  47. this._pendingDispatchEvent = true;
  48. await (0, _event_utils.waitOnEventOrTimeout)({
  49. target: this.eventBus,
  50. name: "annotationlayerrendered",
  51. delay: 1000
  52. });
  53. if (!this._pendingDispatchEvent) {
  54. return;
  55. }
  56. }
  57. this._pendingDispatchEvent = false;
  58. this.eventBus.dispatch("attachmentsloaded", {
  59. source: this,
  60. attachmentsCount
  61. });
  62. }
  63. _bindLink(element, {
  64. content,
  65. filename
  66. }) {
  67. element.onclick = () => {
  68. this.downloadManager.openOrDownloadData(element, content, filename);
  69. return false;
  70. };
  71. }
  72. render({
  73. attachments,
  74. keepRenderedCapability = false
  75. }) {
  76. if (this._attachments) {
  77. this.reset(keepRenderedCapability);
  78. }
  79. this._attachments = attachments || null;
  80. if (!attachments) {
  81. this._dispatchEvent(0);
  82. return;
  83. }
  84. const names = Object.keys(attachments).sort(function (a, b) {
  85. return a.toLowerCase().localeCompare(b.toLowerCase());
  86. });
  87. const fragment = document.createDocumentFragment();
  88. let attachmentsCount = 0;
  89. for (const name of names) {
  90. const item = attachments[name];
  91. const content = item.content,
  92. filename = (0, _pdf.getFilenameFromUrl)(item.filename, true);
  93. const div = document.createElement("div");
  94. div.className = "treeItem";
  95. const element = document.createElement("a");
  96. this._bindLink(element, {
  97. content,
  98. filename
  99. });
  100. element.textContent = this._normalizeTextContent(filename);
  101. div.append(element);
  102. fragment.append(div);
  103. attachmentsCount++;
  104. }
  105. this._finishRendering(fragment, attachmentsCount);
  106. }
  107. #appendAttachment({
  108. filename,
  109. content
  110. }) {
  111. const renderedPromise = this._renderedCapability.promise;
  112. renderedPromise.then(() => {
  113. if (renderedPromise !== this._renderedCapability.promise) {
  114. return;
  115. }
  116. const attachments = this._attachments || Object.create(null);
  117. for (const name in attachments) {
  118. if (filename === name) {
  119. return;
  120. }
  121. }
  122. attachments[filename] = {
  123. filename,
  124. content
  125. };
  126. this.render({
  127. attachments,
  128. keepRenderedCapability: true
  129. });
  130. });
  131. }
  132. }
  133. exports.PDFAttachmentViewer = PDFAttachmentViewer;