Commit 2cc65e09 authored by csillag's avatar csillag

Fix URI and metadata handling for PDF documents

 * Don't load the Document plugin for PDF files; Annotator's PDF plugin
   provides the fuctionality (uri+metadata extraction) in these cases.
   Use async metadata extraction for PDF

 * Updated PDF plugin to fcdf1eac (pdf-update branch)
parent 06aa76f0
...@@ -31,6 +31,9 @@ class Annotator.Guest extends Annotator ...@@ -31,6 +31,9 @@ class Annotator.Guest extends Annotator
super super
delete @options.noScan delete @options.noScan
if window.PDFJS
delete @options.Document
@frame = $('<div></div>') @frame = $('<div></div>')
.appendTo(@wrapper) .appendTo(@wrapper)
.addClass('annotator-frame annotator-outer annotator-collapsed') .addClass('annotator-frame annotator-outer annotator-collapsed')
...@@ -40,8 +43,7 @@ class Annotator.Guest extends Annotator ...@@ -40,8 +43,7 @@ class Annotator.Guest extends Annotator
this.addPlugin 'Bridge', this.addPlugin 'Bridge',
formatter: (annotation) => formatter: (annotation) =>
formatted = {} formatted = {}
if annotation.document? formatted['uri'] = @getHref()
formatted['uri'] = @plugins.Document.uri()
for k, v of annotation when k isnt 'anchors' for k, v of annotation when k isnt 'anchors'
formatted[k] = v formatted[k] = v
# Work around issue in jschannel where a repeated object is considered # Work around issue in jschannel where a repeated object is considered
...@@ -113,6 +115,10 @@ class Annotator.Guest extends Annotator ...@@ -113,6 +115,10 @@ class Annotator.Guest extends Annotator
# Announce the new positions, so that the sidebar knows # Announce the new positions, so that the sidebar knows
this.plugins.Bridge.sync([highlight.annotation]) this.plugins.Bridge.sync([highlight.annotation])
# Utility function to get the decoded form of the document URI
getHref: =>
@plugins.PDF?.uri() ? @plugins.Document.uri() ? super
_setupXDM: (options) -> _setupXDM: (options) ->
# jschannel chokes FF and Chrome extension origins. # jschannel chokes FF and Chrome extension origins.
if (options.origin.match /^chrome-extension:\/\//) or if (options.origin.match /^chrome-extension:\/\//) or
...@@ -160,11 +166,21 @@ class Annotator.Guest extends Annotator ...@@ -160,11 +166,21 @@ class Annotator.Guest extends Annotator
return return
) )
.bind('getDocumentInfo', => .bind('getDocumentInfo', (trans) =>
return { (@plugins.PDF?.getMetaData() ? Promise.reject())
uri: @plugins.Document.uri() .then (md) =>
metadata: @plugins.Document.metadata # console.log "Returning PDF metadata", md
} trans.complete
uri: @getHref()
metadata: md
.catch (problem) =>
# console.log "Returning standard metadata, because"
# console.log problem.stack ? problem
trans.complete
uri: @getHref()
metadata: @plugins.Document?.metadata
trans.delayReturn(true)
) )
.bind('setTool', (ctx, name) => .bind('setTool', (ctx, name) =>
......
// Generated by CoffeeScript 1.6.3 // Generated by CoffeeScript 1.6.3
/* /*
** Annotator 1.2.6-dev-9e0eff3 ** Annotator 1.2.6-dev-fcdf1ea
** 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-09-25 22:41:02Z ** Built at: 2014-10-23 22:40:29Z
*/ */
...@@ -27,8 +27,7 @@ ...@@ -27,8 +27,7 @@
__extends(PDFTextMapper, _super); __extends(PDFTextMapper, _super);
PDFTextMapper.applicable = function() { PDFTextMapper.applicable = function() {
var _ref; return typeof PDFView !== "undefined" && PDFView !== null;
return (_ref = typeof PDFView !== "undefined" && PDFView !== null ? PDFView.initialized : void 0) != null ? _ref : false;
}; };
PDFTextMapper.prototype.requiresSmartStringPadding = true; PDFTextMapper.prototype.requiresSmartStringPadding = true;
...@@ -106,20 +105,24 @@ ...@@ -106,20 +105,24 @@
}; };
PDFTextMapper.prototype._waitForInit = function() { PDFTextMapper.prototype._waitForInit = function() {
var tryIt; var tryIt,
_this = this;
tryIt = function(resolve) { tryIt = function(resolve) {
var _this = this; var _this = this;
if (PDFView.documentFingerprint) { if (PDFView.documentFingerprint && PDFView.documentInfo) {
return resolve(); return resolve();
} else { } else {
return setTimeout((function() { return setTimeout((function() {
console.log("OK, let's try again");
return tryIt(resolve); return tryIt(resolve);
}), 100); }), 100);
} }
}; };
return new Promise(function(resolve, reject) { return new Promise(function(resolve, reject) {
return tryIt(resolve); if (typeof PDFView !== "undefined" && PDFView !== null) {
return tryIt(resolve);
} else {
return reject("Not a PDF.js document");
}
}); });
}; };
...@@ -199,6 +202,7 @@ ...@@ -199,6 +202,7 @@
function PDF() { function PDF() {
this.beforeAnnotationCreated = __bind(this.beforeAnnotationCreated, this); this.beforeAnnotationCreated = __bind(this.beforeAnnotationCreated, this);
this.getMetaData = __bind(this.getMetaData, this);
_ref = PDF.__super__.constructor.apply(this, arguments); _ref = PDF.__super__.constructor.apply(this, arguments);
return _ref; return _ref;
} }
...@@ -221,9 +225,15 @@ ...@@ -221,9 +225,15 @@
}; };
PDF.prototype._getDocumentURI = function() { PDF.prototype._getDocumentURI = function() {
var uri, _ref1, _ref2; var match, matches, uri;
uri = window.location.href; uri = window.location.href;
return (_ref1 = (_ref2 = uri.match('chrome-extension://[a-z]{32}/(.*)')) != null ? _ref2[1] : void 0) != null ? _ref1 : uri; matches = uri.match('chrome-extension://[a-z]{32}/(content/web/viewer.html\\?file=)?(.*)');
match = matches != null ? matches[matches.length - 1] : void 0;
if (match) {
return decodeURIComponent(match);
} else {
return uri;
}
}; };
PDF.prototype._getFingerPrintURI = function() { PDF.prototype._getFingerPrintURI = function() {
...@@ -249,10 +259,7 @@ ...@@ -249,10 +259,7 @@
} }
}; };
PDF.prototype.metadata = function() { PDF.prototype._metadata = function() {
if (!this._isPDF()) {
return null;
}
return { return {
link: [ link: [
{ {
...@@ -265,12 +272,30 @@ ...@@ -265,12 +272,30 @@
}; };
}; };
PDF.prototype.getMetaData = function() {
var _this = this;
return new Promise(function(resolve, reject) {
var init;
init = _this.annotator.domMapper._waitForInit;
if (init != null) {
return init().then(function() {
return resolve(_this._metadata());
});
} else {
return reject("Not a PDF dom mapper.");
}
});
};
PDF.prototype.events = { PDF.prototype.events = {
'beforeAnnotationCreated': 'beforeAnnotationCreated' 'beforeAnnotationCreated': 'beforeAnnotationCreated'
}; };
PDF.prototype.beforeAnnotationCreated = function(annotation) { PDF.prototype.beforeAnnotationCreated = function(annotation) {
return annotation.document = this.metadata(); if (!this._isPDF()) {
return;
}
return annotation.document = this._metadata();
}; };
return PDF; return PDF;
...@@ -278,3 +303,6 @@ ...@@ -278,3 +303,6 @@
})(Annotator.Plugin); })(Annotator.Plugin);
}).call(this); }).call(this);
//
//# sourceMappingURL=annotator.pdf.map
\ No newline at end of file
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