From 3178cad796867f871e58ea0c09d91a5719909dea Mon Sep 17 00:00:00 2001 From: Pleasure1234 <3196812536@qq.com> Date: Fri, 30 Jan 2026 22:37:22 +0000 Subject: [PATCH] fix: handle empty replace term in search and replace functionality (#1562) - Fix 'Empty text nodes are not allowed' error when replace field is empty - Update both replace() and replaceAll() functions to check for empty replaceTerm --- .../search-and-replace/search-and-replace.ts | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/packages/editor-ext/src/lib/search-and-replace/search-and-replace.ts b/packages/editor-ext/src/lib/search-and-replace/search-and-replace.ts index 1ed7632d..2326a50a 100644 --- a/packages/editor-ext/src/lib/search-and-replace/search-and-replace.ts +++ b/packages/editor-ext/src/lib/search-and-replace/search-and-replace.ts @@ -197,11 +197,15 @@ const replace = ( }); const marks = Array.from(marksSet); - - // Delete the old text and insert new text with preserved marks + + // Delete the old text tr.delete(from, to); - tr.insert(from, state.schema.text(replaceTerm, marks)); + // Only insert new text if replaceTerm is not empty (allows for deletion when replaceTerm is empty) + if (replaceTerm) { + tr.insert(from, state.schema.text(replaceTerm, marks)); + } + dispatch(tr); } }; @@ -228,10 +232,14 @@ const replaceAll = ( }); const marks = Array.from(marksSet); - - // Delete and insert with preserved marks + + // Delete the old text tr.delete(from, to); - tr.insert(from, tr.doc.type.schema.text(replaceTerm, marks)); + + // Only insert new text if replaceTerm is not empty (allows for deletion when replaceTerm is empty) + if (replaceTerm) { + tr.insert(from, tr.doc.type.schema.text(replaceTerm, marks)); + } } dispatch(tr);