fix[Tinymce]: fixed bug when init multiple tinymces at the same time (#2152)

This commit is contained in:
花裤衩 2019-05-27 15:35:07 +08:00 committed by Pan
parent 786d9662fb
commit f1bc06b082
1 changed files with 27 additions and 7 deletions

View File

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