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:
John Factotum
2023-04-02 00:32:33 +08:00
parent c173f5b523
commit 182ab10182
2 changed files with 3 additions and 45 deletions
+1 -1
View File
@@ -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'
-42
View File
@@ -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