Create your first PRESENTA Block plugin

Posted on March 5, 2021 in
2 min read

I've updated the documentation of PRESENTA Lib adding the plugin development section few days ago.

This was the first step to lowering the barriers for those who want to develop their own plugin.

The next step, no doubt, is tutorials. Here the first one:

Let's create our first Block!

A Block is the minimal content unit in a PRESENTA scene responsible to render a content in a specific way.

Here the GOAL:

Render an SVG circle in the middle of the block area.

Furthermore, let the user choose the color.

Here the code with some comments between the lines:

function aCircle(blockElement, blockConfig){

  // check the options
  const color = blockConfig.color || 'black'
  // create the SVG fragment
  const str = `<svg style="width:100%;height:100%;" xmlns="" viewBox="0 0 100 100">
        <circle fill="${color}" r="20" cx="50" cy="50"></circle>
  // convert to a proper DOM fragment
  const parser = new DOMParser()
  const frag = parser.parseFromString(str, 'text/html').body.childNodes[0]
  // append it to the block container

// register the plugin with a unique namespace
Presenta.addBlock('aCircle', aCircle)

Keep in mind that blockElement is the block container DOM element where to append your content and blockConfig is the config object defined by the user.

This block has been registered with the name aCircle, thus, here how to use it in a PRESENTA Lib config:

const config = {
      type: 'aCircle',
      color: 'blue'

new Presenta('#app', config)

You can play with this plugin in Playground tool or check its source code here.