mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-09-09 14:40:41 +02:00
Update selection in insertText operation
This commit is contained in:
@@ -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`.
|
* Extend the focus point to the start of a `node`.
|
||||||
*
|
*
|
||||||
|
@@ -106,11 +106,23 @@ function insertNode(state, operation) {
|
|||||||
|
|
||||||
function insertText(state, operation) {
|
function insertText(state, operation) {
|
||||||
const { path, offset, text, marks } = 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)
|
let node = document.assertPath(path)
|
||||||
|
|
||||||
|
// Update the document
|
||||||
node = node.insertText(offset, text, marks)
|
node = node.insertText(offset, text, marks)
|
||||||
document = document.updateDescendant(node)
|
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
|
return state
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user