Get rect in reader instead of view

Also expose index in `loaded` event
This commit is contained in:
John Factotum
2022-10-25 05:55:57 +00:00
committed by GitHub
parent d63905346c
commit 6977ac005d
2 changed files with 7 additions and 9 deletions
+3 -2
View File
@@ -1,5 +1,5 @@
/* global zip: false, fflate: false */ /* global zip: false, fflate: false */
import { View } from './view.js' import { View, getPosition } 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'
import { createPopover } from './ui/popover.js' import { createPopover } from './ui/popover.js'
@@ -234,7 +234,8 @@ class Reader {
if (tocItem?.href) this.#tocView?.setCurrentHref?.(tocItem.href) if (tocItem?.href) this.#tocView?.setCurrentHref?.(tocItem.href)
} }
#onReference(obj) { #onReference(obj) {
const { content, pos: { point, dir } } = obj const { content, element } = obj
const { point, dir } = getPosition(element)
const iframe = document.createElement('iframe') const iframe = document.createElement('iframe')
iframe.sandbox = 'allow-same-origin' iframe.sandbox = 'allow-same-origin'
iframe.srcdoc = content iframe.srcdoc = content
+4 -7
View File
@@ -194,7 +194,6 @@ export class View {
this.emit?.({ type: 'external-link', uri }) this.emit?.({ type: 'external-link', uri })
else if (SSV.isRef(a)) { else if (SSV.isRef(a)) {
const { index, anchor } = book.resolveHref(uri) const { index, anchor } = book.resolveHref(uri)
const pos = getPosition(a)
Promise.resolve(book.sections[index].createDocument()) Promise.resolve(book.sections[index].createDocument())
.then(doc => [anchor(doc), doc.contentType]) .then(doc => [anchor(doc), doc.contentType])
.then(([el, type]) => .then(([el, type]) =>
@@ -203,14 +202,14 @@ export class View {
? this.emit?.({ ? this.emit?.({
type: 'reference', type: 'reference',
href: isNote ? null : uri, href: isNote ? null : uri,
content, contentType, pos, content, contentType, element: a,
}) : null) }) : null)
.catch(e => console.error(e)) .catch(e => console.error(e))
return return
} else this.goTo(uri) } else this.goTo(uri)
}) })
this.emit?.({ type: 'loaded', doc }) this.emit?.({ type: 'loaded', doc, index })
} }
#drawAnnotation(doc, overlayer, annotation) { #drawAnnotation(doc, overlayer, annotation) {
const { value } = annotation const { value } = annotation
@@ -230,8 +229,7 @@ export class View {
doc.addEventListener('click', e => { doc.addEventListener('click', e => {
const [value, range] = overlayer.hitTest(e) const [value, range] = overlayer.hitTest(e)
if (value) { if (value) {
const pos = getPosition(range) this.emit?.({ type: 'show-annotation', value, range })
this.emit?.({ type: 'show-annotation', value, pos })
} }
}, false) }, false)
return overlayer return overlayer
@@ -241,8 +239,7 @@ export class View {
const { index, anchor } = await this.goTo(value) const { index, anchor } = await this.goTo(value)
const { doc } = this.#getOverlayer(index) const { doc } = this.#getOverlayer(index)
const range = anchor(doc) const range = anchor(doc)
const pos = getPosition(range) this.emit?.({ type: 'show-annotation', value, range })
this.emit?.({ type: 'show-annotation', value, pos })
} }
getCFI(index, range) { getCFI(index, range) {
if (!range) return '' if (!range) return ''