1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-26 08:34:28 +02:00
This commit is contained in:
Ian Storm Taylor
2016-06-23 23:21:59 -07:00
parent a4c8e8fab5
commit 122f571090
2 changed files with 36 additions and 27 deletions

View File

@@ -621,14 +621,14 @@ const Node = {
},
/**
* Insert `text` at a `range`.
* Insert text `string` at a `range`.
*
* @param {Selection} range
* @param {String} text
* @param {String} string
* @return {Node} node
*/
insertTextAtRange(range, text) {
insertTextAtRange(range, string) {
let node = this
range = range.normalize(node)
@@ -638,31 +638,13 @@ const Node = {
range = range.moveToStart()
}
let { startKey, startOffset } = range
let startNode = node.getDescendant(startKey)
let { characters } = startNode
// Insert text at the range's offset.
const { startKey, startOffset } = range
let text = node.getDescendant(startKey)
text = text.insertText(string, startOffset)
node = node.updateDescendant(text)
// Create a list of the new characters, with the marks from the previous
// character if one exists.
const prev = characters.get(startOffset - 1)
const marks = prev ? prev.marks : null
const newChars = Character.createList(text.split('').map((char) => {
const obj = { text: char }
if (marks) obj.marks = marks
return obj
}))
// Splice in the new characters.
characters = characters.slice(0, startOffset)
.concat(newChars)
.concat(characters.slice(startOffset))
// Update the existing text node.
startNode = startNode.merge({ characters })
node = node.updateDescendant(startNode)
// Normalize the node.
return node.normalize()
return node
},
/**

View File

@@ -1,5 +1,6 @@
import Character from './character'
import Mark from './mark'
import uid from 'uid'
import { List, Record } from 'immutable'
@@ -82,6 +83,32 @@ class Text extends Record(DEFAULTS) {
return this.merge({ characters })
}
/**
* 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 })
}
}
/**