grapesjs icon indicating copy to clipboard operation
grapesjs copied to clipboard

[Bug]: Preview mode & automatic components borders toggling

Open mcottret opened this issue 6 years ago • 7 comments

Hello again :)

Our team recently started customising the editor & removing the webpage preset, and we might have found some bugs related to the preview mode:

  1. When exiting preview mode, an error can be raised if the default options panel has been removed, or if the sw-visibility button has been removed from it (from here, if editor.Panels.getButton('options', 'sw-visibility') yields null, cf #2589)

A proposed solution would be to loop through the panels collection (retrieved via editor.Panels.getPanels) to find the sw-visibility button, as well as adding null-checks to avoid errors.


  1. When the preview & the sw-visibility buttons are in the same panel, the behaviour introduced by #2589 breaks, this seems to come from the ButtonView's active change listener, which deactivates all active buttons in the panel. So if the preview is activated, it deactivates the sw-visibility button before running the command, which later breaks its active check.

I'm afraid I cannot propose a solution regarding this point 2. due to my lack of knowledge on possible side effects modifying the buttons active change listener. But I could work your suggestions in a PR if needed.

I'd be happy to open a PR, or since this is pretty closely related to #2636, fix both issues in one.

Cheers :)

mcottret avatar Mar 11 '20 17:03 mcottret

Thanks @mcottret for all the references, really well-written issue :)

When the preview & the sw-visibility buttons are in the same panel, the behaviour introduced by #2589 breaks, this seems to come from the ButtonView's active change listener , which deactivates all active buttons in the panel. So if the preview is activated, it deactivates the sw-visibility button before running the command, which later breaks its active check.

I think, in this case, we should actually create a variable flag by checking if the sw-visibility is active, before doing this: https://github.com/artf/grapesjs/blob/85b7b82103e12fb01a3700b1002a57a7b60ccaf1/src/commands/view/Preview.js#L20 Then on stop, we check the variable stored on start (instead of the button)

artf avatar Mar 18 '20 13:03 artf

Thanks for the reply @artf !

I'm sorry but I'm not sure how to make this work, from what I understand the active change listener gets executed before the Preview's run, so the sw-visibility button would already be deactivated by the time Preview's run would be executed no matter what, is that correct ?

From your reply on the linked issue, I understand this behaviour is intended, so maybe the solution is simply to separate the two buttons contexts as you suggested ?


What do you think about point 1. & my proposed solution ? I already added the safeguard in case the sw-visibility button would have been removed in this PR, but it does not handle a case where the sw-visibility button would be present in another / custom panel (with an id not equal to 'options').

mcottret avatar Mar 20 '20 15:03 mcottret

button would already be deactivated by the time Preview's run would be executed no matter what, is that correct ?

oh yeah, right 🤦‍♂

From your reply on the linked issue, I understand this behaviour is intended, so maybe the solution is simply to separate the two buttons contexts as you suggested ?

Ok, it might actually work... being in a different context it will not be stopped by other buttons, and you can also stop relying on buttons at all (as you said I can place it in another panel) and check instead the state of the command by using Commands.getActive API

if ('sw-visibility' in editor.Commands.getActive()) {
	// sw-visibility is active
}

artf avatar Mar 21 '20 00:03 artf

Thanks for all the hints ! I've tried to put this all together in the PR (I've also noticed some other possible problems with multiple preview runs and programmatic borders toggling in the meantime), hope this is correct !

#2689 opened.

mcottret avatar Mar 27 '20 18:03 mcottret

Has this been fixed @artf? I'm having the same problem - when pressing any button inside the same panel as the sw-visibility button, the sw-visibility button will be deactivated. I could obviously just copy the code from the PRs into each function (undo, redo, preview etc.), but this seems counter-productive. Has an easier solution been provided since?

The demo seems to handle it fine, I'm just wondering how it's been achieved there (perhaps the sw-visibility button is actually in its own separate panel?).

benryanwilliams avatar Jan 03 '23 11:01 benryanwilliams

@benryanwilliams might be related to the context key which indicates the button command is independent from other buttons in the same panel (check the default options)

artf avatar Jan 06 '23 09:01 artf

Yep, adding context: 'sw-visibility' to the button works perfectly. Thanks!

benryanwilliams avatar Jan 09 '23 06:01 benryanwilliams