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
@@ -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);