66aabe3ec7
*Besides, obviously, adding viewer support:* This patch attempts to improve the general API for Optional Content Groups slightly, by adding a couple of new methods for interacting with the (more complex) data structures of `OptionalContentConfig`-instances. (Thus allowing us to mark some of the data as "private", given that it probably shouldn't be manipulated directly.) By utilizing not just the "raw" Optional Content Groups, but the data from the `/Order` array when available, we can thus display the Layers in a proper tree-structure with collapsible headings for PDF documents that utilizes that feature. Note that it's possible to reset all Optional Content Groups to their default visibility state, simply by double-clicking on the Layers-button in the sidebar. (Currently that's indicated in the Layers-button tooltip, which is obviously easy to overlook, however it's probably the best we can do for now without adding more buttons, or even a dropdown-toolbar, to the sidebar.) Also, the current Layers-button icons are a little rough around the edges, quite literally, but given that the viewer will soon have its UI modernized anyway they hopefully suffice in the meantime. To give users *full* control of the visibility of the various Optional Content Groups, even those which according to the `/Order` array should not (by default) be toggleable in the UI, this patch will place those under a *custom* heading which: - Is collapsed by default, and placed at the bottom of the Layers-tree, to be a bit less obtrusive. - Uses a slightly different formatting, compared to the "regular" headings. - Is localizable. Finally, note that the thumbnails are *purposely* always rendered with all Optional Content Groups at their default visibility state, since that seems the most useful and it's also consistent with other viewers. To ensure that this works as intended, we'll thus disable the `PDFThumbnailView.setImage` functionality when the Optional Content Groups have been changed in the viewer. (This obviously means that we'll re-render thumbnails instead of using the rendered pages. However, this situation ought to be rare enough for this to not really be a problem.)
244 lines
10 KiB
JavaScript
244 lines
10 KiB
JavaScript
/* Copyright 2016 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.
|
|
*/
|
|
|
|
"use strict";
|
|
|
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME")) {
|
|
var defaultUrl; // eslint-disable-line no-var
|
|
|
|
(function rewriteUrlClosure() {
|
|
// Run this code outside DOMContentLoaded to make sure that the URL
|
|
// is rewritten as soon as possible.
|
|
const queryString = document.location.search.slice(1);
|
|
const m = /(^|&)file=([^&]*)/.exec(queryString);
|
|
defaultUrl = m ? decodeURIComponent(m[2]) : "";
|
|
|
|
// Example: chrome-extension://.../http://example.com/file.pdf
|
|
const humanReadableUrl = "/" + defaultUrl + location.hash;
|
|
history.replaceState(history.state, "", humanReadableUrl);
|
|
if (top === window) {
|
|
// eslint-disable-next-line no-undef
|
|
chrome.runtime.sendMessage("showPageAction");
|
|
}
|
|
})();
|
|
}
|
|
|
|
let pdfjsWebApp, pdfjsWebAppOptions;
|
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("PRODUCTION")) {
|
|
pdfjsWebApp = require("./app.js");
|
|
pdfjsWebAppOptions = require("./app_options.js");
|
|
}
|
|
|
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) {
|
|
require("./firefoxcom.js");
|
|
require("./firefox_print_service.js");
|
|
}
|
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC")) {
|
|
require("./genericcom.js");
|
|
}
|
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME")) {
|
|
require("./chromecom.js");
|
|
}
|
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME || GENERIC")) {
|
|
require("./pdf_print_service.js");
|
|
}
|
|
|
|
function getViewerConfiguration() {
|
|
return {
|
|
appContainer: document.body,
|
|
mainContainer: document.getElementById("viewerContainer"),
|
|
viewerContainer: document.getElementById("viewer"),
|
|
eventBus: null,
|
|
toolbar: {
|
|
container: document.getElementById("toolbarViewer"),
|
|
numPages: document.getElementById("numPages"),
|
|
pageNumber: document.getElementById("pageNumber"),
|
|
scaleSelectContainer: document.getElementById("scaleSelectContainer"),
|
|
scaleSelect: document.getElementById("scaleSelect"),
|
|
customScaleOption: document.getElementById("customScaleOption"),
|
|
previous: document.getElementById("previous"),
|
|
next: document.getElementById("next"),
|
|
zoomIn: document.getElementById("zoomIn"),
|
|
zoomOut: document.getElementById("zoomOut"),
|
|
viewFind: document.getElementById("viewFind"),
|
|
openFile: document.getElementById("openFile"),
|
|
print: document.getElementById("print"),
|
|
presentationModeButton: document.getElementById("presentationMode"),
|
|
download: document.getElementById("download"),
|
|
viewBookmark: document.getElementById("viewBookmark"),
|
|
},
|
|
secondaryToolbar: {
|
|
toolbar: document.getElementById("secondaryToolbar"),
|
|
toggleButton: document.getElementById("secondaryToolbarToggle"),
|
|
toolbarButtonContainer: document.getElementById(
|
|
"secondaryToolbarButtonContainer"
|
|
),
|
|
presentationModeButton: document.getElementById(
|
|
"secondaryPresentationMode"
|
|
),
|
|
openFileButton: document.getElementById("secondaryOpenFile"),
|
|
printButton: document.getElementById("secondaryPrint"),
|
|
downloadButton: document.getElementById("secondaryDownload"),
|
|
viewBookmarkButton: document.getElementById("secondaryViewBookmark"),
|
|
firstPageButton: document.getElementById("firstPage"),
|
|
lastPageButton: document.getElementById("lastPage"),
|
|
pageRotateCwButton: document.getElementById("pageRotateCw"),
|
|
pageRotateCcwButton: document.getElementById("pageRotateCcw"),
|
|
cursorSelectToolButton: document.getElementById("cursorSelectTool"),
|
|
cursorHandToolButton: document.getElementById("cursorHandTool"),
|
|
scrollVerticalButton: document.getElementById("scrollVertical"),
|
|
scrollHorizontalButton: document.getElementById("scrollHorizontal"),
|
|
scrollWrappedButton: document.getElementById("scrollWrapped"),
|
|
spreadNoneButton: document.getElementById("spreadNone"),
|
|
spreadOddButton: document.getElementById("spreadOdd"),
|
|
spreadEvenButton: document.getElementById("spreadEven"),
|
|
documentPropertiesButton: document.getElementById("documentProperties"),
|
|
},
|
|
fullscreen: {
|
|
contextFirstPage: document.getElementById("contextFirstPage"),
|
|
contextLastPage: document.getElementById("contextLastPage"),
|
|
contextPageRotateCw: document.getElementById("contextPageRotateCw"),
|
|
contextPageRotateCcw: document.getElementById("contextPageRotateCcw"),
|
|
},
|
|
sidebar: {
|
|
// Divs (and sidebar button)
|
|
outerContainer: document.getElementById("outerContainer"),
|
|
viewerContainer: document.getElementById("viewerContainer"),
|
|
toggleButton: document.getElementById("sidebarToggle"),
|
|
// Buttons
|
|
thumbnailButton: document.getElementById("viewThumbnail"),
|
|
outlineButton: document.getElementById("viewOutline"),
|
|
attachmentsButton: document.getElementById("viewAttachments"),
|
|
layersButton: document.getElementById("viewLayers"),
|
|
// Views
|
|
thumbnailView: document.getElementById("thumbnailView"),
|
|
outlineView: document.getElementById("outlineView"),
|
|
attachmentsView: document.getElementById("attachmentsView"),
|
|
layersView: document.getElementById("layersView"),
|
|
},
|
|
sidebarResizer: {
|
|
outerContainer: document.getElementById("outerContainer"),
|
|
resizer: document.getElementById("sidebarResizer"),
|
|
},
|
|
findBar: {
|
|
bar: document.getElementById("findbar"),
|
|
toggleButton: document.getElementById("viewFind"),
|
|
findField: document.getElementById("findInput"),
|
|
highlightAllCheckbox: document.getElementById("findHighlightAll"),
|
|
caseSensitiveCheckbox: document.getElementById("findMatchCase"),
|
|
entireWordCheckbox: document.getElementById("findEntireWord"),
|
|
findMsg: document.getElementById("findMsg"),
|
|
findResultsCount: document.getElementById("findResultsCount"),
|
|
findPreviousButton: document.getElementById("findPrevious"),
|
|
findNextButton: document.getElementById("findNext"),
|
|
},
|
|
passwordOverlay: {
|
|
overlayName: "passwordOverlay",
|
|
container: document.getElementById("passwordOverlay"),
|
|
label: document.getElementById("passwordText"),
|
|
input: document.getElementById("password"),
|
|
submitButton: document.getElementById("passwordSubmit"),
|
|
cancelButton: document.getElementById("passwordCancel"),
|
|
},
|
|
documentProperties: {
|
|
overlayName: "documentPropertiesOverlay",
|
|
container: document.getElementById("documentPropertiesOverlay"),
|
|
closeButton: document.getElementById("documentPropertiesClose"),
|
|
fields: {
|
|
fileName: document.getElementById("fileNameField"),
|
|
fileSize: document.getElementById("fileSizeField"),
|
|
title: document.getElementById("titleField"),
|
|
author: document.getElementById("authorField"),
|
|
subject: document.getElementById("subjectField"),
|
|
keywords: document.getElementById("keywordsField"),
|
|
creationDate: document.getElementById("creationDateField"),
|
|
modificationDate: document.getElementById("modificationDateField"),
|
|
creator: document.getElementById("creatorField"),
|
|
producer: document.getElementById("producerField"),
|
|
version: document.getElementById("versionField"),
|
|
pageCount: document.getElementById("pageCountField"),
|
|
pageSize: document.getElementById("pageSizeField"),
|
|
linearized: document.getElementById("linearizedField"),
|
|
},
|
|
},
|
|
errorWrapper: {
|
|
container: document.getElementById("errorWrapper"),
|
|
errorMessage: document.getElementById("errorMessage"),
|
|
closeButton: document.getElementById("errorClose"),
|
|
errorMoreInfo: document.getElementById("errorMoreInfo"),
|
|
moreInfoButton: document.getElementById("errorShowMore"),
|
|
lessInfoButton: document.getElementById("errorShowLess"),
|
|
},
|
|
printContainer: document.getElementById("printContainer"),
|
|
openFileInputName: "fileInput",
|
|
debuggerScriptPath: "./debugger.js",
|
|
};
|
|
}
|
|
|
|
function webViewerLoad() {
|
|
const config = getViewerConfiguration();
|
|
if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
|
|
Promise.all([
|
|
import("pdfjs-web/app.js"),
|
|
import("pdfjs-web/app_options.js"),
|
|
import("pdfjs-web/genericcom.js"),
|
|
import("pdfjs-web/pdf_print_service.js"),
|
|
]).then(function ([app, appOptions, genericCom, pdfPrintService]) {
|
|
window.PDFViewerApplication = app.PDFViewerApplication;
|
|
window.PDFViewerApplicationOptions = appOptions.AppOptions;
|
|
app.PDFViewerApplication.run(config);
|
|
});
|
|
} else {
|
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("CHROME")) {
|
|
pdfjsWebAppOptions.AppOptions.set("defaultUrl", defaultUrl);
|
|
}
|
|
|
|
window.PDFViewerApplication = pdfjsWebApp.PDFViewerApplication;
|
|
window.PDFViewerApplicationOptions = pdfjsWebAppOptions.AppOptions;
|
|
|
|
if (typeof PDFJSDev !== "undefined" && PDFJSDev.test("GENERIC")) {
|
|
// Give custom implementations of the default viewer a simpler way to
|
|
// set various `AppOptions`, by dispatching an event once all viewer
|
|
// files are loaded but *before* the viewer initialization has run.
|
|
const event = document.createEvent("CustomEvent");
|
|
event.initCustomEvent("webviewerloaded", true, true, {
|
|
source: window,
|
|
});
|
|
try {
|
|
// Attempt to dispatch the event at the embedding `document`,
|
|
// in order to support cases where the viewer is embedded in
|
|
// a *dynamically* created <iframe> element.
|
|
parent.document.dispatchEvent(event);
|
|
} catch (ex) {
|
|
// The viewer could be in e.g. a cross-origin <iframe> element,
|
|
// fallback to dispatching the event at the current `document`.
|
|
console.error(`webviewerloaded: ${ex}`);
|
|
document.dispatchEvent(event);
|
|
}
|
|
}
|
|
|
|
pdfjsWebApp.PDFViewerApplication.run(config);
|
|
}
|
|
}
|
|
|
|
if (
|
|
document.readyState === "interactive" ||
|
|
document.readyState === "complete"
|
|
) {
|
|
webViewerLoad();
|
|
} else {
|
|
document.addEventListener("DOMContentLoaded", webViewerLoad, true);
|
|
}
|