Repository navigation
setValue without triggering change events
#3628
Replies: 20 comments 1 reply
|
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: angular/angular#20230 (comment) |
|
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. |
Hi, I have same problem. |
|
I need this to prevent event if the change is made by formatter. |
|
@DanielJefferson1990 |
|
Any updates on this? |
|
Facing similar issue; the content change through the code is triggering the monaco.editor.IModelContentChangedEvent. And there is no way it can be ignored; there is no parameter to differentiate between keyboard/user-interaction originated event and code update generated event. |
|
Setting Monaco's value without triggering change events is a common need when you want to apply programmatic changes (e.g., loading initial content, applying remote edits) without triggering your own change handlers. The cleanest approach: use const model = editor.getModel()!;
// This doesn't trigger 'onDidChangeContent' in the same way as setValue
// But it still fires change events — this is intentional in Monaco
// Workaround: flag-based suppression
let ignoreChanges = false;
editor.onDidChangeModelContent(() => {
if (ignoreChanges) return;
// your normal change handler
handleChange(editor.getValue());
});
// When you want to set value without triggering handler:
ignoreChanges = true;
editor.setValue(newContent);
// NOTE: setValue also resets undo history!
ignoreChanges = false;Better approach: use // If you know the diff (from remote change):
const model = editor.getModel()!;
model.applyEdits([{
range: model.getFullModelRange(),
text: newContent,
}]);
// This fires onDidChangeContent but preserves undo stackIf you need to preserve undo history AND suppress events: // Decorations approach: apply without changing model content
// (Only works if you're highlighting, not changing text)
// For actual content changes without history:
model.setValue(newContent); // Clears undo history (intentional for "loading" scenario)Practical patterns:
What's your use case — initial load, collaborative editing, or something else? |
Uh oh!
There was an error while loading. Please reload this page.
We only want to listen to user-triggered events. It looks like
_resetValuewould allow for this (I get it, it's supposed to be private). However, we can't seem to use it because the necessary functiontoRawTextdefined onTextModelseems to be unaccessible.How can we change the editor text value without triggering a change event?
All reactions