A basic command in GrapesJS it's a simple function, but you will see in this guide how powerfull they can be. The main goal of the Command module is to centralize functions and be easily reused across the editor. Another big advantage of using commands is the ability to track them, extend or even interrupt beside some conditions.
::: warning
This guide is referring to GrapesJS v0.14.59 or higher
This guide is referring to GrapesJS v0.14.60 or higher
console.log('After `my-command-modal` is stopped');
});
editor.on('stop:my-command-modal:before', () => {
console.log('Before `my-command-modal` is stopped');
});
```
If you need, you can also listen to all commands
```js
editor.on('run', commandId => {
console.log('Run', commandId);
});
editor.on('stop', commandId => {
console.log('Stop', commandId);
});
```
### Interrupt command flow
Let's use our previosly created `my-command-modal` command to describe
* *`run:{commandName}` - Triggered when some command is called to run (eg. editor.runCommand('preview'))
* *`stop:{commandName}` - Triggered when some command is called to stop (eg. editor.stopCommand('preview'))
* *`run:{commandName}:before` - Triggered before the command is called
* *`stop:{commandName}:before` - Triggered before the command is called to stop
* *`abort:{commandName}` - Triggered when the command execution is aborted (`editor.on(`run:preview:before`, opts => opts.abort = 1);`)
* *`run` - Triggered on run of any command. The id and the result are passed as arguments to the callback
* *`stop` - Triggered on stop of any command. The id and the result are passed as arguments to the callback
Sometimes you might need to interrupt the execution of an existant command due to some condition. In that case, you have to use `run:{COMMAND-ID}:before` event and set to `true` the abort option
console.log('Prevent `my-command-modal` from execution');
}
});
```
## Conclusion
The Commands module is quite simple but, at the same time, really powerfull if used correctly. So, if you're creating a plugin for GrapesJS, use commands as much as possible, this will allow a higher reusability and control over your logic.