spatie/laravel-mailcoach-codemirror
Optional add-on for Mailcoach that adds the CodeMirror HTML editor to the campaign editor, with MJML autocompletion. Works with Mailcoach v7+ where MJML can be compiled automatically.
Installation Require the package via Composer in your Mailcoach project:
composer require spatie/laravel-mailcoach-codemirror
Publish the package assets (if needed):
php artisan vendor:publish --provider="Spatie\MailcoachCodemirror\MailcoachCodemirrorServiceProvider"
Enable in Mailcoach
Ensure you’re using Mailcoach v7+ (for MJML support). Add the editor to your Mailcoach installation by configuring it in config/mailcoach.php:
'editor' => [
'enabled' => true,
'codemirror' => [
'mode' => 'htmlmixed', // or 'mjml' if using MJML
],
],
First Use Case
HTML Editing with Syntax Highlighting
htmlmixed, xml, javascript) via config:
'codemirror' => [
'mode' => 'htmlmixed',
'lineNumbers' => true,
'theme' => 'default', // or 'dracula', 'monokai', etc.
],
MJML Integration (v7+)
'codemirror' => [
'mode' => 'mjml',
],
<mjml> tags) and let Mailcoach compile it to HTML on send.mj-button, mj-section) reduces manual errors.Customizing the Editor
'codemirror' => [
'plugins' => [
'foldcode',
'lintjs', // Example: Add JavaScript linting
],
],
Ctrl+Enter for send):
'codemirror' => [
'extraKeys' => [
'Ctrl-Enter' => null, // Disable default send shortcut
],
],
Integration with Mailcoach Hooks
mailcoach.campaign.saving event to validate editor content before save:
Mailcoach::campaignSaving(function ($campaign) {
if ($campaign->html && strpos($campaign->html, '<script>') !== false) {
throw new \Exception('Scripts are not allowed in emails.');
}
});
MJML Mode Limitations
Asset Conflicts
// vite.config.js
exclude: ['resources/vendor/spatie/mailcoach-codemirror/**'],
Performance with Large Templates
Plugin Incompatibility
show-hint) may conflict with Mailcoach’s existing JS. Test thoroughly after adding custom plugins./vendor/spatie/mailcoach-codemirror/js/codemirror.js. Ensure assets are published:
php artisan vendor:publish --tag=mailcoach-codemirror-assets
mode is set to 'mjml' in config. Clear config cache if needed:
php artisan config:clear
Keyboard Shortcuts
Ctrl+Shift+[/] to collapse MJML/HTML blocks.Ctrl+G (default CodeMirror shortcut).Custom Themes
<link rel="stylesheet" href="{{ asset('css/codemirror-theme.css') }}">
'codemirror' => [
'theme' => 'custom-theme',
],
Validation Rules
use Spatie\Html\HtmlFacade;
Mailcoach::campaignSaving(function ($campaign) {
$cleanHtml = HtmlFacade::clean($campaign->html);
$campaign->html = $cleanHtml;
});
Extending MJML Support
// resources/js/mailcoach-codemirror-extensions.js
CodeMirror.registerHelper('hint', 'mjml', function (editor) {
// Custom MJML hints logic
});
<script src="{{ asset('vendor/spatie/mailcoach-codemirror/js/codemirror.js') }}"></script>
<script src="{{ asset('js/mailcoach-codemirror-extensions.js') }}"></script>
How can I help you explore Laravel packages today?