Skip to content

[editor] Fix Undo handling of Markdown text added programmatically

When we insert text into a <textarea> programmatically, such as when adding markdown via a toolbar button (bolding, etc) or auto-continuing a list, we have been simply updating the value of the textarea. This bypasses the natural undo handling of the browser, making it impossible for the user to properly undo the added text.

The execCommand is officially deprecated. However, for insertText, there is currently no alternative. We need to use it in order to trigger the browser's undo tracking when we insert text.

Per https://developer.mozilla.org/en-US/docs/Web/API/Document/execCommand on 2022-04-11,

The Clipboard API can be used instead of execCommand in many cases, but execCommand is still sometimes useful. In particular, the Clipboard API doesn't replace the insertText command

So we attempt to use it if possible. Otherwise, fall back to just replacing the value as before. In this case, Undo will be broken with inserted text.

Testing on older versions of Firefox:

  • 87 and below: does not work and falls through to just replacing value. 87 was released in Mar 2021
  • 89 and above: works well. 89 was released in May 2021
before after
2022-04-13_17.02.56 2022-04-13_17.22.46

Related to #250634 (closed)

Edited by Brett Walker

Merge request reports