all files / model/ SelectionState.js

88.89% Statements 64/72
84.62% Branches 22/26
65% Functions 13/20
88.89% Lines 64/72
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                  209×   209× 209× 209×         560×   556×       560× 560× 560×       2754×       1765× 1765× 1560×   205×                 2453×                                                           202×         155×     155×       560× 560× 560× 560× 560×           560× 560× 560× 519× 519× 519× 519× 519× 519× 519× 73×   519× 519× 519× 503× 503×   16× 16×   519× 336×   519×         560× 560×     560× 560× 560× 31×     560×     560× 560× 519× 519×       560×       560× 560× 560× 560×       769×                               769×          
import { TreeIndex } from '../util'
import Selection from './Selection'
import documentHelpers from './documentHelpers'
import { isFirst, isLast } from './selectionHelpers'
// import printStacktrace from '../util/printStacktrace'
 
class SelectionState {
 
  constructor(doc) {
    this.document = doc
 
    this.selection = Selection.nullSelection
    this._state = {}
    this._resetState()
  }
 
  setSelection(sel) {
    // printStacktrace()
    if (!sel) {
      sel = Selection.nullSelection
    } else {
      sel.attach(this.document)
    }
    // TODO: selection state is selection plus derived state,
    // thus we need to return false only if both did not change
    this._deriveState(sel)
    this.selection = sel
    return true
  }
 
  getSelection() {
    return this.selection
  }
 
  getAnnotationsForType(type) {
    const state = this._state
    if (state.annosByType) {
      return state.annosByType.get(type) || []
    }
    return []
  }
 
  getMarkers() {
    // returns markers under the current selection
    return this._state.markers || []
  }
 
  isInlineNodeSelection() {
    return this._state.isInlineNodeSelection
  }
 
  getContainer() {
    return this._state.container
  }
 
  getPreviousNode() {
    return this._state.previousNode
  }
 
  getNextNode() {
    return this._state.nextNode
  }
 
  /*
    @returns if the previous node is one char away
  */
  isFirst() {
    return Boolean(this._state.isFirst)
  }
 
  /*
    @returns if the next node is one char away
  */
  isLast() {
    return Boolean(this._state.isLast)
  }
 
  get(key) {
    return this._state[key]
  }
 
  // used to store custom states (e.g. IsolatedNodeComponent uses this)
  set(key, value) {
    Iif (this._state[key]) {
      throw new Error(`State ${key} is already set`)
    }
    this._state[key] = value
  }
 
  _deriveState(sel) {
    this._resetState()
    this._deriveContainerSelectionState(sel)
    this._deriveAnnoState(sel)
    Eif (this.document.getIndex('markers')) {
      this._deriveMarkerState(sel)
    }
    // console.log('SelectionState:', this._state)
  }
 
  _deriveContainerSelectionState(sel) {
    let state = this._state
    let doc = this.document
    if (sel.containerId) {
      let container = doc.get(sel.containerId)
      state.container = container
      let startId = sel.start.getNodeId()
      let endId = sel.end.getNodeId()
      let startNode = doc.get(startId).getRoot()
      let startPos = container.getPosition(startNode)
      if (startPos > 0) {
        state.previousNode = container.getNodeAt(startPos-1)
      }
      state.isFirst = isFirst(doc, sel.start)
      let endNode, endPos
      if (endId === startId) {
        endNode = startNode
        endPos = startPos
      } else {
        endNode = doc.get(endId).getRoot()
        endPos = container.getPosition(endNode)
      }
      if (endPos < container.getLength()-1) {
        state.nextNode = container.getNodeAt(endPos+1)
      }
      state.isLast = isLast(doc, sel.end)
    }
  }
 
  _deriveAnnoState(sel) {
    const doc = this.document
    const state = this._state
 
    // create a mapping by type for the currently selected annotations
    let annosByType = new TreeIndex.Arrays()
    const propAnnos = documentHelpers.getPropertyAnnotationsForSelection(doc, sel)
    propAnnos.forEach(function(anno) {
      annosByType.add(anno.type, anno)
    })
 
    if (propAnnos.length === 1 && propAnnos[0].isInline()) {
      state.isInlineNodeSelection = propAnnos[0].getSelection().equals(sel)
    }
 
    const containerId = sel.containerId
    if (containerId) {
      const containerAnnos = documentHelpers.getContainerAnnotationsForSelection(doc, sel, containerId)
      containerAnnos.forEach(function(anno) {
        annosByType.add(anno.type, anno)
      })
    }
    state.annosByType = annosByType
  }
 
  _deriveMarkerState(sel) {
    const doc = this.document
    let state = this._state
    let markers = documentHelpers.getMarkersForSelection(doc, sel)
    state.markers = markers
  }
 
  _resetState() {
    this._state = {
      // all annotations under the current selection
      annosByType: null,
      // markers under the current selection
      markers: null,
      // flags for inline nodes
      isInlineNodeSelection: false,
      // container information (only for ContainerSelection)
      container: null,
      previousNode: null,
      nextNode: null,
      // if the previous node is one char away
      isFirst: false,
      // if the next node is one char away
      isLast: false
    }
    return this._state
  }
}
 
export default SelectionState