1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-25 01:33:37 +01:00
slate/lib/models/document.js
2016-06-23 11:32:24 -07:00

80 lines
1.2 KiB
JavaScript

import Block from './block'
import Node from './node'
import { OrderedMap, Record } from 'immutable'
/**
* Defaults.
*/
const DEFAULTS = {
nodes: new OrderedMap()
}
/**
* Document.
*/
class Document extends Record(DEFAULTS) {
/**
* Create a new `Document` with `properties`.
*
* @param {Objetc} properties
* @return {Document} document
*/
static create(properties = {}) {
if (properties instanceof Document) return properties
properties.nodes = Block.createList(properties.nodes)
return new Document(properties).normalize()
}
/**
* Get the node's kind.
*
* @return {String} kind
*/
get kind() {
return 'document'
}
/**
* 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() {
return this.nodes
.map(node => node.text)
.join('')
}
}
/**
* Mix in `Node` methods.
*/
for (const method in Node) {
Document.prototype[method] = Node[method]
}
/**
* Export.
*/
export default Document