Issue #2134Opened July 12, 2019by fmr4112 reactions

[QUESTIONS] create trait like video trait

Question

how do I make a trait like a video trait, I have a provider type, the other trait type depends on the provider I choose, can it be demonstrated? Thank you very much. provider

Answers (2)

giorgiosjamesJuly 12, 20191 reactions

The video component is a great demonstration in and of itself. Can be found at grapesjs repo root > src > dom_components > model > ComponentVideo.js. Here's some example code with the bare minimum necessary code:


      var domComps = editor.DomComponents;
      var dType = domComps.getType('default');
      var dModel = dType.model;
      var dView = dType.view;

      domComps.addType('testBlock', {
          model: dModel.extend({
            init() {
              const traits = [this.getProviderTrait(), this.getOption1Trait()];
              this.set('traits', traits);
              this.listenTo(this, 'change:provider', this.updateTraits);
            },
            updateTraits() {
              var prov = this.get('provider');
              var traits = [];
              switch (prov) {
                case 'traitOption1':
                  traits = [this.getProviderTrait(), this.getOption1Trait()];
                  break;
                case 'traitOption2':
                  traits = [this.getProviderTrait(), this.getOption2Trait()];
                  break;
                default:
                  break;
              }
              this.loadTraits(traits);
              this.em.trigger('component:toggled');
            },
            getProviderTrait() {
              return {
                type: 'select',
                label: 'Provider',
                name: 'provider',
                changeProp: 1,
                options: [
                  { value: 'traitOption1', name: 'traitOption1' },
                  { value: 'traitOption2', name: 'traitOption2' }
                ]
              }
            },
            getOption1Trait() {
              return {
                type: 'text',
                label: 'Option1',
                name: 'option1',
                changeProp: 1,
              }
            },
            getOption2Trait() {
              return {
                type: 'text',
                label: 'Option2',
                name: 'option2',
                changeProp: 1,
              }
            }
          }, {
            isComponent: function(el) {
              if(el.tagName == 'DIV'){
                return {type: 'testBlock'};
              }
            },
          }),

          view: dView,
      });

You can test this out by adding a simple div to the Block Manager

      editor.BlockManager.add('testBlock', {
        label: 'Block',
        type: 'testBlock',
        content: `<div>Test block</div>`
      });

The important parts are this.listenTo(this, 'change:provider', this.updateTraits); in the type init() function to listen for changes to the main trait and making sure changeProp is set to 1 on the traits, otherwise change:provider won't pick up the updates.

fmr411July 12, 20191 reactions

Okkay,Thank you very much, I understand

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.