don't deselect on click, only scroll if not in view

This commit is contained in:
j 2019-02-01 14:27:28 +05:30
parent 2c5c56c036
commit 9c765bf863
2 changed files with 10 additions and 8 deletions

View File

@ -113,9 +113,7 @@ function getText(book, cfiRange, cb) {
function onHighlightClicked(e) {
console.log("highlight clicked", e.target.dataset.epubcfi);
if(e.target.classList.contains('selected')) {
e.target.classList.remove('selected')
} else {
if(!e.target.classList.contains('selected')) {
document.querySelectorAll('.epubjs-hl.selected').forEach(function(other) {
other.classList.remove('selected')
})

View File

@ -22,7 +22,7 @@ Ox.load({
}
setTimeout(function() {
var el = document.querySelector('.a' + annotation.id);
if (el) {
if (el && !isInView(el)) {
document.querySelector('#viewerContainer').scrollTop = el.offsetTop + el.parentElement.offsetTop - 64;
}
}, delay)
@ -134,10 +134,7 @@ function renderAnnotation(annotation) {
'width:' + Math.abs(bounds[0] - bounds[2]) + 'px; height:' + Math.abs(bounds[1] - bounds[3]) + 'px;');
el.addEventListener('click', function() {
if (el.classList.contains('selected')) {
deselectAnnotation(annotation.id)
Ox.$parent.postMessage('selectAnnotation', {id: null})
} else {
if (!el.classList.contains('selected')) {
selectAnnotation(annotation.id)
Ox.$parent.postMessage('selectAnnotation', {id: annotation.id})
}
@ -204,3 +201,10 @@ function loadAnnotations(page) {
})
}
function isInView(element) {
var docViewTop = $(window).scrollTop();
var docViewBottom = docViewTop + $(window).height();
var elementTop = $(element).offset().top;
var elementBottom = elementTop + $(element).height();
return elementTop < docViewBottom && elementBottom > docViewTop;
}