all files / model/ PropertySelection.js

64.75% Statements 79/122
60% Branches 57/95
80% Functions 20/25
69.3% Lines 79/114
1 statement, 4 branches 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 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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368                                                        1015×   1015× 1008× 1008× 1008× 1008× 1008× 1008× 1008×     1015×       1015× 1015×           1015×   1015×           1015×       339×                                     296×                       2314×       28×       5350×       2198×       223×       14×                                                                         988×                             67× 67×   66×       65×                           18× 18×                     31× 31×     24× 24×     24×                     13× 13×       13×                     25× 25×       25×                                                                                                                                                                             285×        
import { isArrayEqual, isNumber } from '../util'
import Selection from './Selection'
import Coordinate from './Coordinate'
 
/**
  A selection which is bound to a property. Implements {@link model/Selection}.
 
  @example
 
  ```js
  var propSel = doc.createSelection({
    type: 'property',
    path: ['p1', 'content'],
    startOffset: 3,
    endOffset: 6
  })
*/
class PropertySelection extends Selection {
 
  /**
    @param {array} path
    @param {int} startOffset
    @param {int} endOffset
    @param {bool} reverse
    @param {string} [containerId]
    @param {string} [surfaceId]
  */
  constructor(path, startOffset, endOffset, reverse, containerId, surfaceId) {
    super()
 
    if (arguments.length === 1) {
      let data = arguments[0]
      path = data.path
      startOffset = data.startOffset
      endOffset = data.endOffset
      reverse = data.reverse
      containerId = data.containerId
      surfaceId = data.surfaceId
    }
 
    Iif (!path || !isNumber(startOffset)) {
      throw new Error('Invalid arguments: `path` and `startOffset` are mandatory');
    }
 
    this.start = new Coordinate(path, startOffset)
    this.end = new Coordinate(path, isNumber(endOffset) ? endOffset : startOffset)
 
    /**
      Selection direction.
      @type {Boolean}
    */
    this.reverse = Boolean(reverse)
 
    this.containerId = containerId
 
    /**
      Identifier of the surface this selection should be active in.
      @type {String}
    */
    this.surfaceId = surfaceId;
  }
 
  get path() {
    return this.start.path
  }
 
  get startOffset() {
    console.warn('DEPRECATED: Use sel.start.offset instead')
    return this.start.offset
  }
 
  get endOffset() {
    console.warn('DEPRECATED: Use sel.end.offset instead')
    return this.end.offset
  }
 
  /**
    Convert container selection to JSON.
 
    @returns {Object}
  */
  toJSON() {
    return {
      type: 'property',
      path: this.start.path,
      startOffset: this.start.offset,
      endOffset: this.end.offset,
      reverse: this.reverse,
      containerId: this.containerId,
      surfaceId: this.surfaceId
    }
  }
 
  isPropertySelection() {
    return true
  }
 
  getType() {
    return 'property'
  }
 
  isNull() {
    return false
  }
 
  isCollapsed() {
    return this.start.offset === this.end.offset;
  }
 
  isReverse() {
    return this.reverse
  }
 
  equals(other) {
    return (
      Selection.prototype.equals.call(this, other) &&
      (this.start.equals(other.start) && this.end.equals(other.end))
    )
  }
 
  toString() {
    /* istanbul ignore next */
    return [
      "PropertySelection(", JSON.stringify(this.path), ", ",
      this.start.offset, " -> ", this.end.offset,
      (this.reverse?", reverse":""),
      (this.surfaceId?(", "+this.surfaceId):""),
      ")"
    ].join('')
  }
 
  /**
    Collapse a selection to chosen direction.
 
    @param {String} direction either left of right
    @returns {PropertySelection}
  */
  collapse(direction) {
    var offset
    Eif (direction === 'left') {
      offset = this.start.offset;
    } else {
      offset = this.end.offset;
    }
    return this.createWithNewRange(offset, offset)
  }
 
  // Helper Methods
  // ----------------------
 
  /**
    Get path of a selection, e.g. target property where selected data is stored.
 
    @returns {String[]} path
  */
  getPath() {
    return this.start.path;
  }
 
  getNodeId() {
    return this.start.path[0];
  }
 
  /**
    Checks if this selection is inside another one.
 
    @param {Selection} other
    @param {Boolean} [strict] true if should check that it is strictly inside the other
    @returns {Boolean}
  */
  isInsideOf(other, strict) {
    Iif (other.isNull()) return false
    if (other.isContainerSelection()) {
      return other.contains(this, strict)
    }
    if (strict) {
      return (isArrayEqual(this.path, other.path) &&
        this.start.offset > other.start.offset &&
        this.end.offset < other.end.offset);
    } else {
      return (isArrayEqual(this.path, other.path) &&
        this.start.offset >= other.start.offset &&
        this.end.offset <= other.end.offset);
    }
  }
 
  /**
    Checks if this selection contains another one.
 
    @param {Selection} other
    @param {Boolean} [strict] true if should check that it is strictly contains the other
    @returns {Boolean}
  */
  contains(other, strict) {
    Iif (other.isNull()) return false
    return other.isInsideOf(this, strict)
  }
 
