grapesjs icon indicating copy to clipboard operation
grapesjs copied to clipboard

QUESTION: How to keep default sections in the canvas without the possibility to be removed by the user?

Open jesusdp opened this issue 6 years ago • 2 comments

I want to generate a PDF Tool to have three main sections: HEADER, BODY and FOOTER. The three sections needs to be there always and I want to disable the possibility to allow the users to delete those sections. The rest of the components like images, text, columns, etc. needs to be editable like remove them if the user want to do that.

Captura de Pantalla 2020-09-24 a la(s) 8 01 30 a m

jesusdp avatar Sep 24 '20 13:09 jesusdp

By default all components are removable, so that's already covered. In terms of not allowing certain components to be removed, you can take a look at the component api:https://grapesjs.com/docs/api/component.html#component

You would need to set 'removable':false for your three containers.

editor.DomComponents.addType('Header', {
	model: {
		defaults: {
			tagName: 'div',
                        // Component won't be removable
                       removable:false
		}
	},
	view: {
		tagName: 'div',
		onRender() {

		}
	}
});

Adding components during initialization:

const requiredComps = [{
 type: 'Header',
...
},{
 type: 'Body',
...
},{
 type: 'Footer',
...
}];
const editor = grapesjs.init({
   components: requiredComps
});

Another approach: If you are creating a new 'PDF' always add those three components https://grapesjs.com/docs/api/editor.html#setcomponents

editor.setComponents([{
 type: 'Header',
...
},{
 type: 'Body',
...
},{
 type: 'Footer',
...
}]);

If you have an event for creating a new pdf in your custom app that's a good way to attach this logic, if not you could add this logic after the editor is loaded and you could check for an 'empty' project. Events:https://grapesjs.com/docs/api/editor.html#available-events

// Run after the editor is laoded
editor.on('load', (some, argument) => {
   // There are no components(empty PDF), so add the necessary containers
   if(editor.getComponents().models.length === 0){
         editor.setComponents({
              type: 'Header',
              ...
          },{
              type: 'Body',
              ...
          },{
              type: 'Footer',
              ...
         });
   }
});

MartinPutz avatar Sep 27 '20 12:09 MartinPutz

By default all components are removable, so that's already covered. In terms of not allowing certain components to be removed, you can take a look at the component api:https://grapesjs.com/docs/api/component.html#component

You would need to set 'removable':false for your three containers.

editor.DomComponents.addType('Header', {
	model: {
		defaults: {
			tagName: 'div',
                        // Component won't be removable
                       removable:false
		}
	},
	view: {
		tagName: 'div',
		onRender() {

		}
	}
});

Adding components during initialization:

const requiredComps = [{
 type: 'Header',
...
},{
 type: 'Body',
...
},{
 type: 'Footer',
...
}];
const editor = grapesjs.init({
   components: requiredComps
});

Another approach: If you are creating a new 'PDF' always add those three components https://grapesjs.com/docs/api/editor.html#setcomponents

editor.setComponents([{
 type: 'Header',
...
},{
 type: 'Body',
...
},{
 type: 'Footer',
...
}]);

If you have an event for creating a new pdf in your custom app that's a good way to attach this logic, if not you could add this logic after the editor is loaded and you could check for an 'empty' project. Events:https://grapesjs.com/docs/api/editor.html#available-events

// Run after the editor is laoded
editor.on('load', (some, argument) => {
   // There are no components(empty PDF), so add the necessary containers
   if(editor.getComponents().models.length === 0){
         editor.setComponents({
              type: 'Header',
              ...
          },{
              type: 'Body',
              ...
          },{
              type: 'Footer',
              ...
         });
   }
});

It works like a magic! Thanks for your help @MartinPutz ! I'm really appreciate it.

jesusdp avatar Sep 30 '20 13:09 jesusdp