Issue #484Opened November 5, 2017by roytang1 reactions

Can I restrict what blocks that can be dropped into a container

Question

My intent is to create a block that should allow only image components (I'm using the default one from grapes-blocks-basic) to be dropped inside it.

I have the following test code:

  comps.addType('image-container', 
  {
    model: defaultModel.extend(
    {
      // Extend default properties
      defaults: Object.assign({}, defaultModel.prototype.defaults, 
      {
        droppable: "img",
      }),
    },
    {
      isComponent: function(el) 
      {
        if(el.tagName == 'DIV' && $(el).hasClass("image-container"))
        {
          return {type: "image-container"};
        }
      },
    }),
    view: defaultView
  });

  blockManager.add('image-container', 
  {
    label: 'Image Container',
    attributes: {class:'fa fa-pencil'},
    content: '<DIV class="image-container"></DIV>',
    category: 'Test Blocks',
  });  

However, when I run the above code, what happens is that I can't drop an image component inside the Image Container. The green drop cursor only appears around the top/bottom/sides of the container but it won't appear inside it. Actually, I can't drop ANY component inside it.

I kind of assumed that the "droppable" property could work the same way as the "draggable" property does in the example here: https://github.com/artf/grapesjs/wiki/Components. The source for Component.js says:

    // Indicates if it's possible to drag the component inside others
    // Tip: Indicate an array of selectors where it could be dropped inside
    draggable: true,

    // Indicates if it's possible to drop other components inside
    // Tip: Indicate an array of selectors which could be dropped inside
    droppable: true,

So it looks like they should work the same way. Am I doing something wrong?

Is it better to do this the other way around? i.e. Create a custom image component, and restrict it's draggable property? (I haven't tried this yet - I'm not sure if I can override/extend the default image type's behavior)

Answers (3)

roytangNovember 11, 20171 reactions

This fix works. Thanks!

artfNovember 6, 20170 reactions

@roytang your code seems ok, probably the problem is just because your component is "too much empty" so the mouse pointer just can't get in. Try to add a little bit of padding and see if it works

content: '<div class="image-container" style="padding:20px"></div>',
roytangNovember 7, 20170 reactions

Mmm. I have image-container styled with width and height 100px. Also, removing the droppable property in the extended model allows me to drop text and images inside it.

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.