grapesjs-mjml

repository·master·Indexed 20 days ago

https://github.com/grapesjs/mjml

A GrapesJS plugin that integrates MJML (Mailjet Markup Language) components, providing a real-time visual editor for building responsive email templates. It supports the official v4 compiler, custom MJML parsers, and a set of specialized components including mj-body, mj-button, mj-column, mj-divider, mj-navbar-link, and mj-text. The plugin includes commands for retrieving MJML code, converting MJML to HTML, and switching between desktop, tablet, and mobile device views.

Tokens
5.4K
Snippets
20
Records
23
Agent score
72%

What's inside grapesjs-mjml

  1. Integrate grapesjs-mjml into GrapesJS

    master

    You can integrate the plugin using either standard script tags or ESM imports. The plugin enables real-time rendering of MJML components using the official v4 compiler.

    import 'grapesjs/dist/css/grapes.min.css'
    import grapesJS from 'grapesjs'
    import grapesJSMJML from 'grapesjs-mjml'
    
    grapesJS.init({
       fromElement: true,
       container: '#gjs',
       plugins: [grapesJSMJML],
       pluginsOpts: {
          [grapesJSMJML]: {/* ...options */}
       },
    });
  2. How MJML components and views work together

    master

    In the grapesjs-mjml plugin, MJML components are built using a dual-layer approach consisting of a coreMjmlModel and a coreMjmlView.

    Core MJML Model

    The model manages the component's state, specifically its MJML attributes and styles. It synchronizes GrapesJS style and attributes so that changes to one reflect in the other. It also handles the conversion of component data into valid MJML XML attributes via getMjmlAttributes() and provides a toHTML() method for direct HTML generation.

    Core MJML View

    Because MJML must be compiled to produce valid HTML, the view is responsible for the compilation lifecycle:

    1. Template Generation: It uses getMjmlTemplate() (for the full document wrapper) and getInnerMjmlTemplate() (for the specific component's XML tag and attributes).
    2. Compilation: It uses getTemplateFromMjml() to wrap the component in an <mjml> document, runs it through the mjmlParser, and extracts the resulting HTML.
    3. Rendering: The render() method updates the element's inner HTML with the compiled output, renders child components, and applies styles.

    When creating custom MJML components, you can override these helpers to change how your component's XML is structured or how it is compiled.

    // Conceptual overview of the model/view relationship
    // The model handles: attributes, styles, and MJML XML generation
    // The view handles: MJML compilation, template wrapping, and DOM rendering
  3. Configure grapesjs-mjml plugin options

    master

    The grapesjs-mjml plugin accepts several configuration options to customize the MJML experience. Key options include:

    • blocks: Which blocks to add (default: all).
    • block: Function to add custom block options based on block ID: (blockId) => ({}).
    • mjmlParser: Custom mjml-browser instance to extend MJML functionality or add custom components.
    • customComponents: List of components to be added to the default set.
    • useXmlParser: Experimental option to use an XML parser instead of HTML, allowing void MJML elements like <mj-image/>.
    • columnsPadding: Sets column padding to make selection easier (default: '10px 0').
    • overwriteExport: Whether to overwrite the default export command (default: true).
    • resetBlocks, resetDevices, resetStyleManager: Boolean flags to clean/reset specific parts of the editor (default: true).
  4. Install and use the GrapesJS MJML plugin

    master

    The grapesjs-mjml plugin integrates MJML support into the GrapesJS editor. It provides MJML-specific blocks, components, commands, and panels. You can use it by passing the plugin function to the GrapesJS editor initialization.

    import grapesjs from 'grapesjs';
    import mjmlPlugin from 'grapesjs-mjml';
    
    const editor = grapesjs.init({
      container: '#gjs',
      plugins: [mjmlPlugin],
      pluginsOpts: {
        [mjmlPlugin]: {
          // Your plugin options here
        }
      }
    });
  5. Configure i18n for grapesjs-mjml

    master

    To localize the MJML plugin, pass an i18n object within the pluginsOpts for grapesjs-mjml.

    import 'grapesjs/dist/css/grapes.min.css'
    import grapesJS from 'grapesjs'
    import nl from 'grapesjs/locale/nl'
    import grapesJSMJML from 'grapesjs-mjml'
    import mjmlNL from 'grapesjs-mjml/locale/nl'
    
    grapesJS.init({
       fromElement: true,
       container: '#gjs',
       i18n: {
          messages: { nl: nl },
       },
       plugins: [grapesJSMJML],
       pluginsOpts: {
          [grapesJSMJML]: {
            i18n: { nl: mjmlNL }
          }
       },
    });
  6. Use a custom MJML parser and custom components

    master

    If you have a custom version of MJML with extended components, you can override the default mjmlParser and provide a list of customComponents via the plugin options.

    import 'grapesjs/dist/css/grapes.min.css'
    import grapesJS from 'grapesjs'
    import grapesJSMJML from 'grapesjs-mjml'
    import customMjmlParser from 'custom-mjml-parser';
    
    import customImage from 'custom/components/path'
    
    grapesJS.init({
       fromElement: true,
       container: '#gjs',
       plugins: [grapesJSMJML],
       pluginsOpts: {
          [grapesJSMJML]: {
            mjmlParser: customMjmlParser,
            customComponents: [
              customImage,
            ]
          }
       },
    });
  7. Configure the GrapesJS MJML plugin via PluginOptions

    master

    When initializing the GrapesJS MJML plugin, you can pass a PluginOptions object to customize its behavior. This includes controlling which blocks are loaded, adding custom MJML components, configuring the parser, and managing how the editor handles MJML-specific elements like fonts and themes.

    Key configuration areas include:

    • Blocks: Use blocks to specify a subset of blocks or block to provide a function that returns custom options for specific block IDs.
    • Components: Use customComponents to register your own MJML components with the editor.
    • Export/Import: Customize the MJML export output using preMjml and postMjml, or set an importPlaceholder for the import modal.
    • Parser: Provide a custom mjmlParser instance if you are not using the default mjml-browser build.
    • Styling: Configure fonts for the exported HTML header and columnsPadding to improve column selection in the editor.
    // Example configuration for the MJML plugin
    {
      blocks: ['mj-hero', 'mj-text'],
      block: (blockId) => (blockId === 'mj-hero' ? { attributes: { custom: 'value' } } : {}),
      customComponents: [(editor, componentOptions) => {
        // Logic to add custom MJML components
      }],
      fonts: {
        Montserrat: 'https://fonts.googleapis.com/css?family=Montserrat',
        'Open Sans': 'https://fonts.googleapis.com/css?family=Open+Sans'
      },
      useXmlParser: true // Experimental: allows importing void elements like <mj-image/>
    }
  8. Configure the GrapesJS MJML plugin options

    master

    When initializing the plugin, you can pass a PluginOptions object to customize its behavior.

    Key configuration options include:

    • blocks: An array of MJML block identifiers to include (e.g., 'mj-text', 'mj-image').
    • customComponents: An array of custom MJML components to register.
    • mjmlParser: The function used to parse MJML into HTML (defaults to mjml2html).
    • useXmlParser: If true, configures the editor's parser to use text/xml.
    • hideSelector: If true, hides the default selector manager.
    • useCustomTheme: If true (and running in a browser), injects a default CSS theme for the plugin UI.
    • i18n: An object containing translation messages to extend or override the default English locale.
    • resetBlocks, resetStyleManager, resetDevices: Boolean flags to determine if the plugin should reset these editor features.
    const pluginOptions = {
      blocks: ['mj-text', 'mj-button'], // Only include specific blocks
      useXmlParser: true,
      hideSelector: true,
      i18n: {
        // Custom translations
      }
    };
  9. mj-navbar-link component properties and styling

    master

    The mj-navbar-link component is an MJML component used for navigation links within an mj-navbar.

    Styling

    You can apply the following styles to this component:

    • Typography: font-style, font-size, font-weight, font-family, color, text-decoration, text-transform.
    • Spacing: padding, padding-top, padding-left, padding-right, padding-bottom.

    Default Styles:

    • font-size: 13px
    • padding-top: 25px
    • padding-bottom: 25px
    • padding-left: 10px
    • padding-right: 10px
    • text-transform: uppercase

    Traits

    • href: Used to define the link destination.
  10. Configure options for the MJML to HTML command

    master

    When calling the command to convert MJML to HTML, you can pass CommandOptionsMjmlToHtml to specify the MJML source and other parsing options inherited from MJMLParsingOptions.

    // Example usage of command options
    // Note: This is a type definition for the options object passed to the command
    const options: CommandOptionsMjmlToHtml = {
      mjml: '<mjml>...</mjml>',
      // ... other MJMLParsingOptions
    };