Error when including a custom component inside another block
Seeing as how your comment fixed the original error I encountered, I'll close this comment and post a separate one for my questions about usage of the droppable property.
Read full answer below βQuestion
Sorry for the vague title. I'm not sure how to describe this concisely. I have a custom block where i want to restrict that only images can be dropped into a certain part of the block. I tried something like this:
comps.addType('image-container', {
model: defaultModel.extend({
// Extend default properties
defaults: Object.assign({}, defaultModel.prototype.defaults, {
// Can only drop these elements inside it
droppable: "img",
}),
},
{
isComponent: function(el) {
if(el.tagName == 'DIV' && $(el).hasClass("image-container")){
return {type: "image-container"};
}
},
})
});
blockManager.add('test-with-image-container', {
label: 'Test Block With Image Container',
attributes: {class:'fa fa-pencil'},
content: '<div class="test-block">You can drop an image below:<div class="image-container"></div></div>',
category: 'Blocks',
});
However, when I do this, trying to drag the block off the block palette raises the following error:
... grapes.min.js:2 Uncaught TypeError: s is not a constructor at F.i.addToCollection (VM6196 grapes.min.js:2) at VM6196 grapes.min.js:2 at Function.C.each.C.forEach (VM6196 grapes.min.js:2) at F.i.each (VM6196 grapes.min.js:2) at F.i.render (VM6196 grapes.min.js:2) at F.i.renderChildren (VM6196 grapes.min.js:2) at F.i.render (VM6196 grapes.min.js:2) at F.i.addToCollection (VM6196 grapes.min.js:2) at F.i.addTo (VM6196 grapes.min.js:2) at y (VM6196 grapes.min.js:2) ...
Any advice? Is there a better way to accomplish what I want to do?
Thanks!
Answers (3)
Seeing as how your comment fixed the original error I encountered, I'll close this comment and post a separate one for my questions about usage of the droppable property.
The view is mandatory when you're defining a new Component type
comps.addType('image-container', {
model: defaultModel...
view: defaultView;
});
Thanks! Adding the defaultView did indeed get rid of the error, but I'm still unable to do what I want - I can't seem to drop an image inside the "image-container" part of the sample above. When I try to drop the an image component there, it's inserted into the containing "test-block" instead.
My test code is now:
comps.addType('image-container',
{
model: defaultModel.extend(
{
// Extend default properties
defaults: Object.assign({}, defaultModel.prototype.defaults,
{
// Can only drop these elements inside it
droppable: "img",
}),
},
{
isComponent: function(el)
{
if(el.tagName == 'DIV' && $(el).hasClass("image-container"))
{
return {type: "image-container"};
}
},
}),
view: defaultView
});
blockManager.add('test-with-image-container',
{
label: 'Test Block With Image Container',
attributes: {class:'fa fa-pencil'},
content: '<div class="test-block">You can drop an image below:<div class="image-container"></div></div>',
category: 'Blocks',
});
Am I using the "droppable" property incorrectly? It just needs to be a CSS selector right?
Related Questions and Answers
Continue research with similar issue discussions.
Issue #484
Can I restrict what blocks that can be dropped into a container
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 i...
Issue #1868
How to enable functionality only for specific image component, not all
I've enabled the fa fa-pencil icon into my image components, but I want it to be enabled only in specific image components, not all of them...
Issue #580
[Question] Any/Trigger event when any block is dropped inside canvas?
Hey, How can I trigger some event when any new block or content inside canvas is updated? Something like editor.onCanvasUpdate() Thanks!
Issue #839
[QUESTION] Create custom selected component settings
There is a option in panel to change setting of component (For links, there is an option no change link - HREF - and target, for texts, id...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins βPremium plugins ship with support, regular updates, and production-ready features β save days of integration work.
Browse premium plugins βRelated tutorials
In-depth guides on the same topic.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.