Merge pull request #10337 from wojtekmaj/turn-on-eslint-in-examples

Turn on ESLint in examples
This commit is contained in:
Tim van der Meij 2018-12-11 23:44:54 +01:00 committed by GitHub
commit cad0e61262
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 119 additions and 66 deletions

View File

@ -2,7 +2,6 @@ build/
l10n/ l10n/
docs/ docs/
node_modules/ node_modules/
examples/
external/bcmaps/ external/bcmaps/
external/webL10n/ external/webL10n/
external/cmapscompress/ external/cmapscompress/

24
examples/.eslintrc Normal file
View File

@ -0,0 +1,24 @@
{
"extends": [
"../.eslintrc"
],
"parserOptions": {
"ecmaVersion": 5,
},
"env": {
"es6": false,
},
"globals": {
"pdfjsImageDecoders": false,
"pdfjsLib": false,
"pdfjsViewer": false,
"Uint8Array": false,
},
"rules": {
"object-shorthand": ["error", "never"]
}
}

View File

@ -38,7 +38,8 @@ loadingTask.promise.then(function(doc) {
id: pageNum, id: pageNum,
scale: DEFAULT_SCALE, scale: DEFAULT_SCALE,
defaultViewport: pdfPage.getViewport(DEFAULT_SCALE), defaultViewport: pdfPage.getViewport(DEFAULT_SCALE),
annotationLayerFactory: new pdfjsViewer.DefaultAnnotationLayerFactory(), annotationLayerFactory:
new pdfjsViewer.DefaultAnnotationLayerFactory(),
renderInteractiveForms: true, renderInteractiveForms: true,
}); });

View File

