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 (4)
@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
Thanks for reporting this, @aliibrahim123.
Thanks for sharing your report about comment component type do not support adding traits. To help the team investigate and prioritize this:
Please provide:
- A minimal reproducible example (CodeSandbox/JSFiddle)
- Your GrapesJS version number
- Browser and OS information
- Any error messages from the browser console
- Steps to reproduce the issue
What helps most:
- Minimal code example (not your full project)
- Screen recording or screenshot showing the issue
- Expected vs actual behavior clearly stated
- GrapesJS configuration you're using
With these details, the maintainers can identify and prioritize a fix much faster. The GrapesJS team is very responsive to well-documented issues.
Related Questions and Answers
Continue research with similar issue discussions.
Issue #3500
Remove custom trait by its type.
I have created a custom trait but the name of that trait will be dynamic which is unknown. The problem is that the current removeTrait() on...
Issue #3578
version 0.17.4 the components missing when try to add background image to body
I add domComponents: { storeWrapper: 1 }, when initialize the webeditor, to enable add background image to body. it works fine when use the...
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 #3546
How can I add the custom left toolbar for Image component similar to text
We want to add a custom toolbar to the image component to be shown at left side (similar to what we show for text component). Please let me...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Check the open-source GrapesJS plugins on GitHub or run a quick search in our free catalog.
Browse free plugins βPremium plugins ship with support, regular updates, and production-ready features β save days of integration work.
Browse premium plugins βRelated tutorials
In-depth guides on the same topic.
Tutorial
How to Build a Production GrapesJS Editor: The Complete Walkthrough of Brief, Preset, Plugins, and Services
A complete walkthrough of building a production GrapesJS editor: how to choose a preset, pick plugins, and scope setup services without burning a sprint.
Tutorial
Embed GrapesJS in Your SaaS: A Weekend Guide
Embed GrapesJS in your SaaS and ship a white-label page builder over a weekend. Honest tradeoffs, real code, and the plugins that close the UX gap.
Tutorial
Big Updates: TinyMCE 8 and Placeholder 2.0 for GrapesJS
In May we shipped major updates to two of our most popular GrapesJS plugins β TinyMCE Inline Text Editor and Placeholder.
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.