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