Skip to content

Commit 81b5939

Browse files
committed
fix: preserve line breaks when pasting text
Pasting text with line breaks via document.execCommand stopped working: - It used to preserve line breaks. - Now it replaces them with brs. So I switched to the Selection API.
1 parent 4815839 commit 81b5939

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

src/editor.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,10 @@ class CodeElement extends EventTarget {
101101
// get text representation of clipboard
102102
const text = (event.originalEvent || event).clipboardData.getData("text/plain");
103103
// insert text manually
104-
document.execCommand("insertText", false, text);
104+
const selection = window.getSelection();
105+
selection.deleteFromDocument();
106+
selection.getRangeAt(0).insertNode(document.createTextNode(text));
107+
selection.collapseToEnd();
105108
}
106109

107110
// fixFormatting removes invalid formatting from the code,

0 commit comments

Comments
 (0)