Skip to content

Commit a73e131

Browse files
committed
perf(Tinymce): 动态加载 tinymce
1 parent d1a5f21 commit a73e131

File tree

5 files changed

+251
-159
lines changed

5 files changed

+251
-159
lines changed

public/index.html

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,6 @@
1111
<noscript>
1212
<strong>很抱歉,如果没有启用JavaScript,此项目将无法正常运行。请启用它。</strong>
1313
</noscript>
14-
<!-- import cdn js -->
15-
<% for(var js of htmlWebpackPlugin.options.cdn.js) { %>
16-
<script src="<%=js%>"></script>
17-
<% } %>
1814
<div id="app"></div>
1915
<!-- built files will be auto injected -->
2016
</body>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* eslint-disable */
2+
/* tslint:disable */
3+
// @ts-ignore
4+
5+
let callbacks: any = [];
6+
7+
function loadedTinymce() {
8+
// to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2144
9+
// check is successfully downloaded script
10+
return window.tinymce;
11+
}
12+
13+
const dynamicLoadScript = (src, callback) => {
14+
const existingScript = document.getElementById(src);
15+
const cb = callback || function () {
16+
};
17+
18+
if (!existingScript) {
19+
const script = document.createElement('script');
20+
script.src = src; // src url for the third-party library being loaded.
21+
script.id = src;
22+
document.body.appendChild(script);
23+
callbacks.push(cb);
24+
const onEnd = 'onload' in script ? stdOnEnd : ieOnEnd;
25+
onEnd(script);
26+
}
27+
28+
if (existingScript && cb) {
29+
if (loadedTinymce()) {
30+
cb(null, existingScript);
31+
} else {
32+
callbacks.push(cb);
33+
}
34+
}
35+
36+
function stdOnEnd(script) {
37+
// tslint:disable-next-line:space-before-function-paren
38+
script.onload = function () {
39+
// this.onload = null here is necessary
40+
// because even IE9 works not like others
41+
this.onerror = this.onload = null;
42+
for (const cb of callbacks) {
43+
cb(null, script);
44+
}
45+
callbacks = null;
46+
};
47+
// tslint:disable-next-line:space-before-function-paren
48+
script.onerror = function () {
49+
this.onerror = this.onload = null;
50+
cb(new Error('Failed to load ' + src), script);
51+
};
52+
}
53+
54+
function ieOnEnd(script) {
55+
// tslint:disable-next-line:space-before-function-paren
56+
script.onreadystatechange = function () {
57+
if (this.readyState !== 'complete' && this.readyState !== 'loaded') return;
58+
this.onreadystatechange = null;
59+
for (const cb of callbacks) {
60+
cb(null, script); // there is no way to catch loading errors in IE8
61+
}
62+
callbacks = null;
63+
};
64+
}
65+
};
66+
67+
export default dynamicLoadScript;

0 commit comments

Comments
 (0)