Is there a way to prevent auto adding div or any other tag when executing insertText on contentEditable div?
For instance I want to copy and paste a paragraph containing many newLine character:
Lorem ipsum dolor sit amet,
consectetur adipiscing elit,
sed do eiusmod tempor
incididunt
it has 4 lines. When I copy and paste it into my contentEditable div, it became:
<div>Lorem ipsum dolor sit amet,</div>
<div>consectetur adipiscing elit,</div>
<div>sed do eiusmod tempor</div>
<div>incididunt</div>
I works fine with insertHTML, but the problem is that sometimes I want to copy and paste HTML elements too like:
<h1>Title</h1>
<p>Hello world</p>
<div>Horayy</div>
I do not want to create html elements inside my contentEditable div.
How can I achieve this?
I can prevent adding div if I input enter key manually, but this case is different, I need to prevent it for copied text.
This is what I have done:
const pastePlainText = useCallback((e: ClipboardEvent) => {
e.preventDefault();
if (e && e.clipboardData) {
var text = e.clipboardData.getData('text/plain');
document.execCommand("insertText", false, text);
}
}, []);