Skip to content

High performance, self-hosted, newsletter and mailing list manager with a modern dashboard. Single binary app.

Fix visual editor change event/init/import sequences.

Kailash Nadh5207dff733b1
diff --git a/frontend/src/components/CampaignPreview.vue b/frontend/src/components/CampaignPreview.vue
index 7426420b..632d1d6b 100644
--- a/frontend/src/components/CampaignPreview.vue
+++ b/frontend/src/components/CampaignPreview.vue
@@ -10,9 +10,9 @@
<section expanded class="modal-card-body preview">
<b-loading :active="isLoading" :is-full-page="false" />
<form v-if="isPost" method="post" :action="previewURL" target="iframe" ref="form">
- <input type="hidden" name="template_id" :value="templateId" />
- <input type="hidden" name="content_type" :value="contentType" />
- <input type="hidden" name="template_type" :value="templateType" />
+ <input v-if="templateId" type="hidden" name="template_id" :value="templateId" />
+ <input v-if="contentType" type="hidden" name="content_type" :value="contentType" />
+ <input v-if="templateType" type="hidden" name="template_type" :value="templateType" />
<input type="hidden" name="body" :value="body" />
</form>
diff --git a/frontend/src/components/Editor.vue b/frontend/src/components/Editor.vue
index dc5d24a5..0fb46fa7 100644
--- a/frontend/src/components/Editor.vue
+++ b/frontend/src/components/Editor.vue
@@ -78,7 +78,7 @@
<!-- visual editor //-->
<visual-editor v-if="self.contentType === 'visual'" :source="self.bodySource" @change="onVisualEditorChange"
- height="65vh" />
+ height="65vh" ref="visualEditor" />
<!-- raw html editor //-->
<html-editor v-if="self.contentType === 'html'" v-model="self.body" />
@@ -324,6 +324,8 @@ export default {
this.self.body = data.body;
this.self.bodySource = data.bodySource;
this.isVisualTplDisabled = true;
+
+ this.$refs.visualEditor.render(JSON.parse(data.bodySource));
});
},
);
diff --git a/frontend/src/components/VisualEditor.vue b/frontend/src/components/VisualEditor.vue
index 51bf0bd7..0dd019c9 100644
--- a/frontend/src/components/VisualEditor.vue
+++ b/frontend/src/components/VisualEditor.vue
@@ -34,21 +34,6 @@ export default {
},
methods: {
- initEditor() {
- let source = null;
- if (this.$props.source) {
- source = JSON.parse(this.$props.source);
- }
-
- const iframe = this.$refs.visualEditor;
- iframe.contentWindow.EmailBuilder.render('visual-editor-container', {
- data: source,
- onChange: (data, body) => {
- this.$emit('change', { source: JSON.stringify(data), body });
- },
- });
- },
-
loadScript() {
return new Promise((resolve, reject) => {
const iframe = this.$refs.visualEditor;
@@ -70,6 +55,45 @@ export default {
});
},
+ render(source) {
+ const iframe = this.$refs.visualEditor;
+
+ // If the editor is not-rendered, render it the first time. This can happen
+ // on first loads and importing an email template via render().
+ const em = iframe.contentWindow.EmailBuilder;
+ if (!em || !em.isRendered('visual-editor-container')) {
+ iframe.contentWindow.EmailBuilder.render('visual-editor-container', {
+ data: {},
+ onChange: (data, body) => {
+ this.$emit('change', { source: JSON.stringify(data), body });
+ },
+ });
+ } else {
+ // setDocument appends the content to the container. Clear it first.
+ // em.resetDocument();
+ }
+
+ // setDocument() will trigger onChange() that produces both bodySource and body (HTML).
+ // On init, the `data: source` above sets the content in the editor, but doesn't trigger
+ // onChange(), which is required to set the source+HTML state in the parent for preview to work.
+ // Couldn't figure out if there was an on load/on init event etc. in email-builder, so brute force it
+ // with a timer.
+ let n = 10;
+ const timer = window.setInterval(() => {
+ const container = iframe.contentWindow.document.getElementById('visual-editor-container');
+ if (container && container.hasChildNodes()) {
+ em.resetDocument(source);
+ window.clearInterval(timer);
+ return;
+ }
+
+ n += 1;
+ if (n > 10) {
+ window.clearInterval(timer);
+ }
+ }, 100);
+ },
+
// Inject media URL into the image URL input field in the visual edior sidebar.
onMediaSelect(media) {
const iframe = this.$refs.visualEditor;
@@ -122,7 +146,12 @@ export default {
iframe.onload = () => {
this.loadScript().then(() => {
- this.initEditor();
+ let source = null;
+ if (this.$props.source) {
+ source = JSON.parse(this.$props.source);
+ }
+
+ this.render(source);
}).catch((error) => {
/* eslint-disable-next-line no-console */
console.error('Failed to load email-builer script:', error);
@@ -135,21 +164,6 @@ export default {
unmounted() {
window.removeEventListener('message', this.onSidebarMount, false);
},
-
- watch: {
- source(val) {
- const iframe = this.$refs.visualEditor;
- if (iframe.contentWindow.EmailBuilder?.isRendered('visual-editor-container')) {
- if (val) {
- iframe.contentWindow.EmailBuilder.setDocument(JSON.parse(val));
- } else {
- iframe.contentWindow.EmailBuilder.setDocument(iframe.contentWindow.EmailBuilder.DEFAULT_SOURCE);
- }
- } else {
- this.initEditor();
- }
- },
- },
};
</script>