I was able to make a little progress here:
https://github.com/discourse/discourse/commit/75d9c16156f79f5a9f30a04433c3cb0ae82d914c
This is nowhere near as comprehensive as the long-term work @sam described. But I think it should help in the short-term. This should preserve undo history when pasting rich text, quoting, and when using (most) composer buttons/keyboard-shortcuts.
It’s live on Meta now - shout if you notice any issues.