Skip to content

Commit 6770963

Browse files
authored
perf[Tinymce]: dynamic import tinymce(PanJiaChen#2102)
1 parent 131b9b9 commit 6770963

File tree

4 files changed

+57
-16
lines changed

4 files changed

+57
-16
lines changed

public/index.html

-4
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99
<title><%= webpackConfig.name %></title>
1010
</head>
1111
<body>
12-
<!-- import cdn js -->
13-
<% for(var js of htmlWebpackPlugin.options.cdn.js) { %>
14-
<script src="<%=js%>"></script>
15-
<% } %>
1612
<div id="app"></div>
1713
<!-- built files will be auto injected -->
1814
</body>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
const dynamicLoadScript = (src, callback) => {
2+
const existingScript = document.getElementById(src)
3+
const cb = callback || function() {}
4+
5+
if (!existingScript) {
6+
const script = document.createElement('script')
7+
script.src = src // src url for the third-party library being loaded.
8+
script.id = src
9+
document.body.appendChild(script)
10+
11+
const onEnd = 'onload' in script ? stdOnEnd : ieOnEnd
12+
onEnd(script, cb)
13+
}
14+
15+
if (existingScript && cb) cb(null, existingScript)
16+
17+
function stdOnEnd(script, cb) {
18+
script.onload = function() {
19+
// this.onload = null here is necessary
20+
// because even IE9 works not like others
21+
this.onerror = this.onload = null
22+
cb(null, script)
23+
}
24+
script.onerror = function() {
25+
this.onerror = this.onload = null
26+
cb(new Error('Failed to load ' + src), script)
27+
}
28+
}
29+
30+
function ieOnEnd(script, cb) {
31+
script.onreadystatechange = function() {
32+
if (this.readyState !== 'complete' && this.readyState !== 'loaded') return
33+
this.onreadystatechange = null
34+
cb(null, script) // there is no way to catch loading errors in IE8
35+
}
36+
}
37+
}
38+
39+
export default dynamicLoadScript

src/components/Tinymce/index.vue

+18-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@
1515
import editorImage from './components/EditorImage'
1616
import plugins from './plugins'
1717
import toolbar from './toolbar'
18+
import load from './dynamicLoadScript'
19+
20+
// why use this cdn, detail see https://github.com/PanJiaChen/tinymce-all-in-one
21+
const tinymceCDN = 'https://cdn.jsdelivr.net/npm/[email protected]/tinymce.min.js'
1822
1923
export default {
2024
name: 'Tinymce',
@@ -82,10 +86,12 @@ export default {
8286
}
8387
},
8488
mounted() {
85-
this.initTinymce()
89+
this.init()
8690
},
8791
activated() {
88-
this.initTinymce()
92+
if (window.tinymce) {
93+
this.initTinymce()
94+
}
8995
},
9096
deactivated() {
9197
this.destroyTinymce()
@@ -94,6 +100,16 @@ export default {
94100
this.destroyTinymce()
95101
},
96102
methods: {
103+
init() {
104+
// dynamic load tinymce from cdn
105+
load(tinymceCDN, (err) => {
106+
if (err) {
107+
this.$message.error(err.message)
108+
return
109+
}
110+
this.initTinymce()
111+
})
112+
},
97113
initTinymce() {
98114
const _this = this
99115
window.tinymce.init({

vue.config.js

-10
Original file line numberDiff line numberDiff line change
@@ -57,16 +57,6 @@ module.exports = {
5757
}
5858
},
5959
chainWebpack(config) {
60-
const cdn = {
61-
// inject tinymce into index.html
62-
// why use this cdn, detail see https://github.com/PanJiaChen/tinymce-all-in-one
63-
js: ['https://cdn.jsdelivr.net/npm/[email protected]/tinymce.min.js']
64-
}
65-
config.plugin('html')
66-
.tap(args => {
67-
args[0].cdn = cdn
68-
return args
69-
})
7060
config.plugins.delete('preload') // TODO: need test
7161
config.plugins.delete('prefetch') // TODO: need test
7262

0 commit comments

Comments
 (0)