We could definitely do better here. A lot of our toolbar buttons and fancy-paste behaviour directly set the value of the textarea in JavaScript. This totally breaks the native browser undo/redo history.
Instead, whenever we make programmatic modifications to the textarea, we should be using document.execCommand (as @seanblue mentioned). That way, the browser interprets it the same as a user action, and inserts it cleanly into the undo/redo history.
the
insertTextcommand, which you can use to programmatically replace text at the cursor while preserving the undo buffer (edit history) in plaintextareaandinputelements.