Commit 59b9508b authored by Randall Leeds's avatar Randall Leeds

Merge pull request #1775 from hypothesis/update-pdfjs-support

Update PDF plugin
parents de6e7f4a ad1a4b16
// Generated by CoffeeScript 1.6.3 // Generated by CoffeeScript 1.6.3
/* /*
** Annotator 1.2.6-dev-95b7a09 ** Annotator 1.2.6-dev-18d8f72
** https://github.com/okfn/annotator/ ** https://github.com/okfn/annotator/
** **
** Copyright 2012 Aron Carroll, Rufus Pollock, and Nick Stenning. ** Copyright 2012 Aron Carroll, Rufus Pollock, and Nick Stenning.
** Dual licensed under the MIT and GPLv3 licenses. ** Dual licensed under the MIT and GPLv3 licenses.
** https://github.com/okfn/annotator/blob/master/LICENSE ** https://github.com/okfn/annotator/blob/master/LICENSE
** **
** Built at: 2014-11-26 22:32:31Z ** Built at: 2014-12-08 17:27:52Z
*/ */
...@@ -18,44 +18,75 @@ ...@@ -18,44 +18,75 @@
// Generated by CoffeeScript 1.6.3 // Generated by CoffeeScript 1.6.3
(function() { (function() {
var _ref, var PDFTextMapper, _ref,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__hasProp = {}.hasOwnProperty, __hasProp = {}.hasOwnProperty,
__extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
window.PDFTextMapper = (function(_super) { PDFTextMapper = (function(_super) {
__extends(PDFTextMapper, _super); __extends(PDFTextMapper, _super);
PDFTextMapper.isPDFDocument = function() {
return (typeof PDFView !== "undefined" && PDFView !== null) || (typeof PDFViewerApplication !== "undefined" && PDFViewerApplication !== null);
};
PDFTextMapper.applicable = function() { PDFTextMapper.applicable = function() {
return typeof PDFView !== "undefined" && PDFView !== null; return this.isPDFDocument();
}; };
PDFTextMapper.prototype.requiresSmartStringPadding = true; PDFTextMapper.prototype.requiresSmartStringPadding = true;
PDFTextMapper.prototype.getPageCount = function() { PDFTextMapper.prototype.getPageCount = function() {
return PDFView.pages.length; return this._viewer.pages.length;
}; };
PDFTextMapper.prototype.getPageIndex = function() { PDFTextMapper.prototype.getPageIndex = function() {
return PDFView.page - 1; return this._app.page - 1;
}; };
PDFTextMapper.prototype.setPageIndex = function(index) { PDFTextMapper.prototype.setPageIndex = function(index) {
return PDFView.page = index + 1; return this._app.page = index + 1;
}; };
PDFTextMapper.prototype._isPageRendered = function(index) { PDFTextMapper.prototype._isPageRendered = function(index) {
var _ref, _ref1; var _ref, _ref1;
return (_ref = PDFView.pages[index]) != null ? (_ref1 = _ref.textLayer) != null ? _ref1.renderingDone : void 0 : void 0; return (_ref = this._viewer.pages[index]) != null ? (_ref1 = _ref.textLayer) != null ? _ref1.renderingDone : void 0 : void 0;
}; };
PDFTextMapper.prototype.getRootNodeForPage = function(index) { PDFTextMapper.prototype.getRootNodeForPage = function(index) {
return PDFView.pages[index].textLayer.textLayerDiv; return this._viewer.pages[index].textLayer.textLayerDiv;
}; };
function PDFTextMapper() { function PDFTextMapper() {
this._finishScan = __bind(this._finishScan, this); this._finishScan = __bind(this._finishScan, this);
this._parseExtractedText = __bind(this._parseExtractedText, this); this._parseExtractedText = __bind(this._parseExtractedText, this);
var _ref,
_this = this;
if (typeof PDFViewerApplication !== "undefined" && PDFViewerApplication !== null) {
this._app = PDFViewerApplication;
this._viewer = this._app.pdfViewer;
this._tryExtractPage = function(index) {
return _this._viewer.getPageTextContent(index);
};
} else {
this._app = this._viewer = PDFView;
this._finder = (_ref = this._app.findController) != null ? _ref : PDFFindController;
this._tryExtractPage = function(index) {
return new Promise(function(resolve, reject) {
var tryIt;
tryIt = function() {
var page;
page = _this._finder.pdfPageSource.pages[index];
if ((page != null ? page.pdfPage : void 0) != null) {
return page.getTextContent().then(resolve);
} else {
return setTimeout(tryIt, 100);
}
};
return tryIt();
});
};
}
this.setEvents(); this.setEvents();
} }
...@@ -93,7 +124,7 @@ ...@@ -93,7 +124,7 @@
} }
} }
}); });
return $(PDFView.container).on('scroll', function() { return $(this._viewer.container).on('scroll', function() {
return _this._onScroll(); return _this._onScroll();
}); });
}; };
...@@ -104,12 +135,11 @@ ...@@ -104,12 +135,11 @@
return text.replace(this._extractionPattern, " "); return text.replace(this._extractionPattern, " ");
}; };
PDFTextMapper.prototype._waitForInit = function() { PDFTextMapper.prototype.waitForInit = function() {
var tryIt, var tryIt,
_this = this; _this = this;
tryIt = function(resolve) { tryIt = function(resolve) {
var _this = this; if (_this._app.documentFingerprint && _this._app.documentInfo) {
if (PDFView.documentFingerprint && PDFView.documentInfo) {
return resolve(); return resolve();
} else { } else {
return setTimeout((function() { return setTimeout((function() {
...@@ -118,7 +148,7 @@ ...@@ -118,7 +148,7 @@
} }
}; };
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
if (typeof PDFView !== "undefined" && PDFView !== null) { if (PDFTextMapper.applicable()) {
return tryIt(resolve); return tryIt(resolve);
} else { } else {
return reject("Not a PDF.js document"); return reject("Not a PDF.js document");
...@@ -130,9 +160,8 @@ ...@@ -130,9 +160,8 @@
var _this = this; var _this = this;
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
_this._pendingScanResolve = resolve; _this._pendingScanResolve = resolve;
return _this._waitForInit().then(function() { return _this.waitForInit().then(function() {
return PDFView.getPage(1).then(function() { return _this._app.pdfDocument.getPage(1).then(function() {
console.log("Scanning document for text...");
_this.pageInfo = []; _this.pageInfo = [];
return _this._extractPageText(0); return _this._extractPageText(0);
}); });
...@@ -141,19 +170,10 @@ ...@@ -141,19 +170,10 @@
}; };
PDFTextMapper.prototype._extractPageText = function(pageIndex) { PDFTextMapper.prototype._extractPageText = function(pageIndex) {
var page, pageSource, _ref, var _this = this;
_this = this; return this._tryExtractPage(pageIndex).then(function(data) {
pageSource = (_ref = PDFFindController.pdfPageSource) != null ? _ref : PDFView.findController.pdfPageSource; var content, rawContent, text, textData, _ref, _ref1;
page = pageSource.pages[pageIndex]; textData = (_ref = (_ref1 = data.bidiTexts) != null ? _ref1 : data.items) != null ? _ref : data;
if ((page != null ? page.pdfPage : void 0) == null) {
setTimeout((function() {
return _this._extractPageText(pageIndex);
}), 100);
return;
}
return page.getTextContent().then(function(data) {
var content, rawContent, text, textData, _ref1, _ref2;
textData = (_ref1 = (_ref2 = data.bidiTexts) != null ? _ref2 : data.items) != null ? _ref1 : data;
rawContent = ((function() { rawContent = ((function() {
var _i, _len, _results; var _i, _len, _results;
_results = []; _results = [];
...@@ -168,7 +188,7 @@ ...@@ -168,7 +188,7 @@
index: pageIndex, index: pageIndex,
content: content content: content
}; };
if (pageIndex === PDFView.pages.length - 1) { if (pageIndex === _this.getPageCount() - 1) {
return _this._finishScan(); return _this._finishScan();
} else { } else {
return _this._extractPageText(pageIndex + 1); return _this._extractPageText(pageIndex + 1);
...@@ -192,9 +212,17 @@ ...@@ -192,9 +212,17 @@
return this.pageInfo[index]; return this.pageInfo[index];
}; };
PDFTextMapper.prototype.getDocumentFingerprint = function() {
return this._app.documentFingerprint;
};
PDFTextMapper.prototype.getDocumentInfo = function() {
return this.app.documentInfo;
};
return PDFTextMapper; return PDFTextMapper;
})(window.PageTextMapperCore); })(PageTextMapperCore);
Annotator.Plugin.PDF = (function(_super) { Annotator.Plugin.PDF = (function(_super) {
var $; var $;
...@@ -222,7 +250,7 @@ ...@@ -222,7 +250,7 @@
}; };
PDF.prototype._isPDF = function() { PDF.prototype._isPDF = function() {
return window.PDFJS; return PDFTextMapper.applicable();
}; };
PDF.prototype._getDocumentURI = function() { PDF.prototype._getDocumentURI = function() {
...@@ -239,7 +267,7 @@ ...@@ -239,7 +267,7 @@
PDF.prototype._getFingerPrintURI = function() { PDF.prototype._getFingerPrintURI = function() {
var fingerprint; var fingerprint;
fingerprint = PDFView.documentFingerprint; fingerprint = this.annotator.domMapper.getDocumentFingerprint();
return "urn:x-pdf:" + fingerprint; return "urn:x-pdf:" + fingerprint;
}; };
...@@ -252,7 +280,7 @@ ...@@ -252,7 +280,7 @@
PDF.prototype._getTitle = function() { PDF.prototype._getTitle = function() {
var title, _ref1; var title, _ref1;
title = (_ref1 = PDFView.documentInfo.Title) != null ? _ref1.trim() : void 0; title = (_ref1 = this.annotator.domMapper.getDocumentInfo().Title) != null ? _ref1.trim() : void 0;
if ((title != null) && title !== "") { if ((title != null) && title !== "") {
return title; return title;
} else { } else {
...@@ -284,10 +312,8 @@ ...@@ -284,10 +312,8 @@
PDF.prototype.getMetaData = function() { PDF.prototype.getMetaData = function() {
var _this = this; var _this = this;
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
var init; if (_this.annotator.domMapper.waitForInit != null) {
init = _this.annotator.domMapper._waitForInit; return _this.annotator.domMapper.waitForInit().then(function() {
if (init != null) {
return init().then(function() {
return resolve(_this._metadata()); return resolve(_this._metadata());
}); });
} else { } else {
...@@ -312,4 +338,3 @@ ...@@ -312,4 +338,3 @@
})(Annotator.Plugin); })(Annotator.Plugin);
}).call(this); }).call(this);
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment