mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-31 10:51:44 +02:00
refactor splitBlock
This commit is contained in:
@@ -218,38 +218,12 @@ export function setInline(transform, properties) {
|
|||||||
|
|
||||||
export function splitBlock(transform, depth = 1) {
|
export function splitBlock(transform, depth = 1) {
|
||||||
let { state } = transform
|
let { state } = transform
|
||||||
let { document, selection } = state
|
let { selection } = state
|
||||||
|
transform
|
||||||
transform.unsetSelection()
|
.snapshotSelection()
|
||||||
transform.splitBlockAtRange(selection, depth)
|
.splitBlockAtRange(selection, depth)
|
||||||
|
.collapseToEnd()
|
||||||
state = transform.state
|
.snapshotSelection()
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user