Issue #2433Opened November 29, 2019by Davidvlv0 reactions

[Bug] Changing layerable in an an event at runtime doesn't immediately update

Question

When I change the layerable option of a custom component in an event, it still shows up in the layers until you drag it once. Is there some way to get around this by forcing the layers to update? Or can we get a bug fix for this, thanks.

import grapesjs from 'grapesjs';
import 'grapesjs-blocks-basic';

const editor = grapesjs.init({
  container: '#gjs',
  fromElement: 1,
  storageManager: { type: 0 },
  plugins: ['gjs-blocks-basic']
});

var comps = editor.DomComponents;

var defaultType = comps.getType('default');
var defaultModel = defaultType.model;

comps.addType('test', {
  model: defaultModel.extend({
    init: function() {
      this.on("change:attributes:id", () => {
        let attributes = this.getAttributes();
        this.set({'layerable': attributes.id === 'layerable'});
      });
    }
  })
});

editor.BlockManager.add('test', {
  label: 'test',
  category: 'something',
  content: '<div data-gjs-type="test" style="height: 20px; background-color: blue;" id="layerable"></div>'
});

Video: Grapesjs layerable.zip

Answers (3)

DavidvlvNovember 29, 20190 reactions

The same thing is happening when I try to add/remove a toolbar button in an event - the toolbar doesn't update until I unselect the component and reselect it:

      this.on("change:attributes:id", () => {
        let tb = model.get('toolbar');
        if (attributes.id === 'layerable') {
            if (tb.filter(b => b.attributes.class === 'fa fa-plus').length > 0)
                tb.pop();
        }
        else {
            if (tb.filter(b => b.attributes.class === 'fa fa-plus').length === 0)
                tb.push({
                    attributes: { class: 'fa fa-plus' },
                    command: () => {
                        model.components().add('<div>test</div');
                    }
                });
            }
      });
DavidvlvNovember 29, 20190 reactions

Also, if you set 'removable' to false in the same place as where 'layerable' is being set in my post, the toolbar doesn't update to remove the delete button until you save and reload.

artfNovember 30, 20190 reactions

Try to trigger this event after your updates editor.trigger('component:toggled')

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.