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

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

Closed
roytang opened this issue Nov 5, 2017 · 5 comments
Closed

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

roytang opened this issue Nov 5, 2017 · 5 comments
Labels

Comments

@roytang
Copy link

roytang commented Nov 5, 2017

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)

@artf
Copy link
Member

artf commented Nov 6, 2017

@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>',

@roytang
Copy link
Author

roytang commented Nov 6, 2017

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.

@artf
Copy link
Member

artf commented Nov 7, 2017

I think it might be a problem with the d&d of the block manager. Are you able to drop images dragged from canvas instead of the block panel?

@artf artf closed this as completed in 4aa3ba7 Nov 9, 2017
@roytang
Copy link
Author

roytang commented Nov 11, 2017

This fix works. Thanks!

@lock
Copy link

lock bot commented Sep 18, 2019

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

@lock lock bot added the outdated label Sep 18, 2019
@lock lock bot locked as resolved and limited conversation to collaborators Sep 18, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

2 participants