How to toggle case without losing marks

I’m trying to implement a “toggle case” button for my editor.

Easy enough using replaceRange; however, the range might have some marks defined, and replaceRange seems to wipe out any marks that were defined in the range, and I need to avoid that.

Is there any way I could change the case of the text (even if it was one character at a time), but leave the marks intact?

There’s currently no way to change text without impacting the markers in it.