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

74 lines
1.1 KiB
JavaScript

import Character from './character'
import uid from 'uid'
import { List, Record } from 'immutable'
/**
* Default properties.
*/
const DEFAULTS = {
characters: new List(),
key: null
}
/**
* Text.
*/
class Text extends Record(DEFAULTS) {
/**
* Create a new `Text` with `properties`.
*
* @param {Object} properties
* @return {Node} node
*/
static create(properties = {}) {
if (properties instanceof Text) return properties
properties.key = uid(4)
properties.characters = Character.createList(properties.characters)
return new Text(properties)
}
/**
* Get the node's kind.
*
* @return {String} kind
*/
get kind() {
return 'text'
}
/**
* Get the length of the concatenated text of the node.
*
* @return {Number} length
*/
get length() {
return this.text.length
}
/**
* Get the concatenated text of the node.
*
* @return {String} text
*/
get text() {
return this.characters
.map(char => char.text)
.join('')
}
}
/**
* Export.
*/
export default Text