Skip to content

Commit

Permalink
Fix normalizeNode to keep text/inline nodes when removing blocks (#…
Browse files Browse the repository at this point in the history
…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
  • Loading branch information
ahoisl authored Nov 23, 2024
1 parent 90fbcde commit 4bc552f
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 3 deletions.
5 changes: 5 additions & 0 deletions .changeset/large-cars-itch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'slate': patch
---

Keep data in normalization when blocks are not allowed
6 changes: 5 additions & 1 deletion packages/slate/src/core/normalize-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,11 @@ export const normalizeNode: WithEditorFirstArg<Editor['normalizeNode']> = (
// other inline nodes, or parent blocks that only contain inlines and
// text.
if (isInlineOrText !== shouldHaveInlines) {
Transforms.removeNodes(editor, { at: path.concat(n), voids: true })
if (isInlineOrText) {
Transforms.removeNodes(editor, { at: path.concat(n), voids: true })
} else {
Transforms.unwrapNodes(editor, { at: path.concat(n), voids: true })
}
n--
} else if (Element.isElement(child)) {
// Ensure that inline nodes are surrounded by text nodes.
Expand Down
2 changes: 1 addition & 1 deletion packages/slate/test/normalization/block/remove-block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const input = (
export const output = (
<editor>
<block>
<text>one</text>
<text>onetwo</text>
</block>
</editor>
)
2 changes: 1 addition & 1 deletion packages/slate/test/normalization/inline/remove-block.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const output = (
<block>
<text />
<inline>
<text>twofour</text>
<text>onetwothreefour</text>
</inline>
<text />
</block>
Expand Down

0 comments on commit 4bc552f

Please sign in to comment.