#896: Ensure updateMap calls endCompoundOperation

Ensure endCompoundOperation is called whenever beginCompoundOperation completes, even if removeRange (or anything in the range) fails. Moreover, call `removeRange` only when there is text to remove, since apparently the realtime API complains otherwise.

This is a quick hack — I tested locally "essentially the same changes" done with TamperMonkey, and it appears to work well.