From 4bc552f71ff75c4b7ad18386a65dabdd8b4e1bba Mon Sep 17 00:00:00 2001 From: Alex <32290337+ahoisl@users.noreply.github.com> Date: Sat, 23 Nov 2024 01:01:11 +0100 Subject: [PATCH] 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 --- .changeset/large-cars-itch.md | 5 +++++ packages/slate/src/core/normalize-node.ts | 6 +++++- packages/slate/test/normalization/block/remove-block.tsx | 2 +- packages/slate/test/normalization/inline/remove-block.tsx | 2 +- 4 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 .changeset/large-cars-itch.md diff --git a/.changeset/large-cars-itch.md b/.changeset/large-cars-itch.md new file mode 100644 index 0000000000..059ffdc2cb --- /dev/null +++ b/.changeset/large-cars-itch.md @@ -0,0 +1,5 @@ +--- +'slate': patch +--- + +Keep data in normalization when blocks are not allowed diff --git a/packages/slate/src/core/normalize-node.ts b/packages/slate/src/core/normalize-node.ts index 467f96b3ba..ba704d9a1c 100644 --- a/packages/slate/src/core/normalize-node.ts +++ b/packages/slate/src/core/normalize-node.ts @@ -53,7 +53,11 @@ export const normalizeNode: WithEditorFirstArg = ( // 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. diff --git a/packages/slate/test/normalization/block/remove-block.tsx b/packages/slate/test/normalization/block/remove-block.tsx index cc5d1e27d3..717ee448de 100644 --- a/packages/slate/test/normalization/block/remove-block.tsx +++ b/packages/slate/test/normalization/block/remove-block.tsx @@ -12,7 +12,7 @@ export const input = ( export const output = ( - one + onetwo ) diff --git a/packages/slate/test/normalization/inline/remove-block.tsx b/packages/slate/test/normalization/inline/remove-block.tsx index ad0e8a6220..f2c8064421 100644 --- a/packages/slate/test/normalization/inline/remove-block.tsx +++ b/packages/slate/test/normalization/inline/remove-block.tsx @@ -20,7 +20,7 @@ export const output = ( - twofour + onetwothreefour