mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-24 16:02:55 +02:00
Fix insertTextAtRange selection (#2256)
* Make a test case for buggy insertTextAtRange selection behavior * Insert text at previous path, not selection key, closes #2209
This commit is contained in:
committed by
Ian Storm Taylor
parent
7192a97600
commit
996edd19f5
@@ -794,7 +794,8 @@ Commands.insertTextAtRange = (change, range, text, marks) => {
|
|||||||
const { document } = value
|
const { document } = value
|
||||||
const { start } = range
|
const { start } = range
|
||||||
let key = start.key
|
let key = start.key
|
||||||
let offset = start.offset
|
const offset = start.offset
|
||||||
|
const path = start.path
|
||||||
const parent = document.getParent(start.key)
|
const parent = document.getParent(start.key)
|
||||||
|
|
||||||
if (change.isVoid(parent)) {
|
if (change.isVoid(parent)) {
|
||||||
@@ -805,10 +806,11 @@ Commands.insertTextAtRange = (change, range, text, marks) => {
|
|||||||
if (range.isExpanded) {
|
if (range.isExpanded) {
|
||||||
change.deleteAtRange(range)
|
change.deleteAtRange(range)
|
||||||
|
|
||||||
|
const startText = change.value.document.getNode(path)
|
||||||
|
|
||||||
// Update range start after delete
|
// Update range start after delete
|
||||||
if (change.value.selection.start.key !== key) {
|
if (startText && startText.key !== key) {
|
||||||
key = change.value.selection.start.key
|
key = startText.key
|
||||||
offset = change.value.selection.start.offset
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -0,0 +1,36 @@
|
|||||||
|
/** @jsx h */
|
||||||
|
|
||||||
|
import { Point, Range } from 'slate'
|
||||||
|
|
||||||
|
import h from '../../../helpers/h'
|
||||||
|
|
||||||
|
export default function(change) {
|
||||||
|
const { key } = change.value.document.getFirstText()
|
||||||
|
const range = new Range({
|
||||||
|
anchor: new Point({ key, offset: 0 }),
|
||||||
|
focus: new Point({ key, offset: 3 }),
|
||||||
|
})
|
||||||
|
change.insertTextAtRange(range, 'That')
|
||||||
|
}
|
||||||
|
|
||||||
|
export const input = (
|
||||||
|
<value>
|
||||||
|
<document>
|
||||||
|
<line>The change will be here.</line>
|
||||||
|
<line>
|
||||||
|
The cursor is <cursor />over here.
|
||||||
|
</line>
|
||||||
|
</document>
|
||||||
|
</value>
|
||||||
|
)
|
||||||
|
|
||||||
|
export const output = (
|
||||||
|
<value>
|
||||||
|
<document>
|
||||||
|
<line>That change will be here.</line>
|
||||||
|
<line>
|
||||||
|
The cursor is <cursor />over here.
|
||||||
|
</line>
|
||||||
|
</document>
|
||||||
|
</value>
|
||||||
|
)
|
Reference in New Issue
Block a user