fix[Tinymce]: fixed bug when init multiple tinymce at the same time

This commit is contained in:
Pan 2019-05-26 17:08:28 +08:00
parent ab1922ef14
commit 0d4f3c79b8
1 changed files with 28 additions and 6 deletions

View File

@ -1,7 +1,19 @@
let callbacks = []
function loadedTinymce() {
// check is successfully downloaded script
return window.tinymce
}
const dynamicLoadScript = (src, callback) => {
const existingScript = document.getElementById(src)
const cb = callback || function() {}
// to fixed https://github.com/PanJiaChen/vue-element-admin/issues/2144
if (!loadedTinymce()) {
callbacks.push(cb)
}
if (!existingScript) {
const script = document.createElement('script')
script.src = src // src url for the third-party library being loaded.
@ -9,17 +21,24 @@ const dynamicLoadScript = (src, callback) => {
document.body.appendChild(script)
const onEnd = 'onload' in script ? stdOnEnd : ieOnEnd
onEnd(script, cb)
onEnd(script)
}
if (existingScript && cb) cb(null, existingScript)
if (existingScript && cb) {
if (loadedTinymce()) {
cb(null, existingScript)
}
}
function stdOnEnd(script, cb) {
function stdOnEnd(script) {
script.onload = function() {
// this.onload = null here is necessary
// because even IE9 works not like others
this.onerror = this.onload = null
cb(null, script)
for (const cb of callbacks) {
cb(null, script)
}
callbacks = null
}
script.onerror = function() {
this.onerror = this.onload = null
@ -27,11 +46,14 @@ const dynamicLoadScript = (src, callback) => {
}
}
function ieOnEnd(script, cb) {
function ieOnEnd(script) {
script.onreadystatechange = function() {
if (this.readyState !== 'complete' && this.readyState !== 'loaded') return
this.onreadystatechange = null
cb(null, script) // there is no way to catch loading errors in IE8
for (const cb of callbacks) {
cb(null, script) // there is no way to catch loading errors in IE8
}
callbacks = null
}
}
}