Hello,
I am testing a ProseMirror based editor as a part of a form. The problem I’ve encountered is how to get the value
of the editor when it is empty.
Currently I am using the following code to get the value
(editor content)
function getHTML(state: EditorState) {
const fragment = DOMSerializer
.fromSchema(state.schema)
.serializeFragment(state.doc.content);
const element = document.createElement('div');
element.appendChild(fragment);
return element.innerHTML;
};
I’ve noticed this returns <p></p>
even if there is no content and the user did not interact with the editor area.
The more interesting part is that I’ve got a <p><br /><p>
result if there was content and the user deleted it using the backspace key.
Is there any clever solution that will allow me to get consistent HTML result if there is no content in the editor so I can validate and/or execute other business logic?