I can easily do execcommand on a contenteditable selection if using a button. However using any other element fails.
Why is this and how can I make it work using a div element.
Thanks.
I can easily do execcommand on a contenteditable selection if using a button. However using any other element fails.
Why is this and how can I make it work using a div element.
Thanks.
What is happening is that when you click a text node, the browser wants to select that text. Buttons are not a part of the text flow and will therefore not trigger a new selection.
All you have to do is prevent the browser from performing a re-selection. When the
click
event you are currently observing runs the selection has already occurred and it's too late to act. The action starts to take place onmousedown
for all browsers expect MSIE, which has a special selection events.This works for me across browsers:
There are a few complications with using MSIE's selection events (such as having to block dragging as well), so it's easier just to knock out the selectability of the element you are using as a button with the specialized attribute
unselectable
(needs the value "on").You can obviously still perform the
execCommand
handling inclick
events and just run the selection prevention code on all elements intended as "buttons":NicEdit uses a combination of methods.
Most toolbar elements have the "unselectable" attribute set - this works for Internet Explorer.
On the same elements, it registers for the "mousedown" event and overrides the default action - this prevents both text solution and focus. This is for browsers other than IE only.
Some toolbar elements need to be able to receive text focus and make selections, such as the field for inserting a link or image. For these, it saves the selection before the editor loses focus, then restores it after the user has finished editing and the changes need to be made.
For how to implement it, check NicEdit's code. Search for the function names:
...
This uses the browser's getSelection() or document.selection to get the selection, getRangeAt() to convert it to a range, and addRange() or select() to restore that range to as a selection.
Most, if not all, of the WYSIWYG editors out there use an
iframe
element in order to not lose the selection. Another approach, although I haven't tried it, would be to store each selection made on that page after themouseup
event triggers.Take a look at this page about Midas, Gecko's built-in rich text editor.
You could save the selection by using the
mousedown
event on the element being used instead of a button and restore it again after focussing the editable element in theclick
event.I've modified the example code: http://jsbin.com/atike/40/edit. Here's the code: