all files / model/ Annotation.js

91.49% Statements 43/47
78.57% Branches 11/14
100% Functions 13/13
91.49% Lines 43/47
16 statements, 8 functions Ignored     
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144              2107×     2107× 2107×                                                                                                                             123×                   2107×               12× 12×       12× 12×     12×   12× 12×       12× 12× 12× 12× 12× 2095× 693×   2107×        
import DocumentNode from './DocumentNode'
import Coordinate from './Coordinate'
import documentHelpers from './documentHelpers'
 
class Annotation extends DocumentNode {
 
  constructor(doc, props) {
    super(doc, _normalizedProps(props))
 
    // wrap coordinates
    this.start = new Coordinate(this.start)
    this.end = new Coordinate(this.end)
  }
 
  /* istanbul ignore start */
 
  get startPath() {
    console.warn('DEPRECATED: use Annotation.start.path instead.')
    return this.start.path
  }
 
  set startPath(path) {
    console.warn('DEPRECATED: use Annotation.start.path instead.')
    this.start.path = path
  }
 
  get startOffset() {
    console.warn('DEPRECATED: use Annotation.start.offset instead.')
    return this.start.offset
  }
 
  set startOffset(offset) {
    console.warn('DEPRECATED: use Annotation.start.offset instead.')
    this.start.offset = offset
  }
 
  get endPath() {
    console.warn('DEPRECATED: use Annotation.end.path instead.')
    return this.end.path
  }
 
  set endPath(path) {
    console.warn('DEPRECATED: use Annotation.end.path instead.')
    this.end.path = path
  }
 
  get endOffset() {
    console.warn('DEPRECATED: use Annotation.end.offset instead.')
    return this.end.offset
  }
 
  set endOffset(offset) {
    console.warn('DEPRECATED: use Annotation.end.offset instead.')
    this.end.offset = offset
  }
 
  /* istanbul ignore end */
 
  /**
    Get the plain text spanned by this annotation.
 
    @return {String}
  */
  getText() {
    var doc = this.getDocument()
    Iif (!doc) {
      console.warn('Trying to use a Annotation which is not attached to the document.')
      return ""
    }
    return documentHelpers.getTextForSelection(doc, this.getSelection())
  }
 
  /**
    Determines if an annotation can be split e.g., when breaking a node.
 
    In these cases, a new annotation will be created attached to the created node.
 
    For certain annotation types,you may want to the annotation truncated
    rather than split, where you need to override this method returning `false`.
  */
  canSplit() {
    return true
  }
 
  /**
    If this annotation is an Anchor.
 
    Anchors are annotations with a zero width.
    For instance, ContainerAnnotation have a start and an end anchor,
    or rendered cursors are modeled as anchors.
 
    @returns {Boolean}
  */
  isAnchor() {
    return false
  }
}
 
Annotation.define({
  type: "annotation",
  start: "coordinate",
  end: "coordinate"
})
 
Annotation.prototype._isAnnotation = true
 
function _normalizedProps(props) {
  if (!props.hasOwnProperty('start')) {
    /*
      Instead of
        { path: [...], startOffset: 0, endOffset: 10 }
      use
        { start: { path: [], offset: 0 }, end: { path: [], offset: 10 } }
    */
    // console.warn('DEPRECATED: create Annotation with "start" and "end" coordinate instead.')
    props = Object.assign({}, props)
    props.start = {
      path: props.startPath || props.path,
      offset: props.startOffset
    }
    props.end = {}
    Iif (props.hasOwnProperty('endPath')) {
      props.end.path = props.endPath
    } else {
      props.end.path = props.start.path
    }
    Eif (props.hasOwnProperty('endOffset')) {
      props.end.offset = props.endOffset
    } else {
      props.end.offset = props.start.offset
    }
    delete props.path
    delete props.startPath
    delete props.endPath
    delete props.startOffset
    delete props.endOffset
  } else if (props.hasOwnProperty('end') && !props.end.path) {
    props.end.path = props.start.path
  }
  return props
}
 
export default Annotation