From 8d502d2cb7a60409193e973a7b26d4e97df02cce Mon Sep 17 00:00:00 2001 From: Alessio Gravili <70709113+AlessioGr@users.noreply.github.com> Date: Mon, 12 Feb 2024 14:27:58 +0100 Subject: [PATCH] fix(richtext-lexical): do not remove adjacent paragraph node when inserting certain nodes in empty editor (#5061) --- .../src/field/features/Blocks/plugin/index.tsx | 12 ++++++++++-- .../field/features/Relationship/plugins/index.tsx | 12 ++++++++++-- .../src/field/features/Upload/plugin/index.tsx | 12 ++++++++++-- 3 files changed, 30 insertions(+), 6 deletions(-) diff --git a/packages/richtext-lexical/src/field/features/Blocks/plugin/index.tsx b/packages/richtext-lexical/src/field/features/Blocks/plugin/index.tsx index 2ead139056..f471cb2989 100644 --- a/packages/richtext-lexical/src/field/features/Blocks/plugin/index.tsx +++ b/packages/richtext-lexical/src/field/features/Blocks/plugin/index.tsx @@ -39,8 +39,16 @@ export function BlocksPlugin(): JSX.Element | null { const { focus } = selection const focusNode = focus.getNode() - // First, delete currently selected node if it's an empty paragraph - if ($isParagraphNode(focusNode) && focusNode.getTextContentSize() === 0) { + // First, delete currently selected node if it's an empty paragraph and if there are sufficient + // paragraph nodes (more than 1) left in the parent node, so that we don't "trap" the user + if ( + $isParagraphNode(focusNode) && + focusNode.getTextContentSize() === 0 && + focusNode + .getParent() + .getChildren() + .filter((node) => $isParagraphNode(node)).length > 1 + ) { focusNode.remove() } diff --git a/packages/richtext-lexical/src/field/features/Relationship/plugins/index.tsx b/packages/richtext-lexical/src/field/features/Relationship/plugins/index.tsx index 3658f65843..ac1a40a38b 100644 --- a/packages/richtext-lexical/src/field/features/Relationship/plugins/index.tsx +++ b/packages/richtext-lexical/src/field/features/Relationship/plugins/index.tsx @@ -54,8 +54,16 @@ export function RelationshipPlugin(props?: RelationshipFeatureProps): JSX.Elemen const { focus } = selection const focusNode = focus.getNode() - // First, delete currently selected node if it's an empty paragraph - if ($isParagraphNode(focusNode) && focusNode.getTextContentSize() === 0) { + // First, delete currently selected node if it's an empty paragraph and if there are sufficient + // paragraph nodes (more than 1) left in the parent node, so that we don't "trap" the user + if ( + $isParagraphNode(focusNode) && + focusNode.getTextContentSize() === 0 && + focusNode + .getParent() + .getChildren() + .filter((node) => $isParagraphNode(node)).length > 1 + ) { focusNode.remove() } diff --git a/packages/richtext-lexical/src/field/features/Upload/plugin/index.tsx b/packages/richtext-lexical/src/field/features/Upload/plugin/index.tsx index 2be5cd9203..0c45d57e8c 100644 --- a/packages/richtext-lexical/src/field/features/Upload/plugin/index.tsx +++ b/packages/richtext-lexical/src/field/features/Upload/plugin/index.tsx @@ -53,8 +53,16 @@ export function UploadPlugin(): JSX.Element | null { const { focus } = selection const focusNode = focus.getNode() - // First, delete currently selected node if it's an empty paragraph - if ($isParagraphNode(focusNode) && focusNode.getTextContentSize() === 0) { + // First, delete currently selected node if it's an empty paragraph and if there are sufficient + // paragraph nodes (more than 1) left in the parent node, so that we don't "trap" the user + if ( + $isParagraphNode(focusNode) && + focusNode.getTextContentSize() === 0 && + focusNode + .getParent() + .getChildren() + .filter((node) => $isParagraphNode(node)).length > 1 + ) { focusNode.remove() }