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

58 lines
1.3 KiB
CoffeeScript
Raw Normal View History

define [
"base"
], (App) ->
# uses the PDFJS text layer renderer to provide invisible overlayed
# text for searching
App.factory 'pdfTextLayer', [ () ->
class pdfTextLayer
constructor: (options) ->
@textLayerDiv = options.textLayerDiv
@divContentDone = false
@viewport = options.viewport
@textDivs = []
@renderer = options.renderer
@renderingDone = false
render: (timeout) ->
if @renderingDone or not @divContentDone
return
if @textLayerRenderTask?
@textLayerRenderTask.cancel()
@textLayerRenderTask = null
@textDivs = []
textLayerFrag = document.createDocumentFragment()
@textLayerRenderTask = @renderer {
textContent: this.textContent,
container: textLayerFrag,
viewport: this.viewport,
textDivs: this.textDivs,
timeout: timeout,
enhanceTextSelection: this.enhanceTextSelection,
}
textLayerSuccess = () =>
@textLayerDiv.appendChild(textLayerFrag)
@renderingDone = true
2014-12-05 09:33:06 -05:00
textLayerFailure = () ->
return # canceled or failed to render text layer -- skipping errors
@textLayerRenderTask.promise.then(textLayerSuccess, textLayerFailure)
setTextContent: (textContent) ->
if (@textLayerRenderTask)
@textLayerRenderTask.cancel();
@textLayerRenderTask = null;
@textContent = textContent;
@divContentDone = true;
]