Commit 91dc8d28 authored by Randall Leeds's avatar Randall Leeds

Merge pull request #1563 from hypothesis/1334-pdfjs-6

[WIP] add support for packaging with PDF.js extension - try 4
parents 06aa76f0 03398c61
...@@ -179,10 +179,17 @@ AnnotationController = [ ...@@ -179,10 +179,17 @@ AnnotationController = [
# Extract the document metadata. # Extract the document metadata.
if model.document if model.document
domain = extractURIComponent(model.uri, 'hostname') uri = model.uri
if uri.indexOf("urn") is 0
# This URI is not clickable, see if we have something better
for link in model.document.link when link.href.indexOf("urn")
uri = link.href
break
domain = extractURIComponent(uri, 'hostname')
@document = @document =
uri: model.uri uri: uri
domain: domain domain: domain
title: model.document.title or domain title: model.document.title or domain
......
...@@ -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,4 @@ ...@@ -278,3 +303,4 @@
})(Annotator.Plugin); })(Annotator.Plugin);
}).call(this); }).call(this);
...@@ -231,5 +231,3 @@ ...@@ -231,5 +231,3 @@
}).call(this); }).call(this);
//
//# sourceMappingURL=annotator.permissions.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