lexical icon indicating copy to clipboard operation
lexical copied to clipboard

Feature: Trim empty html tags or tags only with spaces

Open Tabris841 opened this issue 3 years ago • 4 comments

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?.

Tabris841 avatar May 09 '23 10:05 Tabris841

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.

ivailop7 avatar May 09 '23 12:05 ivailop7

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 🥳!!!

Tabris841 avatar May 10 '23 10:05 Tabris841

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?

MonstraG avatar Oct 21 '24 12:10 MonstraG

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.

etrepum avatar Oct 21 '24 13:10 etrepum