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

Fix bug where insertBlock and insertBlockAtRange always inserted the block below the target, even though focus was collapsed to start (#1037)

This commit is contained in:
Per-Kristian Nordnes
2017-09-05 18:22:58 +02:00
committed by Ian Storm Taylor
parent e79ec24f39
commit a0c4073f22
10 changed files with 52 additions and 9 deletions

View File

@@ -597,7 +597,11 @@ Transforms.insertBlockAtRange = (transform, range, block, options = {}) => {
const index = parent.nodes.indexOf(startBlock)
if (startBlock.isVoid) {
transform.insertNodeByKey(parent.key, index + 1, block, { normalize })
let beforeOrAfterIndex = index
if (range.isAtEndOf(startBlock)) {
beforeOrAfterIndex++
}
transform.insertNodeByKey(parent.key, beforeOrAfterIndex, block, { normalize })
}
else if (startBlock.isEmpty) {

View File

@@ -15,10 +15,10 @@ export default function (state) {
const next = state
.transform()
.select(range)
.insertBlock('image')
.insertBlock('video')
.apply()
const updated = next.document.getTexts().last()
const updated = next.document.getTexts().first()
assert.deepEqual(
next.selection.toJS(),

View File

@@ -6,3 +6,8 @@ nodes:
nodes:
- kind: text
text: ""
- kind: block
type: paragraph
nodes:
- kind: text
text: "Some text"

View File

@@ -1,10 +1,15 @@
nodes:
- kind: block
type: image
isVoid: true
- kind: block
type: image
type: video
nodes:
- kind: text
text: ""
- kind: block
isVoid: true
type: image
- kind: block
type: paragraph
nodes:
- kind: text
text: "Some 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: 1,
focusKey: first.key,
focusOffset: 1
})
return state
.transform()
.insertBlockAtRange(range, 'image')
.apply()
}

View File

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

View File

@@ -1,7 +1,7 @@
nodes:
- kind: block
type: image
type: video
isVoid: true
- kind: block
type: image

View File

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

View File

@@ -2,7 +2,9 @@
nodes:
- kind: block
type: image
isVoid: true
nodes:
- kind: text
text: ""
- kind: block
type: video
isVoid: true