grapesjs icon indicating copy to clipboard operation
grapesjs copied to clipboard

Prevent editing some components

Open cmcintosh opened this issue 9 years ago • 30 comments

I would like to prevent users from editing the internal components of specific components.

IE lets say I have an element called Drupal-Block

<div data-block>
... html inside ..
</div>

I want to prevent the users from editing the ... html inside... bit

cmcintosh avatar Oct 15 '17 05:10 cmcintosh

Are you aware already of data-gjs-* attributes? eg.

<div data-gjs-editable="false" data-gjs-removable="false" ...>
... html inside ..
</div>

artf avatar Oct 16 '17 13:10 artf

hmm, Where in the workflow would I add this. In my case, I am loading the template information via a remote store. Could I add it during that, or can a add it on a editor.init callback.

I tried adding it manually, but was still able to edit the inner elements.

cmcintosh avatar Oct 17 '17 02:10 cmcintosh

this attributes data-gjs-editable="false" data-gjs-removable="false" only works for the current selected element not for their childs, @cmcintosh I also tried put manually but it only works when I set this attributes on every child elements, like this:

image

mklemersson avatar Oct 17 '17 08:10 mklemersson

Yeah, @MichaelKlemersson is right currently you have to set them manually on any element. Another trick would be to update all children via model

const updateAll = model => {
    model.set({editable: false, ...});
    model.get('components').each(model => updateAll(model));
}
updateAll(component);

But what I think would be the best it's some kind of property which propagates (suggestions for a better term are welcome) properties from the parent to its children, eg.

<div data-gjs-editable="false" data-gjs-propagate="['editable', ...]">
     <!-- this will be not editable too and the same for its children -->
     <div>...</div>
     
    <!-- possibility to overwrite and 'break' the chain -->
     <div data-gjs-editable="true" data-gjs-propagate=""></div>
</div>

what do you think?

artf avatar Oct 17 '17 12:10 artf

Yes i think that the propagation idea is great. To be clear the reason im looking for this is im building out a module for Drupal 8 that will allow creating themes via the GrapeJS tool. In Drupal you have elements called Blocks, and my thought was to block of editing of those on the Main editor, and instead have a popup editor to edit them and allow reusing those across various pages.

This is due to how Drupal renders content via PHP and thus I need to convert the html to php variables when storing the template into a twig file.

cmcintosh avatar Oct 17 '17 12:10 cmcintosh

+1 on the propagation idea

Geczy avatar Oct 17 '17 13:10 Geczy

@cmcintosh It's a great idea, I vote for propagates and this will be really helpful using in a trait, so se could toggle this functionality ;)

mklemersson avatar Oct 17 '17 14:10 mklemersson

thanks for the feedback guys, I'll put it on the Roadmap

artf avatar Oct 18 '17 10:10 artf

@artf can you expand on the first option on your previous comment, ''' const updateAll = model => { model.set({editable: false, ...}); model.get('components').each(model => updateAll(model)); } updateAll(component); '''

I am not familiar with what framework your using so am still picking up how its supposed to work (actually dont know what framwork you are using and have been leaning mostly on jquery)

cmcintosh avatar Oct 18 '17 11:10 cmcintosh

He is using backbone js, if I'm not wrong.

mklemersson avatar Oct 18 '17 11:10 mklemersson

Thanks, looks like time for me to learn a new framework ;)

cmcintosh avatar Oct 18 '17 11:10 cmcintosh

Out of curiosity. What's preventing from looking to see if any parent elements have the "data-gjs-editable" attribute to prevent a the current element from being editable?

rip747 avatar Oct 18 '17 17:10 rip747

nothing, I had thought that would be the ideal path. Not totally sure to implement it yet

cmcintosh avatar Oct 19 '17 02:10 cmcintosh

An example you can use in the demo

  1. Select an element (eg. some section)
  2. Execute this in console
editor.getSelected().append(`
<div data-gjs-removable="false" data-gjs-draggable="false" data-gjs-propagate='["removable", "draggable"]' style="color:blue">
  <div>Comp01</div>
	<div>
    <div>Comp11</div>
    <div data-gjs-stop="1" data-gjs-removable="true" data-gjs-draggable="true" data-gjs-propagate='["stop"]' style="color:red">
      <div>Comp21</div>
      <div>Comp22</div>
    </div>
    <div>
      <div>Comp31</div>
      <div>Comp32</div>
    </div>
  </div>
  <div>Comp03</div>
</div>
`)

artf avatar Oct 20 '17 13:10 artf

Thanks, ill look at this tomorrow ;)

cmcintosh avatar Oct 20 '17 13:10 cmcintosh

@artf - Will there be a propagate html attribute? Like what you detailed below?

<div data-gjs-editable="false" data-gjs-propagate="['editable', ...]">

austinbiggs avatar Jan 03 '18 20:01 austinbiggs

@cmcintosh How far did you get into your Drupal 8 Module as I am working to do the same in Drupal 7? Thanks.

Coyote6 avatar Jan 24 '18 15:01 Coyote6

@AustinTBiggs is already there be careful because ['editable', ...] is not a valid JSON (you have to use double quotes for keys), therefore data-gjs-propagate='["editable", ...]'

artf avatar Jan 24 '18 20:01 artf

@Coyote6 I am still working on things for this. Was busy on other things, but am now back at it.

cmcintosh avatar Apr 11 '18 07:04 cmcintosh

@cmcintosh awesome. I completely understand how that goes. I have been updating my repository that I left a link to on your drupal project once I found it. I am looking forward to getting a good solid version working.

Coyote6 avatar Apr 11 '18 14:04 Coyote6