monaco-editor: `setValue` without triggering change events

We only want to listen to user-triggered events. It looks like _resetValue would allow for this (I get it, it’s supposed to be private). However, we can’t seem to use it because the necessary function toRawText defined on TextModel seems to be unaccessible.

How can we change the editor text value without triggering a change event?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Reactions: 21
  • Comments: 17 (2 by maintainers)

Most upvoted comments

Thats essentially what I’m doing @alexandrudima. To avoid race conditions I think it would be great to have a way to know what caused the event, e.g. the user or the code. This would also be valuable for widgets and other feedback.

I had the same problem and found a solution/workaround : isFlush property from onDidChangeModelContent event. Try this :

editor.onDidChangeModelContent(e => {
  if (e.isFlush) {
    // true if setValue call
  } else {
    // false if user input
  }
});

This is a common problem I also hit often. Usually I use a code pattern like the following:

var ignoreEventBecauseIAmTriggeringIt = false;

emitter.onBla(function(e) {
  if (ignoreEventBecauseIAmTriggeringIt) {
    return;
  }
  console.log('Now I should react to ', e, ' because someone else caused it');
});

...

// Do something that makes `onBla` fire:
try {
  ignoreEventBecauseIAmTriggeringIt = true;
  emitter.changeBla(..);
} finally {
  ignoreEventBecauseIAmTriggeringIt = false;
}

+1. For eg. In Codemirror for every change you also get it’s origin - if it was a user input or setValue call.

The distinction lies in the event source. Are events triggered by the user or code? In my case I want to listen to user triggered events, but also set the editor content and cursor position using code. The current solution is to unbind and rebind listeners each time. Is this clearer?

agreed it would be useful to have this distinction - would make it play with angular’s ControlValueAccessor API much better: https://github.com/angular/angular/issues/20230#issuecomment-361799387