monaco-editor icon indicating copy to clipboard operation
monaco-editor copied to clipboard

`setValue` without triggering change events

Open den1k opened this issue 7 years ago • 17 comments

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?

den1k avatar Apr 28 '17 18:04 den1k

I'm not sure I understand the intent here. Any changes done to the model will result in change events being emitted (e.g. to update the view or the language services that provide rich language features). What would be the use case for changing a model and inhibiting events?

alexdima avatar Jun 07 '17 17:06 alexdima

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?

den1k avatar Jun 07 '17 18:06 den1k

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;
}

alexdima avatar Jun 09 '17 09:06 alexdima

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.

den1k avatar Jun 09 '17 12:06 den1k

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

chinchang avatar Nov 22 '18 13:11 chinchang

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

artparks avatar Mar 21 '19 12:03 artparks

It would be great if the 'source' parameter of the 'executeEdits' function could be obtained in the 'onDidChangeModelContent' function.

maxxcs avatar Mar 29 '19 02:03 maxxcs

after toggling to a different file , im unable to get previous changes using ctrl + z for previous file..

any help would be appreciable

manideepmothe641 avatar May 06 '19 12:05 manideepmothe641

This is still an issue and a fairly important one. Any update on this?

stanchovy avatar Sep 04 '19 12:09 stanchovy

This has cropped up for me as well. Trying to build an instance so an instance of the editor and model can be edited from a web socket event, but I don't want it to trigger the event

worksofliam avatar Jul 12 '20 13:07 worksofliam

I'll just add here that it's fairly standard practice to be have this functionality.... Ace editor, Code mirror, even back as far as Jquery widgets have this.

stanchovy avatar Jul 12 '20 16:07 stanchovy

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
  }
});

Sharkou avatar Dec 21 '20 21:12 Sharkou

Any update on this issue?

shaneapen avatar Jun 15 '21 12:06 shaneapen

Hi, I have same problem. I want to use websocket to sync every code change, but is will trigger a dead loop

InfinityDJKing avatar Aug 05 '21 05:08 InfinityDJKing

I'm not sure I understand the intent here. Any changes done to the model will result in change events being emitted (e.g. to update the view or the language services that provide rich language features). What would be the use case for changing a model and inhibiting events?

Hi, I have same problem. I want to use websocket to sync every code change, but is will trigger a dead loop. Can I get which source did it come from like onDidChangeCursorSelection?(ICursorSelectionChangedEvent)

InfinityDJKing avatar Aug 05 '21 05:08 InfinityDJKing

I need this to prevent event if the change is made by formatter. e.isFlush seems not the solution to me. :(

softmarshmallow avatar Apr 10 '22 08:04 softmarshmallow

@DanielJefferson1990

editor. onDidChangeCursorSelection(ev => {
if (ev.reason === CursorChangeReason.ContentFlush) {
    // true if setValue call
  } else {
    // false if user input
  }
}

Maksclub avatar Jul 26 '22 07:07 Maksclub