mirror of
https://github.com/overleaf/overleaf.git
synced 2024-11-21 20:47:08 -05:00
6f6a9e714d
This reverts commit 37be3901abf1044d93d83cb684e4e32721550d5a. GitOrigin-RevId: 07d422401f298a667aa635e0232985a81ce6ef4a
61 lines
1.7 KiB
JavaScript
61 lines
1.7 KiB
JavaScript
import App from '../../../base'
|
|
import PDFJS from './pdfJsLoader'
|
|
const EXTERNAL_LINK_TARGET = '_blank'
|
|
const REL_NOOPENER = 'noreferrer noopener'
|
|
|
|
App.factory('pdfAnnotations', function() {
|
|
class pdfAnnotations {
|
|
constructor(options) {
|
|
this.annotationsLayerDiv = options.annotations
|
|
this.viewport = options.viewport
|
|
this.navigateFn = options.navigateFn
|
|
}
|
|
|
|
setAnnotations(annotations) {
|
|
const result = []
|
|
for (let annotation of annotations) {
|
|
switch (annotation.subtype) {
|
|
case 'Link':
|
|
result.push(this.addLink(annotation))
|
|
break
|
|
case 'Text':
|
|
continue
|
|
default:
|
|
result.push(undefined)
|
|
}
|
|
}
|
|
}
|
|
|
|
addLink(link) {
|
|
const element = this.buildLinkElementFromRect(link.rect)
|
|
this.setLinkTarget(element, link)
|
|
this.annotationsLayerDiv.appendChild(element)
|
|
}
|
|
|
|
buildLinkElementFromRect(rect) {
|
|
rect = this.viewport.convertToViewportRectangle(rect)
|
|
rect = PDFJS.Util.normalizeRect(rect)
|
|
const element = document.createElement('a')
|
|
element.style.left = Math.floor(rect[0]) + 'px'
|
|
element.style.top = Math.floor(rect[1]) + 'px'
|
|
element.style.width = Math.ceil(rect[2] - rect[0]) + 'px'
|
|
element.style.height = Math.ceil(rect[3] - rect[1]) + 'px'
|
|
return element
|
|
}
|
|
|
|
setLinkTarget(element, link) {
|
|
if (link.url) {
|
|
element.href = link.url
|
|
element.target = EXTERNAL_LINK_TARGET
|
|
element.rel = REL_NOOPENER
|
|
} else if (link.dest) {
|
|
element.href = `#${link.dest}`
|
|
element.onclick = () => {
|
|
this.navigateFn(link)
|
|
return false
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return pdfAnnotations
|
|
})
|