mirror of
https://github.com/john-okeefe/foliate-js.git
synced 2026-09-09 11:29:14 -04:00
Remove getPosition()
The original idea of including this function in `./view.js` seems to be that it's shared between the demo reader and Foliate. But - It's currently not used in the demo reader any more. - It works, but it lacks features. - In any case it's not coupled with anything else in `./view.js`, so at best it should be a separate module. Also: remove unused variable and fix indentation
This commit is contained in:
@@ -1,4 +1,4 @@
|
|||||||
import { View, getPosition } from './view.js'
|
import { View } from './view.js'
|
||||||
import { createTOCView } from './ui/tree.js'
|
import { createTOCView } from './ui/tree.js'
|
||||||
import { createMenu } from './ui/menu.js'
|
import { createMenu } from './ui/menu.js'
|
||||||
|
|
||||||
|
|||||||
@@ -26,46 +26,6 @@ const textWalker = function* (doc, func) {
|
|||||||
for (const match of func(strs, makeRange)) yield match
|
for (const match of func(strs, makeRange)) yield match
|
||||||
}
|
}
|
||||||
|
|
||||||
const frameRect = (frame, rect, sx = 1, sy = 1) => {
|
|
||||||
const left = sx * rect.left + frame.left
|
|
||||||
const right = sx * rect.right + frame.left
|
|
||||||
const top = sy * rect.top + frame.top
|
|
||||||
const bottom = sy * rect.bottom + frame.top
|
|
||||||
return { left, right, top, bottom }
|
|
||||||
}
|
|
||||||
|
|
||||||
const pointIsInView = ({ x, y }) =>
|
|
||||||
x > 0 && y > 0 && x < window.innerWidth && y < window.innerHeight
|
|
||||||
|
|
||||||
export const getPosition = target => {
|
|
||||||
// TODO: vertical text
|
|
||||||
const frameElement = (target.getRootNode?.() ?? target?.endContainer?.getRootNode?.())
|
|
||||||
?.defaultView?.frameElement
|
|
||||||
|
|
||||||
const transform = frameElement ? getComputedStyle(frameElement).transform : ''
|
|
||||||
const match = transform.match(/matrix\((.+)\)/)
|
|
||||||
const [sx, , , sy] = match?.[1]?.split(/\s*,\s*/)?.map(x => parseFloat(x)) ?? []
|
|
||||||
|
|
||||||
const frame = frameElement?.getBoundingClientRect() ?? { top: 0, left: 0 }
|
|
||||||
const rects = Array.from(target.getClientRects())
|
|
||||||
const first = frameRect(frame, rects[0], sx, sy)
|
|
||||||
const last = frameRect(frame, rects.at(-1), sx, sy)
|
|
||||||
const start = {
|
|
||||||
point: { x: (first.left + first.right) / 2, y: first.top },
|
|
||||||
dir: 'up',
|
|
||||||
}
|
|
||||||
const end = {
|
|
||||||
point: { x: (last.left + last.right) / 2, y: last.bottom },
|
|
||||||
dir: 'down',
|
|
||||||
}
|
|
||||||
const startInView = pointIsInView(start.point)
|
|
||||||
const endInView = pointIsInView(end.point)
|
|
||||||
if (!startInView && !endInView) return { point: { x: 0, y: 0 } }
|
|
||||||
if (!startInView) return end
|
|
||||||
if (!endInView) return start
|
|
||||||
return start.point.y > window.innerHeight - end.point.y ? start : end
|
|
||||||
}
|
|
||||||
|
|
||||||
export class View {
|
export class View {
|
||||||
#sectionProgress
|
#sectionProgress
|
||||||
#tocProgress
|
#tocProgress
|
||||||
@@ -134,8 +94,6 @@ export class View {
|
|||||||
this.emit?.({ type: 'relocated', ...progress, tocItem, pageItem, cfi })
|
this.emit?.({ type: 'relocated', ...progress, tocItem, pageItem, cfi })
|
||||||
}
|
}
|
||||||
#onLoad(doc, index) {
|
#onLoad(doc, index) {
|
||||||
const { book } = this
|
|
||||||
|
|
||||||
// set language and dir if not already set
|
// set language and dir if not already set
|
||||||
doc.documentElement.lang ||= this.language
|
doc.documentElement.lang ||= this.language
|
||||||
doc.documentElement.dir ||= this.isCJK ? '' : this.textDirection
|
doc.documentElement.dir ||= this.isCJK ? '' : this.textDirection
|
||||||
@@ -158,8 +116,8 @@ export class View {
|
|||||||
.then(x => x ? null : window.open(uri, '_blank'))
|
.then(x => x ? null : window.open(uri, '_blank'))
|
||||||
.catch(e => console.error(e))
|
.catch(e => console.error(e))
|
||||||
else Promise.resolve(emit?.({ type: 'link', a, uri }))
|
else Promise.resolve(emit?.({ type: 'link', a, uri }))
|
||||||
.then(x => x ? null : this.goTo(uri))
|
.then(x => x ? null : this.goTo(uri))
|
||||||
.catch(e => console.error(e))
|
.catch(e => console.error(e))
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
async addAnnotation(annotation, remove) {
|
async addAnnotation(annotation, remove) {
|
||||||
|
|||||||
Reference in New Issue
Block a user