How to customize a component
Question
I read this document https://grapesjs.com/docs/modules/Components.html#define-custom-component-type and tried to write the following code. I want to make mj-body impossible to delete:
const myNewComponentTypes = editor => {
editor.DomComponents.addType("my-body-type",{
isComponent: el => el.tagName === 'mj-body',
model:{
default:{
removable:false,
tagName:'mj-body',
}
}
});
};
var editor = grapesjs.init({
container: '#gjs',
// Get the content for the canvas directly from the element
// As an alternative we could use: `components: '<h1>Hello World Component!</h1>'`,
fromElement: true,
// Size of the editor
height:600,
width: 'auto',
// Disable the storage manager for the moment
storageManager: false,
// Avoid any default panel
panels: { defaults: [] },
plugins: ['grapesjs-mjml','gjs-plugin-ckeditor', myNewComponentTypes],
pluginsOpts: {
'grapesjs-mjml': {},
'gjs-plugin-ckeditor': {/* ...options */}
},
})
<div id="gjs" style="height: 0px; overflow: hidden">
<mjml>
<mj-head>
<mj-font
name="Barlow"
href="https://fonts.googleapis.com/css?family=Barlow"
/>
<mj-style> .slogan { background: #000; } </mj-style>
</mj-head>
<mj-body>
But I don't see the effect, please help me
Answers (1)
Do this isComponent: el => (el.tagName || '').toLowerCase() === 'mj-body',
Related Questions and Answers
Continue research with similar issue discussions.
Issue #1930
[QUESTIONS] change 2 attributes with 1 trait
I have an input trait, I want to change the 2 attributes (name and type) on the selected input element, on canvas is appropriate, but in th...
Issue #2055
[QUESTION]: How to remove the components correctly?
I want to remove the components programmatically, the following is my code snippet. After the components have been removed, then undo the d...
Issue #2079
Make Css style to not be returned
How to prevent the CSS of a block/component to be not returned when we used editor.getCss(); it's like https://grapesjs.com/docs/api/compon...
Issue #6152
BUG: CSS added via custom code persists after custom code component is removed
GrapesJS version [X] I confirm to use the latest version of GrapesJS What browser are you using? any Reproducible demo link https://grapesj...
Paid Plugins That Match This Issue
Curated by issue keywords and label relevance to help you ship faster.
Loading paid plugin recommendations...
Browse Plugin Categories
Jump directly to plugin category pages on the marketplace.