From 182ab1018268a3e4e210ebdcdbf45b8a96a8bd32 Mon Sep 17 00:00:00 2001 From: John Factotum <50942278+johnfactotum@users.noreply.github.com> Date: Sun, 2 Apr 2023 00:32:33 +0800 Subject: [PATCH] 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 --- reader.js | 2 +- view.js | 46 ++-------------------------------------------- 2 files changed, 3 insertions(+), 45 deletions(-) diff --git a/reader.js b/reader.js index b291167..3d14cf3 100644 --- a/reader.js +++ b/reader.js @@ -1,4 +1,4 @@ -import { View, getPosition } from './view.js' +import { View } from './view.js' import { createTOCView } from './ui/tree.js' import { createMenu } from './ui/menu.js' diff --git a/view.js b/view.js index 1bb0b2d..f2d063f 100644 --- a/view.js +++ b/view.js @@ -26,46 +26,6 @@ const textWalker = function* (doc, func) { 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 { #sectionProgress #tocProgress @@ -134,8 +94,6 @@ export class View { this.emit?.({ type: 'relocated', ...progress, tocItem, pageItem, cfi }) } #onLoad(doc, index) { - const { book } = this - // set language and dir if not already set doc.documentElement.lang ||= this.language doc.documentElement.dir ||= this.isCJK ? '' : this.textDirection @@ -158,8 +116,8 @@ export class View { .then(x => x ? null : window.open(uri, '_blank')) .catch(e => console.error(e)) else Promise.resolve(emit?.({ type: 'link', a, uri })) - .then(x => x ? null : this.goTo(uri)) - .catch(e => console.error(e)) + .then(x => x ? null : this.goTo(uri)) + .catch(e => console.error(e)) }) } async addAnnotation(annotation, remove) {