overleaf/services/web/public/coffee/ide/pdfng/directives/pdfAnnotations.coffee

45 lines
1.3 KiB
CoffeeScript
Raw Normal View History

define [
"base"
], (App) ->
# App = angular.module 'pdfAnnotations', []
App.factory 'pdfAnnotations', [ () ->
class pdfAnnotations
2014-11-25 11:00:21 -05:00
@EXTERNAL_LINK_TARGET = "_blank";
2014-11-25 11:00:21 -05:00
constructor: (options) ->
@annotationsLayerDiv = options.annotations;
@viewport = options.viewport
@navigateFn = options.navigateFn
2014-11-25 11:00:21 -05:00
setAnnotations: (annotations) ->
for annotation in annotations
switch annotation.subtype
when 'Link' then @addLink(annotation);
when 'Text' then continue
2014-11-25 11:00:21 -05:00
addLink: (link) ->
element = @buildLinkElementFromRect(link.rect);
@setLinkTarget(element, link);
@annotationsLayerDiv.appendChild(element);
2014-11-25 11:00:21 -05:00
buildLinkElementFromRect: (rect) ->
rect = @viewport.convertToViewportRectangle(rect);
rect = PDFJS.Util.normalizeRect(rect);
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';
element
2014-11-25 11:00:21 -05:00
setLinkTarget: (element, link) ->
if link.url
element.href = link.url;
element.target = @EXTERNAL_LINK_TARGET;
else if (link.dest)
element.href = "#" + link.dest;
element.onclick = (e) =>
@navigateFn link
]