GrapesJS Preset Newsletter

repository·master·Indexed 42 days ago

https://github.com/grapesjs/preset-newsletter

A GrapesJS plugin version 1.0.2 that configures the editor for building email newsletters. It provides specialized blocks and commands to ensure layouts render correctly across major email clients, featuring automatic CSS inlining via juice, customizable block attributes, and specific styling options for tables and cells.

Tokens
3.3K
Snippets
8
Records
8
Agent score
3%

What's inside GrapesJS Preset Newsletter

  1. Use grapesjs-preset-newsletter in your project

    master

    The preset can be integrated into GrapesJS using either a direct script inclusion in the browser or via modern JavaScript modules.

    Browser usage

    Include the GrapesJS core files and the preset script in your HTML, then initialize the editor with the plugin name.

    Modern JavaScript usage

    Import the plugin and pass it to the plugins array. You can provide configuration via pluginsOpts using the plugin object as the key.

    import grapesjs from 'grapesjs';
    import plugin from 'grapesjs-preset-newsletter';
    
    const editor = grapesjs.init({
      container : '#gjs',
      // ...
      plugins: [plugin],
      pluginsOpts: {
        [plugin]: { /* options */ }
      }
      // or
      // plugins: [
      //   editor => plugin(editor, { /* options */ }),
      // ],
    });
  2. Install grapesjs-preset-newsletter

    master

    You can install the newsletter preset using NPM, via CDN, or by cloning the repository directly.

    NPM:

    npm i grapesjs-preset-newsletter

    CDN: Use the following URL: https://unpkg.com/grapesjs-preset-newsletter

  3. Configure grapesjs-preset-newsletter options

    master

    The newsletter preset accepts several configuration options to customize blocks, commands, and behavior.

    Key options include:

    • blocks: Control which blocks are added (defaults to all).
    • block: A function (blockId) => ({}) to add custom options to specific blocks.
    • inlineCss: If true, inlines CSS on export (defaults to true).
    • juiceOpts: Custom options for the juice HTML inliner.
    • cellStyle: Default style used inside blocks tds.
    • tableStyle: Default style used for blocks tables.
    • updateStyleManager: Updates the Style Manager with newsletter-reliable properties (defaults to true).
    pluginsOpts: {
      'grapesjs-preset-newsletter': {
        // options
      }
    }
  4. Configure the GrapesJS Newsletter Preset

    master

    The grapesjs-preset-newsletter is a plugin for GrapesJS designed for email template creation. You can initialize it by passing a PluginOptions object to the GrapesJS plugin configuration.

    Key capabilities include:

    • Block Management: Control which newsletter-specific blocks are loaded and customize their attributes.
    • Email Optimization: Automatically inline CSS using juice during export.
    • UI Customization: Customize modal titles, labels, and button text for import/export workflows.
    • Styling: Set default styles for tables and table cells to ensure email compatibility.
    import grapesjs from 'grapesjs';
    import newsletterPreset from 'grapesjs-preset-newsletter';
    
    grapesjs.init({
      // ... other GrapesJS config
      plugins: [newsletterPreset],
      pluginsOpts: {
        [newsletterPreset]: {
          blocks: ['button', 'text'], // Only load specific blocks
          inlineCss: true,            // Ensure CSS is inlined on export
          tableStyle: { width: '100%' },
        },
      },
    });
  5. Reference: grapesjs-preset-newsletter configuration options

    master

    The following table lists all available configuration options for the grapesjs-preset-newsletter plugin.

    | Option | Description | Default |
    | - | - | - |
    | `blocks` | Which blocks to add | `All available blocks` |
    |`block`| Add custom block options, based on block id|`(blockId) => ({})`|
    |`cmdOpenImport`| Import command id |`gjs-open-import-template`|
    |`cmdTglImages`| Toggle images command id |`gjs-toggle-images`|
    |`cmdInlineHtml`| Get inlined HTML command id |`gjs-get-inlined-html`|
    |`modalTitleImport`| Title for the import modal |`Import template`|
    |`modalTitleExport`| Title for the export modal |`Export template`|
    |`modalLabelExport`| Label for the export modal |`''`|
    |`modalLabelImport`| Label for the import modal |`''`|
    |`modalBtnImport`| Label for the import button |`Import`|
    |`importPlaceholder`| Template as a placeholder inside import modal |`''`|
    |`inlineCss`| If `true`, inlines CSS on export |`true`|
    |`updateStyleManager`| Update Style Manager with more reliable style properties to use for newsletters |`true`|
    |`showStylesOnChange`| Show the Style Manager on component change |`true`|
    |`showBlocksOnLoad`| Show the Block Manager on load |`true`|
    |`codeViewerTheme`| Code viewer theme |`hopscotch`|
    |`juiceOpts`| Custom options for the `juice` HTML inliner |`{}`|
    |`textCleanCanvas`| Confirm text before clearing the canvas |`Are you sure you want to clear the canvas?`|
    |`useCustomTheme`| Load custom preset theme |`true`|
    |`cellStyle`|  Default style used inside blocks `td`s |`{ padding: 0, margin: 0, 'vertical-align': 'top' }`|
    |`tableStyle`| Default style used for blocks tables |`{ height: '150px', margin: '0 auto 10px auto', padding: '5px 5px 5px 5px', width: '100%' }`|
  6. Reference: grapesjs-preset-newsletter commands and blocks

    master

    The preset provides specific commands and blocks optimized for email newsletter construction.

    ### Commands
    * `gjs-get-inlined-html`: Get html with inlined CSS
    * `gjs-open-import-template`: Opens a modal for the import
    * `gjs-toggle-images`: Enable/Disable images
    
    ### Blocks
    * `sect100`: A section with 1 100% cell inside
    * `sect50`: A section with 2 50% cells inside
    * `sect30`: A section with 3 33.3333% cells inside
    * `sect37`: A section with 2 cells inside: 30% and 70%
    * `button`: Simple button
    * `divider`: Divider block
    * `text`: Simple text component
    * `text-sect`: A block with 2 text components, respectively for the heading and paragraph
    * `image`: Simple image component
    * `quote`: Text component for quotes
    * `grid-items`: Block of 2 components in row
    * `list-items`: List of 2 components
  7. Customize block attributes with the block option

    master

    The block option in PluginOptions allows you to inject custom attributes or properties into specific blocks based on their blockId. This is useful for setting default behaviors or styles for specific newsletter components.

    const newsletterOptions = {
      block: (blockId: string) => {
        if (blockId === 'quote') {
          return { attributes: { class: 'custom-quote' } };
        }
        return {};
      }
    };
  8. PluginOptions Reference

    master

    The PluginOptions interface allows you to customize the behavior and appearance of the newsletter preset. Below are the available configuration keys:

    export interface PluginOptions {
      /** Which blocks to add. */
      blocks?: string[];
    
      /** Add custom block options, based on block id. */
      block?: (blockId: string) => ({});
    
      /** Custom style for table blocks. */
      tableStyle?: Record<string, string>;
    
      /** Custom style for table cell blocks. */
      cellStyle?: Record<string, string>;
    
      /** Import command id. @default 'gjs-open-import-template' */
      cmdOpenImport?: string;
    
      /** Toggle images command id. @default 'gjs-toggle-images' */
      cmdTglImages?: string;
    
      /** Get inlined HTML command id. @default 'gjs-get-inlined-html' */
      cmdInlineHtml?: string;
    
      /** Title for the import modal. @default 'Import template' */
      modalTitleImport?: string;
    
      /** Title for the export modal. @default 'Export template' */
      modalTitleExport?: string;
    
      /** Label for the export modal. @default '' */
      modalLabelExport?: string;
    
      /** Label for the import modal. @default '' */
      modalLabelImport?: string;
    
      /** Label for the import button. @default 'Import' */
      modalBtnImport?: string;
    
      /** Template as a placeholder inside import modal. @default '' */
      importPlaceholder?: string;
    
      /** If `true`, inlines CSS on export. @default true */
      inlineCss?: boolean;
    
      /** Update Style Manager with more reliable style properties to use for newsletters. @default true */
      updateStyleManager?: boolean;
    
      /** Show the Style Manager on component change. @default true */
      showStylesOnChange?: boolean;
    
      /** Show the Block Manager on load. @default true */
      showBlocksOnLoad?: boolean;
    
      /** Code viewer theme. @default 'hopscotch' */
      codeViewerTheme?: string;
    
      /** Custom options for `juice` HTML inliner. @default {} */
      juiceOpts?: juice.Options;
    
      /** Confirm text before clearing the canvas. @default 'Are you sure you want to clear the canvas?' */
      textCleanCanvas?: string;
    
      /** Load custom preset theme. @default true */
      useCustomTheme?: boolean;
    }