When switching to code view in Summernote, the cursor is always moved all the way to end of the document. Is there any way to keep the cursor location when switching? Sometimes the people who use it want to write some custom HTML because it's faster than using the editor's buttons, but after switching to code view, they have to scroll up and try to find where they were before. It's not very practical.
Here's a simple stackblitz for this.
Basically, what I need to achieve is: when the cursor is here
I want to click the "code view" button and go here:
I post an incomplete and dirty answer to your question, as you asked for it in the comments:
function getCaretCharacterOffsetWithin(element) {
var caretOffset = 0;
var doc = element.ownerDocument || element.document;
var win = doc.defaultView || doc.parentWindow;
var sel;
if (typeof win.getSelection != "undefined") {
sel = win.getSelection();
if (sel.rangeCount > 0) {
var range = win.getSelection().getRangeAt(0);
var preCaretRange = range.cloneRange();
preCaretRange.selectNodeContents(element);
preCaretRange.setEnd(range.endContainer, range.endOffset);
caretOffset = preCaretRange.toString().length;
}
} else if ( (sel = doc.selection) && sel.type != "Control") {
var textRange = sel.createRange();
var preCaretTextRange = doc.body.createTextRange();
preCaretTextRange.moveToElementText(element);
preCaretTextRange.setEndPoint("EndToEnd", textRange);
caretOffset = preCaretTextRange.text.length;
}
return caretOffset;
}
$(document).ready(function() {
$('#summernote').summernote({
callbacks: {
onKeydown: function(e) {
const editable = document.getElementsByClassName('note-editable')[0]
const pos = getCaretCharacterOffsetWithin(editable)
console.log(pos)
const codable = document.getElementsByClassName('note-codable')[0]
codable.setSelectionRange(pos,pos)
}
}
})
$('#summernote').summernote('fullscreen.toggle');
});
The credits for getCaretCharacterOffsetWithin
goes to Tim Down
getCaretCharacterOffsetWithin()
gets caret position in .note-editable
, but not quite right, and the value is not even consistent when you move over paragraph borders back and forth (as Tim warns in his original post).
setSelectionRange(pos,pos)
mirrors the position in .note-editable
to .note-codable
.
Also
code view
and back before it starts to work. And somehow fix this issue.onKeydown
.textarea
Here is https://js-pvbgkh.stackblitz.io
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With