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

Add event and getJs for a component #2956

Closed dat15021999 closed 4 years ago

dat15021999 commented 4 years ago

Hi artf, I'm newbie with this Grapesjs. How to add event for a component and get the corresponding javascript or jquery ? Thank all

RJCAM commented 4 years ago

Hi @dat15021999 GrapesJS doesn't have support for javascript code, only html and css. Although, you can retrieve all js code by running editor.getJs() function.

Ju99ernaut commented 4 years ago

You can use something like:

const target = editor.getSelected();
const code = target.get('script') || '';
// some logic to view and edit code
targer.set('script', code);

To add an event for example you set code to:

const el = this;
el.addEventListener('event', eventHandler);
function eventHandler(e) {
  // handle event
};

You can check out grapesjs-script-editor and grapesjs-blockly plugins.

EDIT: You should use target.getScriptString() instead of target.get('script').