Commit b1bedc79 authored by Randall Leeds's avatar Randall Leeds

PDF anchoring to unrendered pages

parent 67d87893
...@@ -20,8 +20,8 @@ getNodeTextLayer = (node) -> ...@@ -20,8 +20,8 @@ getNodeTextLayer = (node) ->
return node.getElementsByClassName('textLayer')[0] return node.getElementsByClassName('textLayer')[0]
getPageTextLayer = (pageIndex) -> getPage = (pageIndex) ->
return PDFViewerApplication.pdfViewer.pages[pageIndex].textLayer return PDFViewerApplication.pdfViewer.pages[pageIndex]
getPageTextContent = (pageIndex) -> getPageTextContent = (pageIndex) ->
...@@ -112,14 +112,27 @@ exports.anchor = (selectors) -> ...@@ -112,14 +112,27 @@ exports.anchor = (selectors) ->
promise = promise.catch -> promise = promise.catch ->
findPage(position.start) findPage(position.start)
.then(({index, offset}) -> .then(({index, offset}) ->
textLayer = getPageTextLayer(index) page = getPage(index)
if page.textLayer?.renderingDone
root = page.textLayer.textLayerDiv
start = position.start - offset start = position.start - offset
end = position.end - offset end = position.end - offset
root = textLayer.textLayerDiv
Promise.resolve(TextPositionAnchor.fromSelector({start, end}, {root})) Promise.resolve(TextPositionAnchor.fromSelector({start, end}, {root}))
.then((a) -> Promise.resolve(a.toRange({root}))) .then((a) -> Promise.resolve(a.toRange({root})))
)
.then(assertQuote) .then(assertQuote)
else
el = page.el
placeholder = el.getElementsByClassName('annotator-placeholder')[0]
unless placeholder?
placeholder = document.createElement('span')
placeholder.classList.add('annotator-placeholder')
placeholder.textContent = 'Loading annotations…'
page.el.appendChild(placeholder)
range = document.createRange()
range.setStartBefore(placeholder)
range.setEndAfter(placeholder)
return range
)
return promise return promise
......
...@@ -206,9 +206,7 @@ module.exports = class Guest extends Annotator ...@@ -206,9 +206,7 @@ module.exports = class Guest extends Annotator
deleteAnnotation: (annotation) -> deleteAnnotation: (annotation) ->
for info in @anchored when info.annotation is annotation for info in @anchored when info.annotation is annotation
for h in info.highlights when h.parentNode? highlighter.removeHighlights(info.highlights)
child = h.childNodes[0]
$(h).replaceWith(h.childNodes)
@anchored = (a for a in @anchored when a.annotation isnt annotation) @anchored = (a for a in @anchored when a.annotation isnt annotation)
@unanchored = (a for a in @unanchored when a.annotation isnt annotation) @unanchored = (a for a in @unanchored when a.annotation isnt annotation)
......
...@@ -22,3 +22,9 @@ exports.highlightRange = (normedRange, cssClass='annotator-hl') -> ...@@ -22,3 +22,9 @@ exports.highlightRange = (normedRange, cssClass='annotator-hl') ->
nodes = $(normedRange.textNodes()).filter((i) -> not white.test @nodeValue) nodes = $(normedRange.textNodes()).filter((i) -> not white.test @nodeValue)
r = nodes.wrap(hl).parent().show().toArray() r = nodes.wrap(hl).parent().show().toArray()
exports.removeHighlights = (highlights) ->
for h in highlights when h.parentNode?
child = h.childNodes[0]
$(h).replaceWith(h.childNodes)
...@@ -2,6 +2,8 @@ raf = require('raf') ...@@ -2,6 +2,8 @@ raf = require('raf')
Promise = require('es6-promise').Promise Promise = require('es6-promise').Promise
Annotator = require('annotator') Annotator = require('annotator')
highlighter = require('../highlighter')
class PDF extends Annotator.Plugin class PDF extends Annotator.Plugin
documentPromise: null documentPromise: null
...@@ -46,8 +48,7 @@ class PDF extends Annotator.Plugin ...@@ -46,8 +48,7 @@ class PDF extends Annotator.Plugin
return {title, link} return {title, link}
onpagerendered: (event) => onpagerendered: (event) =>
annotator = @annotator annotator = {anchored, unanchored} = @annotator
unanchored = @annotator.unanchored
page = PDFViewerApplication.pdfViewer.pages[event.detail.pageNumber - 1] page = PDFViewerApplication.pdfViewer.pages[event.detail.pageNumber - 1]
waitForTextLayer = -> waitForTextLayer = ->
...@@ -56,6 +57,28 @@ class PDF extends Annotator.Plugin ...@@ -56,6 +57,28 @@ class PDF extends Annotator.Plugin
reanchor = -> reanchor = ->
unanchored = unanchored.splice(0, unanchored.length) unanchored = unanchored.splice(0, unanchored.length)
placeholder = page.el.getElementsByClassName('annotator-placeholder')[0]
if placeholder?
unchanged = []
for info in anchored
attempt = false
for hl in info.highlights
if placeholder.contains(hl)
attempt = true
break
if attempt
highlighter.removeHighlights(info.highlights)
delete info.highlights
unanchored.push(info)
else
unchanged.push(info)
anchored.splice(0, anchored.length, unchanged...)
page.el.removeChild(placeholder)
for obj in unanchored for obj in unanchored
annotator.setupAnnotation(obj.annotation) annotator.setupAnnotation(obj.annotation)
......
...@@ -131,6 +131,10 @@ $base-font-size: 14px; ...@@ -131,6 +131,10 @@ $base-font-size: 14px;
@include transition(none !important); @include transition(none !important);
} }
.annotator-placeholder {
position: absolute;
}
//CONTROLBAR STUFF//////////////////////////////// //CONTROLBAR STUFF////////////////////////////////
.annotator-frame .annotator-toolbar { .annotator-frame .annotator-toolbar {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment