jquery.selection icon indicating copy to clipboard operation
jquery.selection copied to clipboard

Implement undo/redo when changing the content of the textarea

Open cnicodeme opened this issue 10 years ago • 0 comments

Hi! :)

Great plugin, I'm gonna use it for my project, but I think it's missing something really interesting : the ability to undo/redo (ctrl+z / ctrl+shit+z) from the natural work of the browser.

Let me explain

If you write on a textarea, and then hit ctrl+z, you'll do a undo changes (based on your browser).

But if you do a insert/replace from your code, and then hit ctrl+z, it won't work.

The idea behind it is to not change the val() of the textarea, but use the builtin changes of the browser, like this :

var element = document.getElementById('someTextarea');
var text = 'This text will be inserted in the textarea';
var event = document.createEvent('TextEvent');

event.initTextEvent('textInput', true, true, null, text);
element.dispatchEvent(event); // fire the event on the the textarea

(source: http://stackoverflow.com/questions/7553430/javascript-textarea-undo-redo)

Drawback: this does not work in IE9 and Firefox :/ (Maybe i'm asking for an unicorn feature ?)

Here's a possible implementation : http://stackoverflow.com/questions/19814465/is-it-possible-to-insert-text-in-textarea-and-update-undo-redo-queue

And for IE (11) : http://stackoverflow.com/questions/20124212/internet-explorer-alternative-to-document-execcommandinserttext-for-tex

In case you have some spare time! ;)

cnicodeme avatar Mar 17 '14 09:03 cnicodeme