Skip to content

Commit

Permalink
Fix mergeNodes moving node into parent sibling (#4296)
Browse files Browse the repository at this point in the history
* test: add test case for bug

* prefer next will transforming selection in remove_node

* add remove_node test

* Add changeset

* review: handle nested blocks

* refactor

* Revert "refactor"

This reverts commit 45a8aab.

Co-authored-by: Dylan Schiemann <dylan@dojotoolkit.org>
  • Loading branch information
kellyjosephprice and dylans authored Aug 11, 2021
1 parent 4dea740 commit 479a759
Show file tree
Hide file tree
Showing 8 changed files with 187 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/orange-jeans-sing.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate': patch
---

Fix mergeNodes moving node into parent sibling
13 changes: 12 additions & 1 deletion packages/slate/src/transforms/general.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,18 @@ const applyToDraft = (editor: Editor, selection: Selection, op: Operation) => {
}
}

if (prev) {
let preferNext = false
if (prev && next) {
if (Path.equals(next[1], path)) {
preferNext = !Path.hasPrevious(next[1])
} else {
preferNext =
Path.common(prev[1], path).length <
Path.common(next[1], path).length
}
}

if (prev && !preferNext) {
point.path = prev[1]
point.offset = prev[0].text.length
} else if (next) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/** @jsx jsx */
import { jsx } from '../..'

export const input = (
<editor>
<block>
<text />
</block>
<block>
<block>
<cursor />
<text />
</block>
</block>
</editor>
)
export const output = (
<editor>
<block>
<text />
</block>
<block>
<block>
<cursor />
</block>
</block>
</editor>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/** @jsx jsx */
import { jsx } from '../..'

export const input = (
<editor>
<block>
<text />
</block>
<block>
<cursor />
<text />
</block>
</editor>
)
export const output = (
<editor>
<block>
<text />
</block>
<block>
<cursor />
</block>
</editor>
)
35 changes: 35 additions & 0 deletions packages/slate/test/operations/remove_node/cursor-nested.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/** @jsx jsx */
import { jsx } from 'slate-hyperscript'

export const input = (
<editor>
<element>
<text />
</element>
<element>
<element>
<cursor />
<text />
</element>
</element>
</editor>
)
export const operations = [
{
type: 'remove_node',
path: [1, 0, 0],
node: { text: '' },
},
]
export const output = (
<editor>
<element>
<text />
</element>
<element>
<element>
<cursor />
</element>
</element>
</editor>
)
31 changes: 31 additions & 0 deletions packages/slate/test/operations/remove_node/cursor.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/** @jsx jsx */
import { jsx } from 'slate-hyperscript'

export const input = (
<editor>
<element>
<text />
</element>
<element>
<cursor />
<text />
</element>
</editor>
)
export const operations = [
{
type: 'remove_node',
path: [1, 0],
node: { text: '' },
},
]
export const output = (
<editor>
<element>
<text />
</element>
<element>
<cursor />
</element>
</editor>
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/** @jsx jsx */
import { Transforms, Text } from 'slate'
import { jsx } from '../../..'

export const input = (
<editor>
<block>one</block>
<block>
<block>
<cursor />
<text />
</block>
</block>
</editor>
)
export const run = editor => {
Transforms.mergeNodes(editor, { at: [1, 0, 1], match: Text.isText })
}
export const output = (
<editor>
<block>one</block>
<block>
<block>
<cursor />
</block>
</block>
</editor>
)
24 changes: 24 additions & 0 deletions packages/slate/test/transforms/mergeNodes/path/text-hanging.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/** @jsx jsx */
import { Transforms, Text } from 'slate'
import { jsx } from '../../..'

export const input = (
<editor>
<block>one</block>
<block>
<cursor />
<text />
</block>
</editor>
)
export const run = editor => {
Transforms.mergeNodes(editor, { at: [1, 1], match: Text.isText })
}
export const output = (
<editor>
<block>one</block>
<block>
<cursor />
</block>
</editor>
)

0 comments on commit 479a759

Please sign in to comment.