I am looking to create the following:
Contenteditable div, that has simple HTML in it, for example:
<div contenteditable="true">
<p>This is a paragraph of <b>bold</b> <span class="draggable" draggable="true" contenteditable="false">text</span></p>
<p>This is another para<br />graph</p>
</div>
My goal is for the user to be able to edit the contents of the div, but not the individual .draggable
spans. Instead, they should be able to drag and drop them to where they like - but I would also like that while dragging, the cursor/caret would snap to spaces between words - so that the user can only drop the span
between words, not inside them.
Ideally, once the user drops the span
, it would be placed exactly between words, ie, there should be 1 space before and after it (and the spaces from its previous position should be removed).
My main question is, how can I snap/position the caret (blinking cursor) between the words while dragging the span
elements? I need this solution to work in all modern browsers and if possible, also IE8 and up.
My secondary question is how can I ensure that there is always a space before and after the span
after dropping it?