Skip to content

Commit

Permalink
Fix splitBlock bug with hanging selection (#1747)
Browse files Browse the repository at this point in the history
* Fix splitBlock bug with hanging selection

* Restore marks
  • Loading branch information
zhujinxuan authored and ianstormtaylor committed Apr 27, 2018
1 parent 6e6e9cf commit bbbf73f
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 8 deletions.
6 changes: 5 additions & 1 deletion packages/slate/src/changes/at-current-range.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}
}

/**
Expand Down
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 @@ -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)
Expand All @@ -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 })
}
}

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

0 comments on commit bbbf73f

Please sign in to comment.