Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Manipulating undo state in codemirror

So I'm using CodeMirror, and I'd like a way to omit certain edits from the undo state. In particular, I've got a situation where I want one keystroke to

  1. Replace a portion of the mirror text AND
  2. Auto-indent the fresh region

Doing this naively would mean that using the keystroke, then hitting undo would leave the mirror containing the new text without the indentation. I'd like a single undo to restore the initial text rather than going to the unindented version of the replaced text.

The only API-supported approach seems to be doing a .getHistory call before the indent, followed by a .setHistory call immediately afterwards, but the docs imply that this is a bad idea. Specifically, the effects of this are undefined if the contents of the mirror changed between .getHistory and .setHistory calls, which is the whole point in this situation.

There's also an addToHistory flag in the text marking API, but it's only available marking rather than arbitrary edits like indentation.

Is there a good way to do what I'm looking for here?

like image 767
Inaimathi Avatar asked Dec 06 '25 09:12

Inaimathi


1 Answers

Changes made within a single operation will result in only a single history event.

If arranging for a single operation isn't viable, the origin field of a change (settable as an argument to replaceRange and replaceSelection, and in other cases a little more awkwardly by registering a beforeChange event handler) determines the type of history-event-combination that CodeMirror does. If you assign an origin that starts with an asterisk (*) character, subsequent changes with the same origin will be combined. If the origin starts with a +, subsequent same-origin changes will be combined when they occur within options.historyEventDelay milliseconds.

like image 191
Marijn Avatar answered Dec 10 '25 13:12

Marijn



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!