Feature: Trim empty html tags or tags only with spaces
Hello, I have this use case. User write some content in the lexical editor with some spaces in the front and in the end and when submitting the editor state, I have to trim the spaces.
For example:
I am getting the editor state using $generateHtmlFromNodes as the docs suggests
const submitEditorState = useCallback(() => {
editor.update(async () => {
const htmlString = $generateHtmlFromNodes(editor, null);
editor.setEditable(false);
await cb(htmlString);
editor.dispatchCommand(CLEAR_EDITOR_COMMAND, undefined);
editor.setEditable(true);
});
}, [cb, editor]);
And as output I get for example this string:
'<p><br></p><p><br></p><p><span> </span></p><p dir="ltr"><span>Some text</span></p><p dir="ltr"><br></p><p dir="ltr"><br></p><p dir="ltr"><span>Second text</span></p><p dir="ltr"><br></p><p dir="ltr"><br></p>'
So my question is. Is there a way to trim/remove empty spaces/html tags in the beginning and in the end of editor state, when trying to submit the editor state?.
You can iterate through the editorState nodes before you serialize into HTML and call 'isEmpty()' on the node and delete it if true until the first non-empty node and the same in resvere for the trailing empty paragraphs, however an empty paragraph/quote/heading is a valid node state, so this logic will need to be on your side.
Hello @ivailop7, thanks for the idea! After some more investigation and playing around with the editor state, I came up the this solution, to clear empty spaces before and after the content.
const submitEditorState = useCallback(() => {
editor.update(async () => {
const editorState = editor.getEditorState();
const nodes = Array.from(editorState._nodeMap);
const filterEmptyNodes = (nodes: [string, LexicalNode][]) => {
for (const [, node] of nodes) {
if (node instanceof ParagraphNode) {
const content = node.getTextContent().trim();
if (content) {
break;
}
node.remove();
}
}
};
filterEmptyNodes(nodes);
filterEmptyNodes(nodes.reverse());
const htmlString = $generateHtmlFromNodes(editor, null);
editor.setEditable(false);
await cb(htmlString);
editor.dispatchCommand(CLEAR_EDITOR_COMMAND, undefined);
editor.setEditable(true);
});
}, [cb, editor]);
There could be probably smarter solution, but it works in my case. I will leave the code snipped, for anyone who faces the same problem/requirement. Again, thanks for the advice and cheers 🥳!!!
This solution isn't working for me. I need to support autosaving that happens while writing and therefore I cannot just remove the nodes because that might change cursor position, for example.
If I was able to clone editorState, mutate it, and then pass to $generateHtmlFromNodes, it would have been nice.
Now, I think I will have to parse the html output myself and remove the empty tags, which feels like a waste.
Any ideas?
You don't need to clone EditorState, they are already read-only by the time you can access it with getEditorState. In order to clone and mutate it without affecting your editor, you need to create a new editor with the same config and EditorState and do the update there. It doesn't need to be in the DOM.