Skip to content

Commit

Permalink
Allow selecting across pages in both directions
Browse files Browse the repository at this point in the history
Also reduce timeout a bit
  • Loading branch information
johnfactotum committed Sep 14, 2024
1 parent e907e84 commit c9de12c
Showing 1 changed file with 17 additions and 7 deletions.
24 changes: 17 additions & 7 deletions src/reader/reader.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,13 @@ 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 @@ -504,18 +511,21 @@ class Reader {
})

if (!this.view.isFixedLayout)
// go to the next page when selecting to the end of a page
// this makes it possible to select across pages
// makes it possible to select across pages
doc.addEventListener('selectionchange', debounce(() => {
if (!isSelecting) return
if (this.view.renderer.getAttribute('flow') !== 'paginated') return
const { lastLocation } = this.view
if (!lastLocation) return
const selRange = getSelectionRange(doc.getSelection())
const { lastLocation: { range } } = this.view
if (!range) return
const sel = doc.getSelection()
const selRange = getSelectionRange(sel)
if (!selRange) return
if (selRange.compareBoundaryPoints(Range.END_TO_END, lastLocation.range) >= 0)
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()
}, 1000))
}, 700))

this.#cursorAutohider.cloneFor(doc.documentElement)
}
Expand Down

0 comments on commit c9de12c

Please sign in to comment.