I have a contenteditable div which contains typical wysiwyg editor html (bold, anchors, lists).
I need to determine if the current cursor is, onKeyDown, at the start and at the end of the div. The reason for this is, based on the cursor position, and the key pressed, I might want to merge this div with the previous div on a backspace, or create a new following div on enter.
I've been fiddling around with ranges, but when you're working with html inside the element, things get pretty complicated.
I'm hoping I must be overlooking some simple solution.
Is there a relatively simple way to determine this - I'm open to using a library like Rangy.
Thanks!
Edit: I'm thinking something along these lines:
$('.mycontenteditable').bind('keydown', handle_keydown)
handle_keydown = function(e) {
range = window.getSelection().getRangeAt(0)
start_range = document.createRange()
start_range.selectNodeContents(this.firstChild)
start_range.collapse(true) // collapse to start
is_start = start_range.compareBoundaryPoints(Range.START_TO_START,range)
end_range = document.createRange()
end_range.selectNodeContents(this.lastChild)
end_range.collapse(false)
is_end = end_range.compareBoundaryPoints(Range.END_TO_END,range)
}
Am I going to run into any odd issues with something like this?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…