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

106 lines
1.5 KiB
JavaScript
Raw Normal View History

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'
import Node from './node'
2016-08-17 02:19:13 -07:00
import uid from '../utils/uid'
import { OrderedMap, Record } from 'immutable'
/**
* Defaults.
*/
const DEFAULTS = {
2016-08-17 02:19:13 -07:00
key: null,
nodes: new OrderedMap(),
}
/**
* Document.
*/
2016-07-06 20:19:19 -07:00
class Document extends new Record(DEFAULTS) {
/**
* 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)
properties.nodes = Block.createList(properties.nodes)
2016-08-17 02:19:13 -07:00
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