BUG: comment component type do not support adding traits
Question
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
});}
Answers (3)
@artf
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', ]
},
},
})
});
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
Related Questions and Answers
Continue research with similar issue discussions.
Issue #2666
Adding trait makes element uneditable
Hi, I wanted to add a trait that enables me to edit the heading tag of a component. I was able to achieve that by following below code I fo...
Issue #2417
API in Component not working after Deployment
Block.js => Component.js => We are able to call the API and it works in Deployment when we are passing the whole html with script, body, st...
Issue #3388
FEAT: adding indication as a Trait without any input.
I want to add an indication for a trait. For my component, I have the traits: and for the type 'dateformat-indication', I created the type:...
Issue #2456
[QUESTION] (Possibly a bug) How to apply traits to components used for GrapesJs initialization
I am using This works but only for blocks which are added/drag-dropped to the editor manually after it is initialized. If we supply compone...
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.