all files / model/ Schema.js

53.57% Statements 15/28
40% Branches 4/10
54.55% Functions 6/11
55.56% Lines 15/27
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                                      363×       363×         363×         363×     363×                 726× 726× 8349×     8349×           8349× 8349×                       10348×                                         75× 75× 75×                                                                                    
import { forEach } from '../util'
import NodeRegistry from './NodeRegistry'
import Node from './Node'
 
/**
  Schema for Data Objects.
 
  @internal
 */
class Schema {
 
  /**
    @param {String} name
    @param {String} version
  */
  constructor(name, version) {
    /**
      @type {String}
    */
    this.name = name
    /**
      @type {String}
    */
    this.version = version
    /**
      @type {NodeRegistry}
      @private
    */
    this.nodeRegistry = new NodeRegistry()
    /**
      @type {Array} all Node classes which have `Node.tocType = true`
      @private
    */
    this.tocTypes = []
 
    // add built-in node classes
    this.addNodes(this.getBuiltIns())
  }
 
  /**
    Add nodes to the schema.
 
    @param {Array} nodes Array of Node classes
  */
  addNodes(nodes) {
    Iif (!nodes) return
    forEach(nodes, function(NodeClass) {
      Iif (!NodeClass.prototype._isNode) {
        console.error('Illegal node class: ', NodeClass)
      } else {
        this.addNode(NodeClass)
      }
    }.bind(this))
  }
 
  addNode(NodeClass) {
    this.nodeRegistry.register(NodeClass)
    Iif (NodeClass.tocType) {
      this.tocTypes.push(NodeClass.type)
    }
  }
 
  /**
    Get the node class for a type name.
 
    @param {String} name
    @returns {Class}
  */
  getNodeClass(name) {
    return this.nodeRegistry.get(name)
  }
 
  /**
    Provide all built-in node classes.
 
    @private
    @returns {Node[]} An array of Node classes.
  */
  getBuiltIns() {
    return []
  }
 
  /**
    Checks if a given type is of given parent type.
 
    @param {String} type
    @param {String} parentType
    @returns {Boolean} true if type is and instance of parentType.
  */
  isInstanceOf(type, parentType) {
    var NodeClass = this.getNodeClass(type)
    Eif (NodeClass) {
      return Node.isInstanceOf(NodeClass, parentType)
    }
    return false
  }
 
  /**
    Iterate over all registered node classes.
 
    See {@link util/Registry#each}
 
    @param {Function} callback
    @param {Object} context
  */
  each() {
    this.nodeRegistry.each.apply(this.nodeRegistry, arguments)
  }
 
  /**
    @returns {Node[]} list of types that should appear in a TOC
  */
  getTocTypes() {
    return this.tocTypes
  }
 
  /**
    @returns {String} the name of the default textish node (e.g. 'paragraph')
  */
  getDefaultTextType() {
    throw new Error('Schmema.prototype.getDefaultTextType() must be overridden.')
  }
 
  getNodeSchema(type) {
    var NodeClass = this.getNodeClass(type)
    if (!NodeClass) {
      console.error('Unknown node type ', type)
      return null
    }
    return NodeClass.schema
  }
}
 
export default Schema