Commit 806ad948 authored by Randall Leeds's avatar Randall Leeds

Remove all the fancy throttling of anchoring

This is premature optimization and makes things way more difficult
to reason about.
parent e693f4ba
......@@ -149,6 +149,12 @@ module.exports = class Guest extends Annotator
setupAnnotation: (annotation) ->
self = this
anchors = []
anchoredTargets = []
deadHighlights = []
annotation.target ?= []
locate = (target) ->
options = {ignoreSelector: '[class^="annotator-"]'}
return new Promise(raf)
......@@ -165,32 +171,6 @@ module.exports = class Guest extends Annotator
return anchor
return anchor
setup = ->
anchors = []
anchoredTargets = []
deadHighlights = []
for anchor in self.anchors.splice(0, self.anchors.length)
if anchor.annotation is annotation
if anchor.range? and anchor.target in annotation.target
anchors.push(anchor)
anchoredTargets.push(anchor.target)
else if anchor.highlights?
deadHighlights.push(anchor.highlights)
delete anchor.highlights
delete anchor.range
else
self.anchors.push(anchor)
deadHighlights = Array::concat(deadHighlights...)
new Promise(raf).then(-> highlighter.removeHighlights(deadHighlights))
for target in annotation.target when target not in anchoredTargets
anchor = locate(target).then(highlight)
anchors.push(anchor)
return Promise.all(anchors)
sync = (anchors) ->
annotation.$orphan = anchors.length > 0
for anchor in anchors
......@@ -201,14 +181,26 @@ module.exports = class Guest extends Annotator
self.plugins.BucketBar.update()
self.plugins.CrossFrame.sync([annotation])
cleanup = ->
delete annotation.anchors
for anchor in self.anchors.splice(0, self.anchors.length)
if anchor.annotation is annotation
if anchor.range? and anchor.target in annotation.target
anchors.push(anchor)
anchoredTargets.push(anchor.target)
else if anchor.highlights?
deadHighlights.push(anchor.highlights)
delete anchor.highlights
delete anchor.range
else
self.anchors.push(anchor)
deadHighlights = Array::concat(deadHighlights...)
new Promise(raf).then(-> highlighter.removeHighlights(deadHighlights))
annotation.target ?= []
annotation.anchors ?= Promise.resolve(annotation.anchors)
.then(setup)
.then(sync)
.then(cleanup, cleanup)
for target in annotation.target when target not in anchoredTargets
anchor = locate(target).then(highlight)
anchors.push(anchor)
Promise.all(anchors).then(sync)
return annotation
......
......@@ -6,8 +6,6 @@ class PDF extends Annotator.Plugin
documentLoaded: null
observer: null
pdfViewer: null
updatePromise: null
updateTimeout: null
pluginInit: ->
@pdfViewer = PDFViewerApplication.pdfViewer
......@@ -59,50 +57,39 @@ class PDF extends Annotator.Plugin
return {title, link}
update: ->
self = this
{annotator, pdfViewer} = this
_throttle = ->
if self.updateTimeout?
clearTimeout(self.updateTimeout)
self.updateTimeout = setTimeout(_update, 200)
stableAnchors = []
pendingAnchors = []
refreshAnnotations = []
_update = ->
anchors = []
annotations = []
for page in pdfViewer.pages when page.textLayer?.renderingDone
div = page.div ? page.el
placeholder = div.getElementsByClassName('annotator-placeholder')[0]
for page in pdfViewer.pages when page.textLayer?.renderingDone
div = page.div ? page.el
placeholder = div.getElementsByClassName('annotator-placeholder')[0]
switch page.renderingState
when RenderingStates.INITIAL
page.textLayer = null
when RenderingStates.FINISHED
if placeholder?
placeholder.parentNode.removeChild(placeholder)
switch page.renderingState
when RenderingStates.INITIAL
page.textLayer = null
when RenderingStates.FINISHED
if placeholder?
placeholder.parentNode.removeChild(placeholder)
for anchor in annotator.anchors when anchor.highlights?
if anchor.annotation in refreshAnnotations
continue
for anchor in annotator.anchors when anchor.annotation not in annotations
unless anchor.range? and anchor.highlights?
for hl in anchor.highlights
if not document.body.contains(hl)
delete anchor.highlights
delete anchor.range
annotations.push(anchor.annotation)
continue
refreshAnnotations.push(anchor.annotation)
break
for hl in anchor.highlights
if not document.body.contains(hl)
delete anchor.range
annotations.push(anchor.annotation)
break
for annotation in refreshAnnotations
annotator.setupAnnotation(annotation)
pendingAnchors.push(annotation.anchors)
for annotation in annotations
annotator.setupAnnotation(annotation)
anchors.push(annotation.anchors)
self.updateTimeout = null
self.updatePromise = Promise.all(anchors)
@annotator.plugins.BucketBar?.update()
Promise.resolve(@updatePromise).then(_throttle)
annotator.plugins.BucketBar?.update()
Annotator.Plugin.PDF = PDF
......
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