Commit 45430a09 authored by csillag's avatar csillag

Added some forgotten Annotator files

These should have been added along with the rest in 8c7db59e05dd7757989f6b31a19edef6cfb686fd
parent 930a68cf
# This anchor type stores information about a piece of text,
# described using start and end character offsets
class TextPositionAnchor extends Annotator.Anchor
@Annotator = Annotator
constructor: (annotator, annotation, target,
@start, @end, startPage, endPage,
quote, diffHTML, diffCaseOnly) ->
super annotator, annotation, target,
startPage, endPage,
quote, diffHTML, diffCaseOnly
# This pair of offsets is the key information,
# upon which this anchor is based upon.
unless @start? then throw new Error "start is required!"
unless @end? then throw new Error "end is required!"
@Annotator = TextPositionAnchor.Annotator
# This is how we create a highlight out of this kind of anchor
_createHighlight: (page) ->
# First we create the range from the stored stard and end offsets
mappings = @annotator.domMapper.getMappingsForCharRange @start, @end, [page]
# Get the wanted range out of the response of DTM
realRange = mappings.sections[page].realRange
# Get a BrowserRange
browserRange = new @Annotator.Range.BrowserRange realRange
# Get a NormalizedRange
normedRange = browserRange.normalize @annotator.wrapper[0]
# Create the highligh
new @Annotator.TextHighlight this, page, normedRange
# Annotator plugin for text position-based anchoring
class Annotator.Plugin.TextPosition extends Annotator.Plugin
pluginInit: ->
@Annotator = Annotator
# Do we have the basic text anchors plugin loaded?
unless @annotator.plugins.DomTextMapper
throw new Error "The TextPosition Annotator plugin requires the DomTextMapper plugin."
# Register the creator for text quote selectors
@annotator.selectorCreators.push
name: "TextPositionSelector"
describe: @_getTextPositionSelector
@annotator.anchoringStrategies.push
# Position-based strategy. (The quote is verified.)
# This can handle document structure changes,
# but not the content changes.
name: "position"
code: @createFromPositionSelector
# Export the anchor type
@Annotator.TextPositionAnchor = TextPositionAnchor
# Create a TextPositionSelector around a range
_getTextPositionSelector: (selection) =>
return [] unless selection.type is "text range"
startOffset = (@annotator.domMapper.getInfoForNode selection.range.start).start
endOffset = (@annotator.domMapper.getInfoForNode selection.range.end).end
[
type: "TextPositionSelector"
start: startOffset
end: endOffset
]
# Create an anchor using the saved TextPositionSelector.
# The quote is verified.
createFromPositionSelector: (annotation, target) =>
# We need the TextPositionSelector
selector = @annotator.findSelector target.selector, "TextPositionSelector"
return unless selector?
content = @annotator.domMapper.getCorpus()[selector.start .. selector.end-1].trim()
currentQuote = @annotator.normalizeString content
savedQuote = @annotator.getQuoteForTarget? target
if savedQuote? and currentQuote isnt savedQuote
# We have a saved quote, let's compare it to current content
#console.log "Could not apply position selector" +
# " [#{selector.start}:#{selector.end}] to current document," +
# " because the quote has changed. " +
# "(Saved quote is '#{savedQuote}'." +
# " Current quote is '#{currentQuote}'.)"
return null
# Create a TextPositionAnchor from this data
new TextPositionAnchor @annotator, annotation, target,
selector.start, selector.end,
(@annotator.domMapper.getPageIndexForPos selector.start),
(@annotator.domMapper.getPageIndexForPos selector.end),
currentQuote
# This plugin defines the TextQuote selector
class Annotator.Plugin.TextQuote extends Annotator.Plugin
@Annotator = Annotator
@$ = Annotator.$
# Plugin initialization
pluginInit: ->
# Register the creator for text quote selectors
@annotator.selectorCreators.push
name: "TextQuoteSelector"
describe: @_getTextQuoteSelector
# Register function to get quote from this selector
@annotator.getQuoteForTarget = (target) =>
selector = @annotator.findSelector target.selector, "TextQuoteSelector"
if selector?
@annotator.normalizeString selector.exact
else
null
# Create a TextQuoteSelector around a range
_getTextQuoteSelector: (selection) =>
return [] unless selection.type is "text range"
unless selection.range?
throw new Error "Called getTextQuoteSelector() with null range!"
rangeStart = selection.range.start
unless rangeStart?
throw new Error "Called getTextQuoteSelector() on a range with no valid start."
rangeEnd = selection.range.end
unless rangeEnd?
throw new Error "Called getTextQuoteSelector() on a range with no valid end."
[
if @annotator.plugins.DomTextMapper
# Calculate the quote and context using DTM
startOffset = (@annotator.domMapper.getInfoForNode rangeStart).start
endOffset = (@annotator.domMapper.getInfoForNode rangeEnd).end
quote = @annotator.domMapper.getCorpus()[startOffset .. endOffset-1].trim()
[prefix, suffix] = @annotator.domMapper.getContextForCharRange startOffset, endOffset
type: "TextQuoteSelector"
exact: quote
prefix: prefix
suffix: suffix
else
# Get the quote directly from the range
type: "TextQuoteSelector"
exact: selection.range.text().trim()
]
# This anhor type stores information about a piece of text,
# described using the actual reference to the range in the DOM.
#
# When creating this kind of anchor, you are supposed to pass
# in a NormalizedRange object, which should cover exactly
# the wanted piece of text; no character offset correction is supported.
#
# Also, please note that these anchors can not really be virtualized,
# because they don't have any truly DOM-independent information;
# the core information stored is the reference to an object which
# lives in the DOM. Therefore, no lazy loading is possible with
# this kind of anchor. For that, use TextPositionAnchor instead.
#
# This plugin also adds a strategy to reanchor based on range selectors.
# If the TextQuote plugin is also loaded, then it will also check
# the saved quote against what is available now.
#
# If the TextPosition plugin is loaded, it will create a TextPosition
# anchor; otherwise it will record a TextRangeAnchor.
class TextRangeAnchor extends Annotator.Anchor
@Annotator = Annotator
constructor: (annotator, annotation, target, @range, quote) ->
super annotator, annotation, target, 0, 0, quote
unless @range? then throw new Error "range is required!"
@Annotator = TextRangeAnchor.Annotator
# This is how we create a highlight out of this kind of anchor
_createHighlight: ->
# Create the highligh
new @Annotator.TextHighlight this, 0, @range
# Annotator plugin for creating, and anchoring based on text range
# selectors
class Annotator.Plugin.TextRange extends Annotator.Plugin
pluginInit: ->
@Annotator = Annotator
# Register the creator for range selectors
@annotator.selectorCreators.push
name: "RangeSelector"
describe: @_getRangeSelector
# Register our anchoring strategies
@annotator.anchoringStrategies.push
# Simple strategy based on DOM Range
name: "range"
code: @createFromRangeSelector
# Export these anchor types
@annotator.TextRangeAnchor = TextRangeAnchor
# Create a RangeSelector around a range
_getRangeSelector: (selection) =>
return [] unless selection.type is "text range"
sr = selection.range.serialize @annotator.wrapper[0]
[
type: "RangeSelector"
startContainer: sr.startContainer
startOffset: sr.startOffset
endContainer: sr.endContainer
endOffset: sr.endOffset
]
# Create and anchor using the saved Range selector.
# The quote is verified.
createFromRangeSelector: (annotation, target) =>
selector = @annotator.findSelector target.selector, "RangeSelector"
unless selector? then return null
# Try to apply the saved XPath
try
range = @Annotator.Range.sniff selector
normedRange = range.normalize @annotator.wrapper[0]
catch error
return null
# Get the text of this range
currentQuote = @annotator.normalizeString if @annotator.plugins.DomTextMapper
# Determine the current content of the given range using DTM
startInfo = @annotator.domMapper.getInfoForNode normedRange.start
startOffset = startInfo.start
endInfo = @annotator.domMapper.getInfoForNode normedRange.end
endOffset = endInfo.end
@annotator.domMapper.getCorpus()[startOffset .. endOffset-1].trim()
else
# Determine the current content of the given range directly
normedRange.text().trim()
# Look up the saved quote
savedQuote = @annotator.getQuoteForTarget? target
if savedQuote? and currentQuote isnt savedQuote
#console.log "Could not apply XPath selector to current document, " +
# "because the quote has changed. (Saved quote is '#{savedQuote}'." +
# " Current quote is '#{currentQuote}'.)"
return null
if @annotator.plugins.TextPosition
# Create a TextPositionAnchor from the start and end offsets
# of this range
# (to be used with dom-text-mapper)
new @Annotator.TextPositionAnchor @annotator, annotation, target,
startInfo.start, endInfo.end,
(startInfo.pageIndex ? 0), (endInfo.pageIndex ? 0),
currentQuote
else
# Create a TextRangeAnchor from this range
# (to be used whithout dom-text-mapper)
new TextRangeAnchor @annotator, annotation, target,
normedRange, currentQuote
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