Li_Xia
Li_Xia

Reputation: 101

ace editor change event and setvalue

I am listening on ACE editor's change event to handle with user's input while sometimes I will do setvalue() by js.

So is there a way to avoid the setvalue() triggering the change event?

Upvotes: 5

Views: 3499

Answers (2)

Badyam
Badyam

Reputation: 39

You may suppress firing events before setting a new value (and/or doing other manipulations with editor), and restore it after.

const editorValueChangeHandler = () => {
  console.log('Value handled', editor.getValue());
};

editor.off('change', editorValueChangeHandler);
editor.session.setValue('newValue');
// ... other operations with editor...
editor.on('change', editorValueChangeHandler);

Upvotes: 0

a user
a user

Reputation: 24104

There is no way to avoid change event. But because change event is fired synchronously, you can set a flag to not handle the events created by you. Something like

var fromSetValue = false;
editor.on("change", function() {
    if (!fromSetValue) {
        // user input
    }
})

fromSetValue = true;
editor.setValue("hi")
fromSetValue = false;

Upvotes: 7

Related Questions