1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-24 17:23:07 +01:00
slate/lib/models/document.js

101 lines
1.4 KiB
JavaScript

/**
* Prevent circuit.
*/
import './block'
import './inline'
/**
* Dependencies.
*/
import Block from './block'
import Node from './node'
import { OrderedMap, Record } from 'immutable'
/**
* Defaults.
*/
const DEFAULTS = {
nodes: new OrderedMap()
}
/**
* Document.
*/
class Document extends new 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'
}
/**
* Is the document empty?
*
* @return {Boolean} isEmpty
*/
get isEmpty() {
return this.text == ''
}
/**
* Get the length of the concatenated text of the document.
*
* @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