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,10 +171,15 @@ module.exports = class Guest extends Annotator ...@@ -165,10 +171,15 @@ module.exports = class Guest extends Annotator
return anchor return anchor
return anchor return anchor
setup = -> sync = (anchors) ->
anchors = [] annotation.$orphan = anchors.length > 0
anchoredTargets = [] for anchor in anchors
deadHighlights = [] if anchor.range?
annotation.$orphan = false
self.anchors = self.anchors.concat(anchors)
self.plugins.BucketBar.update()
self.plugins.CrossFrame.sync([annotation])
for anchor in self.anchors.splice(0, self.anchors.length) for anchor in self.anchors.splice(0, self.anchors.length)
if anchor.annotation is annotation if anchor.annotation is annotation
...@@ -189,26 +200,7 @@ module.exports = class Guest extends Annotator ...@@ -189,26 +200,7 @@ module.exports = class Guest extends Annotator
anchor = locate(target).then(highlight) anchor = locate(target).then(highlight)
anchors.push(anchor) anchors.push(anchor)
return Promise.all(anchors) Promise.all(anchors).then(sync)
sync = (anchors) ->
annotation.$orphan = anchors.length > 0
for anchor in anchors
if anchor.range?
annotation.$orphan = false
self.anchors = self.anchors.concat(anchors)
self.plugins.BucketBar.update()
self.plugins.CrossFrame.sync([annotation])
cleanup = ->
delete annotation.anchors
annotation.target ?= []
annotation.anchors ?= Promise.resolve(annotation.anchors)
.then(setup)
.then(sync)
.then(cleanup, cleanup)
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,17 +57,11 @@ class PDF extends Annotator.Plugin ...@@ -59,17 +57,11 @@ 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 = ->
anchors = []
annotations = []
for page in pdfViewer.pages when page.textLayer?.renderingDone for page in pdfViewer.pages when page.textLayer?.renderingDone
div = page.div ? page.el div = page.div ? page.el
...@@ -82,27 +74,22 @@ class PDF extends Annotator.Plugin ...@@ -82,27 +74,22 @@ class PDF extends Annotator.Plugin
if placeholder? if placeholder?
placeholder.parentNode.removeChild(placeholder) placeholder.parentNode.removeChild(placeholder)
for anchor in annotator.anchors when anchor.annotation not in annotations for anchor in annotator.anchors when anchor.highlights?
unless anchor.range? and anchor.highlights? if anchor.annotation in refreshAnnotations
delete anchor.range
annotations.push(anchor.annotation)
continue continue
for hl in anchor.highlights for hl in anchor.highlights
if not document.body.contains(hl) if not document.body.contains(hl)
delete anchor.highlights
delete anchor.range delete anchor.range
annotations.push(anchor.annotation) refreshAnnotations.push(anchor.annotation)
break break
for annotation in annotations for annotation in refreshAnnotations
annotator.setupAnnotation(annotation) annotator.setupAnnotation(annotation)
anchors.push(annotation.anchors) pendingAnchors.push(annotation.anchors)
self.updateTimeout = null
self.updatePromise = Promise.all(anchors)
@annotator.plugins.BucketBar?.update() 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