diff options
| author | Andrew Dolgov <fox@fakecake.org> | 2026-05-24 12:26:08 +0300 |
|---|---|---|
| committer | Andrew Dolgov <fox@fakecake.org> | 2026-05-24 12:26:08 +0300 |
| commit | eb8de915973bcedec0c127f5c44211759a073b1d (patch) | |
| tree | 3e43cade5aaa9447a63c51e8913368c6a7e6137f /src | |
| parent | ec57c6d63526e422275bc1e92c0fee823c932c95 (diff) | |
| download | the-epube-eb8de915973bcedec0c127f5c44211759a073b1d.tar.gz the-epube-eb8de915973bcedec0c127f5c44211759a073b1d.tar.xz | |
shorten and inline initial implementation, make it use side columns for the time being to prevent text selection issues
Diffstat (limited to 'src')
| -rw-r--r-- | src/reader.d.ts | 14 | ||||
| -rw-r--r-- | src/reader.js | 100 | ||||
| -rw-r--r-- | src/swipes.js | 410 |
3 files changed, 94 insertions, 430 deletions
diff --git a/src/reader.d.ts b/src/reader.d.ts index 322db63..bb4fc68 100644 --- a/src/reader.d.ts +++ b/src/reader.d.ts @@ -8,3 +8,17 @@ declare var hyphenationPatternsRu: any; declare interface HTMLElement { checked: boolean } declare interface Event { deltaY: int, deltaX: int } declare interface Node { data: string } + +export type SwipeState = { + active: boolean, + startX: number, + startY: number, + startTime: number, + direction: 'left'|'right'|null +} + +export type SwipeConfig = { + minDistance: number, + velocityThreshold: number, + verticalDistanceThreshold: number +} diff --git a/src/reader.js b/src/reader.js index 480abf1..fe253ec 100644 --- a/src/reader.js +++ b/src/reader.js @@ -33,9 +33,77 @@ class ReaderClass extends AppCommon { */ book; + /** @type {SwipeState} */ + _swipeState = { + active: false, + startX: 0, + startY: 0, + startTime: 0, + direction: null, + }; + + /** @type {SwipeConfig} */ + _swipeConfig = { + minDistance: 50, // px — minimum horizontal travel to count as swipe + velocityThreshold: 0.3, // px/ms — minimum swipe speed + verticalDistanceThreshold: 80, // px — vertical movement before cancelling + }; + + onTouchStart(evt) { + const touch = evt.touches[0] || evt.changedTouches[0]; + + this._swipeState = { + active: true, + startX: touch.clientX, + startY: touch.clientY, + startTime: Date.now(), + direction: evt.target.id == "right" ? 'left' : 'right', + }; + } + + onTouchEnd(evt) { + if (!this._swipeState.active) return; + + const touch = evt.changedTouches[0] || evt.changedTouches[evt.changedTouches.length - 1]; + const deltaX = touch.clientX - this._swipeState.startX; + const deltaTime = Date.now() - this._swipeState.startTime; + + const velocity = deltaTime > 0 ? Math.abs(deltaX) / deltaTime : 0; + + const isSwipe = Math.abs(deltaX) >= this._swipeConfig.minDistance && velocity >= this._swipeConfig.velocityThreshold; + const isLeft = this._swipeState.direction == "left"; + + const directionMatches = isLeft ? deltaX < 0 : deltaX > 0; + + if (isSwipe && directionMatches) { + evt.preventDefault(); + evt.stopPropagation(); + + if (isLeft) { + this.nextPage(); + } else { + this.prevPage(); + } + } + } + + onTouchMove(evt) { + if (!this._swipeState.active) return; + + const touch = evt.touches[0] || evt.changedTouches[0]; + const deltaY = Math.abs(touch.clientY - this._swipeState.startY); + + // Cancel if too much vertical movement + if (deltaY > this._swipeConfig.verticalDistanceThreshold) { + this._swipeState.active = false; + return; + } + } + async init() { this.csrf_token = Cookie.get('epube_csrf_token'); + /** global setupCsrfPrefilter */ setupCsrfPrefilter(this.csrf_token); $(document).on("keyup", (e) => { @@ -62,6 +130,18 @@ class ReaderClass extends AppCommon { this.nextPage(); }); + $("#left, #right").on("touchstart", (evt) => { + this.onTouchStart(evt); + }); + + $("#left, #right").on("touchmove", (evt) => { + this.onTouchMove(evt); + }); + + $("#left, #right").on("touchend", (evt) => { + this.onTouchEnd(evt); + }); + this.loader = new ReaderLoader(); const load_failed = await this.loader.init(); @@ -123,9 +203,6 @@ class ReaderClass extends AppCommon { this.applyTheme(); - // Activate swipe navigation (overlay is ready as soon as #reader exists in DOM) - activateSwipeNavigation(this); - const bookinfo = await localforage.getItem(this.cacheId()); if (!bookinfo) { @@ -450,23 +527,6 @@ class ReaderClass extends AppCommon { } } }); - - // need to figure out how to do swipes safely (invisible overlay which passes text selection?) - - /* if (typeof EpubeApp != "undefined" || Reader.isTouchDevice()) { - $(window).on("swipeleft", function() { - console.log('swipe left'); - - this.nextPage(); - }); - - $(window).on("swiperight", function() { - console.log('swipe right'); - - this.prevPage(); - }); - } */ - }); rendition.on("keyup", (e) => { diff --git a/src/swipes.js b/src/swipes.js deleted file mode 100644 index 0f04517..0000000 --- a/src/swipes.js +++ /dev/null @@ -1,410 +0,0 @@ -// @ts-check - -'use strict'; - -/** - * Swipe navigation handler for the epub reader. - * - * Creates an invisible overlay over the reader area with three zones: - * - Left third: swipe left → previous page - * - Center third: transparent, passes touches through for text selection - * - Right third: swipe right → next page - * - * Uses velocity + distance thresholds to distinguish intentional swipes - * from accidental touches or text-selection drags. - * - * Also provides mouse-event fallbacks for desktop testing. - * - * @see https://stackoverflow.com/questions/4817029/whats-an-optimal-or-efficient-way-to-detect-a-touch-screen-device-using-javas - */ - -/* global ReaderClass */ - -/** - * @typedef {{ - * minDistance: number, - * velocityThreshold: number, - * verticalDistanceThreshold: number - * }} SwipeConfig - */ - -/** - * @typedef {{ - * active: boolean, - * startX: number, - * startY: number, - * startTime: number, - * direction: 'left'|'right'|null - * }} SwipeState - */ - -class SwipeHandler { - /** @type {SwipeConfig} */ - _config = { - minDistance: 50, // px — minimum horizontal travel to count as swipe - velocityThreshold: 0.3, // px/ms — minimum swipe speed - verticalDistanceThreshold: 80, // px — vertical movement before cancelling - }; - - /** @type {SwipeState} */ - _state = { - active: false, - startX: 0, - startY: 0, - startTime: 0, - direction: null, - }; - - /** @type {HTMLElement|null} */ - _overlay = null; - - /** @type {SwipeZone|null} */ - _leftZone = null; - - /** @type {SwipeZone|null} */ - _rightZone = null; - - /** @type {ReaderClass} */ - _reader; - - /** - * @param {ReaderClass} reader - The ReaderClass instance to call for page navigation. - */ - constructor(reader) { - this._reader = reader; - } - - /** - * Create and attach the swipe overlay to the reader container. - * Must be called after #reader element exists in the DOM. - */ - activate() { - const container = document.getElementsByClassName("container")[0]; - if (!container) { - console.warn('[swipes] container element not found'); - return; - } - - this._overlay = this._createOverlay(); - container.appendChild(this._overlay); - } - - /** - * Remove the overlay and clean up all event listeners. - */ - deactivate() { - if (this._overlay && this._overlay.parentNode) { - this._overlay.parentNode.removeChild(this._overlay); - } - - this._overlay = null; - this._leftZone = null; - this._rightZone = null; - this._state = { active: false, startX: 0, startY: 0, startTime: 0, direction: null }; - - // Clean up document-level mouse listeners - document.removeEventListener('mousemove', this._boundMouseMove); - document.removeEventListener('mouseup', this._boundMouseUp); - } - - /** - * Create the three-zone overlay structure. - * @returns {HTMLDivElement} - */ - _createOverlay() { - const overlay = document.createElement('div'); - overlay.id = 'swipe-overlay'; - overlay.style.cssText = ` - position: absolute; - top: 0; left: 0; right: 0; bottom: 0; - z-index: 5; - touch-action: none; - `; - - // Left zone: swipe left → prev page - this._leftZone = this._createZone('left', overlay); - - // Right zone: swipe right → next page - this._rightZone = this._createZone('right', overlay); - - // Center zone: transparent, passes touches through for text selection - const centerZone = document.createElement('div'); - centerZone.style.cssText = ` - position: absolute; - top: 0; - left: 16.66%; - width: 66.68%; - height: 100%; - `; - overlay.appendChild(centerZone); - - return overlay; - } - - /** - * Create a swipe-capturing zone for the left or right third. - * @param {'left'|'right'} side - * @param {HTMLElement} parent - * @returns {SwipeZone} - */ - _createZone(side, parent) { - const isLeft = side === 'left'; - const zone = document.createElement('div'); - zone.className = `swipe-zone swipe-zone-${side}`; - zone.style.cssText = ` - position: absolute; - top: 0; - left: ${isLeft ? '0' : '66.66%'}; - width: 33.34%; - height: 100%; - touch-action: none; - `; - - // Create bound handlers so we can remove them later - const boundTouchStart = this._makeTouchStart(zone, isLeft).bind(this); - const boundTouchMove = this._makeTouchMove(zone, isLeft).bind(this); - const boundTouchEnd = this._makeTouchEnd(zone, isLeft).bind(this); - const boundMouseDown = this._makeMouseDown(zone, isLeft).bind(this); - - zone.addEventListener('touchstart', boundTouchStart, { passive: true }); - zone.addEventListener('touchmove', boundTouchMove, { passive: true }); - zone.addEventListener('touchend', boundTouchEnd, { passive: true }); - zone.addEventListener('mousedown', boundMouseDown, { passive: true }); - - // Store bound handlers for cleanup - zone._swipeBoundHandlers = { boundTouchStart, boundTouchMove, boundTouchEnd, boundMouseDown }; - - // Store isLeft for mouse event handlers - zone._swipeIsLeft = isLeft; - - parent.appendChild(zone); - - return /** @type {SwipeZone} */ (zone); - } - - /** - * @param {HTMLElement} zone - * @param {boolean} isLeft - * @returns {(evt: TouchEvent) => void} - */ - _makeTouchStart(zone, isLeft) { - return (evt) => { - const touch = evt.touches[0] || evt.changedTouches[0]; - this._state = { - active: true, - startX: touch.clientX, - startY: touch.clientY, - startTime: Date.now(), - direction: isLeft ? 'left' : 'right', - }; - }; - } - - /** - * @param {HTMLElement} zone - * @param {boolean} isLeft - * @returns {(evt: TouchEvent) => void} - */ - _makeTouchMove(zone, isLeft) { - return (evt) => { - if (!this._state.active) return; - - const touch = evt.touches[0] || evt.changedTouches[0]; - const deltaX = touch.clientX - this._state.startX; - const deltaY = Math.abs(touch.clientY - this._state.startY); - - // Cancel if too much vertical movement (likely text selection) - if (deltaY > this._config.verticalDistanceThreshold) { - this._state.active = false; - return; - } - - // Visual feedback: subtle background color shift - const progress = Math.min(Math.abs(deltaX) / this._config.minDistance, 1); - const shouldHighlight = isLeft ? deltaX < 0 : deltaX > 0; - const alpha = shouldHighlight ? progress * 0.08 : 0; - zone.style.backgroundColor = `rgba(0, 125, 113, ${alpha})`; - }; - } - - /** - * @param {HTMLElement} zone - * @param {boolean} isLeft - * @returns {(evt: TouchEvent) => void} - */ - _makeTouchEnd(zone, isLeft) { - return (evt) => { - if (!this._state.active) { - zone.style.backgroundColor = ''; - return; - } - - const touch = evt.changedTouches[0] || evt.changedTouches[evt.changedTouches.length - 1]; - const deltaX = touch.clientX - this._state.startX; - const deltaTime = Date.now() - this._state.startTime; - - const velocity = deltaTime > 0 ? Math.abs(deltaX) / deltaTime : 0; - - const isSwipe = Math.abs(deltaX) >= this._config.minDistance && - velocity >= this._config.velocityThreshold; - - const directionMatches = isLeft ? deltaX > 0 : deltaX < 0; - - if (isSwipe && directionMatches) { - evt.preventDefault(); - evt.stopPropagation(); - - if (isLeft) { - this._reader.prevPage(); - } else { - this._reader.nextPage(); - } - } - - zone.style.backgroundColor = ''; - this._state.active = false; - }; - } - - /** - * @param {HTMLElement} zone - * @param {boolean} isLeft - * @returns {(evt: MouseEvent) => void} - */ - _makeMouseDown(zone, isLeft) { - return (evt) => { - this._state = { - active: true, - startX: evt.clientX, - startY: evt.clientY, - startTime: Date.now(), - direction: isLeft ? 'left' : 'right', - }; - }; - } - - /** - * @param {MouseEvent} evt - */ - _mouseMove(evt) { - if (!this._state.active) return; - - const zone = this._state.direction === 'left' ? - this._leftZone : - this._rightZone; - - if (!zone) return; - - const deltaX = evt.clientX - this._state.startX; - const deltaY = Math.abs(evt.clientY - this._state.startY); - - if (deltaY > this._config.verticalDistanceThreshold) { - this._state.active = false; - return; - } - - const isLeft = this._state.direction === 'left'; - const progress = Math.min(Math.abs(deltaX) / this._config.minDistance, 1); - const shouldHighlight = isLeft ? deltaX < 0 : deltaX > 0; - const alpha = shouldHighlight ? progress * 0.08 : 0; - zone.style.backgroundColor = `rgba(0, 125, 113, ${alpha})`; - } - - /** - * @param {MouseEvent} evt - */ - _mouseUp(evt) { - if (!this._state.active) return; - - const zone = this._state.direction === 'left' ? - this._leftZone : - this._rightZone; - - if (!zone) return; - - const deltaX = evt.clientX - this._state.startX; - const deltaTime = Date.now() - this._state.startTime; - const velocity = deltaTime > 0 ? Math.abs(deltaX) / deltaTime : 0; - - const isSwipe = Math.abs(deltaX) >= this._config.minDistance && - velocity >= this._config.velocityThreshold; - - const isLeft = this._state.direction === 'left'; - const directionMatches = isLeft ? deltaX < 0 : deltaX > 0; - - if (isSwipe && directionMatches) { - if (isLeft) { - this._reader.prevPage(); - } else { - this._reader.nextPage(); - } - } - - zone.style.backgroundColor = ''; - this._state.active = false; - } - - /** - * Initialize document-level mouse event listeners (bound once). - */ - _initMouseListeners() { - this._boundMouseMove = this._mouseMove.bind(this); - this._boundMouseUp = this._mouseUp.bind(this); - document.addEventListener('mousemove', this._boundMouseMove); - document.addEventListener('mouseup', this._boundMouseUp); - } -} - -/** - * Extended HTMLElement with our swipe-specific properties. - * @typedef {HTMLElement & { - * _swipeBoundHandlers?: { - * boundTouchStart: (evt: TouchEvent) => void; - * boundTouchMove: (evt: TouchEvent) => void; - * boundTouchEnd: (evt: TouchEvent) => void; - * boundMouseDown: (evt: MouseEvent) => void; - * }; - * _swipeIsLeft?: boolean; - * }} SwipeZone - */ - -/** - * @type {SwipeHandler} - */ -let _instance = null; - -/** - * Get or create the singleton SwipeHandler instance. - * @param {ReaderClass} reader - * @returns {SwipeHandler} - */ -function getSwipeHandler(reader) { - if (!_instance) { - _instance = new SwipeHandler(reader); - _instance._initMouseListeners(); - } - return _instance; -} - -/** - * Activate swipe navigation for the reader. - * Creates the overlay and starts listening for gestures. - * @param {ReaderClass} reader - */ -function activateSwipeNavigation(reader) { - const handler = getSwipeHandler(reader); - handler.activate(); -} - -/** - * Deactivate swipe navigation. - * Removes the overlay and cleans up event listeners. - */ -function deactivateSwipeNavigation() { - if (_instance) { - _instance.deactivate(); - _instance = null; - } -} - -/* exported SwipeHandler, activateSwipeNavigation, deactivateSwipeNavigation */ |
