High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.
JavaScript campaigncampaign-managementemail-marketingemail-subscriptionlistmonkmailing-listnewsletternewsletter-managementnewsletter-softwareself-hostedsms-gatewaysmtptransactional-emails
Add support for converting all types to visual editor blocks.
Kailash Nadh503e98551c2a
diff --git a/frontend/src/components/Editor.vue b/frontend/src/components/Editor.vueindex 5bc7806f..696e2413 100644--- a/frontend/src/components/Editor.vue+++ b/frontend/src/components/Editor.vue@@ -101,6 +101,7 @@ import HTMLEditor from './HTMLEditor.vue';import MarkdownEditor from './MarkdownEditor.vue';import VisualEditor from './VisualEditor.vue';import RichtextEditor from './RichtextEditor.vue';+import markdownToVisualBlock from './editor';const turndown = new TurndownService();@@ -146,10 +147,14 @@ export default {methods: {onContentTypeChange(to, from) {+ if (!this.self.body.trim()) {+ this.convertContentType(to, from);+ return;+ }+// Ask for confirmation as pretty much all conversions are lossy.- const msgKey = to === 'visual' ? 'campaigns.confirmOverwriteContent' : 'campaigns.confirmSwitchFormat';this.$utils.confirm(- this.$t(msgKey),+ this.$t('campaigns.confirmSwitchFormat'),() => {this.convertContentType(to, from);},@@ -162,6 +167,7 @@ export default {convertContentType(to, from) {let body = this.self.body ?? '';+ let bodySource = null;// Skip UI update (markdown => richtext, html requires a backenbd call).let skip = false;@@ -190,6 +196,12 @@ export default {break;}+ case 'visual': {+ const md = turndown.turndown(body).replace(/\n\n+/ig, '\n\n');+ bodySource = JSON.stringify(markdownToVisualBlock(md));+ break;+ }+default:// Switching between HTML formats, no need to do anything further// as body is already beautified.@@ -214,14 +226,11 @@ export default {// Plain to an HTML type, change plain line breaks to HTML breaks.} else if (from === 'plain' && (to === 'richtext' || to === 'html')) {body = body.replace(/\n/ig, '<br>\n');+ } else if (to === 'visual') {+ bodySource = JSON.stringify(markdownToVisualBlock(body));}// =======================================================================- // If the target is visual, empty the visual editor's block content source.- if (to !== 'visual') {- this.self.bodySource = null;- }-// Reset the campaign template ID if its converted to or from visual template.if (to === 'visual' || from === 'visual') {this.templateId = null;@@ -236,6 +245,7 @@ export default {// multiple events.this.self.contentType = to;this.self.body = body;+ this.self.bodySource = bodySource;});}},@@ -392,4 +402,5 @@ export default {},},};+</script>diff --git a/frontend/src/components/VisualEditor.vue b/frontend/src/components/VisualEditor.vueindex 39e24eae..1798ea65 100644--- a/frontend/src/components/VisualEditor.vue+++ b/frontend/src/components/VisualEditor.vue@@ -110,7 +110,7 @@ export default {});});- // Start observing+ // Start observing.observer.observe(iframe.contentDocument.querySelector('#visual-editor-container'), {childList: true,subtree: true,diff --git a/frontend/src/components/editor.js b/frontend/src/components/editor.jsnew file mode 100644index 00000000..0293e964--- /dev/null+++ b/frontend/src/components/editor.js@@ -0,0 +1,74 @@+const markdownToVisualBlock = (markdown) => {+ const lines = markdown.split('\n');+ const blocks = [];+ const idBase = Date.now();+ let textBuf = [];++ const createBlock = (type, props, style = {}) => ({+ id: `block-${idBase + blocks.length}`,+ type,+ data: {+ props,+ style: {+ padding: {+ top: 16, bottom: 16, right: 24, left: 24,+ },+ ...style,+ },+ },+ });++ const flushText = () => {+ if (textBuf.length > 0) {+ blocks.push(createBlock('Text', { markdown: true, text: textBuf.join('\n') }));++ textBuf = [];+ }+ };++ lines.forEach((line) => {+ // Handle ATX headings (# Heading)+ const heading = line.match(/^(#+)\s+(.*)/);+ if (heading) {+ flushText();++ blocks.push(createBlock('Heading', {+ text: heading[2],+ level: `h${Math.min(heading[1].length, 6)}`,+ }));+ return;+ }++ // Handle Setext headings (===== or -----)+ const trimmed = line.trim();+ if (/^(=+|-+)$/.test(trimmed) && textBuf.length > 0) {+ const lastLine = textBuf.pop();+ if (lastLine.trim()) {+ flushText();++ blocks.push(createBlock('Heading', {+ text: lastLine,+ level: trimmed[0] === '=' ? 'h1' : 'h2',+ }));++ return;+ }++ textBuf.push(lastLine, line);+ } else {+ textBuf.push(line);+ }+ });++ flushText();++ return {+ root: {+ type: 'EmailLayout',+ data: { childrenIds: blocks.map((b) => b.id) },+ },+ ...Object.fromEntries(blocks.map((b) => [b.id, { type: b.type, data: b.data }])),+ };+};++export default markdownToVisualBlock;