mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-20 14:11:35 +02:00
refactor splitInline
This commit is contained in:
@@ -235,44 +235,11 @@ export function splitBlock(transform, depth = 1) {
|
|||||||
|
|
||||||
export function splitInline(transform, depth = Infinity) {
|
export function splitInline(transform, depth = Infinity) {
|
||||||
let { state } = transform
|
let { state } = transform
|
||||||
let { document, selection } = state
|
let { selection } = state
|
||||||
|
transform
|
||||||
// If the selection is expanded, remove it first.
|
.snapshotSelection()
|
||||||
if (selection.isExpanded) {
|
.splitInlineAtRange(selection, depth)
|
||||||
transform.delete()
|
.snapshotSelection()
|
||||||
state = transform.state
|
|
||||||
document = state.document
|
|
||||||
selection = state.selection
|
|
||||||
}
|
|
||||||
|
|
||||||
let after = selection
|
|
||||||
const { startKey, startOffset } = selection
|
|
||||||
let startNode = document.assertDescendant(startKey)
|
|
||||||
const furthestInline = document.getFurthestInline(startKey)
|
|
||||||
const offset = furthestInline.getOffset(startNode.key)
|
|
||||||
|
|
||||||
// If the selection is at the start of end of the furthest inline, there isn't
|
|
||||||
// anything to split, so abort.
|
|
||||||
if (
|
|
||||||
(offset + startOffset == 0) ||
|
|
||||||
(offset + startNode.length == startOffset)
|
|
||||||
) {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
transform.unsetSelection()
|
|
||||||
transform.splitInlineAtRange(selection, depth)
|
|
||||||
state = transform.state
|
|
||||||
document = state.document
|
|
||||||
const closestInline = document.getClosestInline(startKey)
|
|
||||||
|
|
||||||
if (closestInline) {
|
|
||||||
startNode = document.getDescendant(startKey)
|
|
||||||
const nextNode = document.getNextText(startNode.key)
|
|
||||||
after = selection.collapseToStartOf(nextNode)
|
|
||||||
}
|
|
||||||
|
|
||||||
transform.moveTo(after)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
Reference in New Issue
Block a user