Search code examples
vaadinrich-text-editoris-emptyvaadin14

What is the best way to check if the Rich Text Editor Content is empty in Vaadin 14?


I want to check if the Rich Text Editor is empty before proceeding a action. But the .isEmpty() method only works if no format button is clicked. So for example if you just press the H1 format button inside the editor without writing anything the .getValue() method returns:

[{"attributes":{"header":1},"insert":"\n"}]

And because of this the .isEmpty() method doesn't work properly in this case. So I want to check if there is any text or a image inside the editor.

Update: So the closest I can get is using this:

private boolean isEditorEmpty(String value) { //value = RichTextEditor.getHtmlValue()
    if (value.replaceAll("<[^>]*>", "").strip().length() < 1)
        return true;
    return false;
}

But I am not able to exclude pictures, because after adding a picture to the RichTextEditor the .getHtmlValue() method always returns an empty string.

Hope someone can help me


Solution

  • It works using this method to check if the editor is empty:

    private boolean isEditorEmpty(String value) { //value = RichTextEditor.getHtmlValue()
       if (value == null || !value.contains("<img") && value.replaceAll("<[^>]*>", "").strip().length() < 1)
            return true;
        return false;
    }