From a7b2c33b30dcac4d5285b2d22db0eabf7cd8bb34 Mon Sep 17 00:00:00 2001 From: witzig Date: Sat, 27 May 2017 13:15:47 +0200 Subject: [PATCH] GrapeJS MJML Integration (Experimental) #215 --- config/default.toml | 5 +- routes/grapejs.js | 47 +++-- views/grapejs/editor.hbs | 357 ++++++++++++++++++++------------ views/grapejs/layout-editor.hbs | 25 ++- 4 files changed, 278 insertions(+), 156 deletions(-) diff --git a/config/default.toml b/config/default.toml index af72e6dd..cd9446d4 100644 --- a/config/default.toml +++ b/config/default.toml @@ -151,7 +151,10 @@ templates=[["versafix-1", "Versafix One"]] [grapejs] # Installed templates -templates=[["demo", "Demo Template"]] +templates=[ + ["demo", "HTML Template"], + ["aves", "MJML Template"] +] [reports] # The whole reporting functionality can be disabled below if the they are not needed and the DB cannot be diff --git a/routes/grapejs.js b/routes/grapejs.js index ea0c072c..5b32cf35 100644 --- a/routes/grapejs.js +++ b/routes/grapejs.js @@ -1,12 +1,13 @@ 'use strict'; -let config = require('config'); -let express = require('express'); -let router = new express.Router(); -let passport = require('../lib/passport'); -let fs = require('fs'); -let path = require('path'); -let editorHelpers = require('../lib/editor-helpers.js') +const config = require('config'); +const express = require('express'); +const router = new express.Router(); +const passport = require('../lib/passport'); +const _ = require('../lib/translate')._; +const fs = require('fs'); +const path = require('path'); +const editorHelpers = require('../lib/editor-helpers') router.all('/*', (req, res, next) => { if (!req.user) { @@ -23,28 +24,38 @@ router.get('/editor', passport.csrfProtection, (req, res) => { return res.redirect('/'); } - resource.editorName = resource.editorName ||  'grapejs'; - resource.editorData = !resource.editorData ? - { + try { + resource.editorData = JSON.parse(resource.editorData); + } catch (err) { + resource.editorData = { template: req.query.template || 'demo' - } : - JSON.parse(resource.editorData); + } + } - if (!resource.html && !resource.editorData.html) { + if (!resource.html && !resource.editorData.html && !resource.editorData.mjml) { + const base = path.join(__dirname, '..', 'public', 'grapejs', 'templates', resource.editorData.template); try { - let file = path.join(__dirname, '..', 'public', 'grapejs', 'templates', resource.editorData.template, 'index.html'); - resource.html = fs.readFileSync(file, 'utf8'); + resource.editorData.mjml = fs.readFileSync(path.join(base, 'index.mjml'), 'utf8'); } catch (err) { - resource.html = err.message || err; + try { + resource.html = fs.readFileSync(path.join(base, 'index.html'), 'utf8'); + } catch (err) { + resource.html = err.message || err; + } } } res.render('grapejs/editor', { layout: 'grapejs/layout-editor', type: req.query.type, + stringifiedResource: JSON.stringify(resource), resource, - editorConfig: config.grapejs, - csrfToken: req.csrfToken(), + editor: { + name: resource.editorName || 'grapejs', + mode: resource.editorData.mjml ? 'mjml' : 'html', + config: config.grapejs + }, + csrfToken: req.csrfToken() }); }); }); diff --git a/views/grapejs/editor.hbs b/views/grapejs/editor.hbs index 42b74a98..1867139a 100644 --- a/views/grapejs/editor.hbs +++ b/views/grapejs/editor.hbs @@ -59,14 +59,7 @@
-
- {{#if resource.editorData.html}} - - {{{resource.editorData.html}}} - {{else}} - {{{resource.html}}} - {{/if}} -
+
@@ -136,21 +129,69 @@ diff --git a/views/grapejs/layout-editor.hbs b/views/grapejs/layout-editor.hbs index 59005bed..07141ddf 100644 --- a/views/grapejs/layout-editor.hbs +++ b/views/grapejs/layout-editor.hbs @@ -3,17 +3,28 @@ GrapesJS Newsletter Editor - + + + - - - - - - + + + + + {{#switch editor.mode}} + {{#case "mjml"}} + + + + {{/case}} + {{#case "html"}} + + + {{/case}} + {{/switch}}