Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix splitBlock bug with hanging selection #1747

Merged
merged 2 commits into from
Apr 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -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>
)
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>
)