Issue #3247Opened January 24, 2021by fmay1 reactions

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)

artfJanuary 28, 20211 reactions

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.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.