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.
|
|
|
|
*/
|
|
|
|
|
2020-08-04 19:40:59 +09:00
|
|
|
import { createPromiseCapability, getFilenameFromUrl } from "pdfjs-lib";
|
|
|
|
import { BaseTreeViewer } from "./base_tree_viewer.js";
|
Remove the `disableCreateObjectURL` option from `web/app_options.js`
Prior to PR 11601, the `disableCreateObjectURL` option was present on `getDocument` in the API, since it was (potentially) used when decoding JPEG images natively in the browser. Hence setting this option, which was done automatically using compatibility-code, were in some browsers necessary in order for e.g. JPEG images to be correctly rendered.
The downside of the `disableCreateObjectURL` option is that memory usage increases significantly, since we're forced to build and use `data:` URIs (rather than `blob:` URLs).
However, at this point in time the `disableCreateObjectURL` option is only necessary for *some* (non-essential) functionality in the default viewer; in particular:
- The openfile functionality, used only when manually opening a new file in the default viewer.
- The download functionality, used when downloading either the PDF document itself or its attached files (if such exists).
- The print functionality, in the generic `PDFPrintService` implementation.
Hence neither the general PDF.js library, nor the *basic* functionality of the default viewer, depends on the `disableCreateObjectURL` option any more; which is why I'm thus proposing that we remove the option since using it is a performance footgun.
*Please note:* To not outright break currently "supported" browsers, which lack proper `URL.createObjectURL` support, this patch purposely keeps the compatibility-code to explicitly disable `URL.createObjectURL` usage *only* for browsers which are known to not work correctly.[1]
While it's certainly possible that there's additional, likely older, browsers with broken `URL.createObjectURL` support, the last time that these types of problems were reported was over *three* years ago.[2]
Hence in the *very* unlikely event that additional problems occur, as a result of these changes, we can either add a new case in the compatibility-code or simply declare the affected browser as unsupported.
---
[1] Which are IE11 (see issue 3977), and Google Chrome on iOS (see PR 8081).
[2] Given that `URL.createObjectURL` is used by default, you'd really expect more reports if these problems were widespread.
2020-08-06 19:43:59 +09:00
|
|
|
import { viewerCompatibilityParams } from "./viewer_compatibility.js";
|
2016-04-09 02:34:27 +09:00
|
|
|
|
2020-08-27 23:00:12 +09:00
|
|
|
const PdfFileRegExp = /\.pdf$/i;
|
|
|
|
|
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(
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
"fileattachmentannotation",
|
|
|
|
this._appendAttachment.bind(this)
|
|
|
|
);
|
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.
|
2017-04-18 00:13:48 +09:00
|
|
|
this._renderedCapability = createPromiseCapability();
|
|
|
|
}
|
2020-08-03 20:58:58 +09:00
|
|
|
if (this._pendingDispatchEvent) {
|
|
|
|
clearTimeout(this._pendingDispatchEvent);
|
|
|
|
}
|
|
|
|
this._pendingDispatchEvent = null;
|
2017-04-18 00:13:48 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
|
|
|
_dispatchEvent(attachmentsCount) {
|
2017-08-17 21:12:42 +09:00
|
|
|
this._renderedCapability.resolve();
|
|
|
|
|
2020-08-03 20:58:58 +09:00
|
|
|
if (this._pendingDispatchEvent) {
|
|
|
|
clearTimeout(this._pendingDispatchEvent);
|
|
|
|
this._pendingDispatchEvent = null;
|
|
|
|
}
|
|
|
|
if (attachmentsCount === 0) {
|
|
|
|
// 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.
|
|
|
|
this._pendingDispatchEvent = setTimeout(() => {
|
|
|
|
this.eventBus.dispatch("attachmentsloaded", {
|
|
|
|
source: this,
|
|
|
|
attachmentsCount: 0,
|
|
|
|
});
|
|
|
|
this._pendingDispatchEvent = null;
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
this.eventBus.dispatch("attachmentsloaded", {
|
2017-04-18 00:13:48 +09:00
|
|
|
source: this,
|
|
|
|
attachmentsCount,
|
|
|
|
});
|
|
|
|
}
|
2015-03-21 21:07:01 +09:00
|
|
|
|
2017-04-18 00:13:48 +09:00
|
|
|
/**
|
2020-01-29 19:37:52 +09:00
|
|
|
* NOTE: Should only be used when `URL.createObjectURL` is natively supported.
|
2017-04-18 00:13:48 +09:00
|
|
|
* @private
|
|
|
|
*/
|
2020-08-04 19:40:59 +09:00
|
|
|
_bindPdfLink(element, { content, filename }) {
|
2017-06-30 19:55:22 +09:00
|
|
|
let blobUrl;
|
2020-08-04 19:40:59 +09:00
|
|
|
element.onclick = () => {
|
2017-04-18 00:13:48 +09:00
|
|
|
if (!blobUrl) {
|
2020-04-24 18:50:58 +09:00
|
|
|
blobUrl = URL.createObjectURL(
|
|
|
|
new Blob([content], { type: "application/pdf" })
|
|
|
|
);
|
2017-04-16 03:49:16 +09:00
|
|
|
}
|
2017-06-30 19:55:22 +09:00
|
|
|
let viewerUrl;
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) {
|
2017-04-18 00:13:48 +09:00
|
|
|
// The current URL is the viewer, let's use it and append the file.
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
viewerUrl = "?file=" + encodeURIComponent(blobUrl + "#" + filename);
|
2020-04-24 20:17:04 +09:00
|
|
|
} else if (PDFJSDev.test("MOZCENTRAL")) {
|
|
|
|
// Let Firefox's content handler catch the URL and display the PDF.
|
[Firefox] Allow PDF attachments to, once again, be opened directly in the browser (bug 1632644)
Apparently the old link format used in MOZCENTRAL-builds, with the blob URL separated from the filename with a `?` character violates the specification; see https://bugzilla.mozilla.org/show_bug.cgi?id=1632644#c5
Obviously just removing the `?`-part of the URL would have worked, but that would also have meant that we'd no longer be able to provide the correct filename when the user attempts to download the opened PDF attachment.
To fix this we'll instead append the filename in the hash-part of the URL, which however required using a *custom* hash-parameter to avoid triggering the fallback "named destination" code-paths in the viewer.
Note that only changing the `web/pdf_attachment_viewer.js` file wasn't sufficient to fix the bug, and we also need to tweak the `webViewerInitialized` function in `web/app.js` since MOZCENTRAL-builds used to ignore *everything* in the URL hash.
This particular code is very old, but changing it *should* be completely safe given that the `PDFViewerApplication.setTitleUsingUrl` method since some time now stores both the original URL (in `this.url`) as well as one without the hash (in `this.baseUrl`). The latter one is already used everywhere where it matters, so this change seem fine to me.
This patch thus restores the original behaviour for PDF attachments in the MOZCENTRAL-build, by once again allowing them to be opened *directly* in the browser without downloading. (The fallback added in PR 11845 is obviously kept, since it seems generally useful to have.)
2020-05-20 18:27:29 +09:00
|
|
|
viewerUrl = blobUrl + "#filename=" + encodeURIComponent(filename);
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
} else if (PDFJSDev.test("CHROME")) {
|
2017-04-18 00:13:48 +09:00
|
|
|
// In the Chrome extension, the URL is rewritten using the history API
|
|
|
|
// in viewer.js, so an absolute URL must be generated.
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
viewerUrl =
|
2019-12-26 04:03:46 +09:00
|
|
|
// eslint-disable-next-line no-undef
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
chrome.runtime.getURL("/content/web/viewer.html") +
|
|
|
|
"?file=" +
|
|
|
|
encodeURIComponent(blobUrl + "#" + filename);
|
2016-02-21 21:36:24 +09:00
|
|
|
}
|
2020-04-24 20:17:04 +09:00
|
|
|
try {
|
2020-08-27 22:50:55 +09:00
|
|
|
window.open(viewerUrl);
|
2020-04-24 20:17:04 +09:00
|
|
|
} catch (ex) {
|
|
|
|
console.error(`_bindPdfLink: ${ex}`);
|
|
|
|
// Release the `blobUrl`, since opening it failed...
|
|
|
|
URL.revokeObjectURL(blobUrl);
|
|
|
|
blobUrl = null;
|
|
|
|
// ... and fallback to downloading the PDF file.
|
|
|
|
this.downloadManager.downloadData(content, filename, "application/pdf");
|
|
|
|
}
|
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
|
|
|
/**
|
|
|
|
* @private
|
|
|
|
*/
|
2020-08-04 19:40:59 +09:00
|
|
|
_bindLink(element, { content, filename }) {
|
|
|
|
element.onclick = () => {
|
2020-08-27 23:00:12 +09:00
|
|
|
const contentType = PdfFileRegExp.test(filename) ? "application/pdf" : "";
|
|
|
|
this.downloadManager.downloadData(content, filename, contentType);
|
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
|
|
|
|
*/
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +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-04-14 19:28:14 +09:00
|
|
|
const names = Object.keys(attachments).sort(function (a, b) {
|
2017-04-18 00:13:48 +09:00
|
|
|
return a.toLowerCase().localeCompare(b.toLowerCase());
|
|
|
|
});
|
|
|
|
|
2020-08-03 19:11:46 +09:00
|
|
|
const fragment = document.createDocumentFragment();
|
2020-08-04 19:40:59 +09:00
|
|
|
let attachmentsCount = 0;
|
|
|
|
for (const name of names) {
|
|
|
|
const item = attachments[name];
|
|
|
|
const filename = getFilenameFromUrl(item.filename);
|
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");
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
if (
|
2020-08-27 23:00:12 +09:00
|
|
|
PdfFileRegExp.test(filename) &&
|
Remove the `disableCreateObjectURL` option from `web/app_options.js`
Prior to PR 11601, the `disableCreateObjectURL` option was present on `getDocument` in the API, since it was (potentially) used when decoding JPEG images natively in the browser. Hence setting this option, which was done automatically using compatibility-code, were in some browsers necessary in order for e.g. JPEG images to be correctly rendered.
The downside of the `disableCreateObjectURL` option is that memory usage increases significantly, since we're forced to build and use `data:` URIs (rather than `blob:` URLs).
However, at this point in time the `disableCreateObjectURL` option is only necessary for *some* (non-essential) functionality in the default viewer; in particular:
- The openfile functionality, used only when manually opening a new file in the default viewer.
- The download functionality, used when downloading either the PDF document itself or its attached files (if such exists).
- The print functionality, in the generic `PDFPrintService` implementation.
Hence neither the general PDF.js library, nor the *basic* functionality of the default viewer, depends on the `disableCreateObjectURL` option any more; which is why I'm thus proposing that we remove the option since using it is a performance footgun.
*Please note:* To not outright break currently "supported" browsers, which lack proper `URL.createObjectURL` support, this patch purposely keeps the compatibility-code to explicitly disable `URL.createObjectURL` usage *only* for browsers which are known to not work correctly.[1]
While it's certainly possible that there's additional, likely older, browsers with broken `URL.createObjectURL` support, the last time that these types of problems were reported was over *three* years ago.[2]
Hence in the *very* unlikely event that additional problems occur, as a result of these changes, we can either add a new case in the compatibility-code or simply declare the affected browser as unsupported.
---
[1] Which are IE11 (see issue 3977), and Google Chrome on iOS (see PR 8081).
[2] Given that `URL.createObjectURL` is used by default, you'd really expect more reports if these problems were widespread.
2020-08-06 19:43:59 +09:00
|
|
|
!viewerCompatibilityParams.disableCreateObjectURL
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
) {
|
2020-08-04 19:40:59 +09:00
|
|
|
this._bindPdfLink(element, { content: item.content, filename });
|
2017-04-18 00:13:48 +09:00
|
|
|
} else {
|
2020-08-04 19:40:59 +09:00
|
|
|
this._bindLink(element, { content: item.content, filename });
|
2017-04-18 00:13:48 +09:00
|
|
|
}
|
2020-08-04 19:40:59 +09:00
|
|
|
element.textContent = this._normalizeTextContent(filename);
|
|
|
|
|
|
|
|
div.appendChild(element);
|
2017-04-18 00:13:48 +09:00
|
|
|
|
2020-08-03 19:11:46 +09:00
|
|
|
fragment.appendChild(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-08-03 19:11:46 +09:00
|
|
|
this.container.appendChild(fragment);
|
2017-04-18 00:13:48 +09:00
|
|
|
|
|
|
|
this._dispatchEvent(attachmentsCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Used to append FileAttachment annotations to the sidebar.
|
|
|
|
* @private
|
|
|
|
*/
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
_appendAttachment({ id, 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.
|
|
|
|
}
|
2020-08-04 19:40:59 +09:00
|
|
|
let attachments = this._attachments;
|
2017-04-18 00:13:48 +09:00
|
|
|
|
|
|
|
if (!attachments) {
|
|
|
|
attachments = Object.create(null);
|
|
|
|
} else {
|
2019-12-27 08:22:32 +09:00
|
|
|
for (const name in attachments) {
|
2017-04-18 00:13:48 +09:00
|
|
|
if (id === 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
|
|
|
}
|
|
|
|
attachments[id] = {
|
|
|
|
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
|
|
|
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
export { PDFAttachmentViewer };
|