There’s a toolbar for manipulation of the text in the textarea. (e.g. surround the selected text with some template)
I was wondering if there is a easy way to achieve this, currently when using the system undo/redo, it messes the text up (I think the system is only keeping track of deltas between edits).
You can probably simulate
events to manipulate the contents of the textarea. The changes made that way are respected by undo/redo, I think (I know they are in Safari)
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
Basically, the text is inserted as though you pasted it yourself. So if something is selected, it’ll be be replaced with the text. If there’s no selection, the text will be inserted at the caret’s position. And undo/redo should work normally (undoing/redoing the entire inserted string in one go), because the browser acts as if you typed/pasted it yourself.
As I said, I know this works like a charm with undo/redo in Safari, so I’d assume it works in Chrome as well.