mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-26 16:44:22 +02:00
fix insertText to do nothing in void nodes
This commit is contained in:
@@ -879,10 +879,15 @@ class State extends new Record(DEFAULTS) {
|
|||||||
insertText(text, marks) {
|
insertText(text, marks) {
|
||||||
let state = this
|
let state = this
|
||||||
let { cursorMarks, document, selection } = state
|
let { cursorMarks, document, selection } = state
|
||||||
let after = selection
|
let after
|
||||||
|
const isVoid = document.hasVoidParent(state.startText)
|
||||||
|
|
||||||
// Determine what the selection should be after inserting.
|
// 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)
|
after = selection.collapseToStart().moveForward(text.length)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@@ -444,6 +444,11 @@ const Transforms = {
|
|||||||
insertTextAtRange(range, string, marks) {
|
insertTextAtRange(range, string, marks) {
|
||||||
let node = this
|
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.
|
// When still expanded, remove the current range first.
|
||||||
if (range.isExpanded) {
|
if (range.isExpanded) {
|
||||||
node = node.deleteAtRange(range)
|
node = node.deleteAtRange(range)
|
||||||
@@ -451,7 +456,6 @@ const Transforms = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Insert text at the range's offset.
|
// Insert text at the range's offset.
|
||||||
const { startKey, startOffset } = range
|
|
||||||
let text = node.getDescendant(startKey)
|
let text = node.getDescendant(startKey)
|
||||||
text = text.insertText(startOffset, string, marks)
|
text = text.insertText(startOffset, string, marks)
|
||||||
node = node.updateDescendant(text)
|
node = node.updateDescendant(text)
|
||||||
|
@@ -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()
|
||||||
|
}
|
@@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: image
|
||||||
|
isVoid: true
|
@@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: image
|
||||||
|
isVoid: true
|
27
test/transforms/fixtures/insert-text/inside-void/index.js
Normal file
27
test/transforms/fixtures/insert-text/inside-void/index.js
Normal 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
|
||||||
|
}
|
@@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: image
|
||||||
|
isVoid: true
|
@@ -0,0 +1,5 @@
|
|||||||
|
|
||||||
|
nodes:
|
||||||
|
- kind: block
|
||||||
|
type: image
|
||||||
|
isVoid: true
|
Reference in New Issue
Block a user