-
-
Notifications
You must be signed in to change notification settings - Fork 30.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf[Tinymce]: dynamic import tinymce(#2102)
- Loading branch information
1 parent
131b9b9
commit 6770963
Showing
4 changed files
with
57 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
const dynamicLoadScript = (src, callback) => { | ||
const existingScript = document.getElementById(src) | ||
const cb = callback || function() {} | ||
|
||
if (!existingScript) { | ||
const script = document.createElement('script') | ||
script.src = src // src url for the third-party library being loaded. | ||
script.id = src | ||
document.body.appendChild(script) | ||
|
||
const onEnd = 'onload' in script ? stdOnEnd : ieOnEnd | ||
onEnd(script, cb) | ||
} | ||
|
||
if (existingScript && cb) cb(null, existingScript) | ||
|
||
function stdOnEnd(script, cb) { | ||
script.onload = function() { | ||
// this.onload = null here is necessary | ||
// because even IE9 works not like others | ||
this.onerror = this.onload = null | ||
cb(null, script) | ||
} | ||
script.onerror = function() { | ||
this.onerror = this.onload = null | ||
cb(new Error('Failed to load ' + src), script) | ||
} | ||
} | ||
|
||
function ieOnEnd(script, cb) { | ||
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 | ||
} | ||
} | ||
} | ||
|
||
export default dynamicLoadScript |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters