Created
August 18, 2020 19:26
-
-
Save jhakonen/27a06112087a99512ceb3b952f52cc21 to your computer and use it in GitHub Desktop.
Added touch event handling for viewer element + logging
This file contains hidden or 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
| diff --git a/src/assets/epub-viewer.js b/src/assets/epub-viewer.js | |
| index 90ec9fb..dd1a944 100644 | |
| --- a/src/assets/epub-viewer.js | |
| +++ b/src/assets/epub-viewer.js | |
| @@ -565,20 +565,45 @@ const setupRendition = () => { | |
| type: 'selection', | |
| payload: { | |
| position: getRect(range, frame), | |
| text: selection.toString(), | |
| cfi: new ePub.CFI(range, contents.cfiBase).toString(), | |
| language: book.package.metadata.language | |
| } | |
| }) | |
| } | |
| + //Enable swipe gesture to flip a page | |
| + let start; | |
| + let end; | |
| + | |
| + contents.document.addEventListener('touchstart', (event) => { | |
| + console.log('CONTENTS: touchstart') | |
| + start = event.changedTouches[0]; | |
| + rendition.touchStarted = true; | |
| + event.preventDefault(); | |
| + }); | |
| + contents.document.addEventListener('touchend', (event) => { | |
| + console.log('CONTENTS: touchend') | |
| + rendition.touchStarted = false; | |
| + if (!paginated) { return }; | |
| + event.preventDefault(); | |
| + end = event.changedTouches[0]; | |
| + const elBook = document.querySelector('#viewer'); //Parent div, which contains the #area div | |
| + if (elBook) { | |
| + const bound = elBook.getBoundingClientRect(); | |
| + const hr = (end.screenX - start.screenX) / bound.width; | |
| + if (hr > 0.25) return rendition.prev(); | |
| + if (hr < -0.25) return rendition.next(); | |
| + } | |
| + }); | |
| + | |
| // auto-hide cursor | |
| let timeout | |
| const hideCursor = () => { | |
| contents.document.documentElement.style.cursor = 'none' | |
| cursorHidden = true | |
| } | |
| const showCursor = () => { | |
| contents.document.documentElement.style.cursor = 'auto' | |
| cursorHidden = false | |
| } | |
| @@ -618,47 +643,49 @@ const setupRendition = () => { | |
| rendition.on('keydown', handleKeydown) | |
| document.addEventListener('keydown', handleKeydown, false) | |
| //Enable swipe gesture to flip a page | |
| let start; | |
| let end; | |
| rendition.touchStarted = false; | |
| document.addEventListener('touchstart', (event) => { | |
| + console.log('DOCUMENT: touchstart') | |
| start = event.changedTouches[0]; | |
| rendition.touchStarted = true; | |
| event.preventDefault(); | |
| }); | |
| document.addEventListener('touchend', (event) => { | |
| + console.log('DOCUMENT: touchend') | |
| rendition.touchStarted = false; | |
| if (!paginated) { return }; | |
| event.preventDefault(); | |
| end = event.changedTouches[0]; | |
| const elBook = document.querySelector('#viewer'); //Parent div, which contains the #area div | |
| if (elBook) { | |
| const bound = elBook.getBoundingClientRect(); | |
| const hr = (end.screenX - start.screenX) / bound.width; | |
| - const vr = Math.abs((end.screenY - start.screenY) / bound.height); | |
| - if (hr > 0.25 && vr < 0.1) return rendition.prev(); | |
| - if (hr < -0.25 && vr < 0.1) return rendition.next(); | |
| + if (hr > 0.25) return rendition.prev(); | |
| + if (hr < -0.25) return rendition.next(); | |
| } | |
| }); | |
| if (paginated) { | |
| // scroll through pages | |
| const onwheel = debounce(event => { | |
| if (rendition.touchStarted) { | |
| event.preventDefault(); | |
| return; | |
| } | |
| + console.log('DOCUMENT: scroll'); | |
| if (getWindowIsZoomed()) return | |
| const { deltaX, deltaY } = event | |
| if (Math.abs(deltaX) > Math.abs(deltaY)) { | |
| if (deltaX > 0) rendition.next() | |
| else if (deltaX < 0) rendition.prev() | |
| } else { | |
| if (deltaY > 0) rendition.next() | |
| else if (deltaY < 0) rendition.prev() | |
| } | |
| event.preventDefault() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment