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-07-22 12:03:55 -07:00
|
|
|
import memoize from '../utils/memoize'
|
2016-06-27 14:08:30 -07:00
|
|
|
import uid from '../utils/uid'
|
2016-07-22 12:12:23 -07:00
|
|
|
import { List, Record, Set } from 'immutable'
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Range.
|
|
|
|
*/
|
|
|
|
|
|
|
|
const Range = new Record({
|
|
|
|
kind: 'range',
|
|
|
|
marks: new Set(),
|
|
|
|
text: ''
|
|
|
|
})
|
2016-06-15 20:00:41 -07:00
|
|
|
|
|
|
|
/**
|
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-27 16:21:55 -07:00
|
|
|
key: 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-07-24 18:57:09 -07:00
|
|
|
properties.key = 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
|
|
|
}
|
|
|
|
|
2016-07-14 16:17:26 -07:00
|
|
|
/**
|
|
|
|
* Create a list of `Texts` from an array.
|
|
|
|
*
|
|
|
|
* @param {Array} elements
|
|
|
|
* @return {List} map
|
|
|
|
*/
|
|
|
|
|
|
|
|
static createList(elements = []) {
|
|
|
|
if (List.isList(elements)) return elements
|
|
|
|
return new List(elements.map(Text.create))
|
|
|
|
}
|
|
|
|
|
2016-06-21 16:44:11 -07:00
|
|
|
/**
|
|
|
|
* Get the node's kind.
|
|
|
|
*
|
|
|
|
* @return {String} kind
|
|
|
|
*/
|
|
|
|
|
|
|
|
get kind() {
|
|
|
|
return 'text'
|
|
|
|
}
|
|
|
|
|
2016-07-24 17:52:22 -07:00
|
|
|
/**
|
|
|
|
* Is the node empty?
|
|
|
|
*
|
|
|
|
* @return {Boolean} isEmpty
|
|
|
|
*/
|
|
|
|
|
|
|
|
get isEmpty() {
|
2016-07-27 16:21:55 -07:00
|
|
|
return this.text == ''
|
2016-07-24 17:52:22 -07:00
|
|
|
}
|
|
|
|
|
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-22 12:36:36 -07:00
|
|
|
/**
|
2016-08-13 19:38:59 -07:00
|
|
|
* Derive a set of decorated characters with `decorators`.
|
2016-07-22 12:36:36 -07:00
|
|
|
*
|
2016-08-13 19:38:59 -07:00
|
|
|
* @param {Array} decorators
|
|
|
|
* @return {List}
|
2016-07-22 12:36:36 -07:00
|
|
|
*/
|
|
|
|
|
2016-08-13 19:38:59 -07:00
|
|
|
getDecorations(decorators) {
|
|
|
|
const node = this
|
|
|
|
let { characters } = node
|
|
|
|
if (characters.size == 0) return characters
|
2016-07-22 12:36:36 -07:00
|
|
|
|
2016-08-13 19:38:59 -07:00
|
|
|
for (const decorator of decorators) {
|
|
|
|
const decorateds = decorator(node)
|
|
|
|
characters = characters.merge(decorateds)
|
|
|
|
}
|
2016-07-22 12:12:23 -07:00
|
|
|
|
2016-08-13 19:38:59 -07:00
|
|
|
return characters
|
2016-07-22 12:12:23 -07:00
|
|
|
}
|
|
|
|
|
2016-07-22 12:03:55 -07:00
|
|
|
/**
|
2016-08-13 19:38:59 -07:00
|
|
|
* Get the decorations for the node from a `schema`.
|
2016-07-22 12:03:55 -07:00
|
|
|
*
|
2016-08-13 19:38:59 -07:00
|
|
|
* @param {Schema} schema
|
|
|
|
* @return {Array}
|
2016-07-22 12:03:55 -07:00
|
|
|
*/
|
|
|
|
|
2016-08-13 19:38:59 -07:00
|
|
|
getDecorators(schema) {
|
|
|
|
return schema.__getDecorators(this)
|
2016-07-22 12:12:23 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Derive the ranges for a list of `characters`.
|
|
|
|
*
|
2016-08-13 19:38:59 -07:00
|
|
|
* @param {Array || Void} decorators (optional)
|
2016-07-22 12:12:23 -07:00
|
|
|
* @return {List}
|
|
|
|
*/
|
|
|
|
|
2016-08-13 19:38:59 -07:00
|
|
|
getRanges(decorators = []) {
|
|
|
|
const node = this
|
|
|
|
const list = new List()
|
|
|
|
let characters = this.getDecorations(decorators)
|
|
|
|
|
|
|
|
// If there are no characters, return one empty range.
|
2016-07-22 12:12:23 -07:00
|
|
|
if (characters.size == 0) {
|
2016-08-13 19:38:59 -07:00
|
|
|
return list.push(new Range())
|
2016-07-22 12:12:23 -07:00
|
|
|
}
|
|
|
|
|
2016-08-13 19:38:59 -07:00
|
|
|
// Convert the now-decorated characters into ranges.
|
|
|
|
const ranges = characters.reduce((memo, char, i) => {
|
|
|
|
const { marks, text } = char
|
|
|
|
|
|
|
|
// The first one can always just be created.
|
|
|
|
if (i == 0) {
|
|
|
|
return memo.push(new Range({ text, marks }))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, compare to the previous and see if a new range should be
|
|
|
|
// created, or whether the text should be added to the previous range.
|
|
|
|
const previous = characters.get(i - 1)
|
|
|
|
const prevMarks = previous.marks
|
|
|
|
const added = marks.filterNot(mark => prevMarks.includes(mark))
|
|
|
|
const removed = prevMarks.filterNot(mark => marks.includes(mark))
|
|
|
|
const isSame = !added.size && !removed.size
|
|
|
|
|
|
|
|
// If the marks are the same, add the text to the previous range.
|
|
|
|
if (isSame) {
|
|
|
|
const index = memo.size - 1
|
|
|
|
let prevRange = memo.get(index)
|
|
|
|
let prevText = prevRange.get('text')
|
|
|
|
prevRange = prevRange.set('text', prevText += text)
|
|
|
|
return memo.set(index, prevRange)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, create a new range.
|
|
|
|
return memo.push(new Range({ text, marks }))
|
|
|
|
}, list)
|
|
|
|
|
|
|
|
// Return the ranges.
|
|
|
|
return ranges
|
2016-07-22 12:03:55 -07:00
|
|
|
}
|
|
|
|
|
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 {Numbder} index
|
2016-07-14 14:34:04 -07:00
|
|
|
* @param {String} string
|
|
|
|
* @param {String} marks (optional)
|
2016-06-23 23:21:59 -07:00
|
|
|
* @return {Text} text
|
|
|
|
*/
|
|
|
|
|
2016-07-14 14:34:04 -07:00
|
|
|
insertText(index, string, marks) {
|
2016-06-23 23:21:59 -07:00
|
|
|
let { characters } = this
|
2016-07-14 14:34:04 -07:00
|
|
|
|
|
|
|
if (!marks) {
|
|
|
|
const prev = index ? characters.get(index - 1) : null
|
|
|
|
marks = prev ? prev.marks : Mark.createSet()
|
|
|
|
}
|
|
|
|
|
2016-06-23 23:21:59 -07:00
|
|
|
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-08-13 16:18:07 -07:00
|
|
|
/**
|
|
|
|
* Validate the node against a `schema`.
|
|
|
|
*
|
|
|
|
* @param {Schema} schema
|
|
|
|
* @return {Object || Void}
|
|
|
|
*/
|
|
|
|
|
|
|
|
validate(schema) {
|
|
|
|
return schema.__validate(this)
|
|
|
|
}
|
|
|
|
|
2016-06-15 20:00:41 -07:00
|
|
|
}
|
|
|
|
|
2016-07-22 12:03:55 -07:00
|
|
|
/**
|
|
|
|
* Memoize read methods.
|
|
|
|
*/
|
|
|
|
|
|
|
|
memoize(Text.prototype, [
|
2016-08-13 19:38:59 -07:00
|
|
|
'getDecorations',
|
|
|
|
'getDecorators',
|
2016-07-22 12:12:23 -07:00
|
|
|
'getRanges',
|
2016-08-13 19:38:59 -07:00
|
|
|
'validate',
|
2016-07-22 12:03:55 -07:00
|
|
|
])
|
|
|
|
|
2016-06-15 20:00:41 -07:00
|
|
|
/**
|
|
|
|
* Export.
|
|
|
|
*/
|
|
|
|
|
|
|
|
export default Text
|