1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-30 18:39:51 +02:00

refactor splitBlock

This commit is contained in:
Ian Storm Taylor
2016-11-22 16:56:22 -08:00
parent fe836b1daa
commit 490498f228

View File

@@ -218,38 +218,12 @@ export function setInline(transform, properties) {
export function splitBlock(transform, depth = 1) {
let { state } = transform
let { document, selection } = state
transform.unsetSelection()
transform.splitBlockAtRange(selection, depth)
state = transform.state
document = state.document
const { startKey, startOffset } = selection
const startText = document.getNode(startKey)
const startBlock = document.getClosestBlock(startKey)
const startInline = startBlock.getFurthestInline(startKey)
const nextText = document.getNextText(startText.key)
let after
// If the selection is at the start of the highest inline child inside the
// block, the starting text node won't need to be split.
if (
(startOffset == 0) &&
(startBlock.text != '') &&
(!startInline || startInline.getOffset(startText.key) == 0)
) {
after = selection.collapseToStartOf(startText)
}
// Otherwise, we'll need to move the selection forward one to account for the
// text node that was split.
else {
after = selection.collapseToStartOf(nextText)
}
transform.moveTo(after)
let { selection } = state
transform
.snapshotSelection()
.splitBlockAtRange(selection, depth)
.collapseToEnd()
.snapshotSelection()
}
/**