Grapesjs: How do I add a trait to all existing component types?

Created on 2 Jan 2020  路  1Comment  路  Source: artf/grapesjs

I want to go through all existing component types and add a trait to the model-> defaults and can't figure out how to do this. According to the documentation, I should be able to (in my plugin):

// now - traits that should be on every component - base characteristics that we want to apply
  domComponents.getTypes().map(type => {

    domComponents.addType(type.id, {

      model: {
        defaults: {

          traits: [
            {
              label: 'Allow editing in pages',
              name: 'allow_editing_in_pages',
              type: 'checkbox'
            }
          ]
        }
      }

    })
  });

But this overrides the traits array, which removes the existing traits, which isn't what I want. My next step was to try to get the old traits and concat them with my new ones - but I can't figure out how to pull the default traits for each type in order to do this.

How can I append a trait to all (or some) component types?

Most helpful comment

@andrewryan1906 for pulling the default traits:

domComponents.getType(type.id).model.prototype.defaults.traits

so your traits definition would be like below:

traits: [
    ...domComponents.getType(type.id).model.prototype.defaults.traits,
    {
        label: 'Allow editing in pages',
        name: 'allow_editing_in_pages',
        type: 'checkbox'
    }
]

cheers!

>All comments

@andrewryan1906 for pulling the default traits:

domComponents.getType(type.id).model.prototype.defaults.traits

so your traits definition would be like below:

traits: [
    ...domComponents.getType(type.id).model.prototype.defaults.traits,
    {
        label: 'Allow editing in pages',
        name: 'allow_editing_in_pages',
        type: 'checkbox'
    }
]

cheers!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kosirm picture kosirm  路  3Comments

desilvaNSP picture desilvaNSP  路  3Comments

tribulant picture tribulant  路  3Comments

ryandeba picture ryandeba  路  3Comments

Geczy picture Geczy  路  3Comments