I have noticed that, when pasting text from certain sources that contain a hard break, an extra <br> is added.
For example, when pasting text from google docs with one hard break between each paragraph, the hard break will be parsed into a <p> with 2 <br> insides, one with “ProseMirror-trailingBreak” and one without.
Another thing i’ve noticed about it, i’m pretty sure that it happens where the <br> in the pasted HTML is not wrapped in a paragraph, then prosemirror will wrap it in a <p> and add another <br>.
if the <br> on the pasted html is already wrapped with a <p>, it doesn’t happen.
I’d love to know if there is a way to prevent the extra <br>. thank you!
It’s parsed as having only one hard break. The second is only added in the view to make sure it renders properly and allows a cursor to appear after the break, it’s not part of the document.
But it does have an affect- the 2 hard breaks are creating 2 empty lines instead of one, I can put my cursor on each one separately, which is the behavior I do not want.
(the view.state.schema actually does not include the extra hard breaks, but they are rendered in the dom)
For now, I have used the Element.remove() method to remove all hard breaks without the prosemirror class on handlePaste() directly on the view.dom, and it is giving me the desired behavior,
but I feel as it is not the best practice (documentation says its probably not a good idea to make changes directly on the view.dom)
I think the issue comes up when the source html in the clipboard has br’s sibling to p’s. So the google docs content has this
When copied to the clipboard the clipboard has <p>Line1</p><p>Line2</p><br><p>AfterBlank</p> and the br gets parsed as <p><br/></p>. So it looks like 2 lines instead of the intended single line (perhaps just as <p></p>.
A <p><br></p>should have 2 lines as the paragraph implies one line and the br represents a hard break so visibly that would/should look like 2 lines. The ProseMirror-trailingBreak is added in for an empty paragraph to ensure it does not collapse. The issue here is that google docs probably should have included an empty paragraph (without the br) -or- a br within the paragraphs it included. Instead they put a <br/> sibling to the <p>. A br is an inline in prosemirror’s nodes and so it ends up generating a containing block here - the <p>. So if you wanted to work around this correctly then would have to do this for that scenario and therefore inspect the html not the resulting fragment/nodes. Because if you instead used Word and had a break within a paragraph you’d lose the explicit break. It would be best if prosemirror could handle this but since technically it doesn’t know about the node types (you supply the schema) I doubt it can handle this.
It’s difficult to comment without specific steps to be able to see the issue. Perhaps try stepping through the code to see where the issue you are having arises?
when parsed with DOMSerializer, the Prosemirror-trailingBreak gets removed, meaning the </br> before also doesn’t get shown. Resulting in something like this:
Arguably the Prosemirror-trailingBreak shouldn’t get removed in this specific situation.
No, this is working as expected. We need the dummy <br> during editing, to make it possible to type after the <br> before it. But it’s not part of the document, so it shouldn’t be serialized.
Understandable. However, the dummy <br> does create a discrepancy between how the text looks in the editor and how the serialised content looks. Do you have any suggestions on how to solve this?
is there any workaround here? I don’t understand how people are able to copy/paste text with multiple new lines into the editor? There is always an extra 1 tag added. So for n number of new lines in a row, I have n+1 new lines. I tried hiding the extra with “display: none”, but that creates other issues which take more work to fix. Would be great if someone has a solution