1
0
mirror of https://github.com/ianstormtaylor/slate.git synced 2025-08-09 08:46:35 +02:00

Correcting the splitting logic in setNodes (#4168)

* Defined conditions to always split nodes

Those conditions are:
- When the anchor does not lie at the start of a node.
- When the focus does not lie at the end of a node.

* prev variable now points to correct updated node.

Previously, in the case the previous node gets merged in last iteration,
prev pointer could be pointing to the wrong node.
That posed problems, especially when normalizing empty text nodes.
So, now in every iteration, we get a copy of updated node structure,
and take value of prev from that.

* Test to check splitting and normalization logic.

- In this, since anchor and focus bleeds on both sides, splits happen.
- Empty text nodes are introduced on either side.
- New properties are set in the new node selection.
- Normalization happens and takes care of those empty text nodes.

* Create two-lies-appear.md

* Update two-lies-appear.md

Co-authored-by: Ian Storm Taylor <ian@ianstormtaylor.com>
This commit is contained in:
Ridham Bhat
2021-04-13 20:45:47 +05:30
committed by GitHub
parent fd70dc0b2c
commit 95f402c594
4 changed files with 53 additions and 1 deletions

View File

@@ -0,0 +1,5 @@
---
slate: patch
---
Fixed a bug in splitting and applying overlapping marks to text nodes.

View File

@@ -224,8 +224,10 @@ export const createEditor = (): Editor => {
let n = 0 let n = 0
for (let i = 0; i < node.children.length; i++, n++) { for (let i = 0; i < node.children.length; i++, n++) {
const currentNode = Node.get(editor, path)
if (Text.isText(currentNode)) continue
const child = node.children[i] as Descendant const child = node.children[i] as Descendant
const prev = node.children[i - 1] as Descendant const prev = currentNode.children[n - 1] as Descendant
const isLast = i === node.children.length - 1 const isLast = i === node.children.length - 1
const isInlineOrText = const isInlineOrText =
Text.isText(child) || Text.isText(child) ||

View File

@@ -592,17 +592,21 @@ export const NodeTransforms: NodeTransforms = {
const rangeRef = Editor.rangeRef(editor, at, { affinity: 'inward' }) const rangeRef = Editor.rangeRef(editor, at, { affinity: 'inward' })
const [start, end] = Range.edges(at) const [start, end] = Range.edges(at)
const splitMode = mode === 'lowest' ? 'lowest' : 'highest' const splitMode = mode === 'lowest' ? 'lowest' : 'highest'
const endAtEndOfNode = Editor.isEnd(editor, end, end.path)
Transforms.splitNodes(editor, { Transforms.splitNodes(editor, {
at: end, at: end,
match, match,
mode: splitMode, mode: splitMode,
voids, voids,
always: !endAtEndOfNode,
}) })
const startAtStartOfNode = Editor.isStart(editor, start, start.path)
Transforms.splitNodes(editor, { Transforms.splitNodes(editor, {
at: start, at: start,
match, match,
mode: splitMode, mode: splitMode,
voids, voids,
always: !startAtStartOfNode,
}) })
at = rangeRef.unref()! at = rangeRef.unref()!

View File

@@ -0,0 +1,41 @@
/** @jsx jsx */
import { Transforms, Text } from 'slate'
import { jsx } from '../../..'
export const run = editor => {
Transforms.setNodes(
editor,
{ key: true },
{ match: Text.isText, split: true }
)
}
export const input = (
<editor>
<block>
<text>
One
<anchor />
</text>
<text key>Two</text>
<text>
<focus />
Three
</text>
</block>
</editor>
)
export const output = (
<editor>
<block>
<text>
One
<anchor />
</text>
<text key>
Two
<focus />
</text>
<text>Three</text>
</block>
</editor>
)