How to enable functionality only for specific image component, not all
Question
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. How can I achieve that?
This is my code
domc.addType("image", {
model: {
initToolbar() {
typeImage.prototype.initToolbar.apply(this, arguments);
const tb = this.get("toolbar");
const tbExists = tb.some(item => item.command === "open-assets");
if (!tbExists) {
tb.unshift({
command: "open-assets",
label: toolbarIcon
});
this.set("toolbar", tb);
}
}
}
});
Answers (3)
You are on the right path. Yes, you have to create a new component for that but you can't override the Image component type otherwise, as you have seen, all the images will get that.
What makes those images unique? How could you differentiate between a regular image and a specific image? That logic you will need to add to your isComponent fn.
Tip: Extends your new component type from the existing Image component type.
I tried this and removed gjs-data-type="image" from the img tag where i want the functionality to be applied. But, neither does it implement the functionality on it and but also messes with the rest of component properties.
domc.addType("image", {
model: defaultModel.extend({
defaults: Object.assign({}, defaultModel.prototype.defaults, {
initToolbar() {
typeImage.prototype.initToolbar.apply(this, arguments);
const tb = this.get("toolbar");
const tbExists = tb.some(item => item.command === "open-assets");
if (!tbExists) {
tb.unshift({
command: "open-assets",
label: toolbarIcon
});
this.set("toolbar", tb);
}
}
}),
isComponent: function(el) {
if (el.tagName == "IMG" && /logo.png/.test(el.src)) {
return { type: "image", src: el.src };
}
}
})
});
@sakshigarg9 to prevent messes use the new addType (in your case you miss the view property and this breaks stuff in the old declaration)
BTW I suggest to create a NEW component type by extending the image instead of just extending the current one (in this way you're editing all images)
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1809
Customize image resizing
If i want to limit resizing an image to only specific dimensions, how can i do that? Also is it possible to enable resizing only from the b...
Issue #459
Error when including a custom component inside another block
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...
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 #701
Modifying a component in the editor clears the model content
What I'm trying to achieve: I have special "data-" attributes on some of the components I pull into the editor. When one of these is modifi...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.