Skip to content

Commit

Permalink
Fix splitBlock bug with hanging selection
Browse files Browse the repository at this point in the history
  • Loading branch information
zhujinxuan committed Mar 30, 2018
1 parent fb8075a commit a1e1b38
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 7 deletions.
21 changes: 14 additions & 7 deletions packages/slate/src/changes/at-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -988,12 +988,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)
Expand All @@ -1006,7 +1001,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 })
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -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>
)

0 comments on commit a1e1b38

Please sign in to comment.