mirror of
https://github.com/john-okeefe/foliate-js.git
synced 2026-09-09 19:39:13 -04:00
View: add ability to automatically hide cursor
Controlled by the boolean attribute `autohide-cursor`
This commit is contained in:
@@ -5,6 +5,38 @@ import { textWalker } from './text-walker.js'
|
|||||||
|
|
||||||
const SEARCH_PREFIX = 'foliate-search:'
|
const SEARCH_PREFIX = 'foliate-search:'
|
||||||
|
|
||||||
|
class CursorAutohider {
|
||||||
|
#timeout
|
||||||
|
#el
|
||||||
|
#check
|
||||||
|
#state
|
||||||
|
constructor(el, check, state = {}) {
|
||||||
|
this.#el = el
|
||||||
|
this.#check = check
|
||||||
|
this.#state = state
|
||||||
|
if (this.#state.hidden) this.hide()
|
||||||
|
this.#el.addEventListener('mousemove', ({ screenX, screenY }) => {
|
||||||
|
// check if it actually moved
|
||||||
|
if (screenX === this.#state.x && screenY === this.#state.y) return
|
||||||
|
this.#state.x = screenX, this.#state.y = screenY
|
||||||
|
this.show()
|
||||||
|
if (this.#timeout) clearTimeout(this.#timeout)
|
||||||
|
if (check()) this.#timeout = setTimeout(this.hide.bind(this), 1000)
|
||||||
|
}, false)
|
||||||
|
}
|
||||||
|
cloneFor(el) {
|
||||||
|
return new CursorAutohider(el, this.#check, this.#state)
|
||||||
|
}
|
||||||
|
hide() {
|
||||||
|
this.#el.style.cursor = 'none'
|
||||||
|
this.#state.hidden = true
|
||||||
|
}
|
||||||
|
show() {
|
||||||
|
this.#el.style.removeProperty('cursor')
|
||||||
|
this.#state.hidden = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class History extends EventTarget {
|
class History extends EventTarget {
|
||||||
#arr = []
|
#arr = []
|
||||||
#index = -1
|
#index = -1
|
||||||
@@ -67,6 +99,8 @@ export class View extends HTMLElement {
|
|||||||
#tocProgress
|
#tocProgress
|
||||||
#pageProgress
|
#pageProgress
|
||||||
#searchResults = new Map()
|
#searchResults = new Map()
|
||||||
|
#cursorAutohider = new CursorAutohider(this, () =>
|
||||||
|
this.hasAttribute('autohide-cursor'))
|
||||||
isFixedLayout = false
|
isFixedLayout = false
|
||||||
lastLocation
|
lastLocation
|
||||||
history = new History()
|
history = new History()
|
||||||
@@ -187,6 +221,7 @@ export class View extends HTMLElement {
|
|||||||
doc.documentElement.dir ||= this.language.direction ?? ''
|
doc.documentElement.dir ||= this.language.direction ?? ''
|
||||||
|
|
||||||
this.#handleLinks(doc, index)
|
this.#handleLinks(doc, index)
|
||||||
|
this.#cursorAutohider.cloneFor(doc.documentElement)
|
||||||
|
|
||||||
this.#emit('load', { doc, index })
|
this.#emit('load', { doc, index })
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user