GrapesJS / grapesjs

Free and Open source Web Builder Framework. Next generation tool for building templates without coding
https://grapesjs.com
BSD 3-Clause "New" or "Revised" License
22.36k stars 4.05k forks source link

[QUESTION] Add Dropdown to block using Traits #2850

Closed Aiyush-G closed 4 years ago

Aiyush-G commented 4 years ago

Hi @artf ,

I hope that you are well,

I was wondering whether you could help me with some simple issue that I am having (I have recently started using GrapeJS.

Issue: using a plugin create a block and within the traits section have a drop down that changes the type of block displayed.

Here is my current plugin code which displays two blocks:

function myPlugin(editor){

  editor.BlockManager.add('my-first-block', {
    label: 'Simple block',
    content: '<div class="my-block">This is a simple block</div>',
    category: 'Apple'

  })

  editor.BlockManager.add('my-second-block', {
    label: 'second block',
    content: '<div class="my-block">This is a simple block</div>',
    category: 'Apple'

  });
}

I would like a dropdown to appear in the settings (traits) tab where I can select other options for content e.g:

Simple Block ------------> Red ------------> Orange ------------> Yellow

And then change the content accordingly as follows:

Red: content: '<div class="my-block">This is a simple red block</div>', Orange: content: '<div class="my-block">This is a simple orange block</div>', Yellow: content: '<div class="my-block">This is a simple yellow block</div>', Kind Regards Aiyush

cartifon commented 4 years ago

I'm not sure if you can change the block once you added it.... Maybe you should just have classes to add to the blocks, that you can change the color, size or something like that.

You can take a look on the CSS composer for it.

https://grapesjs.com/docs/api/css_composer.html#add

artf commented 4 years ago

@Aiyush-G I think you should create a custom component with that trait which then changes the content, the block itself would just contain that component

editor.BlockManager.add('my-first-block', {
    label: 'Simple block',
    content: { type: 'your-custom-component', yourProp: 'red' },
    category: 'Apple'
});
editor.BlockManager.add('my-second-block', {
    label: 'Second block',
    content: { type: 'your-custom-component', yourProp: 'orange' },
    category: 'Apple' 
});
Aiyush-G commented 4 years ago

Hi yes, this is what I was thinking, would you mind doing an example for me that changes the html content e.g. choosing red shows your chose red as a

tag and if you choose orange that is shows you chose orange and a