Skip to content

Commit f891202

Browse files
authored
fix[Tinymce]: fixed bug when init multiple tinymces at the same time (#2152)
1 parent e28701c commit f891202

File tree

1 file changed

+27
-7
lines changed

1 file changed

+27
-7
lines changed

src/components/Tinymce/dynamicLoadScript.js

+27-7
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
let callbacks = []
2+
3+
function loadedTinymce() {
4+
// to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2144
5+
// check is successfully downloaded script
6+
return window.tinymce
7+
}
8+
19
const dynamicLoadScript = (src, callback) => {
210
const existingScript = document.getElementById(src)
311
const cb = callback || function() {}
@@ -7,31 +15,43 @@ const dynamicLoadScript = (src, callback) => {
715
script.src = src // src url for the third-party library being loaded.
816
script.id = src
917
document.body.appendChild(script)
10-
18+
callbacks.push(cb)
1119
const onEnd = 'onload' in script ? stdOnEnd : ieOnEnd
12-
onEnd(script, cb)
20+
onEnd(script)
1321
}
1422

15-
if (existingScript && cb) cb(null, existingScript)
23+
if (existingScript && cb) {
24+
if (loadedTinymce()) {
25+
cb(null, existingScript)
26+
} else {
27+
callbacks.push(cb)
28+
}
29+
}
1630

17-
function stdOnEnd(script, cb) {
31+
function stdOnEnd(script) {
1832
script.onload = function() {
1933
// this.onload = null here is necessary
2034
// because even IE9 works not like others
2135
this.onerror = this.onload = null
22-
cb(null, script)
36+
for (const cb of callbacks) {
37+
cb(null, script)
38+
}
39+
callbacks = null
2340
}
2441
script.onerror = function() {
2542
this.onerror = this.onload = null
2643
cb(new Error('Failed to load ' + src), script)
2744
}
2845
}
2946

30-
function ieOnEnd(script, cb) {
47+
function ieOnEnd(script) {
3148
script.onreadystatechange = function() {
3249
if (this.readyState !== 'complete' && this.readyState !== 'loaded') return
3350
this.onreadystatechange = null
34-
cb(null, script) // there is no way to catch loading errors in IE8
51+
for (const cb of callbacks) {
52+
cb(null, script) // there is no way to catch loading errors in IE8
53+
}
54+
callbacks = null
3555
}
3656
}
3757
}

0 commit comments

Comments
 (0)