2016-06-19 11:51:50 -07:00
|
|
|
|
2016-06-23 23:39:08 -07:00
|
|
|
/**
|
|
|
|
* Prevent circuit.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import './document'
|
|
|
|
import './inline'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dependencies.
|
|
|
|
*/
|
|
|
|
|
2016-06-23 10:43:36 -07:00
|
|
|
import Data from './data'
|
|
|
|
import Inline from './inline'
|
2016-06-19 11:51:50 -07:00
|
|
|
import Node from './node'
|
2016-06-23 10:43:36 -07:00
|
|
|
import Text from './text'
|
2016-06-27 14:08:30 -07:00
|
|
|
import uid from '../utils/uid'
|
2016-06-23 11:32:24 -07:00
|
|
|
import Immutable, { Map, List, Record } from 'immutable'
|
2016-06-19 11:51:50 -07:00
|
|
|
|
|
|
|
/**
|
2016-06-21 16:44:11 -07:00
|
|
|
* Default properties.
|
2016-06-19 11:51:50 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
const DEFAULTS = {
|
|
|
|
data: new Map(),
|
2016-06-30 11:13:56 -07:00
|
|
|
isVoid: false,
|
2016-06-19 11:51:50 -07:00
|
|
|
key: null,
|
2016-06-23 11:32:24 -07:00
|
|
|
nodes: new List(),
|
2016-06-19 11:51:50 -07:00
|
|
|
type: null
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-21 16:44:11 -07:00
|
|
|
* Block.
|
2016-06-19 11:51:50 -07:00
|
|
|
*/
|
|
|
|
|
2016-07-06 20:19:19 -07:00
|
|
|
class Block extends new Record(DEFAULTS) {
|
2016-06-19 11:51:50 -07:00
|
|
|
|
|
|
|
/**
|
2016-06-21 16:44:11 -07:00
|
|
|
* Create a new `Block` with `properties`.
|
2016-06-19 11:51:50 -07:00
|
|
|
*
|
|
|
|
* @param {Object} properties
|
2016-06-21 16:44:11 -07:00
|
|
|
* @return {Block} element
|
2016-06-19 11:51:50 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
static create(properties = {}) {
|
2016-06-23 10:43:36 -07:00
|
|
|
if (properties instanceof Block) return properties
|
|
|
|
if (properties instanceof Inline) return properties
|
|
|
|
if (properties instanceof Text) return properties
|
2016-06-21 16:44:11 -07:00
|
|
|
if (!properties.type) throw new Error('You must pass a block `type`.')
|
2016-06-23 10:43:36 -07:00
|
|
|
|
2016-07-24 18:57:09 -07:00
|
|
|
properties.key = properties.key || uid(4)
|
2016-06-23 10:43:36 -07:00
|
|
|
properties.data = Data.create(properties.data)
|
2016-07-06 20:19:19 -07:00
|
|
|
properties.isVoid = !!properties.isVoid
|
2016-06-23 11:32:24 -07:00
|
|
|
properties.nodes = Block.createList(properties.nodes)
|
2016-06-23 10:43:36 -07:00
|
|
|
|
2016-06-23 11:32:24 -07:00
|
|
|
return new Block(properties).normalize()
|
2016-06-19 11:51:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-23 11:32:24 -07:00
|
|
|
* Create a list of `Blocks` from an array.
|
2016-06-19 11:51:50 -07:00
|
|
|
*
|
|
|
|
* @param {Array} elements
|
2016-06-23 11:32:24 -07:00
|
|
|
* @return {List} list
|
2016-06-19 11:51:50 -07:00
|
|
|
*/
|
|
|
|
|
2016-06-23 11:32:24 -07:00
|
|
|
static createList(elements = []) {
|
|
|
|
if (List.isList(elements)) return elements
|
|
|
|
return new List(elements.map(Block.create))
|
2016-06-19 11:51:50 -07:00
|
|
|
}
|
|
|
|
|
2016-06-21 16:44:11 -07:00
|
|
|
/**
|
|
|
|
* Get the node's kind.
|
|
|
|
*
|
|
|
|
* @return {String} kind
|
|
|
|
*/
|
|
|
|
|
|
|
|
get kind() {
|
|
|
|
return 'block'
|
|
|
|
}
|
|
|
|
|
2016-07-24 17:52:22 -07:00
|
|
|
/**
|
|
|
|
* Is the node empty?
|
|
|
|
*
|
|
|
|
* @return {Boolean} isEmpty
|
|
|
|
*/
|
|
|
|
|
|
|
|
get isEmpty() {
|
|
|
|
return (
|
|
|
|
this.nodes.size == 1 &&
|
|
|
|
this.nodes.first().kind == 'text' &&
|
|
|
|
this.length == 0
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2016-06-19 11:51:50 -07:00
|
|
|
/**
|
|
|
|
* Get the length of the concatenated text of the node.
|
|
|
|
*
|
|
|
|
* @return {Number} length
|
|
|
|
*/
|
|
|
|
|
|
|
|
get length() {
|
|
|
|
return this.text.length
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the concatenated text `string` of all child nodes.
|
|
|
|
*
|
|
|
|
* @return {String} text
|
|
|
|
*/
|
|
|
|
|
|
|
|
get text() {
|
2016-06-19 12:12:23 -07:00
|
|
|
return this.nodes
|
2016-06-19 11:51:50 -07:00
|
|
|
.map(node => node.text)
|
|
|
|
.join('')
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Mix in `Node` methods.
|
|
|
|
*/
|
|
|
|
|
|
|
|
for (const method in Node) {
|
2016-06-21 16:44:11 -07:00
|
|
|
Block.prototype[method] = Node[method]
|
2016-06-19 11:51:50 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export.
|
|
|
|
*/
|
|
|
|
|
2016-06-21 16:44:11 -07:00
|
|
|
export default Block
|