Question : Traits
Question
Hi @artf - what you've built is magnificent. It's been a gentle struggle as I'm far from being a JS wizard. However, I've got just about everything fully configured - except for this!
I want to set traits that display when an image is selected (alt, src, id). I am sure it can be done once at initialisation, but I'm buggered if I can figure out how do do it. I've read everything I can find in the issues and am still stuck.
The only way I got it to work is as follows ...
// Set the traits for <img>
editor.on('component:selected', function(a) {
const component = editor.getSelected()
if( component.attributes.tagName === 'img' ) {
if( !component.getTrait('alt')) component.addTrait( { name: 'alt' } )
if( !component.getTrait('src')) component.addTrait( { name: 'src' } )
if( !component.getTrait('id')) component.addTrait( { name: 'id' } )
}
})
Also - the out-of-the-box "Alt" comes first and I'd really like it to be after src. I can't figure out how to get rid of it as removeTrait() doesn't seem to work.
While my solution works, I'd love to know how it should be done.
Many thanks!
Answers (1)
Hi Freddy, you can simply extend the image component in this case
editor.Components.addType('image', {
model: {
defaults: {
traits: [{name: 'alt'}, {name: 'src'}, {name: 'id'}]
}
}
});
Related Questions and Answers
Continue research with similar issue discussions.
Issue #2066
[QUESTION] Page Limit
I'm currently using GrapesJS as a Letter/Form Composer, and it's been working wonderfully so far. However I'm just finishing up the last fe...
Issue #667
Question about Asset Manager
Hi @artf , I have two question about Asset Manager. How I can set a image by select, current logic after I upload image, I must select imag...
Issue #1741
[QUESTION]: Can't render GrapesJS HTML/CSS when viewing page on frontend v0.14.50
I have built a page and want to render it on frontend, the strategy I have implemented is to save my HTML and JSON style object and on fron...
Issue #3196
Question: Build grapesjs with npm
Hi @artf, Thank you very much for this great framework. I want to contribute to grapesjs. However, I am stupid about using webpack and babe...
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.