1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-02-01 05:16:10 +01:00

Move insertBlocks outside of void inlines (#1987)

When inserting a block, we shouldn't split void inlines -- otherwise,
they're duplicated. Instead, move the insert to either before or after
the void node.
This commit is contained in:
Justin Weiss 2018-07-23 12:51:54 -07:00 committed by Ian Storm Taylor
parent c046652a71
commit 57a4938703
2 changed files with 49 additions and 1 deletions

View File

@ -635,8 +635,9 @@ Changes.insertBlockAtRange = (change, range, block, options = {}) => {
const { value } = change
const { document } = value
const { startKey, startOffset } = range
let { startKey, startOffset } = range
const startBlock = document.getClosestBlock(startKey)
const startInline = document.getClosestInline(startKey)
const parent = document.getParent(startBlock.key)
const index = parent.nodes.indexOf(startBlock)
@ -650,6 +651,20 @@ Changes.insertBlockAtRange = (change, range, block, options = {}) => {
} else if (range.isAtEndOf(startBlock)) {
change.insertNodeByKey(parent.key, index + 1, block, { normalize })
} else {
if (startInline && startInline.isVoid) {
const atEnd = range.isAtEndOf(startInline)
const siblingText = atEnd
? document.getNextText(startKey)
: document.getPreviousText(startKey)
const splitRange = atEnd
? range.moveToStartOf(siblingText)
: range.moveToEndOf(siblingText)
startKey = splitRange.startKey
startOffset = splitRange.startOffset
}
change.splitDescendantsByKey(startBlock.key, startKey, startOffset, {
normalize: false,
})

View File

@ -0,0 +1,33 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change.insertBlock('quote')
}
export const input = (
<value>
<document>
<paragraph>
<emoji>
<cursor />
</emoji>
</paragraph>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>
<emoji />
</paragraph>
<quote>
<cursor />
</quote>
<paragraph />
</document>
</value>
)