Skip to content

Commit

Permalink
No need to handle selection change
Browse files Browse the repository at this point in the history
Selection across pages is now handled in foliate-js.

See johnfactotum/foliate-js@59a5aa2
  • Loading branch information
johnfactotum committed Sep 27, 2024
1 parent e6e19ae commit 9959ff4
Showing 1 changed file with 0 additions and 41 deletions.
41 changes: 0 additions & 41 deletions src/reader/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,6 @@ const format = {}
const emit = x => globalThis.webkit.messageHandlers.viewer
.postMessage(JSON.stringify(x))

const debounce = (f, wait, immediate) => {
let timeout
return (...args) => {
const later = () => {
timeout = null
if (!immediate) f(...args)
}
const callNow = immediate && !timeout
if (timeout) clearTimeout(timeout)
timeout = setTimeout(later, wait)
if (callNow) f(...args)
}
}

const formatLanguageMap = x => {
if (!x) return ''
if (typeof x === 'string') return x
Expand All @@ -36,13 +22,6 @@ const getSelectionRange = sel => {
return range
}

const selectionIsBackward = sel => {
const range = document.createRange()
range.setStart(sel.anchorNode, sel.anchorOffset)
range.setEnd(sel.focusNode, sel.focusOffset)
return range.collapsed
}

const getLang = el => {
const lang = el.lang || el?.getAttributeNS?.('http://www.w3.org/XML/1998/namespace', 'lang')
if (lang) return lang
Expand Down Expand Up @@ -503,10 +482,7 @@ class Reader {
emit({ type: 'show-image', base64, mimetype }))
.catch(e => console.error(e)))

let isSelecting = false
doc.addEventListener('pointerdown', () => isSelecting = true)
doc.addEventListener('pointerup', () => {
isSelecting = false
const sel = doc.getSelection()
const range = getSelectionRange(sel)
if (!range) return
Expand All @@ -517,23 +493,6 @@ class Reader {
this.#showSelection({ index, range, lang, value, pos, text })
})

if (!this.view.isFixedLayout)
// makes it possible to select across pages
doc.addEventListener('selectionchange', debounce(() => {
if (!isSelecting) return
if (this.view.renderer.getAttribute('flow') !== 'paginated') return
const { lastLocation: { range } } = this.view
if (!range) return
const sel = doc.getSelection()
const selRange = getSelectionRange(sel)
if (!selRange) return
const backward = selectionIsBackward(sel)
if (backward && selRange.compareBoundaryPoints(Range.START_TO_START, range) <= 0)
this.view.prev()
else if (!backward && selRange.compareBoundaryPoints(Range.END_TO_END, range) >= 0)
this.view.next()
}, 700))

this.#cursorAutohider.cloneFor(doc.documentElement)
}
#showAnnotation({ index, range, value, pos }) {
Expand Down

0 comments on commit 9959ff4

Please sign in to comment.