[QUESTIONS] create trait like video trait
Question
how do I make a trait like a video trait, I have a provider type, the other trait type depends on the provider I choose, can it be demonstrated? Thank you very much.

Answers (2)
The video component is a great demonstration in and of itself.
Can be found at grapesjs repo root > src > dom_components > model > ComponentVideo.js.
Here's some example code with the bare minimum necessary code:
var domComps = editor.DomComponents;
var dType = domComps.getType('default');
var dModel = dType.model;
var dView = dType.view;
domComps.addType('testBlock', {
model: dModel.extend({
init() {
const traits = [this.getProviderTrait(), this.getOption1Trait()];
this.set('traits', traits);
this.listenTo(this, 'change:provider', this.updateTraits);
},
updateTraits() {
var prov = this.get('provider');
var traits = [];
switch (prov) {
case 'traitOption1':
traits = [this.getProviderTrait(), this.getOption1Trait()];
break;
case 'traitOption2':
traits = [this.getProviderTrait(), this.getOption2Trait()];
break;
default:
break;
}
this.loadTraits(traits);
this.em.trigger('component:toggled');
},
getProviderTrait() {
return {
type: 'select',
label: 'Provider',
name: 'provider',
changeProp: 1,
options: [
{ value: 'traitOption1', name: 'traitOption1' },
{ value: 'traitOption2', name: 'traitOption2' }
]
}
},
getOption1Trait() {
return {
type: 'text',
label: 'Option1',
name: 'option1',
changeProp: 1,
}
},
getOption2Trait() {
return {
type: 'text',
label: 'Option2',
name: 'option2',
changeProp: 1,
}
}
}, {
isComponent: function(el) {
if(el.tagName == 'DIV'){
return {type: 'testBlock'};
}
},
}),
view: dView,
});
You can test this out by adding a simple div to the Block Manager
editor.BlockManager.add('testBlock', {
label: 'Block',
type: 'testBlock',
content: `<div>Test block</div>`
});
The important parts are this.listenTo(this, 'change:provider', this.updateTraits); in the type init() function to listen for changes to the main trait and making sure changeProp is set to 1 on the traits, otherwise change:provider won't pick up the updates.
Okkay,Thank you very much, I understand
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1987
[QUESTIONS] Create toolbar in bottom
can I make a new toolbar that is located at the bottom (as shown by the cursor) when selected component? Thank's
Issue #362
How to create custom link components that prompts users to type link/choose link similar to Images Component Modal
Image Components have logic to call Modal and prompt user's interaction for options/configuration of image url. Would like to do similar to...
Issue #1926
[QUESTIONS] Edit Placeholder on keyup in Trait
How so that Placeholders can change when typing a word in the trait directly, current placeholders can change only when mouseover I have a...
Issue #1835
How to open up 'Settings' all the time?
I'd like make 'Settings' opened at all times by default without having users to open up manually. How'd you do that? Thank you..
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.