all files / model/ Container.js

81.61% Statements 71/87
72.92% Branches 35/48
77.78% Functions 21/27
82.35% Lines 70/85
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219                            898×         898×       68×       1136×       2414×       20×       1085× 532×   1085× 1085× 1085×     1085×       629× 629×     629×       309× 933×       1027× 1027× 1027× 71× 71×     1027× 894×   1027×       19× 19× 19× 19×         329×       329× 329× 329× 329× 20×     20×     309×   329×       1458×             1085× 1085×             1085× 1085× 1085× 1085× 1067×   18× 18×   1085×       399× 399× 1215×   399× 399×       1288×       18× 18×   18×           898× 898× 898×         7778× 1935× 1288×                                                             145× 145×     145×                              
import { isNumber, isString } from '../util'
import DocumentNode from './DocumentNode'
import ContainerAddress from './ContainerAddress'
 
/*
  A Container represents a list of nodes.
 
  While most editing occurs on a property level (such as editing text),
  other things happen on a node level, e.g., breaking or mergin nodes,
  or spanning annotations so called ContainerAnnotations.
*/
class Container extends DocumentNode {
 
  constructor(...args) {
    super(...args)
 
    // NOTE: we are caching positions as they are queried very often,
    // whereas the number of changes to a container are quite rare.
    // The cache gets invalidated whenever the container is changed.
    this._enableCaching()
  }
 
  dispose() {
    this.document.off(this)
  }
 
  getContentPath() {
    return [this.id, 'nodes']
  }
 
  getContent() {
    return this.nodes
  }
 
  contains(nodeId) {
    return this.getPosition(nodeId) >= 0
  }
 
  getPosition(node, strict) {
    if (isString(node)) {
      node = this.document.get(node)
    }
    Iif (!node) return -1
    let pos = this._getPosition(node)
    Iif (strict && pos < 0) {
      throw new Error('Node is not within this container: ' + node.id)
    }
    return pos
  }
 
  getNodeAt(idx) {
    let content = this.getContent()
    Iif (idx < 0 || idx >= content.length) {
      throw new Error('Array index out of bounds: ' + idx + ", " + content.length)
    }
    return this.getDocument().get(content[idx])
  }
 
  getNodes() {
    const doc = this.getDocument()
    return this.getContent().map(id => doc.get(id)).filter(Boolean)
  }
 
  show(nodeId, pos) {
    var doc = this.getDocument()
    var arg1 = arguments[0]
    if (!isString(arg1)) {
      Eif (arg1._isNode) {
        nodeId = arg1.id
      }
    }
    if (!isNumber(pos)) {
      pos = this.getLength()
    }
    doc.update(this.getContentPath(), { type: 'insert', pos: pos, value: nodeId })
  }
 
  hide(nodeId) {
    var doc = this.getDocument()
    var pos = this.getPosition(nodeId)
    Eif (pos >= 0) {
      doc.update(this.getContentPath(), { type: 'delete', pos: pos })
    }
  }
 
  getAddress(coor) {
    Iif (!coor._isCoordinate) {
      // we have broken with an earlier version of this API
      throw new Error('Illegal argument: Container.getAddress(coor) expects a Coordinate instance.')
    }
    var nodeId = coor.path[0]
    var nodePos = this.getPosition(nodeId)
    var offset
    if (coor.isNodeCoordinate()) {
      Iif (coor.offset > 0) {
        offset = Number.MAX_VALUE
      } else {
        offset = 0
      }
    } else {
      offset = coor.offset
    }
    return new ContainerAddress(nodePos, offset)
  }
 
  getLength() {
    return this.getContent().length
  }
 
  get length() {
    return this.getLength()
  }
 
  _getPosition(node) {
    Eif (this._isCaching) {
      return this._getCachedPosition(node)
    } else {
      return this._lookupPosition(node)
    }
  }
 
  _getCachedPosition(node) {
    let cache = this._cachedPositions || this._fillCache()
    let nodeId = node.id
    let pos = -1
    if (cache.hasOwnProperty(nodeId)) {
      pos = cache[nodeId]
    } else {
      pos = this._lookupPosition(node)
      cache[nodeId] = pos
    }
    return pos
  }
 
  _fillCache() {
    let positions = {}
    this.nodes.forEach((id, pos) => {
      positions[id] = pos
    })
    this._cachedPositions = positions
    return positions
  }
 
  _invalidateCache() {
    this._cachedPositions = null
  }
 
  _lookupPosition(node) {
    Eif (node.hasParent()) {
      node = node.getRoot()
    }
    return this.getContent().indexOf(node.id)
  }
 
  _enableCaching() {
    // this hook is used to invalidate cached positions
    // caching is done only in the 'real' document, not in a TransactionDocument
    Eif (this.document) {
      this.document.data.on('operation:applied', this._onOperationApplied, this)
      this._isCaching = true
    }
  }
 
  _onOperationApplied(op) {
    if (op.type === 'set' || op.type === 'update') {
      if (op.path[0] === this.id) {
        this._invalidateCache()
      }
    }
  }
 
  _onDocumentChange(change) {
    if (change.hasUpdated(this.getContentPath())) {
      this._invalidateCache()
    }
  }
 
  // NOTE: this has been in ParentNodeMixin before
  // TODO: try to get rid of this
 
  hasChildren() {
    return this.nodes.length > 0
  }
 
  getChildIndex(child) {
    return this.nodes.indexOf(child.id)
  }
 
  getChildren() {
    var doc = this.getDocument()
    var childrenIds = this.nodes
    return childrenIds.map(function(id) {
      return doc.get(id)
    })
  }
 
  getChildAt(idx) {
    var childrenIds = this.nodes
    Iif (idx < 0 || idx >= childrenIds.length) {
      throw new Error('Array index out of bounds: ' + idx + ", " + childrenIds.length)
    }
    return this.getDocument().get(childrenIds[idx])
  }
 
  getChildCount() {
    return this.nodes.length
  }
 
}
 
Container.prototype._isContainer = true
 
Container.schema = {
  type: 'container',
  nodes: { type: ['array', 'id'], default: [] }
}
 
export default Container