mirror of
https://github.com/ianstormtaylor/slate.git
synced 2025-08-30 02:19:52 +02:00
Fix setNodes when called with 'split' and a collapsed range (#4523)
* Fix setNodes when called with 'split' and a collapsed range * Only bail if it's a non-empty text node * Fix comment
This commit is contained in:
5
.changeset/sharp-donkeys-sparkle.md
Normal file
5
.changeset/sharp-donkeys-sparkle.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
'slate': patch
|
||||
---
|
||||
|
||||
Fix setNodes when called with 'split' and a collapsed range
|
@@ -222,12 +222,16 @@ export const Range: RangeInterface = {
|
||||
let affinityFocus: 'forward' | 'backward' | null
|
||||
|
||||
if (affinity === 'inward') {
|
||||
// If the range is collapsed, make sure to use the same affinity to
|
||||
// avoid the two points passing each other and expanding in the opposite
|
||||
// direction
|
||||
const isCollapsed = Range.isCollapsed(r)
|
||||
if (Range.isForward(r)) {
|
||||
affinityAnchor = 'forward'
|
||||
affinityFocus = 'backward'
|
||||
affinityFocus = isCollapsed ? affinityAnchor : 'backward'
|
||||
} else {
|
||||
affinityAnchor = 'backward'
|
||||
affinityFocus = 'forward'
|
||||
affinityFocus = isCollapsed ? affinityAnchor : 'forward'
|
||||
}
|
||||
} else if (affinity === 'outward') {
|
||||
if (Range.isForward(r)) {
|
||||
|
@@ -594,6 +594,14 @@ export const NodeTransforms: NodeTransforms = {
|
||||
}
|
||||
|
||||
if (split && Range.isRange(at)) {
|
||||
if (
|
||||
Range.isCollapsed(at) &&
|
||||
Editor.leaf(editor, at.anchor)[0].text.length > 0
|
||||
) {
|
||||
// If the range is collapsed in a non-empty node and 'split' is true, there's nothing to
|
||||
// set that won't get normalized away
|
||||
return
|
||||
}
|
||||
const rangeRef = Editor.rangeRef(editor, at, { affinity: 'inward' })
|
||||
const [start, end] = Range.edges(at)
|
||||
const splitMode = mode === 'lowest' ? 'lowest' : 'highest'
|
||||
|
@@ -0,0 +1,34 @@
|
||||
import { Range } from 'slate'
|
||||
|
||||
export const input = {
|
||||
anchor: {
|
||||
path: [0, 0],
|
||||
offset: 1,
|
||||
},
|
||||
focus: {
|
||||
path: [0, 0],
|
||||
offset: 1,
|
||||
},
|
||||
}
|
||||
export const test = value => {
|
||||
return Range.transform(
|
||||
value,
|
||||
{
|
||||
type: 'split_node',
|
||||
path: [0, 0],
|
||||
position: 1,
|
||||
properties: {},
|
||||
},
|
||||
{ affinity: 'inward' }
|
||||
)
|
||||
}
|
||||
export const output = {
|
||||
anchor: {
|
||||
path: [0, 1],
|
||||
offset: 0,
|
||||
},
|
||||
focus: {
|
||||
path: [0, 1],
|
||||
offset: 0,
|
||||
},
|
||||
}
|
@@ -0,0 +1,27 @@
|
||||
/** @jsx jsx */
|
||||
import { Transforms, Text } from 'slate'
|
||||
import { jsx } from '../../..'
|
||||
|
||||
export const run = editor => {
|
||||
Transforms.setNodes(
|
||||
editor,
|
||||
{ someKey: true },
|
||||
{ match: Text.isText, split: true }
|
||||
)
|
||||
}
|
||||
export const input = (
|
||||
<editor>
|
||||
<block>
|
||||
w<cursor />
|
||||
ord
|
||||
</block>
|
||||
</editor>
|
||||
)
|
||||
export const output = (
|
||||
<editor>
|
||||
<block>
|
||||
w<cursor />
|
||||
ord
|
||||
</block>
|
||||
</editor>
|
||||
)
|
Reference in New Issue
Block a user