2020-05-19 05:02:56 -04:00
|
|
|
import App from '../../../base'
|
2021-04-27 07:24:51 -04:00
|
|
|
import PDFJS from './pdfJsLoader'
|
2020-05-19 05:02:56 -04:00
|
|
|
const EXTERNAL_LINK_TARGET = '_blank'
|
|
|
|
const REL_NOOPENER = 'noreferrer noopener'
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2021-04-14 09:17:21 -04:00
|
|
|
App.factory('pdfAnnotations', function () {
|
2020-05-19 05:02:56 -04:00
|
|
|
class pdfAnnotations {
|
|
|
|
constructor(options) {
|
|
|
|
this.annotationsLayerDiv = options.annotations
|
|
|
|
this.viewport = options.viewport
|
|
|
|
this.navigateFn = options.navigateFn
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
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)
|
2019-01-17 09:55:41 -05:00
|
|
|
}
|
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
addLink(link) {
|
|
|
|
const element = this.buildLinkElementFromRect(link.rect)
|
|
|
|
this.setLinkTarget(element, link)
|
|
|
|
this.annotationsLayerDiv.appendChild(element)
|
|
|
|
}
|
2019-01-17 09:55:41 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
buildLinkElementFromRect(rect) {
|
|
|
|
rect = this.viewport.convertToViewportRectangle(rect)
|
2021-04-27 07:24:51 -04:00
|
|
|
rect = PDFJS.Util.normalizeRect(rect)
|
2020-05-19 05:02:56 -04:00
|
|
|
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
|
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
|
2020-05-19 05:02:56 -04:00
|
|
|
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
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2019-01-17 09:55:41 -05:00
|
|
|
}
|
2018-11-05 05:06:39 -05:00
|
|
|
}
|
2020-05-19 05:02:56 -04:00
|
|
|
}
|
|
|
|
return pdfAnnotations
|
2019-01-17 09:55:41 -05:00
|
|
|
})
|