Skip to content

Commit

Permalink
fix unwrapNode when split && add in nested block (#3820)
Browse files Browse the repository at this point in the history
* fix unwrapNode when splite && add in nested block

* Update packages/slate/src/transforms/node.ts

chore: add comment

Co-authored-by: Dylan Schiemann <dylan@dojotoolkit.org>

* chore: prettier fix

* chore: add changeset

* Update packages/slate/src/transforms/node.ts

chore: update comments

Co-authored-by: Dylan Schiemann <dylan@dojotoolkit.org>

Co-authored-by: Dylan Schiemann <dylan@dojotoolkit.org>
  • Loading branch information
githoniel and dylans committed Aug 12, 2021
1 parent 906e5af commit c6203a2
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/shy-planets-eat.md
@@ -0,0 +1,5 @@
---
'slate': minor
---

unwrapNode call liftNode in reverse order to keep nested block
8 changes: 7 additions & 1 deletion packages/slate/src/transforms/node.ts
Expand Up @@ -848,7 +848,13 @@ export const NodeTransforms: NodeTransforms = {

const rangeRef = Range.isRange(at) ? Editor.rangeRef(editor, at) : null
const matches = Editor.nodes(editor, { at, match, mode, voids })
const pathRefs = Array.from(matches, ([, p]) => Editor.pathRef(editor, p))
const pathRefs = Array.from(
matches,
([, p]) => Editor.pathRef(editor, p)
// unwrapNode will call liftNode which does not support splitting the node when nested.
// If we do not reverse the order and call it from top to the bottom, it will remove all blocks
// that wrap target node. So we reverse the order.
).reverse()

for (const pathRef of pathRefs) {
const path = pathRef.unref()!
Expand Down
@@ -0,0 +1,43 @@
/** @jsx jsx */
import { jsx } from '../../..'
import { Transforms } from 'slate'

export const run = editor => {
Transforms.unwrapNodes(editor, {
match: n => !!n.a,
mode: 'all',
split: true,
})
}
export const input = (
<editor>
<block a>
<block a>
<block>one</block>
<block>
<cursor />
word
</block>
<block>now</block>
</block>
</block>
</editor>
)
export const output = (
<editor>
<block a>
<block a>
<block>one</block>
</block>
</block>
<block>
<cursor />
word
</block>
<block a>
<block a>
<block>now</block>
</block>
</block>
</editor>
)

0 comments on commit c6203a2

Please sign in to comment.