@ -9,7 +9,7 @@ var OUTPUT_PATH = '../../build/browserify';
var TMP_FILE_PREFIX = '../../build/browserify_'; var TMP_FILE_PREFIX = '../../build/browserify_';
gulp.task('build-bundle', function() { gulp.task('build-bundle', function() {
return browserify('main.js', {output: TMP_FILE_PREFIX + 'main.tmp'}) return browserify('main.js', { output: TMP_FILE_PREFIX + 'main.tmp', })
.ignore(require.resolve('pdfjs-dist/build/pdf.worker')) // Reducing size .ignore(require.resolve('pdfjs-dist/build/pdf.worker')) // Reducing size
.bundle() .bundle()
.pipe(source(TMP_FILE_PREFIX + 'main.tmp')) .pipe(source(TMP_FILE_PREFIX + 'main.tmp'))
@ -21,12 +21,14 @@ gulp.task('build-bundle', function() {
gulp.task('build-worker', function() { gulp.task('build-worker', function() {
// We can create our own viewer (see worker.js) or use already defined one. // We can create our own viewer (see worker.js) or use already defined one.
var workerSrc = require.resolve('pdfjs-dist/build/pdf.worker.entry'); var workerSrc = require.resolve('pdfjs-dist/build/pdf.worker.entry');
return browserify(workerSrc, {output: TMP_FILE_PREFIX + 'worker.tmp'}) return browserify(workerSrc, { output: TMP_FILE_PREFIX + 'worker.tmp', })
.bundle() .bundle()
.pipe(source(TMP_FILE_PREFIX + 'worker.tmp')) .pipe(source(TMP_FILE_PREFIX + 'worker.tmp'))
.pipe(streamify(uglify({compress:{ .pipe(streamify(uglify({
sequences: false // Chrome has issue with the generated code if true compress: {
}}))) sequences: false, // Chrome has issue with the generated code if true
},
})))
.pipe(rename('pdf.worker.bundle.js')) .pipe(rename('pdf.worker.bundle.js'))
.pipe(gulp.dest(OUTPUT_PATH)); .pipe(gulp.dest(OUTPUT_PATH));
}); });

View File

@ -24,7 +24,7 @@ loadingTask.promise.then(function (pdfDocument) {
var ctx = canvas.getContext('2d'); var ctx = canvas.getContext('2d');
var renderTask = pdfPage.render({ var renderTask = pdfPage.render({
canvasContext: ctx, canvasContext: ctx,
viewport: viewport viewport: viewport,
}); });
return renderTask.promise; return renderTask.promise;
}); });

View File

@ -59,8 +59,8 @@ jpegImage.parse(typedArrayImage);
var width = jpegImage.width, height = jpegImage.height; var width = jpegImage.width, height = jpegImage.height;
var jpegData = jpegImage.getData({ var jpegData = jpegImage.getData({
width, width: width,
height, height: height,
forceRGB: true, forceRGB: true,
}); });
@ -68,12 +68,12 @@ var jpegData = jpegImage.getData({
// //
var imageData = jpegCtx.createImageData(width, height); var imageData = jpegCtx.createImageData(width, height);
var imageBytes = imageData.data; var imageBytes = imageData.data;
for (var i = 0, j = 0, ii = width * height * 4; i < ii;) { for (var j = 0, k = 0, jj = width * height * 4; j < jj;) {
imageBytes[i++] = jpegData[j++]; imageBytes[j++] = jpegData[k++];
imageBytes[i++] = jpegData[j++]; imageBytes[j++] = jpegData[k++];
imageBytes[i++] = jpegData[j++]; imageBytes[j++] = jpegData[k++];
imageBytes[i++] = 255; imageBytes[j++] = 255;
} }
jpegCanvas.width = width, jpegCanvas.height = height; jpegCanvas.width = width;
jpegCanvas.height = height;
jpegCtx.putImageData(imageData, 0, 0); jpegCtx.putImageData(imageData, 0, 0);

View File

@ -47,7 +47,7 @@ var PDFViewerApplication = {
* @returns {Promise} - Returns the promise, which is resolved when document * @returns {Promise} - Returns the promise, which is resolved when document
* is opened. * is opened.
*/ */
open: function (params) { open: function(params) {
if (this.pdfLoadingTask) { if (this.pdfLoadingTask) {
// We need to destroy already opened document // We need to destroy already opened document
return this.close().then(function () { return this.close().then(function () {
@ -104,7 +104,7 @@ var PDFViewerApplication = {
} }
loadingErrorMessage.then(function (msg) { loadingErrorMessage.then(function (msg) {
self.error(msg, {message: message}); self.error(msg, { message: message, });
}); });
self.loadingBar.hide(); self.loadingBar.hide();
}); });
@ -115,7 +115,7 @@ var PDFViewerApplication = {
* @returns {Promise} - Returns the promise, which is resolved when all * @returns {Promise} - Returns the promise, which is resolved when all
* destruction is completed. * destruction is completed.
*/ */
close: function () { close: function() {
var errorWrapper = document.getElementById('errorWrapper'); var errorWrapper = document.getElementById('errorWrapper');
errorWrapper.setAttribute('hidden', 'true'); errorWrapper.setAttribute('hidden', 'true');
@ -154,7 +154,7 @@ var PDFViewerApplication = {
this.setTitle(title); this.setTitle(title);
}, },
setTitleUsingMetadata: function (pdfDocument) { setTitleUsingMetadata: function(pdfDocument) {
var self = this; var self = this;
pdfDocument.getMetadata().then(function(data) { pdfDocument.getMetadata().then(function(data) {
var info = data.info, metadata = data.metadata; var info = data.info, metadata = data.metadata;
@ -196,26 +196,26 @@ var PDFViewerApplication = {
var l10n = this.l10n; var l10n = this.l10n;
var moreInfoText = [l10n.get('error_version_info', var moreInfoText = [l10n.get('error_version_info',
{ version: pdfjsLib.version || '?', { version: pdfjsLib.version || '?',
build: pdfjsLib.build || '?' }, build: pdfjsLib.build || '?', },
'PDF.js v{{version}} (build: {{build}})')]; 'PDF.js v{{version}} (build: {{build}})')];
if (moreInfo) { if (moreInfo) {
moreInfoText.push( moreInfoText.push(
l10n.get('error_message', {message: moreInfo.message}, l10n.get('error_message', { message: moreInfo.message, },
'Message: {{message}}')); 'Message: {{message}}'));
if (moreInfo.stack) { if (moreInfo.stack) {
moreInfoText.push( moreInfoText.push(
l10n.get('error_stack', {stack: moreInfo.stack}, l10n.get('error_stack', { stack: moreInfo.stack, },
'Stack: {{stack}}')); 'Stack: {{stack}}'));
} else { } else {
if (moreInfo.filename) { if (moreInfo.filename) {
moreInfoText.push( moreInfoText.push(
l10n.get('error_file', {file: moreInfo.filename}, l10n.get('error_file', { file: moreInfo.filename, },
'File: {{file}}')); 'File: {{file}}'));
} }
if (moreInfo.lineNumber) { if (moreInfo.lineNumber) {
moreInfoText.push( moreInfoText.push(
l10n.get('error_line', {line: moreInfo.lineNumber}, l10n.get('error_line', { line: moreInfo.lineNumber, },
'Line: {{line}}')); 'Line: {{line}}'));
} }
} }
@ -311,7 +311,7 @@ var PDFViewerApplication = {
linkService.setViewer(pdfViewer); linkService.setViewer(pdfViewer);
this.pdfHistory = new pdfjsViewer.PDFHistory({ this.pdfHistory = new pdfjsViewer.PDFHistory({
linkService: linkService linkService: linkService,
}); });
linkService.setHistory(this.pdfHistory); linkService.setHistory(this.pdfHistory);
@ -339,8 +339,9 @@ var PDFViewerApplication = {
function() { function() {
PDFViewerApplication.page = (this.value | 0); PDFViewerApplication.page = (this.value | 0);
// Ensure that the page number input displays the correct value, even if the // Ensure that the page number input displays the correct value,
// value entered by the user was invalid (e.g. a floating point number). // even if the value entered by the user was invalid
// (e.g. a floating point number).
if (this.value !== PDFViewerApplication.page.toString()) { if (this.value !== PDFViewerApplication.page.toString()) {
this.value = PDFViewerApplication.page; this.value = PDFViewerApplication.page;
} }
@ -359,7 +360,7 @@ var PDFViewerApplication = {
document.getElementById('previous').disabled = (page <= 1); document.getElementById('previous').disabled = (page <= 1);
document.getElementById('next').disabled = (page >= numPages); document.getElementById('next').disabled = (page >= numPages);
}, true); }, true);
} },
}; };
document.addEventListener('DOMContentLoaded', function () { document.addEventListener('DOMContentLoaded', function () {
@ -378,6 +379,6 @@ document.addEventListener('DOMContentLoaded', function () {
// We need to delay opening until all HTML is loaded. // We need to delay opening until all HTML is loaded.
PDFViewerApplication.animationStartedPromise.then(function () { PDFViewerApplication.animationStartedPromise.then(function () {
PDFViewerApplication.open({ PDFViewerApplication.open({
url: DEFAULT_URL url: DEFAULT_URL,
}); });
}); });

9
examples/node/.eslintrc Normal file
View File

@ -0,0 +1,9 @@
{
"extends": [
"../.eslintrc"
],
"env": {
"node": true,
},
}

View File

@ -1,7 +1,7 @@
/* Any copyright is dedicated to the Public Domain. /* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */ * http://creativecommons.org/publicdomain/zero/1.0/ */
function xmlEncode(s){ function xmlEncode(s) {
var i = 0, ch; var i = 0, ch;
s = String(s); s = String(s);
while (i < s.length && (ch = s[i]) !== '&' && ch !== '<' && while (i < s.length && (ch = s[i]) !== '&' && ch !== '<' &&
@ -50,7 +50,7 @@ function DOMElement(name) {
if (name === 'style') { if (name === 'style') {
this.sheet = { this.sheet = {
cssRules: [], cssRules: [],
insertRule: function (rule) { insertRule: function(rule) {
this.cssRules.push(rule); this.cssRules.push(rule);
}, },
}; };
@ -96,7 +96,7 @@ DOMElement.prototype = {
appendChild: function DOMElement_appendChild(element) { appendChild: function DOMElement_appendChild(element) {
var childNodes = this.childNodes; var childNodes = this.childNodes;
if (childNodes.indexOf(element) === -1) { if (!childNodes.includes(element)) {
childNodes.push(element); childNodes.push(element);
} }
}, },
@ -124,8 +124,8 @@ DOMElement.prototype = {
getSerializer: function DOMElement_getSerializer() { getSerializer: function DOMElement_getSerializer() {
return new DOMElementSerializer(this); return new DOMElementSerializer(this);
} },
} };
function DOMElementSerializer(node) { function DOMElementSerializer(node) {
this._node = node; this._node = node;
@ -152,10 +152,12 @@ DOMElementSerializer.prototype = {
return ' xmlns:xlink="http://www.w3.org/1999/xlink"' + return ' xmlns:xlink="http://www.w3.org/1999/xlink"' +
' xmlns:svg="http://www.w3.org/2000/svg"'; ' xmlns:svg="http://www.w3.org/2000/svg"';
} }
/* falls through */
case 2: // Initialize variables for looping over attributes. case 2: // Initialize variables for looping over attributes.
++this._state; ++this._state;
this._loopIndex = 0; this._loopIndex = 0;
this._attributeKeys = Object.keys(node.attributes); this._attributeKeys = Object.keys(node.attributes);
/* falls through */
case 3: // Serialize any attributes and end opening tag. case 3: // Serialize any attributes and end opening tag.
if (this._loopIndex < this._attributeKeys.length) { if (this._loopIndex < this._attributeKeys.length) {
var name = this._attributeKeys[this._loopIndex++]; var name = this._attributeKeys[this._loopIndex++];
@ -170,6 +172,7 @@ DOMElementSerializer.prototype = {
} }
++this._state; ++this._state;
this._loopIndex = 0; this._loopIndex = 0;
/* falls through */
case 5: // Serialize child nodes (only for non-tspan/style elements). case 5: // Serialize child nodes (only for non-tspan/style elements).
var value; var value;
while (true) { while (true) {
@ -186,6 +189,7 @@ DOMElementSerializer.prototype = {
break; break;
} }
} }
/* falls through */
case 6: // Ending tag. case 6: // Ending tag.
++this._state; ++this._state;
return '</' + node.nodeName + '>'; return '</' + node.nodeName + '>';
@ -198,48 +202,48 @@ DOMElementSerializer.prototype = {
}; };
const document = { const document = {
childNodes : [], childNodes: [],
get currentScript() { get currentScript() {
return { src: '' }; return { src: '', };
}, },
get documentElement() { get documentElement() {
return this; return this;
}, },
createElementNS: function (NS, element) { createElementNS: function(NS, element) {
var elObject = new DOMElement(element); var elObject = new DOMElement(element);
return elObject; return elObject;
}, },
createElement: function (element) { createElement: function(element) {
return this.createElementNS('', element); return this.createElementNS('', element);
}, },
getElementsByTagName: function (element) { getElementsByTagName: function(element) {
if (element === 'head') { if (element === 'head') {
return [this.head || (this.head = new DOMElement('head'))]; return [this.head || (this.head = new DOMElement('head'))];
} }
return []; return [];
} },
}; };
function Image () { function Image() {
this._src = null; this._src = null;
this.onload = null; this.onload = null;
} }
Image.prototype = { Image.prototype = {
get src () { get src() {
return this._src; return this._src;
}, },
set src (value) { set src(value) {
this._src = value; this._src = value;
if (this.onload) { if (this.onload) {
this.onload(); this.onload();
} }
} },
} };
exports.document = document; exports.document = document;
exports.Image = Image; exports.Image = Image;

View File

@ -52,7 +52,7 @@ loadingTask.promise.then(function(doc) {
}).then(function () { }).then(function () {
console.log(); console.log();
}); });
}) });
}; };
// Loading of the first page will wait on metadata and subsequent loadings // Loading of the first page will wait on metadata and subsequent loadings
// will wait on the previous pages. // will wait on the previous pages.

View File

@ -66,11 +66,12 @@ loadingTask.promise.then(function(pdfDocument) {
// Render the page on a Node canvas with 100% scale. // Render the page on a Node canvas with 100% scale.
var viewport = page.getViewport(1.0); var viewport = page.getViewport(1.0);
var canvasFactory = new NodeCanvasFactory(); var canvasFactory = new NodeCanvasFactory();
var canvasAndContext = canvasFactory.create(viewport.width, viewport.height); var canvasAndContext =
canvasFactory.create(viewport.width, viewport.height);
var renderContext = { var renderContext = {
canvasContext: canvasAndContext.context, canvasContext: canvasAndContext.context,
viewport: viewport, viewport: viewport,
canvasFactory: canvasFactory canvasFactory: canvasFactory,
}; };
var renderTask = page.render(renderContext); var renderTask = page.render(renderContext);
@ -81,7 +82,8 @@ loadingTask.promise.then(function(pdfDocument) {
if (error) { if (error) {
console.error('Error: ' + error); console.error('Error: ' + error);
} else { } else {
console.log('Finished converting first page of PDF file to a PNG image.'); console.log(
'Finished converting first page of PDF file to a PNG image.');
} }
}); });
}); });

View File

@ -86,8 +86,9 @@ function writeSvgToFile(svgElement, filePath) {
// callback. // callback.
var loadingTask = pdfjsLib.getDocument({ var loadingTask = pdfjsLib.getDocument({
data: data, data: data,
// Try to export JPEG images directly if they don't need any further processing. // Try to export JPEG images directly if they don't need any further
nativeImageDecoderSupport: pdfjsLib.NativeImageDecoding.DISPLAY // processing.
nativeImageDecoderSupport: pdfjsLib.NativeImageDecoding.DISPLAY,
}); });
loadingTask.promise.then(function(doc) { loadingTask.promise.then(function(doc) {
var numPages = doc.numPages; var numPages = doc.numPages;
@ -107,7 +108,8 @@ loadingTask.promise.then(function(doc) {
var svgGfx = new pdfjsLib.SVGGraphics(page.commonObjs, page.objs); var svgGfx = new pdfjsLib.SVGGraphics(page.commonObjs, page.objs);
svgGfx.embedFonts = true; svgGfx.embedFonts = true;
return svgGfx.getSVG(opList, viewport).then(function (svg) { return svgGfx.getSVG(opList, viewport).then(function (svg) {
return writeSvgToFile(svg, getFilePathForPage(pageNum)).then(function () { return writeSvgToFile(svg, getFilePathForPage(pageNum))
.then(function () {
console.log('Page: ' + pageNum); console.log('Page: ' + pageNum);
}, function(err) { }, function(err) {
console.log('Error: ' + err); console.log('Error: ' + err);

View File

@ -49,7 +49,7 @@ function buildSVG(viewport, textContent) {
function pageLoaded() { function pageLoaded() {
// Loading document and page text content // Loading document and page text content
var loadingTask = pdfjsLib.getDocument({url: PDF_PATH}); var loadingTask = pdfjsLib.getDocument({ url: PDF_PATH, });
loadingTask.promise.then(function(pdfDocument) { loadingTask.promise.then(function(pdfDocument) {
pdfDocument.getPage(PAGE_NUMBER).then(function (page) { pdfDocument.getPage(PAGE_NUMBER).then(function (page) {
var viewport = page.getViewport(PAGE_SCALE); var viewport = page.getViewport(PAGE_SCALE);

View File

@ -0,0 +1,9 @@
{
"extends": [
"../.eslintrc"
],
"env": {
"node": true,
},
}

View File

@ -24,7 +24,7 @@ loadingTask.promise.then(function (pdfDocument) {
var ctx = canvas.getContext('2d'); var ctx = canvas.getContext('2d');
var renderTask = pdfPage.render({ var renderTask = pdfPage.render({
canvasContext: ctx, canvasContext: ctx,
viewport: viewport viewport: viewport,
}); });
return renderTask.promise; return renderTask.promise;
}); });

View File

@ -1,16 +1,16 @@
var webpack = require('webpack'); var webpack = require('webpack'); // eslint-disable-line no-unused-vars
var path = require('path'); var path = require('path');
module.exports = { module.exports = {
context: __dirname, context: __dirname,
entry: { entry: {
'main': './main.js', 'main': './main.js',
'pdf.worker': 'pdfjs-dist/build/pdf.worker.entry' 'pdf.worker': 'pdfjs-dist/build/pdf.worker.entry',
}, },
mode: 'none', mode: 'none',
output: { output: {
path: path.join(__dirname, '../../build/webpack'), path: path.join(__dirname, '../../build/webpack'),
publicPath: '../../build/webpack/', publicPath: '../../build/webpack/',
filename: '[name].bundle.js' filename: '[name].bundle.js',
} },
}; };