7
7
</template >
8
8
9
9
<script lang="ts">
10
- import { defineComponent , toRaw } from ' vue' ;
10
+ import { defineComponent , shallowRef , toRaw } from ' vue' ;
11
11
import pkg from ' ../../package.json' ;
12
12
import { loadScript } from ' ./loadScript' ;
13
13
import { EmailEditorProps } from ' ./types' ;
14
- import { ref } from ' vue' ;
15
14
let lastEditorId = 0 ;
16
15
17
16
export default defineComponent ({
@@ -51,11 +50,27 @@ export default defineComponent({
51
50
},
52
51
},
53
52
setup() {
54
- // shallowRef is used to avoid window.postMessage error
55
- const editor = ref <EmailEditorProps [' editor' ] | null >(null ); // Creates a reactive reference
53
+ const editor = shallowRef <EmailEditorProps [' editor' ] | null >(null );
54
+
55
+ const createSerializedEditor = (originalEditor : any ) => {
56
+ if (! originalEditor ) return null ;
57
+
58
+ return {
59
+ ... originalEditor ,
60
+ registerCallback : (event : string , callback : Function ) => {
61
+ originalEditor .registerCallback (event , (data : any , done : Function ) => {
62
+ callback (data , (result : any ) => {
63
+ const rawResult = toRaw (result );
64
+ done (rawResult );
65
+ });
66
+ });
67
+ }
68
+ };
69
+ };
56
70
57
71
return {
58
- editor , // Makes editor available to the template
72
+ editor ,
73
+ createSerializedEditor ,
59
74
};
60
75
},
61
76
mounted() {
@@ -64,11 +79,9 @@ export default defineComponent({
64
79
methods: {
65
80
loadEditor() {
66
81
const options = toRaw (this .options ) || {};
67
- const appearance = toRaw (this .appearance ) || null ;
68
- const tools = toRaw (this .tools ) || null ;
69
82
70
- if (appearance ) {
71
- options .appearance = appearance ;
83
+ if (this . appearance ) {
84
+ options .appearance = toRaw ( this . appearance ) ;
72
85
}
73
86
74
87
if (this .locale ) {
@@ -79,11 +92,11 @@ export default defineComponent({
79
92
options .projectId = this .projectId ;
80
93
}
81
94
82
- if (tools ) {
83
- options .tools = tools ;
95
+ if (this . tools ) {
96
+ options .tools = toRaw ( this . tools ) ;
84
97
}
85
98
86
- this . editor = unlayer .createEditor ({
99
+ const rawEditor = unlayer .createEditor ({
87
100
... options ,
88
101
id: this .id ,
89
102
source: {
@@ -92,8 +105,11 @@ export default defineComponent({
92
105
},
93
106
});
94
107
108
+ // Wrap the editor with serialization handling
109
+ this .editor = this .createSerializedEditor (rawEditor );
110
+
95
111
this .$emit (' load' );
96
- this .editor .addEventListener (' editor:ready' , () => {
112
+ this .editor ? .addEventListener (' editor:ready' , () => {
97
113
this .$emit (' ready' );
98
114
});
99
115
},
0 commit comments