Block Manager

GrapesJS - Block Manager

The Block is a group of Components and can be easily reused inside templates.

The difference between components and blocks: The component is more atomic, so a single image, a text box or a map is a component. The block is what the end user will drag inside the canvas, so it could contain a single image (single Component) or the entire section like, for example, the footer with a lot of components inside (texts, images, inputs, etc).

Check out the Components page to see the list of built-in components and how to create your own.

Let's see how to add a new block to the editor using the Blocks API

var editor = grapesjs.init({...});
var blockManager = editor.BlockManager;

// 'my-first-block' is the ID of the block
blockManager.add('my-first-block', {
  label: 'Simple block',
  content: '<div class="my-block">This is a simple block</div>',
});

With this snippet a new block will be added to the collection. You can also update existent blocks

blockManager.get('my-first-block').set({
  label: 'Updated simple block',
  attributes: {
    title: 'My title'
  }
})

As you see a simple HTML string is enough to create a block, the editor will do the rest. If you want you could also pass an object representing the Component.

blockManager.add('my-map-block', {
  label: 'Simple map block',
  content: {
    type: 'map', // Built-in 'map' component
    style: {
      height: '350px'
    },
    removable: false, // Once inserted it can't be removed
  }
})

From v0.3.70 it's also possible to pass the HTML string with Component's properties as attributes.

blockManager.add('the-row-block', {
  label: '2 Columns',
  content: '<div class="row" data-gjs-droppable=".row-cell" data-gjs-custom-name="Row">' +
      '<div class="row-cell" data-gjs-draggable=".row"></div>' +
      '<div class="row-cell" data-gjs-draggable=".row"></div>' +
    '</div>',
});

In the example above you're defining a row component which will accept only elements which match '.row-cell' selector and cells which could be dragged only inside '.row' elements. We're also defining the custom name which will be seen inside the Layers panel. If you want to check the complete list of available Component's properties, check directly the Component model source: https://github.com/artf/grapesjs/blob/dev/src/dom_components/model/Component.js

Last Updated: 11/3/2018, 1:07:59 PM