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.24k stars 4.04k forks source link

[QUESTION] Custom panel button that dynamically Enable/Disable user scripts #2814

Closed RJCAM closed 4 years ago

RJCAM commented 4 years ago

Hello and thanks for sharing such a good project. I'm trying to customize some things in the editor but I have some questions that I would appreciate if they could be clarified. Recently I create a panel button to switch editor from absolute and default modes and it's working without any problem.

cmmd.add('change_mode', {
  run: function(editor) {
    editor.getModel().set('dmode', 'absolute');
  },
  stop: function(editor) {
    editor.getModel().set('dmode', 'default');
  },
});

pnm.addButton('options', [{
  className: 'fa fa-hand-rock-o',
  command: 'change_mode',
  attributes: {
    title: 'Drag Mode'
  }
}]);

But when I try the same method (pratically the same code) to make another panel button that allows users to enable/disable scripts in the editor (allowScripts: 0 or 1).

cmmd.add('allowScripts', {
  run: function(editor) {
    editor.getModel().set('allowScripts', 1);
  },
  stop: function(editor) {
    editor.getModel().set('allowScripts', 0);
  },
});

pnm.addButton('options', [{
  className: 'fa fa-terminal',
  command: 'allowScripts',
  attributes: {
    title: 'Allow Scripts'
  }
}]);

I really see this is changing the value through the editor.getModel().attributes.allowScripts function but in practise it's not working. We should see an alert when the button is clicked and the Allow Script button is active. But only If we initialize the editor with ...allowScripts: 1... we can see the alert, but it won't deactivate with the custom panel button. Am I missing something here? Here is a codepen demo of the problem: https://codepen.io/RJCAM/pen/xxZbNLw

BTW: This code is just a test, after doing it what I really want is allow user scripts only in preview mode and not in editor mode.

artf commented 4 years ago

You can change it in this way editor.getConfig().allowScripts = ...

RJCAM commented 4 years ago

Thank you for the response @artf . Here's the problem, the code you give me changes the value but still doesn't work as I described above. In the demo I share (https://codepen.io/RJCAM/pen/xxZbNLw) I have a button that shows an alert when clicked.

Expected Behavior: When button AllowScripts is disabled
image --> this button the editor initial config changes the allowScripts to 0 AND when button inside canvas is clicked, it should NOT run the button script and NOT show the alert.

Current Behavior When button AllowScripts is disabled
image --> this button the editor initial config changes the allowScripts to 0 BUT when button inside canvas is clicked, the editor STILL runs the button script and STILL shows the alert.

artf commented 4 years ago

AllowScripts is for enabling parsing of JS, once the script is inserted it's executed, so you can't remove it (one way would be accessing the iframe's context and making changes manually)