Skip to content

Commit b4daa42

Browse files
Resolve issue with window.postMessage by using shallowRef, update EmailEditor Props and Functions, resolve PR comments
1 parent de42396 commit b4daa42

File tree

7 files changed

+77
-55
lines changed

7 files changed

+77
-55
lines changed

Diff for: .nvmrc

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
v18.12.0
1+
v18.20.3

Diff for: package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"build": "vue-cli-service build",
99
"lint": "vue-cli-service lint",
1010
"adjust-types": "sed -i '' '1s/^/\\/\\/\\/ <reference types=\\\"unlayer-types\\/embed.d.ts\\\" \\/>\\n/' ./dist/components/types.d.ts",
11-
"build-bundle": "vite build && cd ./src/components && vue-tsc --emitDeclarationOnly && npm run adjust-types",
11+
"build-types": "vue-tsc --emitDeclarationOnly && npm run adjust-types",
12+
"build-bundle": "vite build && cd ./src/components && npm run build-types",
1213
"release": "npm run build-bundle && npm publish"
1314
},
1415
"dependencies": {

Diff for: src/components/EmailEditor.vue

+60-33
Original file line numberDiff line numberDiff line change
@@ -7,52 +7,60 @@
77
</template>
88

99
<script lang="ts">
10-
import { defineComponent, PropType } from 'vue';
10+
import { defineComponent } from 'vue';
1111
import pkg from '../../package.json';
1212
import { loadScript } from './loadScript';
1313
import { EmailEditorProps } from './types';
14+
import { shallowRef } from 'vue';
1415
let lastEditorId = 0;
1516
1617
export default defineComponent({
1718
name: 'EmailEditor',
1819
props: {
19-
appearance: Object as PropType<EmailEditorProps['appearance']>,
20-
displayMode: String as PropType<EmailEditorProps['displayMode']>,
21-
editorId: String as PropType<EmailEditorProps['editorId']>,
22-
locale: String as PropType<EmailEditorProps['locale']>,
23-
projectId: Number as PropType<EmailEditorProps['projectId']>,
24-
tools: Object as PropType<EmailEditorProps['tools']>,
20+
editorId: String as () => EmailEditorProps['editorId'],
2521
minHeight: {
26-
type: String as PropType<EmailEditorProps['minHeight']>,
22+
type: String as () => EmailEditorProps['minHeight'],
2723
default: '500px',
28-
}
29-
},
30-
data() {
31-
return {
32-
editor: null as EmailEditorProps['editor'],
33-
options: Object as EmailEditorProps['options'],
34-
};
24+
},
25+
options: Object as () => EmailEditorProps['options'],
26+
27+
/**
28+
* @Deprecated Props: Use `options.appearance` instead
29+
*/
30+
appearance: Object as () => EmailEditorProps['appearance'],
31+
/**
32+
* @Deprecated Props: Use `options.locale` instead
33+
*/
34+
locale: String as () => EmailEditorProps['locale'],
35+
/**
36+
* @Deprecated Props: Use `options.projectId` instead
37+
*/
38+
projectId: Number as () => EmailEditorProps['projectId'],
39+
/**
40+
* @Deprecated Props: Use `options.tools` instead
41+
*/
42+
tools: Object as () => EmailEditorProps['tools'],
3543
},
3644
computed: {
3745
id(): string | undefined {
3846
return this.editorId || `editor-${++lastEditorId}`;
3947
},
4048
},
49+
setup() {
50+
// shallowRef is used to avoid window.postMessage error
51+
const editor = shallowRef(null as EmailEditorProps['editor']); // Creates a reactive reference
52+
53+
return {
54+
editor, // Makes editor available to the template
55+
};
56+
},
4157
mounted() {
4258
loadScript(this.loadEditor.bind(this));
4359
},
4460
methods: {
4561
loadEditor() {
4662
const options = this.options || {};
4763
48-
if (this.projectId) {
49-
options.projectId = this.projectId;
50-
}
51-
52-
if (this.tools) {
53-
options.tools = this.tools;
54-
}
55-
5664
if (this.appearance) {
5765
options.appearance = this.appearance;
5866
}
@@ -61,32 +69,51 @@ export default defineComponent({
6169
options.locale = this.locale;
6270
}
6371
64-
/* global unlayer */
72+
if (this.projectId) {
73+
options.projectId = this.projectId;
74+
}
75+
76+
if (this.tools) {
77+
options.tools = this.tools;
78+
}
79+
80+
// @ts-ignore
6581
this.editor = unlayer.createEditor({
6682
...options,
6783
id: this.id,
68-
displayMode: this.displayMode || 'email',
6984
source: {
7085
name: pkg.name,
7186
version: pkg.version,
7287
},
7388
});
7489
7590
this.$emit('load');
76-
91+
// @ts-ignore
7792
this.editor.addEventListener('editor:ready', () => {
7893
this.$emit('ready');
7994
});
8095
},
81-
loadDesign(design: any) {
82-
this.editor?.loadDesign(design);
83-
},
84-
saveDesign(callback: any) {
85-
this.editor?.saveDesign(callback);
86-
},
87-
exportHtml(callback: any) {
96+
/**
97+
* @deprecated This method will be removed in the next major release. Use `editor.exportHtml` instead.
98+
*/
99+
exportHtml(callback: Parameters<EmailEditorProps['exportHtml']>[0]) {
100+
// @ts-ignore
88101
this.editor?.exportHtml(callback);
89102
},
103+
/**
104+
* @deprecated This method will be removed in the next major release. Use `editor.loadDesign` instead.
105+
*/
106+
loadDesign(design: Parameters<EmailEditorProps['loadDesign']>[0]) {
107+
// @ts-ignore
108+
this.editor?.loadDesign(design);
109+
},
110+
/**
111+
* @deprecated This method will be removed in the next major release. Use `editor.saveDesign` instead.
112+
*/
113+
saveDesign(callback: Parameters<EmailEditorProps['saveDesign']>[0]) {
114+
// @ts-ignore
115+
this.editor?.saveDesign(callback)
116+
}
90117
},
91118
});
92119
</script>

Diff for: src/components/types.ts

+8-13
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// ignore next line
2-
/// <reference path="../../node_modules/unlayer-types/embed.d.ts" />
2+
/// <reference types="unlayer-types/embed.d.ts" />
33

44
import Embed from 'embed/index';
55
import { Editor as EditorClass } from 'embed/Editor';
@@ -13,11 +13,7 @@ export interface EmailEditorProps {
1313
editor: Editor | null;
1414
editorId?: string | undefined;
1515
minHeight?: number | string | undefined;
16-
onLoad?(unlayer: Editor): void;
17-
onReady?(unlayer: Editor): void;
1816
options?: Config;
19-
scriptUrl?: string | undefined;
20-
// style?: CSSProperties | undefined;
2117

2218
// redundant props -- already available in options
2319
/** @deprecated */
@@ -30,12 +26,11 @@ export interface EmailEditorProps {
3026
projectId?: number | undefined;
3127
/** @deprecated */
3228
tools?: ToolsConfig | undefined;
33-
}
3429

35-
declare global {
36-
const unlayer: Unlayer;
37-
38-
interface Window {
39-
__unlayer_lastEditorId: number;
40-
}
41-
}
30+
/** @deprecated */
31+
exportHtml: Editor['exportHtml'];
32+
/** @deprecated */
33+
loadDesign: Editor['loadDesign'];
34+
/** @deprecated */
35+
saveDesign: Editor['saveDesign'];
36+
}

Diff for: src/main.ts

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// @ts-ignore
12
import { createApp } from 'vue/dist/vue.esm-bundler.js';
23
import { createRouter, createWebHistory } from 'vue-router';
34

Diff for: src/views/Example.vue

+4-4
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
<script lang="ts">
1717
import { defineComponent } from 'vue';
18-
import { EmailEditor } from '../vue-email-editor'
18+
import { EmailEditor } from '../vue-email-editor';
1919
import sample from '../data/sample.json';
2020
2121
type EmailEditorInstance = InstanceType<typeof EmailEditor>;
@@ -46,14 +46,14 @@ export default defineComponent({
4646
},
4747
saveDesign() {
4848
this.emailEditor?.editor?.saveDesign(
49-
() => {
50-
console.log('saveDesign');
49+
(design: any) => {
50+
console.log('saveDesign', design);
5151
}
5252
)
5353
},
5454
exportHtml() {
5555
this?.emailEditor?.editor?.exportHtml(
56-
(data) => {
56+
(data: any) => {
5757
console.log('exportHtml', data);
5858
}
5959
)

Diff for: tsconfig.json

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,9 @@
1616
"skipLibCheck": true,
1717
},
1818
"include": [
19+
"src/*",
1920
"src/components/*",
2021
"src/vue-email-editor.ts",
2122
"node_modules/unlayer-types/embed.d.ts"
2223
],
23-
"exclude": [
24-
"src/views/*"
25-
]
2624
}

0 commit comments

Comments
 (0)