1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-26 00:27:28 +02:00

fix insertText to do nothing in void nodes

This commit is contained in:
Ian Storm Taylor
2016-08-05 10:32:00 -07:00
parent 90abc993b9
commit 772aadb70a
8 changed files with 76 additions and 3 deletions

View File

@@ -879,10 +879,15 @@ class State extends new Record(DEFAULTS) {
insertText(text, marks) {
let state = this
let { cursorMarks, document, selection } = state
let after = selection
let after
const isVoid = document.hasVoidParent(state.startText)
// Determine what the selection should be after inserting.
if (selection.isExpanded) {
if (isVoid) {
after = selection
}
else if (selection.isExpanded) {
after = selection.collapseToStart().moveForward(text.length)
}

View File

@@ -444,6 +444,11 @@ const Transforms = {
insertTextAtRange(range, string, marks) {
let node = this
// If inside a void node, do nothing.
const { startKey, startOffset } = range
const isVoid = node.hasVoidParent(startKey)
if (isVoid) return node
// When still expanded, remove the current range first.
if (range.isExpanded) {
node = node.deleteAtRange(range)
@@ -451,7 +456,6 @@ const Transforms = {
}
// Insert text at the range's offset.
const { startKey, startOffset } = range
let text = node.getDescendant(startKey)
text = text.insertText(startOffset, string, marks)
node = node.updateDescendant(text)

View File

@@ -0,0 +1,17 @@
export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 0,
focusKey: first.key,
focusOffset: 0
})
return state
.transform()
.insertTextAtRange(range, 'a')
.apply()
}

View File

@@ -0,0 +1,5 @@
nodes:
- kind: block
type: image
isVoid: true

View File

@@ -0,0 +1,5 @@
nodes:
- kind: block
type: image
isVoid: true

View File

@@ -0,0 +1,27 @@
import assert from 'assert'
export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 0,
focusKey: first.key,
focusOffset: 0
})
const next = state
.transform()
.moveTo(range)
.insertText('a')
.apply()
assert.deepEqual(
next.selection.toJS(),
range.toJS()
)
return next
}

View File

@@ -0,0 +1,5 @@
nodes:
- kind: block
type: image
isVoid: true

View File

@@ -0,0 +1,5 @@
nodes:
- kind: block
type: image
isVoid: true