all files / model/ ContainerAnnotation.js

52.17% Statements 12/23
50% Branches 7/14
50% Functions 2/4
52.17% Lines 12/23
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                                                                          14×   14×       14×                                                                                
import { isEqual, forEach } from '../util'
import Annotation from './Annotation'
import Selection from './Selection'
 
/*
  Describes an annotation sticking on a container that can span over multiple
  nodes.
 
  @class
 
  @example
 
  ```js
  {
    "id": "subject_reference_1",
    "type": "subject_reference",
    "containerId": "content",
    "start": {
      "path": ["text_2", "content"],
      "offset": 100,
    },
    "end": {
      "path": ["text_4", "content"],
      "offset": 40
    }
  }
  ```
 */
 
class ContainerAnnotation extends Annotation {
 
  /**
    Provides a selection which has the same range as this annotation.
 
    @return {model/ContainerSelection}
  */
  getSelection() {
    var doc = this.getDocument()
    // Guard: when this is called while this node has been detached already.
    Iif (!doc) {
      console.warn('Trying to use a ContainerAnnotation which is not attached to the document.')
      return Selection.nullSelection()
    }
    return doc.createSelection({
      type: "container",
      containerId: this.containerId,
      startPath: this.start.path,
      startOffset: this.start.offset,
      endPath: this.end.path,
      endOffset: this.end.offset
    })
  }
 
  setHighlighted(highlighted, scope) {
    if (this.highlighted !== highlighted) {
      this.highlighted = highlighted
      this.highlightedScope = scope
      this.emit('highlighted', highlighted, scope)
      forEach(this.fragments, function(frag) {
        frag.emit('highlighted', highlighted, scope)
      })
    }
  }
 
  _updateRange(tx, sel) {
    Iif (!sel.isContainerSelection()) {
      throw new Error('Invalid argument.')
    }
    // TODO: use coordinate ops
    Iif (!isEqual(this.start.path, sel.start.path)) {
      tx.set([this.id, 'start', 'path'], sel.start.path)
    }
    Iif (this.start.offset !== sel.start.offset) {
      tx.set([this.id, 'start', 'offset'], sel.start.offset)
    }
    if (!isEqual(this.end.path, sel.end.path)) {
      tx.set([this.id, 'end', 'path'], sel.end.path)
    }
    Eif (this.end.offset !== sel.end.offset) {
      tx.set([this.id, 'end', 'offset'], sel.end.offset)
    }
  }
}
 
ContainerAnnotation.schema = {
  type: "container-annotation",
  containerId: "string"
}
 
ContainerAnnotation.prototype._isContainerAnnotation = true
 
export default ContainerAnnotation