|
1 |
| -import React, {Component} from 'react' |
| 1 | +import React, { Component } from 'react'; |
| 2 | +import { loadScript } from './loadScript'; |
2 | 3 |
|
3 | 4 | export default class extends Component {
|
4 | 5 | componentDidMount() {
|
5 |
| - const embedJs = "//editor.unlayer.com/embed.js?2" |
6 |
| - const scripts = document.querySelectorAll('script'); |
7 |
| - let scriptLoaded = false |
8 |
| - |
9 |
| - scripts.forEach(script => { |
10 |
| - if (script.src.includes(embedJs)) { |
11 |
| - scriptLoaded = true |
12 |
| - } |
13 |
| - }) |
14 |
| - |
15 |
| - if (!scriptLoaded) { |
16 |
| - const unlayerScript = document.createElement('script'); |
17 |
| - unlayerScript.setAttribute('src', embedJs); |
18 |
| - unlayerScript.onload = () => { |
19 |
| - this.loadEditor(); |
20 |
| - }; |
21 |
| - document.head.appendChild(unlayerScript); |
22 |
| - } else { |
23 |
| - this.loadEditor(); |
24 |
| - } |
| 6 | + loadScript(this.loadEditor); |
25 | 7 | }
|
26 | 8 |
|
27 | 9 | render() {
|
28 | 10 | let {
|
29 |
| - props: { |
30 |
| - minHeight = 500, |
31 |
| - style = {} |
32 |
| - } |
33 |
| - } = this |
| 11 | + props: { minHeight = 500, style = {} }, |
| 12 | + } = this; |
34 | 13 |
|
35 | 14 | return (
|
36 |
| - <div style={{ |
37 |
| - flex: 1, |
38 |
| - display: 'flex', |
39 |
| - minHeight: minHeight |
40 |
| - }}> |
41 |
| - <div |
42 |
| - id="editor" |
43 |
| - style={{...style, flex: 1}} |
44 |
| - /> |
| 15 | + <div |
| 16 | + style={{ |
| 17 | + flex: 1, |
| 18 | + display: 'flex', |
| 19 | + minHeight: minHeight, |
| 20 | + }} |
| 21 | + > |
| 22 | + <div id="editor" style={{ ...style, flex: 1 }} /> |
45 | 23 | </div>
|
46 |
| - ) |
| 24 | + ); |
47 | 25 | }
|
48 | 26 |
|
49 | 27 | loadEditor = () => {
|
50 |
| - const options = (this.props.options || {}) |
| 28 | + const options = this.props.options || {}; |
51 | 29 |
|
52 | 30 | if (this.props.projectId) {
|
53 |
| - options.projectId = this.props.projectId |
| 31 | + options.projectId = this.props.projectId; |
54 | 32 | }
|
55 | 33 |
|
56 | 34 | if (this.props.tools) {
|
57 |
| - options.tools = this.props.tools |
| 35 | + options.tools = this.props.tools; |
58 | 36 | }
|
59 |
| - |
| 37 | + |
60 | 38 | if (this.props.appearance) {
|
61 |
| - options.appearance = this.props.appearance |
| 39 | + options.appearance = this.props.appearance; |
62 | 40 | }
|
63 | 41 |
|
64 | 42 | if (this.props.locale) {
|
65 |
| - options.locale = this.props.locale |
| 43 | + options.locale = this.props.locale; |
66 | 44 | }
|
67 | 45 |
|
68 | 46 | this.editor = unlayer.createEditor({
|
69 | 47 | ...options,
|
70 | 48 | id: 'editor',
|
71 | 49 | displayMode: 'email',
|
72 |
| - }) |
| 50 | + }); |
73 | 51 |
|
74 | 52 | // All properties starting with on[Name] are registered as event listeners.
|
75 | 53 | for (const [key, value] of Object.entries(this.props)) {
|
76 | 54 | if (/^on/.test(key) && key != 'onLoad') {
|
77 |
| - this.addEventListener(key, value) |
| 55 | + this.addEventListener(key, value); |
78 | 56 | }
|
79 | 57 | }
|
80 | 58 |
|
81 |
| - const { onLoad } = this.props |
82 |
| - onLoad && onLoad() |
83 |
| - } |
| 59 | + const { onLoad } = this.props; |
| 60 | + onLoad && onLoad(); |
| 61 | + }; |
84 | 62 |
|
85 | 63 | registerCallback = (type, callback) => {
|
86 |
| - this.editor.registerCallback(type, callback) |
87 |
| - } |
| 64 | + this.editor.registerCallback(type, callback); |
| 65 | + }; |
88 | 66 |
|
89 | 67 | addEventListener = (type, callback) => {
|
90 |
| - this.editor.addEventListener(type, callback) |
91 |
| - } |
| 68 | + this.editor.addEventListener(type, callback); |
| 69 | + }; |
92 | 70 |
|
93 | 71 | loadDesign = (design) => {
|
94 |
| - this.editor.loadDesign(design) |
95 |
| - } |
| 72 | + this.editor.loadDesign(design); |
| 73 | + }; |
96 | 74 |
|
97 | 75 | saveDesign = (callback) => {
|
98 |
| - this.editor.saveDesign(callback) |
99 |
| - } |
| 76 | + this.editor.saveDesign(callback); |
| 77 | + }; |
100 | 78 |
|
101 | 79 | exportHtml = (callback) => {
|
102 |
| - this.editor.exportHtml(callback) |
103 |
| - } |
| 80 | + this.editor.exportHtml(callback); |
| 81 | + }; |
104 | 82 |
|
105 | 83 | setMergeTags = (mergeTags) => {
|
106 |
| - this.editor.setMergeTags(mergeTags) |
107 |
| - } |
| 84 | + this.editor.setMergeTags(mergeTags); |
| 85 | + }; |
108 | 86 | }
|
0 commit comments