pdf_attachment_viewer.js 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /* Copyright 2012 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. import { createPromiseCapability, getFilenameFromUrl } from "pdfjs-lib";
  16. import { BaseTreeViewer } from "./base_tree_viewer.js";
  17. import { waitOnEventOrTimeout } from "./event_utils.js";
  18. /**
  19. * @typedef {Object} PDFAttachmentViewerOptions
  20. * @property {HTMLDivElement} container - The viewer element.
  21. * @property {EventBus} eventBus - The application event bus.
  22. * @property {DownloadManager} downloadManager - The download manager.
  23. */
  24. /**
  25. * @typedef {Object} PDFAttachmentViewerRenderParameters
  26. * @property {Object|null} attachments - A lookup table of attachment objects.
  27. */
  28. class PDFAttachmentViewer extends BaseTreeViewer {
  29. /**
  30. * @param {PDFAttachmentViewerOptions} options
  31. */
  32. constructor(options) {
  33. super(options);
  34. this.downloadManager = options.downloadManager;
  35. this.eventBus._on(
  36. "fileattachmentannotation",
  37. this.#appendAttachment.bind(this)
  38. );
  39. }
  40. reset(keepRenderedCapability = false) {
  41. super.reset();
  42. this._attachments = null;
  43. if (!keepRenderedCapability) {
  44. // The only situation in which the `_renderedCapability` should *not* be
  45. // replaced is when appending FileAttachment annotations.
  46. this._renderedCapability = createPromiseCapability();
  47. }
  48. this._pendingDispatchEvent = false;
  49. }
  50. /**
  51. * @private
  52. */
  53. async _dispatchEvent(attachmentsCount) {
  54. this._renderedCapability.resolve();
  55. if (attachmentsCount === 0 && !this._pendingDispatchEvent) {
  56. // Delay the event when no "regular" attachments exist, to allow time for
  57. // parsing of any FileAttachment annotations that may be present on the
  58. // *initially* rendered page; this reduces the likelihood of temporarily
  59. // disabling the attachmentsView when the `PDFSidebar` handles the event.
  60. this._pendingDispatchEvent = true;
  61. await waitOnEventOrTimeout({
  62. target: this.eventBus,
  63. name: "annotationlayerrendered",
  64. delay: 1000,
  65. });
  66. if (!this._pendingDispatchEvent) {
  67. return; // There was already another `_dispatchEvent`-call`.
  68. }
  69. }
  70. this._pendingDispatchEvent = false;
  71. this.eventBus.dispatch("attachmentsloaded", {
  72. source: this,
  73. attachmentsCount,
  74. });
  75. }
  76. /**
  77. * @private
  78. */
  79. _bindLink(element, { content, filename }) {
  80. element.onclick = () => {
  81. this.downloadManager.openOrDownloadData(element, content, filename);
  82. return false;
  83. };
  84. }
  85. /**
  86. * @param {PDFAttachmentViewerRenderParameters} params
  87. */
  88. render({ attachments, keepRenderedCapability = false }) {
  89. if (this._attachments) {
  90. this.reset(keepRenderedCapability);
  91. }
  92. this._attachments = attachments || null;
  93. if (!attachments) {
  94. this._dispatchEvent(/* attachmentsCount = */ 0);
  95. return;
  96. }
  97. const names = Object.keys(attachments).sort(function (a, b) {
  98. return a.toLowerCase().localeCompare(b.toLowerCase());
  99. });
  100. const fragment = document.createDocumentFragment();
  101. let attachmentsCount = 0;
  102. for (const name of names) {
  103. const item = attachments[name];
  104. const content = item.content,
  105. filename = getFilenameFromUrl(
  106. item.filename,
  107. /* onlyStripPath = */ true
  108. );
  109. const div = document.createElement("div");
  110. div.className = "treeItem";
  111. const element = document.createElement("a");
  112. this._bindLink(element, { content, filename });
  113. element.textContent = this._normalizeTextContent(filename);
  114. div.append(element);
  115. fragment.append(div);
  116. attachmentsCount++;
  117. }
  118. this._finishRendering(fragment, attachmentsCount);
  119. }
  120. /**
  121. * Used to append FileAttachment annotations to the sidebar.
  122. */
  123. #appendAttachment({ filename, content }) {
  124. const renderedPromise = this._renderedCapability.promise;
  125. renderedPromise.then(() => {
  126. if (renderedPromise !== this._renderedCapability.promise) {
  127. return; // The FileAttachment annotation belongs to a previous document.
  128. }
  129. const attachments = this._attachments || Object.create(null);
  130. for (const name in attachments) {
  131. if (filename === name) {
  132. return; // Ignore the new attachment if it already exists.
  133. }
  134. }
  135. attachments[filename] = {
  136. filename,
  137. content,
  138. };
  139. this.render({
  140. attachments,
  141. keepRenderedCapability: true,
  142. });
  143. });
  144. }
  145. }
  146. export { PDFAttachmentViewer };