1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-30 18:39:51 +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:
Steve Marquis
2021-09-23 06:45:03 -07:00
committed by GitHub
parent 15f8f866dd
commit 0da12c17dc
5 changed files with 80 additions and 2 deletions

View File

@@ -0,0 +1,5 @@
---
'slate': patch
---
Fix setNodes when called with 'split' and a collapsed range

View File

@@ -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)) {

View File

@@ -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'

View File

@@ -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,
},
}

View File

@@ -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>
)