all files / model/ DocumentNode.js

59.18% Statements 29/49
25% Branches 3/12
55% Functions 11/20
59.18% Lines 29/49
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                                                                    5226×       5226× 5226×                 2733×                     18×                                     1680× 1680× 213×   1680×                                                                                                         47× 47×               47×                                                                 2637×             154×       854×                                                                                 47×        
import { EventEmitter } from '../util'
import DataNode from './Node'
 
/**
  Base node type for document nodes.
 
  @example
 
  The following example shows how a new node type is defined.
 
  ```js
  class Todo extends TextBlock {}
  Todo.schema = {
    type: 'todo',
    content: 'text',
    done: { type: 'bool', default: false }
  }
  ```
 
  The following data types are supported:
 
  - `string` bare metal string data type
  - `text` a string that carries annotations
  - `number` numeric values
  - `bool` boolean values
  - `id` a node id referencing another node in the document
*/
class DocumentNode extends DataNode {
 
  /**
    @param {Document} doc A document instance
    @param {object} node properties
  */
  constructor(doc, props) {
    super(doc, props)
  }
 
  _initialize(doc, props) {
    this.document = doc
    super._initialize(props)
  }
 
  /**
    Get the Document instance.
 
    @returns {Document}
  */
  getDocument() {
    return this.document
  }
 
  /**
    Whether this node has a parent.
 
    `parent` is a built-in property for implementing nested nodes.
 
    @returns {Boolean}
  */
  hasParent() {
    return Boolean(this.parent)
  }
 
  /**
    @returns {DocumentNode} the parent node
  */
  getParent() {
    return this.document.get(this.parent)
  }
 
  /**
    Get the root node.
 
    The root node is the last ancestor returned
    by a sequence of `getParent()` calls.
 
    @returns {DocumentNode}
  */
  getRoot() {
    let node = this
    while(node.parent) {
      node = node.parent
    }
    return node
  }
 
  /**
    Checks whether this node has children.
 
    @returns {Boolean} default: false
  */
  hasChildren() {
    return false
  }
 
  /**
    Get the index of a given child.
 
    @returns {Number} default: -1
  */
  getChildIndex(child) { // eslint-disable-line
    return -1
  }
 
  /**
    Get a child node at a given position.
 
    @returns {DocumentNode} default: null
  */
  getChildAt(idx) { // eslint-disable-line
    return null
  }
 
  /**
    Get the number of children nodes.
 
    @returns {Number} default: 0
  */
  getChildCount() {
    return 0
  }
 
  // TODO: should this really be here?
  // volatile property necessary to render highlighted node differently
  // TODO: We should get this out here
  setHighlighted(highlighted, scope) {
    if (this.highlighted !== highlighted) {
      this.highlightedScope = scope
      this.highlighted = highlighted
      this.emit('highlighted', highlighted)
    }
  }
 
  // Experimental: we are working on a simpler API replacing the
  // rather inconvenient EventProxy API.
  on(eventName, handler, ctx) {
    var match = _matchPropertyEvent(eventName)
    Iif (match) {
      var propertyName = match[1]
      if (this.constructor.schema[propertyName]) {
        var doc = this.getDocument()
        doc.getEventProxy('path')
          .on([this.id, propertyName], handler, ctx)
      }
    }
    EventEmitter.prototype.on.apply(this, arguments)
  }
 
  off(ctx, eventName, handler) {
    var doc = this.getDocument()
    var match = false
    Eif (!eventName) {
      doc.getEventProxy('path').off(ctx)
    } else {
      match = _matchPropertyEvent(eventName)
    }
    Iif (match) {
      var propertyName = match[1]
      doc.getEventProxy('path')
        .off(ctx, [this.id, propertyName], handler)
    }
    EventEmitter.prototype.off.apply(this, arguments)
  }
 
  _onPropertyChange(propertyName) {
    var args = [propertyName + ':changed']
      .concat(Array.prototype.slice.call(arguments, 1))
    this.emit.apply(this, args)
  }
 
  // Node categories
  // --------------------
 
  /**
    @returns {Boolean} true if node is a block node (e.g. Paragraph, Figure, List, Table)
  */
  isBlock() {
    return Boolean(this.constructor.isBlock)
  }
 
  /**
    @returns {Boolean} true if node is a text node (e.g. Paragraph, Codebock)
  */
  isText() {
    return Boolean(this.constructor.isText)
  }
 
  /**
    @returns {Boolean} true if node is an inline node (e.g. Citation)
  */
  isInline() {
    return Boolean(this.constructor.isInline)
  }
 
  isList() {
    return Boolean(this.constructor.isList)
  }
 
  isIsolatedNode() {
    return !this.isText() && !this.isList()
  }
 
}
 
DocumentNode.prototype._isDocumentNode = true
 
/**
  Declares a node to be treated as block-type node.
 
  BlockNodes are considers the direct descendant of `Container` nodes.
  @type {Boolean} default: false
*/
DocumentNode.isBlock = false
 
/**
  Declares a node to be treated as text-ish node.
 
  @type {Boolean} default: false
*/
DocumentNode.isText = false
 
/**
  Declares a node to be treated as {@link model/PropertyAnnotation}.
 
  @type {Boolean} default: false
*/
DocumentNode.isPropertyAnnotation = false
 
/**
  Declares a node to be treated as {@link model/ContainerAnnotation}.
 
  @type {Boolean} default: false
*/
DocumentNode.isContainerAnnotation = false
 
/**
  Declares a node to be treated as {@link model/InlineNode}.
 
  @type {Boolean} default: false
*/
DocumentNode.isInline = false
 
function _matchPropertyEvent(eventName) {
  return /([a-zA-Z_0-9]+):changed/.exec(eventName)
}
 
export default DocumentNode