Issue #3688Opened August 10, 2021by mingxin-yang0 reactions

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)

artfAugust 10, 20210 reactions

Do this isComponent: el => (el.tagName || '').toLowerCase() === 'mj-body',

Related Questions and Answers

Continue research with similar issue discussions.

Paid Plugins That Match This Issue

Curated by issue keywords and label relevance to help you ship faster.

View all plugins

Loading paid plugin recommendations...

Browse Plugin Categories

Jump directly to plugin category pages on the marketplace.