API Commands

Artur Arseniev edited this page Jun 18, 2016 · 2 revisions

Commands

You can init the editor with all necessary commands via configuration

var editor = grapesjs.init({
  ...
 commands: {...} // Check below for the properties
  ...
});

Before using methods you should get first the module from the editor instance, in this way:

var commands = editor.Commands;

Parameters

  • config Object Configurations
    • config.defaults Array.<Object>= Array of possible commands (optional, default [])

Examples

...
commands: {
  defaults: [{
    id: 'helloWorld',
    run:  function(editor, sender){
      alert('Hello world!');
    },
    stop:  function(editor, sender){
      alert('Stop!');
    },
  }],
},
...

add

Add new command to the collection

Parameters

  • id string Command's ID
  • command Object Object representing you command. Methods run and stop are required

Examples

commands.add('myCommand', {
  run:  function(editor, sender){
    alert('Hello world!');
  },
  stop:  function(editor, sender){
  },
});

Returns this

get

Get command by ID

Parameters

Examples

var myCommand = commands.get('myCommand');
myCommand.run();

Returns Object Object representing the command