From 693341f0080e2aacce8cb3552928b619039f27e1 Mon Sep 17 00:00:00 2001 From: John Factotum <50942278+johnfactotum@users.noreply.github.com> Date: Sat, 28 Sep 2024 14:29:47 +0800 Subject: [PATCH] PDF: fix links --- pdf.js | 1 + view.js | 25 +++++++++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/pdf.js b/pdf.js index 9efcde7..8e49a34 100644 --- a/pdf.js +++ b/pdf.js @@ -51,6 +51,7 @@ const render = async (page, doc, zoom) => { await new pdfjsLib.AnnotationLayer({ page, viewport, div }).render({ annotations: await page.getAnnotations(), linkService: { + goToDestination: () => {}, getDestinationHash: dest => JSON.stringify(dest), addLinkAttributes: (link, url) => link.href = url, }, diff --git a/view.js b/view.js index 5d3eb11..9811559 100644 --- a/view.js +++ b/view.js @@ -229,19 +229,20 @@ export class View extends HTMLElement { #handleLinks(doc, index) { const { book } = this const section = book.sections[index] - for (const a of doc.querySelectorAll('a[href]')) - a.addEventListener('click', e => { - e.preventDefault() - const href_ = a.getAttribute('href') - const href = section?.resolveHref?.(href_) ?? href_ - if (book?.isExternal?.(href)) - Promise.resolve(this.#emit('external-link', { a, href }, true)) - .then(x => x ? globalThis.open(href, '_blank') : null) - .catch(e => console.error(e)) - else Promise.resolve(this.#emit('link', { a, href }, true)) - .then(x => x ? this.goTo(href) : null) + doc.addEventListener('click', e => { + const a = e.target.closest('a[href]') + if (!a) return + e.preventDefault() + const href_ = a.getAttribute('href') + const href = section?.resolveHref?.(href_) ?? href_ + if (book?.isExternal?.(href)) + Promise.resolve(this.#emit('external-link', { a, href }, true)) + .then(x => x ? globalThis.open(href, '_blank') : null) .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) { const { value } = annotation