substance
Version:
Substance is a JavaScript library for web-based content editing. It provides building blocks for realizing custom text editors and web-based publishing system. It is developed to power our online editing platform [Substance](http://substance.io).
98 lines (80 loc) • 1.84 kB
JavaScript
import isArray from '../util/isArray'
import last from '../util/last'
import _isDefined from '../util/_isDefined'
import Coordinate from './Coordinate'
/*
Internal helper class for schema reflection.
*/
export default class NodeProperty {
constructor (name, definition) {
this.name = name
this.definition = definition
Object.freeze(this)
Object.freeze(definition)
}
isArray () {
return isArray(this.definition.type)
}
isReference () {
if (this.isArray()) {
return last(this.definition.type) === 'id'
} else {
return this.definition.type === 'id'
}
}
isText () {
return this.definition.reflectionType === 'text'
}
isContainer () {
return Boolean(this.definition._isContainer)
}
isOwned () {
return Boolean(this.definition.owned)
}
isOptional () {
return this.definition.optional || this.hasDefault()
}
isNotNull () {
return Boolean(this.definition.notNull)
}
hasDefault () {
return _isDefined(this.definition.default)
}
getDefault () {
return this.definition.default
}
createDefaultValue () {
if (isArray(this.definition.type)) {
return []
}
switch (this.definition.type) {
case 'boolean':
return false
case 'string':
return ''
case 'number':
return -1
case 'object':
return {}
case 'coordinate':
return new Coordinate([], 0)
default:
return null
}
}
get type () {
return this.definition.type
}
get reflectionType () {
return this.definition.reflectionType || this.definition.type
}
get targetTypes () {
return this.definition.targetTypes
}
get defaultTextType () {
return this.definition.defaultTextType
}
get values () {
return this.definition.values
}
}