Skip to content
This repository was archived by the owner on Nov 8, 2022. It is now read-only.

Commit dd1b2d4

Browse files
committed
refactor(clean up): mv oss script from document to DocUploader
1 parent 2c4fb92 commit dd1b2d4

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

containers/DocUploader/index.js

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,21 +43,27 @@ class DocUploaderContainer extends React.Component {
4343

4444
state = {
4545
ossClient: null,
46+
ossScriptAnchor: null,
47+
ossScriptLoaded: false,
4648
// use unique id to init the file input, otherwise it will be the some instance
4749
uniqueId: uid.gen(),
4850
initTimestamp: Date.now() / 1000,
4951
}
5052

5153
componentDidMount() {
54+
this.loadScriptAndInitOSS()
55+
5256
const { docUploader, pasteImage } = this.props
5357
init(docUploader)
5458

55-
this.initOssClient()
59+
// if (ossScriptLoaded) this.initOssClient()
5660
if (pasteImage) this.initPasteWatcher()
5761
}
5862

5963
componentWillUnmount() {
6064
log('componentWillUnmount')
65+
const { ossScriptLoaded, ossScriptAnchor } = this.state
66+
6167
/* eslint-disable */
6268
delete this.state.ossClient
6369
/* eslint-enable */
@@ -66,6 +72,31 @@ class DocUploaderContainer extends React.Component {
6672
Global.removeEventListener('paste', this.handlePaste.bind(this), true)
6773
}
6874
uninit()
75+
76+
if (ossScriptLoaded) {
77+
ossScriptAnchor.removeEventListener('load', () => {
78+
this.setState({ ossScriptLoaded: false })
79+
})
80+
}
81+
}
82+
83+
loadScriptAndInitOSS() {
84+
let { ossScriptAnchor } = this.state
85+
86+
ossScriptAnchor = document.createElement('script')
87+
ossScriptAnchor.src =
88+
'https://gosspublic.alicdn.com/aliyun-oss-sdk-5.2.0.min.js'
89+
ossScriptAnchor.async = true
90+
document.getElementsByTagName('head')[0].appendChild(ossScriptAnchor)
91+
92+
ossScriptAnchor.addEventListener('load', () => {
93+
log('load done')
94+
this.initOssClient()
95+
this.setState({ ossScriptLoaded: true, ossScriptAnchor })
96+
})
97+
ossScriptAnchor.addEventListener('error', () => {
98+
this.setState({ ossScriptLoaded: false, ossScriptAnchor: null })
99+
})
69100
}
70101

71102
initPasteWatcher() {
@@ -116,6 +147,8 @@ class DocUploaderContainer extends React.Component {
116147
}
117148

118149
doUploadFile(file) {
150+
const { ossScriptLoaded } = this.state
151+
if (!ossScriptLoaded) return alert('脚本未加载...')
119152
if (!file || !R.startsWith('image/', file.type)) return false
120153

121154
const fileSize = file.size / 1024 / 1024

pages/_document.js

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,12 +86,6 @@ export default class DocumentPage extends Document {
8686
href="https://cdn.staticfile.org/izitoast/1.4.0/css/iziToast.css"
8787
rel="stylesheet"
8888
/>
89-
{/* the ali-oss-sdk es6 import support sucks */}
90-
{/* import from cdn is fine, it's not my money anyway */}
91-
<script
92-
async
93-
src="https://gosspublic.alicdn.com/aliyun-oss-sdk-5.2.0.min.js"
94-
/>
9589
<noscript>
9690
You need to enable JavaScript to get this website runing.
9791
</noscript>

0 commit comments

Comments
 (0)