Skip to content

Commit

Permalink
PDF: fix links
Browse files Browse the repository at this point in the history
  • Loading branch information
johnfactotum committed Sep 28, 2024
1 parent 1e95210 commit 693341f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
1 change: 1 addition & 0 deletions pdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
Expand Down
25 changes: 13 additions & 12 deletions view.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 693341f

Please sign in to comment.