1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-31 10:51:44 +02:00

Update selection in insertText operation

This commit is contained in:
Samy Pessé
2016-10-26 12:15:28 +02:00
parent 6d2b70c750
commit 36d6d8eb3c
2 changed files with 40 additions and 2 deletions

View File

@@ -569,6 +569,32 @@ class Selection extends new Record(DEFAULTS) {
})
}
/**
* Extend the start point forward `n` characters.
*
* @param {Number} n (optional)
* @return {Selection} selection
*/
extendStartOffset(n = 1) {
return this.isBackward
? this.merge({ focusOffset: this.focusOffset + n })
: this.merge({ anchorOffset: this.anchorOffset + n })
}
/**
* Extend the end point forward `n` characters.
*
* @param {Number} n (optional)
* @return {Selection} selection
*/
extendEndOffset(n = 1) {
return this.isBackward
? this.merge({ anchorOffset: this.anchorOffset + n })
: this.merge({ focusOffset: this.focusOffset + n })
}
/**
* Extend the focus point to the start of a `node`.
*

View File

@@ -106,11 +106,23 @@ function insertNode(state, operation) {
function insertText(state, operation) {
const { path, offset, text, marks } = operation
let { document } = state
let { document, selection } = state
const { startKey, endKey, startOffset, endOffset } = selection
let node = document.assertPath(path)
// Update the document
node = node.insertText(offset, text, marks)
document = document.updateDescendant(node)
state = state.merge({ document })
// Update the selection
if (startKey == node.key && startOffset > offset) {
selection = selection.extendStartOffset(text.length)
}
if (endKey == node.key && endOffset > offset) {
selection = selection.extendEndOffset(text.length)
}
state = state.merge({ document, selection })
return state
}