grapesjs icon indicating copy to clipboard operation
grapesjs copied to clipboard

Stop function of all commands are getting executed before the actual run command.

Open slellampati opened this issue 7 years ago • 2 comments

I created 3 custom button(edit, preview, save) and all those have run and stop methods. When i click on any button, it triggers the run command of that button but before that stop function of other buttons are executed.


    // Edit code button
    panelManager.addButton('options', {
      id: 'edit-code',
      className: 'fa fa-edit',
      command: {
        // @ts-ignore
        run(editor: GrapesJS, sender: {}) {
          log.debug('Edit opened');
        },
        stop(editor: GrapesJS) {
          log.debug('Edit complete');
        }
      },
      attributes: { title: 'Edit code' },
      active: false
    });

    // Add preview button
    panelManager.addButton('options', {
      id: 'custom-preview',
      label: 'Preview',
      command: {
        // @ts-ignore
        run(editor: GrapesJS, sender: {}) {
          log.debug('Edit opened');
        },
        stop(editor: GrapesJS) {
          log.debug('Edit complete');
        }
      },
      attributes: { title: 'Preview' },
      active: false
    });

    // Add save button
    panelManager.addButton('options', {
      id: 'save',
      label: 'Save',
      command: {
        run(editor: GrapesJS) {
          log.debug('saved');
        },
        stop(editor: GrapesJS) {
          log.debug('Save complete');
        }
      },
      attributes: { title: 'Save' },
      active: false
    });

When i click on Edit, below is the console output

[Email Template] Stop preview 
[Email Template] Save complete 
[Email Template] Edit opened

When Edit button closed (stop command)

[Email Template] Edit complete 

What is causing the other stop commands to trigger first even when i dont have any event listeners?

slellampati avatar Sep 17 '19 10:09 slellampati

Hi @slellampati,

I'm sorry this answer might be a little late to the party, but I've just stumbled upon the same behaviour & discovered this issue.

To answer the question: GrapesJS's buttons have an active event listener, which deactivates all buttons in a panel in which a button is activated, causing their stop commands to be run

I started a similar discussion on another issue, if you're interested in having more details.

Cheers :)

mcottret avatar Mar 11 '20 17:03 mcottret

You can add a different context: 'eg. save-button' to your buttons to detach them from this default behavior (by default buttons share the same context)

There is actually a tiny reference in getting started guide about this: https://grapesjs.com/docs/getting-started.html#panels-buttons

artf avatar Mar 18 '20 13:03 artf