2014-04-25 04:29:05 +09:00
|
|
|
/* Copyright 2012 Mozilla Foundation
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2022-03-08 01:41:41 +09:00
|
|
|
import { getFilenameFromUrl, PromiseCapability } from "pdfjs-lib";
|
2020-08-04 19:40:59 +09:00
|
|
|
import { BaseTreeViewer } from "./base_tree_viewer.js";
|
2022-05-07 18:37:46 +09:00
|
|
|
import { waitOnEventOrTimeout } from "./event_utils.js";
|
2020-08-27 23:00:12 +09:00
|
|
|
|
2015-01-28 06:22:42 +09:00
|
|
|
/**
|
2016-02-21 21:36:24 +09:00
|
|
|
* @typedef {Object} PDFAttachmentViewerOptions
|
2015-01-28 06:22:42 +09:00
|
|
|
* @property {HTMLDivElement} container - The viewer element.
|
2016-04-26 07:57:15 +09:00
|
|
|
* @property {EventBus} eventBus - The application event bus.
|
2015-01-28 06:22:42 +09:00
|
|
|
* @property {DownloadManager} downloadManager - The download manager.
|
|
|
|
*/
|
|
|
|
|
2016-02-21 21:36:24 +09:00
|
|
|
/**
|
|
|
|
* @typedef {Object} PDFAttachmentViewerRenderParameters
|
2017-06-30 19:55:22 +09:00
|
|
|
* @property {Object|null} attachments - A lookup table of attachment objects.
|
2016-02-21 21:36:24 +09:00
|
|
|
*/
|
|
|
|
|
2020-08-04 19:40:59 +09:00
|
|
|
class PDFAttachmentViewer extends BaseTreeViewer {
|
2015-01-28 06:22:42 +09:00
|
|
|
/**
|
2016-02-21 21:36:24 +09:00
|
|
|
* @param {PDFAttachmentViewerOptions} options
|
2015-01-28 06:22:42 +09:00
|
|
|
*/
|
2020-08-04 19:40:59 +09:00
|
|
|
constructor(options) {
|
|
|
|
super(options);
|
|
|
|
this.downloadManager = options.downloadManager;
|
2017-08-14 19:34:24 +09:00
|
|
|
|
Re-factor the `EventBus` to allow servicing of "external" event listeners *after* the viewer components have updated
Since the goal has always been, essentially since the `EventBus` abstraction was added, to remove all dispatching of DOM events[1] from the viewer components this patch tries to address one thing that came up when updating the examples:
The DOM events are always dispatched last, and it's thus guaranteed that all internal event listeners have been invoked first.
However, there's no such guarantees with the general `EventBus` functionality and the order in which event listeners are invoked is *not* specified. With the promotion of the `EventBus` in the examples, over DOM events, it seems like a good idea to at least *try* to keep this ordering invariant[2] intact.
Obviously this won't prevent anyone from manually calling the new *internal* viewer component methods on the `EventBus`, but hopefully that won't be too common since any existing third-party code would obviously use the `on`/`off` methods and that all of the examples shows the *correct* usage (which should be similarily documented on the "Third party viewer usage" Wiki-page).
---
[1] Looking at the various Firefox-tests, I'm not sure that it'll be possible to (easily) re-write all of them to not rely on DOM events (since getting access to `PDFViewerApplication` might be generally difficult/messy depending on scopes).
In any case, even if technically feasible, it would most likely add *a lot* of complication that may not be desireable in the various Firefox-tests. All-in-all, I'd be fine with keeping the DOM events only for the `MOZCENTRAL` target and gated on `Cu.isInAutomation` (or similar) rather than a preference.
[2] I wouldn't expect any *real* bugs in a custom implementation, simply based on event ordering, but it nonetheless seem like a good idea if any "external" events are still handled last.
2020-02-27 07:33:27 +09:00
|
|
|
this.eventBus._on(
|
2017-01-16 02:12:24 +09:00
|
|
|
"fileattachmentannotation",
|
2022-05-07 03:45:53 +09:00
|
|
|
this.#appendAttachment.bind(this)
|
2017-01-16 02:12:24 +09:00
|
|
|
);
|
2014-04-25 04:29:05 +09:00
|
|
|
}
|
|
|
|
|
2017-04-18 00:13:48 +09:00
|
|
|
reset(keepRenderedCapability = false) {
|
2020-08-04 19:40:59 +09:00
|
|
|
super.reset();
|
|
|
|
this._attachments = null;
|
2017-01-16 02:12:24 +09:00
|
|
|
|
2017-04-18 00:13:48 +09:00
|
|
|
if (!keepRenderedCapability) {
|
2020-08-03 20:58:58 +09:00
|
|
|
// The only situation in which the `_renderedCapability` should *not* be
|
|
|
|
// replaced is when appending FileAttachment annotations.
|
2022-03-08 01:41:41 +09:00
|
|
|
this._renderedCapability = new PromiseCapability();
|
2017-04-18 00:13:48 +09:00
|
|
|
}
|
2022-05-07 18:37:46 +09:00
|
|
|
this._pendingDispatchEvent = false;
|
2017-04-18 00:13:48 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2022-05-07 18:37:46 +09:00
|
|
|
async _dispatchEvent(attachmentsCount) {
|
2017-08-17 21:12:42 +09:00
|
|
|
this._renderedCapability.resolve();
|
|
|
|
|
2022-05-07 18:37:46 +09:00
|
|
|
if (attachmentsCount === 0 && !this._pendingDispatchEvent) {
|
2020-08-03 20:58:58 +09:00
|
|
|
// Delay the event when no "regular" attachments exist, to allow time for
|
|
|
|
// parsing of any FileAttachment annotations that may be present on the
|
|
|
|
// *initially* rendered page; this reduces the likelihood of temporarily
|
|
|
|
// disabling the attachmentsView when the `PDFSidebar` handles the event.
|
2022-05-07 18:37:46 +09:00
|
|
|
this._pendingDispatchEvent = true;
|
|
|
|
|
|
|
|
await waitOnEventOrTimeout({
|
|
|
|
target: this.eventBus,
|
|
|
|
name: "annotationlayerrendered",
|
|
|
|
delay: 1000,
|
2020-08-03 20:58:58 +09:00
|
|
|
});
|
2022-05-07 18:37:46 +09:00
|
|
|
|
|
|
|
if (!this._pendingDispatchEvent) {
|
|
|
|
return; // There was already another `_dispatchEvent`-call`.
|
|
|
|
}
|
2020-08-03 20:58:58 +09:00
|
|
|
}
|
2022-05-07 18:37:46 +09:00
|
|
|
this._pendingDispatchEvent = false;
|
2020-08-03 20:58:58 +09:00
|
|
|
|
2017-04-18 00:13:48 +09:00
|
|
|
this.eventBus.dispatch("attachmentsloaded", {
|
|
|
|
source: this,
|
|
|
|
attachmentsCount,
|
|
|
|
});
|
|
|
|
}
|
2015-03-21 21:07:01 +09:00
|
|
|
|
2017-04-18 00:13:48 +09:00
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2020-08-04 19:40:59 +09:00
|
|
|
_bindLink(element, { content, filename }) {
|
|
|
|
element.onclick = () => {
|
2023-10-16 23:20:46 +09:00
|
|
|
this.downloadManager.openOrDownloadData(content, filename);
|
2017-04-18 00:13:48 +09:00
|
|
|
return false;
|
|
|
|
};
|
|
|
|
}
|
2015-01-28 06:06:19 +09:00
|
|
|
|
2017-04-18 00:13:48 +09:00
|
|
|
/**
|
|
|
|
* @param {PDFAttachmentViewerRenderParameters} params
|
|
|
|
*/
|
2017-06-30 19:55:22 +09:00
|
|
|
render({ attachments, keepRenderedCapability = false }) {
|
2020-08-04 19:40:59 +09:00
|
|
|
if (this._attachments) {
|
|
|
|
this.reset(keepRenderedCapability);
|
2017-04-18 00:13:48 +09:00
|
|
|
}
|
2020-08-04 19:40:59 +09:00
|
|
|
this._attachments = attachments || null;
|
2015-03-21 21:07:01 +09:00
|
|
|
|
2017-04-18 00:13:48 +09:00
|
|
|
if (!attachments) {
|
2020-08-03 19:11:46 +09:00
|
|
|
this._dispatchEvent(/* attachmentsCount = */ 0);
|
2017-04-18 00:13:48 +09:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-08-03 19:11:46 +09:00
|
|
|
const fragment = document.createDocumentFragment();
|
2020-08-04 19:40:59 +09:00
|
|
|
let attachmentsCount = 0;
|
2023-05-21 16:54:24 +09:00
|
|
|
for (const name in attachments) {
|
2020-08-04 19:40:59 +09:00
|
|
|
const item = attachments[name];
|
2021-02-24 21:02:58 +09:00
|
|
|
const content = item.content,
|
2022-11-23 18:40:30 +09:00
|
|
|
filename = getFilenameFromUrl(
|
|
|
|
item.filename,
|
|
|
|
/* onlyStripPath = */ true
|
|
|
|
);
|
2017-04-18 00:13:48 +09:00
|
|
|
|
2019-12-27 08:22:32 +09:00
|
|
|
const div = document.createElement("div");
|
2020-08-04 19:40:59 +09:00
|
|
|
div.className = "treeItem";
|
|
|
|
|
|
|
|
const element = document.createElement("a");
|
2021-02-23 20:58:17 +09:00
|
|
|
this._bindLink(element, { content, filename });
|
2020-08-04 19:40:59 +09:00
|
|
|
element.textContent = this._normalizeTextContent(filename);
|
|
|
|
|
2022-06-12 19:20:25 +09:00
|
|
|
div.append(element);
|
2017-04-18 00:13:48 +09:00
|
|
|
|
2022-06-12 19:20:25 +09:00
|
|
|
fragment.append(div);
|
2020-08-04 19:40:59 +09:00
|
|
|
attachmentsCount++;
|
2017-04-18 00:13:48 +09:00
|
|
|
}
|
2020-08-04 19:40:59 +09:00
|
|
|
|
2020-12-28 22:42:47 +09:00
|
|
|
this._finishRendering(fragment, attachmentsCount);
|
2017-04-18 00:13:48 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to append FileAttachment annotations to the sidebar.
|
|
|
|
*/
|
2022-05-07 03:45:53 +09:00
|
|
|
#appendAttachment({ filename, content }) {
|
2020-08-03 20:58:58 +09:00
|
|
|
const renderedPromise = this._renderedCapability.promise;
|
|
|
|
|
|
|
|
renderedPromise.then(() => {
|
|
|
|
if (renderedPromise !== this._renderedCapability.promise) {
|
|
|
|
return; // The FileAttachment annotation belongs to a previous document.
|
|
|
|
}
|
2022-05-07 03:45:53 +09:00
|
|
|
const attachments = this._attachments || Object.create(null);
|
|
|
|
|
|
|
|
for (const name in attachments) {
|
|
|
|
if (filename === name) {
|
|
|
|
return; // Ignore the new attachment if it already exists.
|
2017-01-16 02:12:24 +09:00
|
|
|
}
|
2017-04-18 00:13:48 +09:00
|
|
|
}
|
2022-05-07 03:45:53 +09:00
|
|
|
attachments[filename] = {
|
2017-04-18 00:13:48 +09:00
|
|
|
filename,
|
|
|
|
content,
|
|
|
|
};
|
|
|
|
this.render({
|
|
|
|
attachments,
|
|
|
|
keepRenderedCapability: true,
|
|
|
|
});
|
2017-05-05 00:09:50 +09:00
|
|
|
});
|
2017-04-18 00:13:48 +09:00
|
|
|
}
|
|
|
|
}
|
2016-04-09 02:34:27 +09:00
|
|
|
|
2017-03-28 08:07:27 +09:00
|
|
|
export { PDFAttachmentViewer };
|