1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-31 19:01:54 +02:00

simplify before-inline split-block test

This commit is contained in:
Ian Storm Taylor
2016-11-18 11:11:27 -08:00
parent 68d26cdf79
commit 7567be2210
4 changed files with 27 additions and 22 deletions

View File

@@ -312,10 +312,27 @@ export function splitBlock(transform, depth = 1) {
state = transform.state
document = state.document
const { startKey } = selection
const startNode = document.getDescendant(startKey)
const nextNode = document.getNextText(startNode.key)
const after = selection.collapseToStartOf(nextNode)
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) &&
(startText == 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)
}
return transform.moveTo(after)
}

View File

@@ -4,12 +4,12 @@ import assert from 'assert'
export default function (state) {
const { document, selection } = state
const texts = document.getTexts()
const first = texts.first()
const second = texts.get(1)
const range = selection.merge({
anchorKey: first.key,
anchorOffset: 4,
focusKey: first.key,
focusOffset: 4
anchorKey: second.key,
anchorOffset: 0,
focusKey: second.key,
focusOffset: 0
})
const next = state
@@ -17,12 +17,6 @@ export default function (state) {
.moveTo(range)
.splitBlock()
.apply()
.transform()
.deleteBackward()
.apply()
.transform()
.splitBlock()
.apply()
const updated = next.document.getTexts().get(1)

View File

@@ -12,8 +12,5 @@ nodes:
nodes:
- kind: text
text: hyperlink
- kind: block
type: paragraph
nodes:
- kind: text
text: word
text: word

View File

@@ -15,8 +15,5 @@ nodes:
nodes:
- kind: text
text: hyperlink
- kind: block
type: paragraph
nodes:
- kind: text
text: word