  /**
    Checks if this selection overlaps another one.
 
    @param {Selection} other
    @param {Boolean} [strict] true if should check that it is strictly overlaps the other
    @returns {Boolean}
  */
  overlaps(other, strict) {
    Iif (other.isNull()) return false
    if (other.isContainerSelection()) {
      // console.log('PropertySelection.overlaps: delegating to ContainerSelection.overlaps...')
      return other.overlaps(this)
    }
    Iif (!isArrayEqual(this.path, other.path)) return false
    Iif (strict) {
      return (! (this.start.offset>=other.end.offset||this.end.offset<=other.start.offset) );
    } else {
      return (! (this.start.offset>other.end.offset||this.end.offset<other.start.offset) );
    }
  }
 
  /**
    Checks if this selection has the right boundary in common with another one.
 
    @param {Selection} other
    @returns {Boolean}
  */
  isRightAlignedWith(other) {
    Iif (other.isNull()) return false
    Iif (other.isContainerSelection()) {
      // console.log('PropertySelection.isRightAlignedWith: delegating to ContainerSelection.isRightAlignedWith...')
      return other.isRightAlignedWith(this)
    }
    return (isArrayEqual(this.path, other.path) &&
      this.end.offset === other.end.offset);
  }
 
  /**
    Checks if this selection has the left boundary in common with another one.
 
    @param {Selection} other
    @returns {Boolean}
  */
  isLeftAlignedWith(other) {
    Iif (other.isNull()) return false
    Iif (other.isContainerSelection()) {
      // console.log('PropertySelection.isLeftAlignedWith: delegating to ContainerSelection.isLeftAlignedWith...')
      return other.isLeftAlignedWith(this)
    }
    return (isArrayEqual(this.path, other.path) &&
      this.start.offset === other.start.offset);
  }
 
  /**
    Expands selection to include another selection.
 
    @param {Selection} other
    @returns {Selection} a new selection
  */
  expand(other) {
    Iif (other.isNull()) return this
 
    // if the other is a ContainerSelection
    // we delegate to that implementation as it is more complex
    // and can deal with PropertySelections, too
    Iif (other.isContainerSelection()) {
      return other.expand(this)
    }
    Iif (!isArrayEqual(this.path, other.path)) {
      throw new Error('Can not expand PropertySelection to a different property.')
    }
    var newStartOffset = Math.min(this.start.offset, other.start.offset);
    var newEndOffset = Math.max(this.end.offset, other.end.offset);
    return this.createWithNewRange(newStartOffset, newEndOffset);
  }
 
  /**
    Creates a new selection by truncating this one by another selection.
 
    @param {Selection} other
    @returns {Selection} a new selection
  */
  truncateWith(other) {
    Iif (other.isNull()) return this
    Iif (other.isInsideOf(this, 'strict')) {
      // the other selection should overlap only on one side
      throw new Error('Can not truncate with a contained selections')
    }
    Iif (!this.overlaps(other)) {
      return this
    }
    var otherStartOffset, otherEndOffset
    Eif (other.isPropertySelection()) {
      otherStartOffset = other.start.offset;
      otherEndOffset = other.end.offset;
    } else if (other.isContainerSelection()) {
      // either the startPath or the endPath must be the same
      if (isArrayEqual(other.start.path, this.start.path)) {
        otherStartOffset = other.start.offset;
      } else {
        otherStartOffset = this.start.offset;
      }
      if (isArrayEqual(other.end.path, this.start.path)) {
        otherEndOffset = other.end.offset;
      } else {
        otherEndOffset = this.end.offset;
      }
    } else {
      return this
    }
 
    var newStartOffset;
    var newEndOffset;
    Iif (this.start.offset > otherStartOffset && this.end.offset > otherEndOffset) {
      newStartOffset = otherEndOffset;
      newEndOffset = this.end.offset;
    } else Iif (this.start.offset < otherStartOffset && this.end.offset < otherEndOffset) {
      newStartOffset = this.start.offset;
      newEndOffset = otherStartOffset;
    } else Iif (this.start.offset === otherStartOffset) {
      if (this.end.offset <= otherEndOffset) {
        return Selection.nullSelection;
      } else {
        newStartOffset = otherEndOffset;
        newEndOffset = this.end.offset;
      }
    } else Eif (this.end.offset === otherEndOffset) {
      Iif (this.start.offset >= otherStartOffset) {
        return Selection.nullSelection;
      } else {
        newStartOffset = this.start.offset;
        newEndOffset = otherStartOffset;
      }
    } else if (other.contains(this)) {
      return Selection.nullSelection
    } else {
      // FIXME: if this happens, we have a bug somewhere above
      throw new Error('Illegal state.')
    }
    return this.createWithNewRange(newStartOffset, newEndOffset)
  }
 
  /**
    Creates a new selection with given range and same path.
 
    @param {Number} startOffset
    @param {Number} endOffset
    @returns {Selection} a new selection
  */
  createWithNewRange(startOffset, endOffset) {
    var sel = new PropertySelection(this.path, startOffset, endOffset, false, this.containerId, this.surfaceId)
    var doc = this._internal.doc
    Eif (doc) {
      sel.attach(doc)
    }
    return sel
  }
 
  _clone() {
    return new PropertySelection(this.start.path, this.start.offset, this.end.offset, this.reverse, this.containerId, this.surfaceId);
  }
 
}
 
PropertySelection.fromJSON = function(json) {
  return new PropertySelection(json)
}
 
export default PropertySelection