Commit e693f4ba authored by Randall Leeds's avatar Randall Leeds

Consistency fixes

- anchor / describe both take options
- TextQuoteAnchor takes position as an option but it's not stored
- helper closures sprinkled throughout have fewer _ prefixes
parent 76f42211
...@@ -72,11 +72,7 @@ exports.anchor = (selectors, options = {}) -> ...@@ -72,11 +72,7 @@ exports.anchor = (selectors, options = {}) ->
return promise return promise
exports.describe = (range) -> exports.describe = (range, options = {}) ->
options =
root: document.body
ignoreSelector: '[class^="annotator-"]'
maybeDescribeWith = (type) -> maybeDescribeWith = (type) ->
return Promise.resolve(type) return Promise.resolve(type)
.then((t) -> t.fromRange(range, options)) .then((t) -> t.fromRange(range, options))
......
...@@ -160,7 +160,7 @@ exports.anchor = (selectors, options = {}) -> ...@@ -160,7 +160,7 @@ exports.anchor = (selectors, options = {}) ->
return promise return promise
exports.describe = (range) -> exports.describe = (range, options = {}) ->
range = new xpathRange.BrowserRange(range).normalize() range = new xpathRange.BrowserRange(range).normalize()
startTextLayer = getNodeTextLayer(range.start) startTextLayer = getNodeTextLayer(range.start)
...@@ -192,8 +192,8 @@ exports.describe = (range) -> ...@@ -192,8 +192,8 @@ exports.describe = (range) ->
r.setStartBefore(startRange.start) r.setStartBefore(startRange.start)
r.setEndAfter(endRange.end) r.setEndAfter(endRange.end)
options = {root: startTextLayer} pageOptions = {root: startTextLayer}
quote = Promise.resolve(TextQuoteAnchor.fromRange(r, options)) quote = Promise.resolve(TextQuoteAnchor.fromRange(r, pageOptions))
.then((a) -> a.toSelector()) .then((a) -> a.toSelector())
return Promise.all([position, quote]) return Promise.all([position, quote])
...@@ -178,11 +178,9 @@ class TextPositionAnchor extends Anchor ...@@ -178,11 +178,9 @@ class TextPositionAnchor extends Anchor
# :param String quote: The anchor text to match. # :param String quote: The anchor text to match.
# :param String prefix: A prefix that preceeds the anchor text. # :param String prefix: A prefix that preceeds the anchor text.
# :param String suffix: A suffix that follows the anchor text. # :param String suffix: A suffix that follows the anchor text.
# :param Number start: The start offset for the anchor text.
# :param Number end: The end offset for the anchor text.
### ###
class TextQuoteAnchor extends Anchor class TextQuoteAnchor extends Anchor
constructor: (@quote, @prefix='', @suffix='', @start, @end) -> constructor: (@quote, @prefix='', @suffix='') ->
unless @quote? then missingParameter('quote') unless @quote? then missingParameter('quote')
@fromRange: (range, options = {}) -> @fromRange: (range, options = {}) ->
...@@ -203,12 +201,11 @@ class TextQuoteAnchor extends Anchor ...@@ -203,12 +201,11 @@ class TextQuoteAnchor extends Anchor
prefix = corpus.substr(prefixStart, start - prefixStart) prefix = corpus.substr(prefixStart, start - prefixStart)
suffix = corpus.substr(end, 32) suffix = corpus.substr(end, 32)
return new TextQuoteAnchor(exact, prefix, suffix, start, end) return new TextQuoteAnchor(exact, prefix, suffix)
@fromSelector: (selector, options = {}) -> @fromSelector: (selector) ->
{start, end} = options.position ? {}
{exact, prefix, suffix} = selector {exact, prefix, suffix} = selector
return new TextQuoteAnchor(exact, prefix, suffix, start, end) return new TextQuoteAnchor(exact, prefix, suffix)
toRange: (options = {}) -> toRange: (options = {}) ->
return this.toPositionAnchor(options).toRange() return this.toPositionAnchor(options).toRange()
...@@ -236,7 +233,7 @@ class TextQuoteAnchor extends Anchor ...@@ -236,7 +233,7 @@ class TextQuoteAnchor extends Anchor
return acc return acc
slices = @quote.match(/(.|[\r\n]){1,32}/g) slices = @quote.match(/(.|[\r\n]){1,32}/g)
loc = @start ? root.textContent.length / 2 loc = options.position?.start ? root.textContent.length / 2
# TODO: use the suffix # TODO: use the suffix
dmp.Match_Distance = root.textContent.length * 2 dmp.Match_Distance = root.textContent.length * 2
......
...@@ -149,19 +149,14 @@ module.exports = class Guest extends Annotator ...@@ -149,19 +149,14 @@ module.exports = class Guest extends Annotator
setupAnnotation: (annotation) -> setupAnnotation: (annotation) ->
self = this self = this
setup = -> locate = (target) ->
anchors = []
anchoredTargets = []
deadHighlights = []
_anchor = (target) ->
options = {ignoreSelector: '[class^="annotator-"]'} options = {ignoreSelector: '[class^="annotator-"]'}
return new Promise(raf) return new Promise(raf)
.then(-> anchoring.anchor(target.selector, options)) .then(-> anchoring.anchor(target.selector, options))
.then((range) -> {annotation, target, range}) .then((range) -> {annotation, target, range})
.catch(-> {annotation, target}) .catch(-> {annotation, target})
_highlight = (anchor) -> highlight = (anchor) ->
if anchor.range? if anchor.range?
return new Promise(raf).then -> return new Promise(raf).then ->
range = Annotator.Range.sniff(anchor.range) range = Annotator.Range.sniff(anchor.range)
...@@ -170,6 +165,11 @@ module.exports = class Guest extends Annotator ...@@ -170,6 +165,11 @@ 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) for anchor in self.anchors.splice(0, self.anchors.length)
if anchor.annotation is annotation if anchor.annotation is annotation
if anchor.range? and anchor.target in annotation.target if anchor.range? and anchor.target in annotation.target
...@@ -186,7 +186,7 @@ module.exports = class Guest extends Annotator ...@@ -186,7 +186,7 @@ module.exports = class Guest extends Annotator
new Promise(raf).then(-> highlighter.removeHighlights(deadHighlights)) new Promise(raf).then(-> highlighter.removeHighlights(deadHighlights))
for target in annotation.target when target not in anchoredTargets for target in annotation.target when target not in anchoredTargets
anchor = _anchor(target).then(_highlight) anchor = locate(target).then(highlight)
anchors.push(anchor) anchors.push(anchor)
return Promise.all(anchors) return Promise.all(anchors)
...@@ -201,11 +201,14 @@ module.exports = class Guest extends Annotator ...@@ -201,11 +201,14 @@ 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 = ->
delete annotation.anchors
annotation.target ?= [] annotation.target ?= []
annotation.anchors ?= Promise.resolve(annotation.anchors) annotation.anchors ?= Promise.resolve(annotation.anchors)
.then(setup) .then(setup)
.then(sync) .then(sync)
.then(-> delete annotation.anchors) .then(cleanup, cleanup)
return annotation return annotation
...@@ -213,6 +216,10 @@ module.exports = class Guest extends Annotator ...@@ -213,6 +216,10 @@ module.exports = class Guest extends Annotator
ranges = @selectedRanges ranges = @selectedRanges
@selectedRanges = null @selectedRanges = null
getSelectors = (range) ->
options = {ignoreSelector: '[class^="annotator-"]'}
return anchoring.describe(range, options)
setDocumentInfo = ({metadata, uri}) -> setDocumentInfo = ({metadata, uri}) ->
annotation.uri = uri annotation.uri = uri
if metadata? if metadata?
...@@ -223,7 +230,7 @@ module.exports = class Guest extends Annotator ...@@ -223,7 +230,7 @@ module.exports = class Guest extends Annotator
annotation.target = ({source, selector} for selector in selectors) annotation.target = ({source, selector} for selector in selectors)
info = this.getDocumentInfo().then(setDocumentInfo) info = this.getDocumentInfo().then(setDocumentInfo)
selectors = Promise.all(ranges.map(anchoring.describe)) selectors = Promise.all(ranges.map(getSelectors))
targets = Promise.all([info, selectors]).then(setTargets) targets = Promise.all([info, selectors]).then(setTargets)
targets.then(=> this.setupAnnotation(annotation)) targets.then(=> this.setupAnnotation(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