Issue #1868Opened March 7, 2019by sakshigarg91 reactions

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)

arthuralmeidapMarch 7, 20191 reactions

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.

sakshigarg9March 12, 20190 reactions

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 };
        }
      }
    })
  });
artfMarch 22, 20190 reactions

@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.

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.