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

74 lines
1.1 KiB
JavaScript
Raw Normal View History

2016-06-15 20:00:41 -07:00
2016-06-23 10:43:36 -07:00
import Character from './character'
2016-06-16 09:24:18 -07:00
import uid from 'uid'
2016-06-15 20:00:41 -07:00
import { List, Record } from 'immutable'
/**
* Default properties.
2016-06-15 20:00:41 -07:00
*/
const DEFAULTS = {
2016-06-16 16:43:02 -07:00
characters: new List(),
2016-06-21 18:00:18 -07:00
key: null
}
2016-06-15 20:00:41 -07:00
/**
* Text.
*/
class Text extends Record(DEFAULTS) {
2016-06-15 20:00:41 -07:00
/**
2016-06-17 18:20:26 -07:00
* Create a new `Text` with `properties`.
2016-06-15 20:00:41 -07:00
*
2016-06-17 18:20:26 -07:00
* @param {Object} properties
2016-06-15 20:00:41 -07:00
* @return {Node} node
*/
2016-06-17 18:20:26 -07:00
static create(properties = {}) {
2016-06-23 10:43:36 -07:00
if (properties instanceof Text) return properties
2016-06-17 18:20:26 -07:00
properties.key = uid(4)
2016-06-23 10:43:36 -07:00
properties.characters = Character.createList(properties.characters)
2016-06-17 18:20:26 -07:00
return new Text(properties)
2016-06-15 20:00:41 -07:00
}
/**
* Get the node's kind.
*
* @return {String} kind
*/
get kind() {
return 'text'
}
2016-06-16 16:43:02 -07:00
/**
* 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('')
}
2016-06-15 20:00:41 -07:00
}
/**
* Export.
*/
export default Text