2016-06-15 20:00:41 -07:00
|
|
|
|
2016-06-23 10:43:36 -07:00
|
|
|
import Character from './character'
|
2016-06-23 23:21:59 -07:00
|
|
|
import Mark from './mark'
|
2016-06-27 14:08:30 -07:00
|
|
|
import uid from '../utils/uid'
|
2016-06-15 20:00:41 -07:00
|
|
|
import { List, Record } from 'immutable'
|
|
|
|
|
|
|
|
/**
|
2016-06-21 16:44:11 -07:00
|
|
|
* Default properties.
|
2016-06-15 20:00:41 -07:00
|
|
|
*/
|
|
|
|
|
2016-06-21 16:44:11 -07:00
|
|
|
const DEFAULTS = {
|
2016-06-16 16:43:02 -07:00
|
|
|
characters: new List(),
|
2016-07-06 14:05:35 -07:00
|
|
|
decorations: null,
|
|
|
|
key: null,
|
|
|
|
cache: null
|
2016-06-21 16:44:11 -07:00
|
|
|
}
|
2016-06-15 20:00:41 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Text.
|
|
|
|
*/
|
|
|
|
|
2016-07-06 20:19:19 -07:00
|
|
|
class Text extends new 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-23 23:04:21 -07:00
|
|
|
* @return {Text} text
|
2016-06-15 20:00:41 -07:00
|
|
|
*/
|
|
|
|
|
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-07-06 14:05:35 -07:00
|
|
|
properties.decorations = null
|
|
|
|
properties.cache = null
|
2016-06-17 18:20:26 -07:00
|
|
|
return new Text(properties)
|
2016-06-15 20:00:41 -07:00
|
|
|
}
|
|
|
|
|
2016-06-21 16:44:11 -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-07-06 14:05:35 -07:00
|
|
|
/**
|
|
|
|
* Decorate the text node's characters with a `decorator` function.
|
|
|
|
*
|
|
|
|
* @param {Function} decorator
|
|
|
|
* @return {Text} text
|
|
|
|
*/
|
|
|
|
|
|
|
|
decorateCharacters(decorator) {
|
|
|
|
let { characters, cache } = this
|
|
|
|
if (characters == cache) return this
|
|
|
|
|
|
|
|
const decorations = decorator(this)
|
|
|
|
if (decorations == characters) return this
|
|
|
|
|
|
|
|
return this.merge({
|
|
|
|
cache: characters,
|
|
|
|
decorations,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2016-06-23 23:04:21 -07:00
|
|
|
/**
|
|
|
|
* Remove characters from the text node from `start` to `end`.
|
|
|
|
*
|
|
|
|
* @param {Number} start
|
|
|
|
* @param {Number} end
|
|
|
|
* @return {Text} text
|
|
|
|
*/
|
|
|
|
|
|
|
|
removeCharacters(start, end) {
|
|
|
|
let { characters } = this
|
|
|
|
|
|
|
|
characters = characters.filterNot((char, i) => {
|
|
|
|
return start <= i && i < end
|
|
|
|
})
|
|
|
|
|
|
|
|
return this.merge({ characters })
|
|
|
|
}
|
|
|
|
|
2016-06-23 23:21:59 -07:00
|
|
|
/**
|
|
|
|
* Insert text `string` at `index`.
|
|
|
|
*
|
|
|
|
* @param {String} string
|
|
|
|
* @param {Numbder} index
|
|
|
|
* @return {Text} text
|
|
|
|
*/
|
|
|
|
|
|
|
|
insertText(string, index) {
|
|
|
|
let { characters } = this
|
|
|
|
const prev = index ? characters.get(index - 1) : null
|
|
|
|
const marks = prev ? prev.marks : Mark.createSet()
|
|
|
|
const chars = Character.createList(string.split('').map((char) => {
|
|
|
|
return {
|
|
|
|
text: char,
|
|
|
|
marks
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
|
|
|
|
characters = characters.slice(0, index)
|
|
|
|
.concat(chars)
|
|
|
|
.concat(characters.slice(index))
|
|
|
|
|
|
|
|
return this.merge({ characters })
|
|
|
|
}
|
|
|
|
|
2016-06-15 20:00:41 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Export.
|
|
|
|
*/
|
|
|
|
|
|
|
|
export default Text
|