# Blocks

You can customize the initial state of the module from the editor initialization, by passing the following Configuration Object (opens new window)

const editor = grapesjs.init({
 blockManager: {
   // options
 }
})

Once the editor is instantiated you can use its API and listen to its events. Before using these methods, you should get the module from the instance.

// Listen to events
editor.on('block:add', (block) => { ... });

// Use the API
const blockManager = editor.Blocks;
blockManager.add(...);

# Available Events

  • block:add - Block added. The Block is passed as an argument to the callback.
  • block:remove - Block removed. The Block is passed as an argument to the callback.
  • block:update - Block updated. The Block and the object containing changes are passed as arguments to the callback.
  • block:drag:start - Started dragging block, the Block is passed as an argument.
  • block:drag - Dragging block, the Block is passed as an argument.
  • block:drag:stop - Dragging of the block is stopped. The dropped Component (if dropped successfully) and the Block are passed as arguments.
  • block - Catch-all event for all the events mentioned above. An object containing all the available data about the triggered event is passed as an argument to the callback.

# Methods

# getConfig

Get configuration object

Returns Object (opens new window)

# add

Add new block.

# Parameters

# Examples

blockManager.add('h1-block', {
  label: 'Heading',
  content: '<h1>Put your title here</h1>',
  category: 'Basic',
  attributes: {
    title: 'Insert h1 block'
  }
});

Returns Block Added block

# get

Get the block by id.

# Parameters

# Examples

const block = blockManager.get('h1-block');
console.log(JSON.stringify(block));
// {label: 'Heading', content: '<h1>Put your ...', ...}

Returns Block

# getAll

Return all blocks.

# Examples

const blocks = blockManager.getAll();
console.log(JSON.stringify(blocks));
// [{label: 'Heading', content: '<h1>Put your ...'}, ...]

Returns Collection<Block>

# getAllVisible

Return the visible collection, which containes blocks actually rendered

Returns Collection<Block>

# remove

Remove block.

# Parameters

# Examples

const removed = blockManager.remove('BLOCK_ID');
// or by passing the Block
const block = blockManager.get('BLOCK_ID');
blockManager.remove(block);

Returns Block Removed block

# getCategories

Get all available categories. It's possible to add categories only within blocks via 'add()' method

Returns (Array (opens new window) | Collection)

# getContainer

Return the Blocks container element

Returns HTMLElement (opens new window)

# render

Render blocks

# Parameters

# Examples

// Render all blocks (inside the global collection)
blockManager.render();

// Render new set of blocks
const blocks = blockManager.getAll();
const filtered = blocks.filter(block => block.get('category') == 'sections')

blockManager.render(filtered);
// Or a new set from an array
blockManager.render([
 {label: 'Label text', content: '<div>Content</div>'}
]);

// Back to blocks from the global collection
blockManager.render();

// You can also render your blocks outside of the main block container
const newBlocksEl = blockManager.render(filtered, { external: true });
document.getElementById('some-id').appendChild(newBlocksEl);

Returns HTMLElement (opens new window) Rendered element