I have an editor which is a contenteditable and I'm trying to figure out how to get pasting to work correctly. I tried the simple:
self.editorIframeDocument.addEventListener('paste', function () {
setTimeout(function () {
_setText(self.editor, _getText(self.editor));
}, 1);
});
editorIframeDocument
== the contenteditable document
.
_setText
is an internal function for cleaning the input used throughout the code.
_getText
is simply innerText
when it can, or a shim when it cant (Firefox).
So, the issue is, upon pasting the cursor moves. For example:
+==========+
+ |
+ WORD
+
+===========
If I paste WORD
there (pretending it's the editor) the cursor ends up where you see the pipe above it.
How can I always have the cursor at the end of what the user pasted, or, the previous cursor position to be exact. I see I can use extendOffset
property in Selection
, but I'm not sure how. I also tried doing collapseToEnd
, but that puts the cursor at the end of the entire editor.
Any ideas? If you want the full source code: https://github.com/OscarGodson/EpicEditor/blob/feature/ticket-100/src/editor.js
It's in the branch feature/ticket-100