-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #275 from tilgovi/hypothesis-integration-update
Update Hypothesis integration
- Loading branch information
Showing
5 changed files
with
114 additions
and
301 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,11 @@ | ||
EPUBJS.Hooks.register("beforeChapterDisplay").hypothesis = function(callback, renderer){ | ||
var folder = EPUBJS.core.folder(location.pathname); | ||
var cssPath = (folder + EPUBJS.cssPath) || folder; | ||
|
||
if(!renderer) return; | ||
EPUBJS.Hooks.register("beforeChapterDisplay").hypothesis = function(callback, renderer) { | ||
renderer.render.window.hypothesisConfig = function () { | ||
return { | ||
constructor: this.Annotator.Guest | ||
}; | ||
}; | ||
|
||
EPUBJS.core.addScript("/hooks/extensions/embedh.js", null, renderer.doc.head); | ||
EPUBJS.core.addScript("//hypothes.is/embed.js", null, renderer.doc.head); | ||
|
||
EPUBJS.core.addScript("https://hypothes.is/embed.js", null, renderer.doc.head); | ||
|
||
EPUBJS.core.addCss( cssPath + "annotations.css", null, renderer.doc.head); | ||
|
||
if(callback) callback(); | ||
}; | ||
if(callback) callback(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,109 +1,46 @@ | ||
EPUBJS.reader.plugins.HypothesisController = function(Book) { | ||
EPUBJS.reader.plugins.HypothesisController = function (Book) { | ||
var reader = this; | ||
var book = reader.book; | ||
var element = document.getElementById("hypothesis"); | ||
var body = window.document.body; | ||
var annotator; | ||
var $main = $("#main"); | ||
|
||
var updateAnnotations = function() { | ||
var annotatations = [], | ||
guestAnnotator = reader.book.renderer.render.window.annotator, | ||
_$, | ||
$annotations, width; | ||
|
||
if(!guestAnnotator) { | ||
if(annotator) annotator.updateViewer([]); | ||
return; | ||
}; | ||
|
||
_$ = guestAnnotator.constructor.$; | ||
|
||
$annotations = _$(".annotator-hl"); | ||
width = reader.book.renderer.render.iframe.clientWidth; | ||
|
||
//-- Find visible annotations | ||
$annotations.each(function(){ | ||
var $this = _$(this), | ||
|
||
var updateAnnotations = function () { | ||
var annotator = Book.renderer.render.window.annotator; | ||
if (annotator && annotator.constructor.$) { | ||
var annotations = getVisibleAnnotations(annotator.constructor.$); | ||
annotator.showAnnotations(annotations) | ||
} | ||
}; | ||
|
||
var getVisibleAnnotations = function ($) { | ||
var width = Book.renderer.render.iframe.clientWidth; | ||
return $('.annotator-hl').map(function() { | ||
var $this = $(this), | ||
left = this.getBoundingClientRect().left; | ||
if(left >= 0 && left <= width) { | ||
annotatations.push($this.data('annotation')); | ||
|
||
if (left >= 0 && left <= width) { | ||
return $this.data('annotation'); | ||
} | ||
}); | ||
|
||
//-- Update viewer | ||
annotator.updateViewer(annotatations); | ||
}).get(); | ||
}; | ||
|
||
var attach = function(){ | ||
annotator = window.annotator; | ||
annotator.frame.appendTo(element); | ||
|
||
annotator.subscribe('annotationEditorShown', function () { | ||
showAnnotations(true); | ||
}); | ||
annotator.subscribe('annotationViewerShown', function () { | ||
showAnnotations(true); | ||
}); | ||
|
||
annotator.subscribe("annotationsLoaded", function(e){ | ||
var _$ = reader.book.renderer.render.window.annotator.constructor.$; | ||
|
||
|
||
reader.annotator = annotator; | ||
updateAnnotations(); | ||
|
||
_$(reader.book.renderer.contents).on("click", ".annotator-hl", function(event){ | ||
var $this = _$(this); | ||
|
||
reader.annotator.updateViewer([$this.data('annotation')]); | ||
|
||
// $scope.$apply(function(){ | ||
// $scope.single = true; | ||
// $scope.noUpdate = true; | ||
// }); | ||
|
||
}); | ||
}); | ||
|
||
$(".h-icon-comment").on("click", function () { | ||
if ($main.hasClass("single")) { | ||
showAnnotations(false); | ||
} else { | ||
showAnnotations(true); | ||
} | ||
}); | ||
|
||
reader.book.on("renderer:locationChanged", function(){ | ||
updateAnnotations(); | ||
}); | ||
|
||
} | ||
|
||
var showAnnotations = function(single) { | ||
var currentPosition = reader.currentLocationCfi; | ||
reader.settings.sidebarReflow = false; | ||
$("#annotations").on("click", function () { | ||
var annotator = Book.renderer.render.window.annotator; | ||
var currentPosition = Book.getCurrentLocationCfi(); | ||
|
||
if(single) { | ||
$main.addClass("single"); | ||
window.annotator.setVisibleHighlights(true); | ||
} else { | ||
if ($main.hasClass("single")) { | ||
$main.removeClass("single"); | ||
window.annotator.setVisibleHighlights(false); | ||
annotator.setVisibleHighlights(false); | ||
} else { | ||
$main.addClass("single"); | ||
annotator.setVisibleHighlights(true); | ||
} | ||
|
||
$main.one("transitionend", function(){ | ||
book.gotoCfi(currentPosition); | ||
Book.gotoCfi(currentPosition); | ||
}); | ||
|
||
}; | ||
|
||
book.ready.all.then(function() { | ||
reader.HypothesisController.attach(); | ||
}); | ||
|
||
return { | ||
'attach': attach | ||
}; | ||
}; | ||
Book.on("renderer:locationChanged", updateAnnotations); | ||
Book.on("renderer:chapterDisplayed", updateAnnotations); | ||
|
||
return {} | ||
}; |
Oops, something went wrong.