2016-06-18 18:21:21 -07:00
|
|
|
|
2016-06-23 23:39:08 -07:00
|
|
|
/**
|
|
|
|
* Prevent circuit.
|
|
|
|
*/
|
|
|
|
|
|
|
|
import './block'
|
|
|
|
import './inline'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Dependencies.
|
|
|
|
*/
|
|
|
|
|
2016-06-23 10:43:36 -07:00
|
|
|
import Block from './block'
|
2016-06-18 18:21:21 -07:00
|
|
|
import Node from './node'
|
2016-08-17 02:19:13 -07:00
|
|
|
import uid from '../utils/uid'
|
2016-06-18 18:21:21 -07:00
|
|
|
import { OrderedMap, Record } from 'immutable'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defaults.
|
|
|
|
*/
|
|
|
|
|
2016-06-19 11:51:50 -07:00
|
|
|
const DEFAULTS = {
|
2016-08-17 02:19:13 -07:00
|
|
|
key: null,
|
|
|
|
nodes: new OrderedMap(),
|
2016-06-18 18:21:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Document.
|
|
|
|
*/
|
|
|
|
|
2016-07-06 20:19:19 -07:00
|
|
|
class Document extends new Record(DEFAULTS) {
|
2016-06-18 18:21:21 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a new `Document` with `properties`.
|
|
|
|
*
|
|
|
|
* @param {Objetc} properties
|
|
|
|
* @return {Document} document
|
|
|
|
*/
|
|
|
|
|
|
|
|
static create(properties = {}) {
|
2016-06-23 10:43:36 -07:00
|
|
|
if (properties instanceof Document) return properties
|
2016-08-17 02:19:13 -07:00
|
|
|
|
|
|
|
properties.key = properties.key || uid(4)
|
2016-06-23 11:32:24 -07:00
|
|
|
properties.nodes = Block.createList(properties.nodes)
|
2016-08-17 02:19:13 -07:00
|
|
|
|
2016-06-23 11:32:24 -07:00
|
|
|
return new Document(properties).normalize()
|
2016-06-21 16:44:11 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the node's kind.
|
|
|
|
*
|
|
|
|
* @return {String} kind
|
|
|
|
*/
|
|
|
|
|
|
|
|
get kind() {
|
|
|
|
return 'document'
|
2016-06-18 18:21:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-07-27 16:21:55 -07:00
|
|
|
* Is the document empty?
|
|
|
|
*
|
|
|
|
* @return {Boolean} isEmpty
|
|
|
|
*/
|
|
|
|
|
|
|
|
get isEmpty() {
|
|
|
|
return this.text == ''
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the length of the concatenated text of the document.
|
2016-06-19 11:51:50 -07:00
|
|
|
*
|
|
|
|
* @return {Number} length
|
2016-06-18 18:21:21 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
get length() {
|
|
|
|
return this.text.length
|
|
|
|
}
|
|
|
|
|
2016-06-19 11:51:50 -07:00
|
|
|
/**
|
|
|
|
* Get the concatenated text `string` of all child nodes.
|
|
|
|
*
|
|
|
|
* @return {String} text
|
|
|
|
*/
|
|
|
|
|
2016-06-18 18:21:21 -07:00
|
|
|
get text() {
|
2016-06-19 12:12:23 -07:00
|
|
|
return this.nodes
|
2016-06-18 18:21:21 -07:00
|
|
|
.map(node => node.text)
|
|
|
|
.join('')
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-06-19 11:51:50 -07:00
|
|
|
* Mix in `Node` methods.
|
2016-06-18 18:21:21 -07:00
|
|
|
*/
|
|
|
|
|
2016-06-19 11:51:50 -07:00
|
|
|
for (const method in Node) {
|
|
|
|
Document.prototype[method] = Node[method]
|
|
|
|
}
|
2016-06-18 18:21:21 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Export.
|
|
|
|
*/
|
|
|
|
|
|
|
|
export default Document
|