Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
Davidvlv opened this issue Nov 29, 2019 · 6 comments
Closed

Comments

@Davidvlv
Copy link

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

@Davidvlv
Copy link
Author

Davidvlv commented Nov 29, 2019

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');
                    }
                });
            }
      });

@Davidvlv
Copy link
Author

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.

@artf
Copy link
Member

artf commented Nov 29, 2019

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

@Davidvlv
Copy link
Author

Davidvlv commented Dec 2, 2019

That seems to solve the second issue - the extra toolbar button now appears and disappears immediately.

However the change to 'layerable' still doesn't update the layers manager, and the 'clone', 'delete' and 'move' toolbar buttons don't disappear when 'copyable', 'removable' and 'draggable' are changed.

This is my code: https://codesandbox.io/s/youthful-hill-s1t0u

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 doesn't work
        this.set({ layerable: attributes.id === "able" });
        this.set({ removable: attributes.id === "able" });

        // this works
        let tb = this.get("toolbar");
        if (attributes.id === "able") {
          if (tb.filter(b => b.attributes.class === "fa fa-plus").length === 0)
            tb.push({
              attributes: { class: "fa fa-plus" },
              command: () => {
                this.components().add("<div>test</div");
              }
            });
        } else {
          if (tb.filter(b => b.attributes.class === "fa fa-plus").length > 0)
            tb.pop();
        }
        editor.trigger("component:toggled");
      });
    }
  })
});

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

@artf
Copy link
Member

artf commented Dec 8, 2019

However the change to 'layerable' still doesn't update the layers manager, and the 'clone', 'delete' and 'move' toolbar buttons don't disappear when 'copyable', 'removable' and 'draggable' are changed.

For the layerable I probably need to add a new listener in LayerManager... And about the toolbar buttons, those can't just disappear, you have to update the array toolbar property in the Component. You might try to re-init them with component.initToolbar()

@artf artf added this to To do in Release v0.15.10 via automation Dec 8, 2019
@artf artf closed this as completed in 5c5e4fe Dec 11, 2019
Release v0.15.10 automation moved this from To do to Done Dec 11, 2019
@sandenson
Copy link

sandenson commented Oct 15, 2021

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

@artf this didn't work for me. I'm creating a component which can only be placed once in the canvas. Here's my code and screenshots:

editor.on('component:add', component => {
   const type = component!.get<string>('type')
   if (type === 'panda-video' && editor.getWrapper().findType(type).length > 1) {
     component!.remove();
     editor.trigger('component:toggled')
   }
});

The duplicate component is removed, but it still shows up in the Layer Manager.

image

Edit: I mate of mine suggested putting a slight timeout in the component!.remove() method call. It worked, but that's pretty ugly.

setTimeout(() => component!.remove(), 0.000001);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
Development

No branches or pull requests

3 participants