mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-26 08:34:28 +02:00
cleanup
This commit is contained in:
@@ -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
|
||||
},
|
||||
|
||||
/**
|
||||
|
@@ -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 })
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
Reference in New Issue
Block a user