1
0
Fork 0
mirror of https://github.com/futurepress/epub.js.git synced 2025-10-05 15:32:55 +02:00
epub.js/hooks/extensions/hypothesis.js
Jake Hartnell 1d3f4ea1e7 Better hypothesis integration
Fixes many problems with the hypothes.is integration, including:
- Improved styling
- Sidebar opens when annotation adder is clicked
- Fix two annotations bug
- Hypothesis buttons and controls work as expected.
- Hypothesis loads correctly when changing chapters
2015-10-31 16:40:14 -07:00

74 lines
2.4 KiB
JavaScript

EPUBJS.Hooks.register("beforeChapterDisplay").hypothesis = function(callback, renderer, chapter) {
renderer.render.window.hypothesisConfig = function () {
this.Annotator.Guest.prototype.createAnnotation = function(annotation) {
var getSelectors, info, metadata, ranges, ref, root, selectors, self, setDocumentInfo, setTargets, targets;
if (annotation == null) {
annotation = {};
}
self = this;
root = this.element[0];
ranges = (ref = this.selectedRanges) != null ? ref : [];
this.selectedRanges = null;
getSelectors = function(range) {
var options;
options = {
cache: self.anchoringCache,
ignoreSelector: '[class^="annotator-"]'
};
return self.anchoring.describe(root, range, options);
};
setDocumentInfo = function(info) {
annotation.document = info.metadata;
return annotation.uri = info.uri;
};
setTargets = function(arg) {
var info, selector, selectors, source;
info = arg[0], selectors = arg[1];
source = info.uri;
return annotation.target = (function() {
var i, len, results;
results = [];
for (i = 0, len = selectors.length; i < len; i++) {
selector = selectors[i];
results.push({
source: source,
selector: selector
});
}
return results;
})();
};
info = this.getDocumentInfo();
selectors = Promise.all(ranges.map(getSelectors));
metadata = info.then(setDocumentInfo);
targets = Promise.all([info, selectors]).then(setTargets);
targets.then(function() {
return self.publish('beforeAnnotationCreated', [annotation]);
});
targets.then(function() {
return self.anchor(annotation);
});
window.parent.annotator.show();
return annotation;
}
return {
constructor: this.Annotator.Guest,
app: 'https://hypothes.is/app.html'
};
};
var firstRun = typeof(renderer.render.window.annotator) === 'undefined';
EPUBJS.core.addScript("//hypothes.is/embed.js", function() {
// If we change chapter, reinstall annotator.
if (!firstRun) {
renderer.render.window.hypothesisInstall();
}
// Must add after base css
EPUBJS.core.addCss("/reader/css/annotations.css", function() {
callback();
}, renderer.doc.head);
}, renderer.doc.head);
};