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.38k stars 4.06k forks source link

QUESTION: Custom Asset Manager. How to set bg image #3415

Closed mmotov closed 3 years ago

mmotov commented 3 years ago

Hi there! I'm building a custom modal for the Asset Manager and basically, it seems that I need to override 'open-assets'command. How I can understand if a user wants to set an image src or background-image property? Thanks in advance!

advancedsoftwarecanada commented 3 years ago

Seconded, just started with GrapeJS and got it operational on my system, looking to set the background image on a header div without having to write css code :/

artf commented 3 years ago

Actually, the knowledge of what to do on asset selection should not be part of the command itself as the action should be decided by who runs the command, eg.

editor.runCommand('open-assets', {
    onSelect(asset) {
      const cmp = editor.getSelected();
      cmp && cmp.addStyle({ 'background-image': `url("${asset.get('src')}")` })
    }
});

I'd suggest inspecting the current implementation of the command, but I have to say that is quite old, so If you'll find anything that might be improved, I'll be happy to hear that.

advancedsoftwarecanada commented 3 years ago

This is not the right solution to our question.

What I'm looking to do is add this to a component, how would we do this?

image

advancedsoftwarecanada commented 3 years ago

Ugh, sorry, I'm a grapejs noob, there's already a setting for this:

image

ronaldohoch commented 3 years ago

Actually, the knowledge of what to do on asset selection should not be part of the command itself as the action should be decided by who runs the command, eg.

editor.runCommand('open-assets', {
    onSelect(asset) {
      const cmp = editor.getSelected();
      cmp && cmp.addStyle({ 'background-image': `url("${asset.get('src')}")` })
    }
});

I'd suggest inspecting the current implementation of the command, but I have to say that is quite old, so If you'll find anything that might be improved, I'll be happy to hear that.

Hello, sorry for comment in here, but it's not working. But i have made a little change to have it working here..

It's my function, i needed to change all the assets implementation. Just set the code as, if using mjml plugin: editor.getSelected().addStyle({ 'background-url': textUrl }); In normal html: editor.getSelected().addStyle({ 'background-image': url(${textUrl}) });

  Commands.add('open-assets', editor => { 
      //... some code from my own assets manager
      if(listOfImageTags.includes(editor.getSelected().attributes.tagName)){
          editor.getSelected().set({ src: txtUrl, alt: txtAlt });
      }else{
          if(stateEditor==="EMAIL"){
              editor.getSelected().addStyle({ 'background-url': textUrl });
          }else{
              editor.getSelected().addStyle({ 'background-image': `url(${textUrl})` });
          }
      }
  });