HTML5 drag & drop text: preventdefault vs move -
i have 2 contenteditable divs formatted content (e.g. paragraphs in bold/italic text etc.) , move selected text 1 another. works natively, want "clean" moved text when dropped, e.g. remove formatting ("<b>hello</b>" become "hello"). decided use html5 drop event this:
drophandler = function(e) { text = e.datatransfer.getdata('text'); if (document.caretrangefrompoint) { range = document.caretrangefrompoint(e.clientx, e.clienty); } else if (document.createrange && oe.rangeparent) { range = document.createrange(); range.setstart(oe.rangeparent, oe.rangeoffset); } range.insertnode(document.createtextnode(text)); return false; };
it works , puts "cleaned" (plain) text correct position, selected text in source contenteditable div not deleted - because of default behaviour prevented "return false" - desired text move becomes text copy. how can achieve default text move behaviour while pre-processing dropped text?
use selection api (except in ie <= 8, doesn't support it):
drophandler = function(e) { text = e.datatransfer.getdata('text'); if (document.caretrangefrompoint) { range = document.caretrangefrompoint(e.clientx, e.clienty); } else if (document.createrange && oe.rangeparent) { range = document.createrange(); range.setstart(oe.rangeparent, oe.rangeoffset); } // keep reference text node can select later var textnode = document.createtextnode(text); range.insertnode(textnode); var sel = window.getselection(); sel.deletefromdocument(); range.selectnodecontents(textnode); sel.removeallranges(); sel.addrange(range); return false; };
Comments
Post a Comment