monaco-editor
monaco-editor copied to clipboard
`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?
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?
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?
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;
}
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.
+1. For eg. In Codemirror for every change you also get it's origin - if it was a user input or setValue call.
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
It would be great if the 'source' parameter of the 'executeEdits' function could be obtained in the 'onDidChangeModelContent' function.
after toggling to a different file , im unable to get previous changes using ctrl + z for previous file..
any help would be appreciable
This is still an issue and a fairly important one. Any update on this?
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
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.
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
}
});
Any update on this issue?
Hi, I have same problem. I want to use websocket to sync every code change, but is will trigger a dead loop
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)
I need this to prevent event if the change is made by formatter.
e.isFlush
seems not the solution to me. :(
@DanielJefferson1990
editor. onDidChangeCursorSelection(ev => {
if (ev.reason === CursorChangeReason.ContentFlush) {
// true if setValue call
} else {
// false if user input
}
}