From 9c765bf863122a1a73f64e64341650704ef728a4 Mon Sep 17 00:00:00 2001 From: j Date: Fri, 1 Feb 2019 14:27:28 +0530 Subject: [PATCH] don't deselect on click, only scroll if not in view --- static/reader/epub.js | 4 +--- static/reader/pdf.js | 14 +++++++++----- 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/static/reader/epub.js b/static/reader/epub.js index b4326a3..9aca10f 100644 --- a/static/reader/epub.js +++ b/static/reader/epub.js @@ -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') }) diff --git a/static/reader/pdf.js b/static/reader/pdf.js index 44db149..1e4b003 100644 --- a/static/reader/pdf.js +++ b/static/reader/pdf.js @@ -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; +}