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
This commit is contained in:
Pleasure1234
2026-01-30 22:37:22 +00:00
committed by GitHub
parent 9d7f8c62c5
commit 3178cad796
@@ -198,9 +198,13 @@ const replace = (
const marks = Array.from(marksSet); 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.delete(from, to);
// 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)); tr.insert(from, state.schema.text(replaceTerm, marks));
}
dispatch(tr); dispatch(tr);
} }
@@ -229,10 +233,14 @@ const replaceAll = (
const marks = Array.from(marksSet); const marks = Array.from(marksSet);
// Delete and insert with preserved marks // Delete the old text
tr.delete(from, to); tr.delete(from, to);
// 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)); tr.insert(from, tr.doc.type.schema.text(replaceTerm, marks));
} }
}
dispatch(tr); dispatch(tr);
}; };