Commit f53359d6 authored by Randall Leeds's avatar Randall Leeds

Add clarifying comments for setupAnnotation

parent 90738870
...@@ -164,13 +164,27 @@ module.exports = class Guest extends Annotator ...@@ -164,13 +164,27 @@ module.exports = class Guest extends Annotator
self = this self = this
root = @element[0] root = @element[0]
# Anchors for all annotations are in the `anchors` instance property. These
# are anchors for this annotation only. After all the targets have been
# processed these will be appended to the list of anchors known to the
# instance. Anchors hold an annotation, a target of that annotation, a
# document range for that target and an Array of highlights.
anchors = [] anchors = []
# The targets that are already anchored. This function consults this to
# determine which targets can be left alone.
anchoredTargets = [] anchoredTargets = []
# These are the highlights for existing anchors of this annotation with
# targets that have since been removed from the annotation. These will
# be removed by this function.
deadHighlights = [] deadHighlights = []
# Initialize the target array.
annotation.target ?= [] annotation.target ?= []
locate = (target) -> locate = (target) ->
# Find a target using the anchoring module.
options = { options = {
cache: self.anchoringCache cache: self.anchoringCache
ignoreSelector: '[class^="annotator-"]' ignoreSelector: '[class^="annotator-"]'
...@@ -180,6 +194,7 @@ module.exports = class Guest extends Annotator ...@@ -180,6 +194,7 @@ module.exports = class Guest extends Annotator
.catch(-> {annotation, target}) .catch(-> {annotation, target})
highlight = (anchor) -> highlight = (anchor) ->
# Highlight the range for an anchor.
return anchor unless anchor.range? return anchor unless anchor.range?
return animationPromise -> return animationPromise ->
range = Annotator.Range.sniff(anchor.range) range = Annotator.Range.sniff(anchor.range)
...@@ -198,35 +213,46 @@ module.exports = class Guest extends Annotator ...@@ -198,35 +213,46 @@ module.exports = class Guest extends Annotator
return anchor return anchor
sync = (anchors) -> sync = (anchors) ->
# Store the results of anchoring.
annotation.$anchors = ({pos} for {pos} in anchors) annotation.$anchors = ({pos} for {pos} in anchors)
annotation.$orphan = anchors.length > 0 annotation.$orphan = anchors.length > 0
for anchor in anchors for anchor in anchors
if anchor.range? if anchor.range?
annotation.$orphan = false annotation.$orphan = false
# Add the anchors for this annotation to instance storage.
self.anchors = self.anchors.concat(anchors) self.anchors = self.anchors.concat(anchors)
# Let plugins know about the new information.
self.plugins.BucketBar?.update() self.plugins.BucketBar?.update()
self.plugins.CrossFrame?.sync([annotation]) self.plugins.CrossFrame?.sync([annotation])
# Remove all the anchors for this annotation from the instance storage.
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
# Anchors are valid as long as they still have a range and their target
# is still in the list of targets for this annotation.
if anchor.range? and anchor.target in annotation.target if anchor.range? and anchor.target in annotation.target
anchors.push(anchor) anchors.push(anchor)
anchoredTargets.push(anchor.target) anchoredTargets.push(anchor.target)
else if anchor.highlights? else if anchor.highlights?
deadHighlights.push(anchor.highlights) # These highlights are no longer valid and should be removed.
deadHighlights = deadHighlights.concat(anchor.highlights)
delete anchor.highlights delete anchor.highlights
delete anchor.range delete anchor.range
else else
# These can be ignored, so push them back onto the new list.
self.anchors.push(anchor) self.anchors.push(anchor)
deadHighlights = Array::concat(deadHighlights...) # Remove all the highlights that have no corresponding target anymore.
raf -> highlighter.removeHighlights(deadHighlights) raf -> highlighter.removeHighlights(deadHighlights)
# Anchor any targets of this annotation that are not anchored already.
for target in annotation.target when target not in anchoredTargets for target in annotation.target when target not in anchoredTargets
anchor = locate(target).then(highlight) anchor = locate(target).then(highlight)
anchors.push(anchor) anchors.push(anchor)
# Wait for all the anchoring tasks to complete then call sync.
Promise.all(anchors).then(sync) Promise.all(anchors).then(sync)
return annotation return annotation
......
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