6.9 KiB
| title |
|---|
| Commands |
Commands
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 :::
Basic configuration
You can create your commands already from the initialization by passing them in the commands.defaults options:
const editor = grapesjs.init({
...
commands: {
defaults: [
{
// id and run are mandatory in this case
id: 'my-command-id',
run() {
alert('This is my command');
},
}, {
id: '...',
// ...
}
],
}
});
For all other available options check directly the configuration source file.
Most commonly commands are created dynamicly post initialization, in that case you'll need to use the Commands API (eg. this is what you need if you create a plugin)
const commands = editor.Commands;
commands.add('my-command-id', editor => {
alert('This is my command');
});
// or it would be the same...
commands.add('my-command-id', {
run(editor) {
alert('This is my command');
},
});
As you see the definiton is quite easy, you just add an ID and the callback function. The Editor instance is passed as the first argument to the callback so you can access any other module or API method.
Now if you want to call that command you should just run this
editor.runCommand('my-command-id');
You could also pass options if you need
editor.runCommand('my-command-id', { some: 'option' });
Then you can get the same object as a third argument of the callback.
commands.add('my-command-id', (editor, sender, options = {}) => {
alert(`This is my command ${options.some}`);
});
The second argument, sender, just indicates who requested the command, in our case will be always the editor
Until now there is nothing exiting except a common entry point for functions, but we'll see later its real advantages.
Default commands
GrapesJS comes along with some default set of commands and you can get a list of all currently availlable commands via editor.Commands.getAll(). This will give you an object of all available commands, so, also those added later, like via plugins. You can recognize default commands by their namespace core:*, we also recommend to use namepsaces in your own custom commands, but let's get a look more in detail here:
core:canvas-clear- Clear all the content from the canvas (HTML and CSS)core:component-delete- Delete a componentcore:component-enter- Select the first children component of the selected onecore:component-exit- Select the parent component of the current selected onecore:component-next- Select the next sibling componentcore:component-prev- Select the previous sibling componentcore:component-outline- Enable outline border on componentscore:component-offset- Enable components offset (margins, paddings)core:component-select- Enable the process of selecting components in the canvascore:copy- Copy the current selected componentcore:paste- Paste copied componentcore:preview- Show the preview of the template in canvascore:fullscreen- Set the editor fullscreencore:open-code- Open a default panel with the template codecore:open-layers- Open a default panel with layerscore:open-styles- Open a default panel with the style managercore:open-traits- Open a default panel with the trait managercore:open-blocks- Open a default panel with the blockscore:open-assets- Open a default panel with the assetscore:undo- Call undo operationcore:redo- Call redo operation
Stateful commands
As we've already seen the command is just a function and once executed nothing is left behined, but in some cases we'd like to keep a track of executed commands. GrapesJS can handle by default this case and to enable it you just need to declare a command as an object with run and stop methods
commands.add('my-command-state', {
run(editor) {
alert('This command is now active');
},
stop(editor) {
alert('This command is disabled');
},
});
So if we now run editor.runCommand('my-command-state') the command will be registered as active. To check the state of the command you can use commands.isActive('my-command-state') or you can even get the list of all active commands via commands.getActive(), it our case the result would be something like this
{
...
'my-command-state': undefined
}
The key of the result object tells you the active command, the value is the last return of the run command, in our case is undefined because we don't return anything, but it's up to your implementation decide what to return and if you actually need it.
// Let's return something
...
run(editor) {
alert('This command is now active');
return {
activated: new Date(),
}
},
...
// Now instead of the `undefined` you'll see object from the run method