1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-20 06:01:24 +02:00

Fix splitBlock bug with hanging selection (#1747)

* Fix splitBlock bug with hanging selection

* Restore marks
This commit is contained in:
Jinxuan Zhu
2018-04-27 17:07:39 -04:00
committed by Ian Storm Taylor
parent 6e6e9cf710
commit bbbf73fc12
4 changed files with 90 additions and 8 deletions

View File

@@ -217,8 +217,12 @@ Changes.insertText = (change, text, marks) => {
Changes.splitBlock = (change, depth = 1) => {
const { value } = change
const { selection } = value
const { selection, document } = value
const marks = selection.marks || document.getInsertMarksAtRange(selection)
change.splitBlockAtRange(selection, depth).collapseToEnd()
if (marks && marks.size !== 0) {
change.select({ marks })
}
}
/**

View File

@@ -992,12 +992,7 @@ Changes.setInlineAtRange = (...args) => {
Changes.splitBlockAtRange = (change, range, height = 1, options = {}) => {
const normalize = change.getFlag('normalize', options)
if (range.isExpanded) {
change.deleteAtRange(range, { normalize })
range = range.collapseToStart()
}
const { startKey, startOffset } = range
const { startKey, startOffset, endOffset, endKey } = range
const { value } = change
const { document } = value
let node = document.assertDescendant(startKey)
@@ -1010,7 +1005,19 @@ Changes.splitBlockAtRange = (change, range, height = 1, options = {}) => {
h++
}
change.splitDescendantsByKey(node.key, startKey, startOffset, { normalize })
change.splitDescendantsByKey(node.key, startKey, startOffset, {
normalize: normalize && range.isCollapsed,
})
if (range.isExpanded) {
if (range.isBackward) range = range.flip()
const nextBlock = change.value.document.getNextBlock(node.key)
range = range.moveAnchorToStartOf(nextBlock)
if (startKey === endKey) {
range = range.moveFocusTo(range.anchorKey, endOffset - startOffset)
}
change.deleteAtRange(range, { normalize })
}
}
/**

View File

@@ -0,0 +1,33 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change.splitBlock()
}
export const input = (
<value>
<document>
<paragraph>zero</paragraph>
<paragraph>
<anchor />word
</paragraph>
<quote>
<focus />cat is cute
</quote>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>zero</paragraph>
<paragraph />
<quote>
<cursor />cat is cute
</quote>
</document>
</value>
)

View File

@@ -0,0 +1,38 @@
/** @jsx h */
import h from '../../../helpers/h'
export default function(change) {
change
.addMark('italic')
.splitBlock()
.insertText('cat is cute')
}
export const input = (
<value>
<document>
<paragraph>
<b>word</b>
<cursor />
</paragraph>
</document>
</value>
)
export const output = (
<value>
<document>
<paragraph>
<b>word</b>
<cursor />
</paragraph>
<paragraph>
<i>
<b>cat is cute</b>
</i>
<cursor />
</paragraph>
</document>
</value>
)