Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-major] Refactor viewer components initialization to reduce their dependency on the global PDFJS object #9479

Merged
merged 10 commits into from
Feb 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions examples/mobile-viewer/viewer.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ if (typeof PDFJS === 'undefined' || !PDFJS.PDFViewer || !PDFJS.getDocument) {
' `gulp dist-install`');
}

PDFJS.useOnlyCssZoom = true;
PDFJS.disableTextLayer = true;
var USE_ONLY_CSS_ZOOM = true;
var TEXT_LAYER_MODE = 0; // DISABLE
PDFJS.maxImageSize = 1024 * 1024;
PDFJS.workerSrc = '../../node_modules/pdfjs-dist/build/pdf.worker.js';
PDFJS.cMapUrl = '../../node_modules/pdfjs-dist/cmaps/';
Expand Down Expand Up @@ -298,6 +298,8 @@ var PDFViewerApplication = {
container: container,
linkService: linkService,
l10n: this.l10n,
useOnlyCssZoom: USE_ONLY_CSS_ZOOM,
textLayerMode: TEXT_LAYER_MODE,
});
this.pdfViewer = pdfViewer;
linkService.setViewer(pdfViewer);
Expand Down
19 changes: 10 additions & 9 deletions extensions/chromium/preferences_schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,16 @@
"type": "boolean",
"default": false
},
"disableTextLayer": {
"title": "Disable text selection layer",
"description": "Whether to disable the text selection layer.",
"type": "boolean",
"default": false
"textLayerMode": {
"title": "Text layer mode",
"description": "Controls if the text layer is enabled, and the selection mode that is used.\n 0 = Disabled.\n 1 = Enabled.\n 2 = (Experimental) Enabled, with enhanced text selection.",
"type": "integer",
"enum": [
0,
1,
2
],
"default": 1
},
"useOnlyCssZoom": {
"type": "boolean",
Expand Down Expand Up @@ -112,10 +117,6 @@
"description": "Whether to prevent the extension from reporting the extension and browser version to the extension developers.",
"default": false
},
"enhanceTextSelection": {
"type": "boolean",
"default": false
},
"renderer": {
"type": "string",
"enum": [
Expand Down
29 changes: 17 additions & 12 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,7 @@
*/

import {
addLinkAttributes, DOMSVGFactory, getDefaultSetting, getFilenameFromUrl,
LinkTarget
addLinkAttributes, DOMSVGFactory, getFilenameFromUrl, LinkTarget
} from './dom_utils';
import {
AnnotationBorderStyleType, AnnotationType, stringToPDFString, unreachable,
Expand All @@ -30,7 +29,8 @@ import {
* @property {PageViewport} viewport
* @property {IPDFLinkService} linkService
* @property {DownloadManager} downloadManager
* @property {string} imageResourcesPath
* @property {string} imageResourcesPath - (optional) Path for image resources,
* mainly for annotation icons. Include trailing slash.
* @property {boolean} renderInteractiveForms
* @property {Object} svgFactory
*/
Expand Down Expand Up @@ -281,17 +281,21 @@ class LinkAnnotationElement extends AnnotationElement {
render() {
this.container.className = 'linkAnnotation';

let { data, linkService, } = this;
let link = document.createElement('a');

addLinkAttributes(link, {
url: this.data.url,
target: (this.data.newWindow ? LinkTarget.BLANK : undefined),
url: data.url,
target: (data.newWindow ?
LinkTarget.BLANK : linkService.externalLinkTarget),
rel: linkService.externalLinkRel,
});

if (!this.data.url) {
if (this.data.action) {
this._bindNamedAction(link, this.data.action);
if (!data.url) {
if (data.action) {
this._bindNamedAction(link, data.action);
} else {
this._bindLink(link, this.data.dest);
this._bindLink(link, data.dest);
}
}

Expand Down Expand Up @@ -1183,7 +1187,9 @@ class FileAttachmentAnnotationElement extends AnnotationElement {
* @property {Array} annotations
* @property {PDFPage} page
* @property {IPDFLinkService} linkService
* @property {string} imageResourcesPath
* @property {DownloadManager} downloadManager
* @property {string} imageResourcesPath - (optional) Path for image resources,
* mainly for annotation icons. Include trailing slash.
* @property {boolean} renderInteractiveForms
*/

Expand All @@ -1208,8 +1214,7 @@ class AnnotationLayer {
viewport: parameters.viewport,
linkService: parameters.linkService,
downloadManager: parameters.downloadManager,
imageResourcesPath: parameters.imageResourcesPath ||
getDefaultSetting('imageResourcesPath'),
imageResourcesPath: parameters.imageResourcesPath || '',
renderInteractiveForms: parameters.renderInteractiveForms || false,
svgFactory: new DOMSVGFactory(),
});
Expand Down
8 changes: 4 additions & 4 deletions src/display/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,8 @@ var PDFDocumentProxy = (function PDFDocumentProxyClosure() {
* calling of PDFPage.getViewport method.
* @property {string} intent - Rendering intent, can be 'display' or 'print'
* (default value is 'display').
* @property {boolean} enableWebGL - (optional) Enables WebGL accelerated
* rendering for some operations. The default value is `false`.
* @property {boolean} renderInteractiveForms - (optional) Whether or not
* interactive form elements are rendered in the display
* layer. If so, we do not render them on canvas as well.
Expand Down Expand Up @@ -743,7 +745,7 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
this.pageIndex = pageIndex;
this.pageInfo = pageInfo;
this.transport = transport;
this._stats = (getDefaultSetting('enableStats') ?
this._stats = (getDefaultSetting('pdfBug') ?
new StatTimer() : DummyStatTimer);
this.commonObjs = transport.commonObjs;
this.objs = new PDFObjects();
Expand Down Expand Up @@ -828,9 +830,7 @@ var PDFPageProxy = (function PDFPageProxyClosure() {
var renderingIntent = (params.intent === 'print' ? 'print' : 'display');
var canvasFactory = params.canvasFactory || new DOMCanvasFactory();
let webGLContext = new WebGLContext({
// TODO: When moving this parameter from `PDFJS` to {RenderParameters},
// change its name to `enableWebGL` instead.
enable: !getDefaultSetting('disableWebGL'),
enable: params.enableWebGL,
});

if (!this.intentStates[renderingIntent]) {
Expand Down
67 changes: 12 additions & 55 deletions src/display/dom_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,15 +274,15 @@ var RenderingCancelledException = (function RenderingCancelledException() {
return RenderingCancelledException;
})();

var LinkTarget = {
const LinkTarget = {
NONE: 0, // Default value.
SELF: 1,
BLANK: 2,
PARENT: 3,
TOP: 4,
};

var LinkTargetStringMap = [
const LinkTargetStringMap = [
'',
'_self',
'_blank',
Expand All @@ -294,31 +294,27 @@ var LinkTargetStringMap = [
* @typedef ExternalLinkParameters
* @typedef {Object} ExternalLinkParameters
* @property {string} url - An absolute URL.
* @property {LinkTarget} target - The link target.
* @property {string} rel - The link relationship.
* @property {LinkTarget} target - (optional) The link target.
* The default value is `LinkTarget.NONE`.
* @property {string} rel - (optional) The link relationship.
* The default value is `DEFAULT_LINK_REL`.
*/

/**
* Adds various attributes (href, title, target, rel) to hyperlinks.
* @param {HTMLLinkElement} link - The link element.
* @param {ExternalLinkParameters} params
*/
function addLinkAttributes(link, params) {
var url = params && params.url;
function addLinkAttributes(link, { url, target, rel, } = {}) {
link.href = link.title = (url ? removeNullCharacters(url) : '');

if (url) {
var target = params.target;
if (typeof target === 'undefined') {
target = getDefaultSetting('externalLinkTarget');
}
link.target = LinkTargetStringMap[target];
const LinkTargetValues = Object.values(LinkTarget);
let targetIndex =
LinkTargetValues.includes(target) ? target : LinkTarget.NONE;
link.target = LinkTargetStringMap[targetIndex];

var rel = params.rel;
if (typeof rel === 'undefined') {
rel = getDefaultSetting('externalLinkRel');
}
link.rel = rel;
link.rel = (typeof rel === 'string' ? rel : DEFAULT_LINK_REL);
}
}

Expand Down Expand Up @@ -349,8 +345,6 @@ function getDefaultSetting(id) {
return globalSettings ? globalSettings.disableFontFace : false;
case 'disableCreateObjectURL':
return globalSettings ? globalSettings.disableCreateObjectURL : false;
case 'disableWebGL':
return globalSettings ? globalSettings.disableWebGL : true;
case 'cMapUrl':
return globalSettings ? globalSettings.cMapUrl : null;
case 'cMapPacked':
Expand All @@ -363,49 +357,13 @@ function getDefaultSetting(id) {
return globalSettings ? globalSettings.workerSrc : null;
case 'maxImageSize':
return globalSettings ? globalSettings.maxImageSize : -1;
case 'imageResourcesPath':
return globalSettings ? globalSettings.imageResourcesPath : '';
case 'isEvalSupported':
return globalSettings ? globalSettings.isEvalSupported : true;
case 'externalLinkTarget':
if (!globalSettings) {
return LinkTarget.NONE;
}
switch (globalSettings.externalLinkTarget) {
case LinkTarget.NONE:
case LinkTarget.SELF:
case LinkTarget.BLANK:
case LinkTarget.PARENT:
case LinkTarget.TOP:
return globalSettings.externalLinkTarget;
}
warn('PDFJS.externalLinkTarget is invalid: ' +
globalSettings.externalLinkTarget);
// Reset the external link target, to suppress further warnings.
globalSettings.externalLinkTarget = LinkTarget.NONE;
return LinkTarget.NONE;
case 'externalLinkRel':
return globalSettings ? globalSettings.externalLinkRel : DEFAULT_LINK_REL;
case 'enableStats':
return !!(globalSettings && globalSettings.enableStats);
default:
throw new Error('Unknown default setting: ' + id);
}
}

function isExternalLinkTargetSet() {
var externalLinkTarget = getDefaultSetting('externalLinkTarget');
switch (externalLinkTarget) {
case LinkTarget.NONE:
return false;
case LinkTarget.SELF:
case LinkTarget.BLANK:
case LinkTarget.PARENT:
case LinkTarget.TOP:
return true;
}
}

class StatTimer {
constructor(enable = true) {
this.enabled = !!enable;
Expand Down Expand Up @@ -483,7 +441,6 @@ class DummyStatTimer {
export {
RenderingCancelledException,
addLinkAttributes,
isExternalLinkTargetSet,
getFilenameFromUrl,
LinkTarget,
getDefaultSetting,
Expand Down
31 changes: 2 additions & 29 deletions src/display/global.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,14 @@
* limitations under the License.
*/

import {
addLinkAttributes, DEFAULT_LINK_REL, getFilenameFromUrl,
isExternalLinkTargetSet, isValidUrl, LinkTarget
} from './dom_utils';
import {
createBlob, createObjectURL, createPromiseCapability, getVerbosityLevel,
InvalidPDFException, isLittleEndian, MissingPDFException, OPS, PageViewport,
PasswordException, PasswordResponses, removeNullCharacters, setVerbosityLevel,
shadow, UnexpectedResponseException, UnknownErrorException,
UNSUPPORTED_FEATURES, Util, VERBOSITY_LEVELS
} from '../shared/util';
import { DEFAULT_LINK_REL, getFilenameFromUrl, LinkTarget } from './dom_utils';
import {
getDocument, LoopbackPort, PDFDataRangeTransport, PDFWorker
} from './api';
Expand All @@ -42,11 +39,6 @@ if (!globalScope.PDFJS) {
}
var PDFJS = globalScope.PDFJS;

if (typeof PDFJSDev !== 'undefined') {
PDFJS.version = PDFJSDev.eval('BUNDLE_VERSION');
PDFJS.build = PDFJSDev.eval('BUNDLE_BUILD');
}

PDFJS.pdfBug = false;

if (PDFJS.verbosity !== undefined) {
Expand All @@ -67,7 +59,6 @@ Object.defineProperty(PDFJS, 'verbosity', {
PDFJS.VERBOSITY_LEVELS = VERBOSITY_LEVELS;
PDFJS.OPS = OPS;
PDFJS.UNSUPPORTED_FEATURES = UNSUPPORTED_FEATURES;
PDFJS.isValidUrl = isValidUrl;
PDFJS.shadow = shadow;
PDFJS.createBlob = createBlob;
PDFJS.createObjectURL = function PDFJS_createObjectURL(data, contentType) {
Expand Down Expand Up @@ -120,14 +111,6 @@ PDFJS.cMapPacked = PDFJS.cMapPacked === undefined ? false : PDFJS.cMapPacked;
PDFJS.disableFontFace = (PDFJS.disableFontFace === undefined ?
false : PDFJS.disableFontFace);

/**
* Path for image resources, mainly for annotation icons. Include trailing
* slash.
* @var {string}
*/
PDFJS.imageResourcesPath = (PDFJS.imageResourcesPath === undefined ?
'' : PDFJS.imageResourcesPath);

/**
* Path and filename of the worker file. Required when the worker is enabled
* in development mode. If unspecified in the production build, the worker
Expand Down Expand Up @@ -192,16 +175,9 @@ PDFJS.postMessageTransfers = (PDFJS.postMessageTransfers === undefined ?
PDFJS.disableCreateObjectURL = (PDFJS.disableCreateObjectURL === undefined ?
false : PDFJS.disableCreateObjectURL);

/**
* Disables WebGL usage.
* @var {boolean}
*/
PDFJS.disableWebGL = (PDFJS.disableWebGL === undefined ?
true : PDFJS.disableWebGL);

/**
* Specifies the |target| attribute for external links.
* The constants from PDFJS.LinkTarget should be used:
* The constants from {LinkTarget} should be used:
* - NONE [default]
* - SELF
* - BLANK
Expand Down Expand Up @@ -233,10 +209,7 @@ PDFJS.LoopbackPort = LoopbackPort;
PDFJS.PDFDataRangeTransport = PDFDataRangeTransport;
PDFJS.PDFWorker = PDFWorker;

PDFJS.LinkTarget = LinkTarget;
PDFJS.addLinkAttributes = addLinkAttributes;
PDFJS.getFilenameFromUrl = getFilenameFromUrl;
PDFJS.isExternalLinkTargetSet = isExternalLinkTargetSet;

PDFJS.AnnotationLayer = AnnotationLayer;

Expand Down
1 change: 1 addition & 0 deletions src/pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,4 +91,5 @@ exports.createBlob = pdfjsSharedUtil.createBlob;
exports.RenderingCancelledException =
pdfjsDisplayDOMUtils.RenderingCancelledException;
exports.getFilenameFromUrl = pdfjsDisplayDOMUtils.getFilenameFromUrl;
exports.LinkTarget = pdfjsDisplayDOMUtils.LinkTarget;
exports.addLinkAttributes = pdfjsDisplayDOMUtils.addLinkAttributes;
Loading