Skip to content

Commit e236f61

Browse files
committed
Improved script loader.
1 parent 09622b7 commit e236f61

File tree

3 files changed

+80
-59
lines changed

3 files changed

+80
-59
lines changed

prettier.config.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
singleQuote: true,
3+
arrowParens: 'always',
4+
trailingComma: 'es5',
5+
};

src/index.js

Lines changed: 37 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,86 @@
1-
import React, {Component} from 'react'
1+
import React, { Component } from 'react';
2+
import { loadScript } from './loadScript';
23

34
export default class extends Component {
45
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);
257
}
268

279
render() {
2810
let {
29-
props: {
30-
minHeight = 500,
31-
style = {}
32-
}
33-
} = this
11+
props: { minHeight = 500, style = {} },
12+
} = this;
3413

3514
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 }} />
4523
</div>
46-
)
24+
);
4725
}
4826

4927
loadEditor = () => {
50-
const options = (this.props.options || {})
28+
const options = this.props.options || {};
5129

5230
if (this.props.projectId) {
53-
options.projectId = this.props.projectId
31+
options.projectId = this.props.projectId;
5432
}
5533

5634
if (this.props.tools) {
57-
options.tools = this.props.tools
35+
options.tools = this.props.tools;
5836
}
59-
37+
6038
if (this.props.appearance) {
61-
options.appearance = this.props.appearance
39+
options.appearance = this.props.appearance;
6240
}
6341

6442
if (this.props.locale) {
65-
options.locale = this.props.locale
43+
options.locale = this.props.locale;
6644
}
6745

6846
this.editor = unlayer.createEditor({
6947
...options,
7048
id: 'editor',
7149
displayMode: 'email',
72-
})
50+
});
7351

7452
// All properties starting with on[Name] are registered as event listeners.
7553
for (const [key, value] of Object.entries(this.props)) {
7654
if (/^on/.test(key) && key != 'onLoad') {
77-
this.addEventListener(key, value)
55+
this.addEventListener(key, value);
7856
}
7957
}
8058

81-
const { onLoad } = this.props
82-
onLoad && onLoad()
83-
}
59+
const { onLoad } = this.props;
60+
onLoad && onLoad();
61+
};
8462

8563
registerCallback = (type, callback) => {
86-
this.editor.registerCallback(type, callback)
87-
}
64+
this.editor.registerCallback(type, callback);
65+
};
8866

8967
addEventListener = (type, callback) => {
90-
this.editor.addEventListener(type, callback)
91-
}
68+
this.editor.addEventListener(type, callback);
69+
};
9270

9371
loadDesign = (design) => {
94-
this.editor.loadDesign(design)
95-
}
72+
this.editor.loadDesign(design);
73+
};
9674

9775
saveDesign = (callback) => {
98-
this.editor.saveDesign(callback)
99-
}
76+
this.editor.saveDesign(callback);
77+
};
10078

10179
exportHtml = (callback) => {
102-
this.editor.exportHtml(callback)
103-
}
80+
this.editor.exportHtml(callback);
81+
};
10482

10583
setMergeTags = (mergeTags) => {
106-
this.editor.setMergeTags(mergeTags)
107-
}
84+
this.editor.setMergeTags(mergeTags);
85+
};
10886
}

src/loadScript.js

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const callbacks = [];
2+
3+
const runCallbacks = () => {
4+
let callback;
5+
6+
while ((callback = callbacks.shift())) {
7+
callback();
8+
}
9+
};
10+
11+
const registerCallback = (callback) => {
12+
callbacks.push(callback);
13+
};
14+
15+
export const loadScript = (callback) => {
16+
const embedJs = '//editor.unlayer.com/embed.js?2';
17+
const scripts = document.querySelectorAll('script');
18+
let scriptLoaded = false;
19+
20+
scripts.forEach((script) => {
21+
if (script.src.includes(embedJs)) {
22+
scriptLoaded = true;
23+
}
24+
});
25+
26+
registerCallback(callback);
27+
28+
if (!scriptLoaded) {
29+
const unlayerScript = document.createElement('script');
30+
unlayerScript.setAttribute('src', embedJs);
31+
unlayerScript.onload = () => {
32+
runCallbacks();
33+
};
34+
document.head.appendChild(unlayerScript);
35+
} else {
36+
runCallbacks();
37+
}
38+
};

0 commit comments

Comments
 (0)