PDF: fix links

This commit is contained in:
John Factotum
2024-09-28 14:29:47 +08:00
parent 1e952102bd
commit 693341f008
2 changed files with 14 additions and 12 deletions
+1
View File
@@ -51,6 +51,7 @@ const render = async (page, doc, zoom) => {
await new pdfjsLib.AnnotationLayer({ page, viewport, div }).render({ await new pdfjsLib.AnnotationLayer({ page, viewport, div }).render({
annotations: await page.getAnnotations(), annotations: await page.getAnnotations(),
linkService: { linkService: {
goToDestination: () => {},
getDestinationHash: dest => JSON.stringify(dest), getDestinationHash: dest => JSON.stringify(dest),
addLinkAttributes: (link, url) => link.href = url, addLinkAttributes: (link, url) => link.href = url,
}, },
+13 -12
View File
@@ -229,19 +229,20 @@ export class View extends HTMLElement {
#handleLinks(doc, index) { #handleLinks(doc, index) {
const { book } = this const { book } = this
const section = book.sections[index] const section = book.sections[index]
for (const a of doc.querySelectorAll('a[href]')) doc.addEventListener('click', e => {
a.addEventListener('click', e => { const a = e.target.closest('a[href]')
e.preventDefault() if (!a) return
const href_ = a.getAttribute('href') e.preventDefault()
const href = section?.resolveHref?.(href_) ?? href_ const href_ = a.getAttribute('href')
if (book?.isExternal?.(href)) const href = section?.resolveHref?.(href_) ?? href_
Promise.resolve(this.#emit('external-link', { a, href }, true)) if (book?.isExternal?.(href))
.then(x => x ? globalThis.open(href, '_blank') : null) Promise.resolve(this.#emit('external-link', { a, href }, true))
.catch(e => console.error(e)) .then(x => x ? globalThis.open(href, '_blank') : null)
else Promise.resolve(this.#emit('link', { a, href }, true))
.then(x => x ? this.goTo(href) : null)
.catch(e => console.error(e)) .catch(e => console.error(e))
}) else Promise.resolve(this.#emit('link', { a, href }, true))
.then(x => x ? this.goTo(href) : null)
.catch(e => console.error(e))
})
} }
async addAnnotation(annotation, remove) { async addAnnotation(annotation, remove) {
const { value } = annotation const { value } = annotation