GrapesJS / grapesjs

Free and Open source Web Builder Framework. Next generation tool for building templates without coding
https://grapesjs.com
BSD 3-Clause "New" or "Revised" License
22.36k stars 4.05k forks source link

BUG: comment component type do not support adding traits #3732

Closed aliibrahim123 closed 3 years ago

aliibrahim123 commented 3 years ago

i am working on a script that add a trait on all the component type , but the comment type do not have the prototype object. code

        ddw = editor.DomComponents.getTypes();

     for (let i = 0; i < ddw.length; i++) {

    ddq = ddw[i].id;
    editor.DomComponents.addType(ddq, {
    model: editor.DomComponents.getType(ddq).model.extend({
      defaults: Object.assign({}, editor.DomComponents.getType(ddq).model.prototype.defaults, {

        traits: editor.DomComponents.getType(ddq).model.prototype.defaults.traits.concat([
          'hallo'
        ])
      })
    })

    view: editor.DomComponents.getType('text').view
     });}
aliibrahim123 commented 3 years ago

@artf

artf commented 3 years ago

Don't use model.extend/view.extend, those are part of the old API. This is an example of how properly extend traits on all components

const { Components } = editor;
Components.getTypes().forEach(({ id, model }) => {
    Components.addType(id, {
        model: {
            defaults: {
              traits: [ ...model.getDefaults().traits, 'new-trait', ]
            },
        },
    })
});
skru commented 2 years ago

This is a great solution artf, any tips on how to use this for components that have traits defined in a function?, as per the docs: https://grapesjs.com/docs/modules/Traits.html#add-traits-to-components