Issue #1507Opened October 12, 2018by himanshubudhlakoti46 reactions

How to add a custom button with custom command in default panel of grapesjs

Question

I have integrated grapesjs with angular6. and i am getting a default panel with a select box appearing at top of screen; i want a custom button with my own custom command on the default panel. i want to add a button looks like below marked by red color.

6666666

i want to when i click on the button then an alert box should be displayed on the screen with hello world message. please let me know how can i add my custom button on default panel

Answers (3)

mcsekar12October 12, 201835 reactions

Use editor.Panels.getPanels() to get all the available panels and then use addButtons method shown below.Here 'options' is the panel name

editor.Panels.addButton('options', [ { id: 'save', className: 'fa fa-floppy-o icon-blank', command: function(editor1, sender) { alert('Hello World') }, attributes: { title: 'Save Template' } }, ]);

TemaMixAugust 27, 20198 reactions

I also had trouble with that when had tried to button to Panel. So, I used the following way: ` editor.Panels.addButton('#target panel id#',

{ id: 'alert-button', className: 'btn-alert-button', label: 'Click my butt(on)', command(editor) { alert('Hello World'); } active: true }); ` However, the panel and button were displayed.

It turns on it needs to use the render method to rerender Panel manager object. I can't find information about it into docs. So, decided this note here: editor.Panels.render(); After that, the button and panel are displayed.

mararn1618October 12, 20183 reactions

This is well documented at https://grapesjs.com/docs/getting-started.html#panels-buttons. You have two methods to add a panel with buttons: (1) When initializing GrapesJS, (2) After GrapesJS was initialized with addPanel.

Please note that this is for a panel that you have defined with <div id='basic-actions'></div>.

GrapesJS.init()

this.editor = grapesjs.init({
      container: '#gjs',
      fromElement: false,/* https://github.com/artf/grapesjs#usage */
      addBasicStyle: true,
      height: '800px',
      width: 'auto',

      panels: {
  id: 'basic-actions',
  el: '.panel__basic-actions',
  buttons: [
    {
      id: 'alert-button',
      className: 'btn-alert-button',
      label: 'Click my butt(on)',
      command(editor) { alert('Hello World'); }
    }
  ]
},
...});

After GrapesJS was initialized

editor.Panels.addPanel({
  id: 'basic-actions',
  el: '.panel__basic-actions',
  buttons: [
    {
      id: 'alert-button',
      className: 'btn-alert-button',
      label: 'Click my butt(on)',
      command(editor) { alert('Hello World'); }
    }
  ]
});

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.