Skip to content

Commit 4bc552f

Browse files
authoredNov 23, 2024··
Fix normalizeNode to keep text/inline nodes when removing blocks (#5768)
* fix: fix data loss in base `normalizeData` if no blocks allowed Use `unwrapNodes` instead of `removeNodes` to convert block to inline/text nodes. * changeset
1 parent 90fbcde commit 4bc552f

File tree

4 files changed

+12
-3
lines changed

4 files changed

+12
-3
lines changed
 

‎.changeset/large-cars-itch.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'slate': patch
3+
---
4+
5+
Keep data in normalization when blocks are not allowed

‎packages/slate/src/core/normalize-node.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,11 @@ export const normalizeNode: WithEditorFirstArg<Editor['normalizeNode']> = (
5353
// other inline nodes, or parent blocks that only contain inlines and
5454
// text.
5555
if (isInlineOrText !== shouldHaveInlines) {
56-
Transforms.removeNodes(editor, { at: path.concat(n), voids: true })
56+
if (isInlineOrText) {
57+
Transforms.removeNodes(editor, { at: path.concat(n), voids: true })
58+
} else {
59+
Transforms.unwrapNodes(editor, { at: path.concat(n), voids: true })
60+
}
5761
n--
5862
} else if (Element.isElement(child)) {
5963
// Ensure that inline nodes are surrounded by text nodes.

‎packages/slate/test/normalization/block/remove-block.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ export const input = (
1212
export const output = (
1313
<editor>
1414
<block>
15-
<text>one</text>
15+
<text>onetwo</text>
1616
</block>
1717
</editor>
1818
)

‎packages/slate/test/normalization/inline/remove-block.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export const output = (
2020
<block>
2121
<text />
2222
<inline>
23-
<text>twofour</text>
23+
<text>onetwothreefour</text>
2424
</inline>
2525
<text />
2626
</block>

0 commit comments

Comments
 (0)
Please sign in to comment.