import Button from './Button' import Input from './Input' import Label from './Label' import React from 'react' import Select from './Select' import Tag from './Tag' import Upload from './Upload' const style = { paramsConfig: { paddingBottom: '10px', borderBottom: '1px solid rgb(217, 217, 217)', display: 'flex', flexWrap: 'wrap', }, insertTitle: { fontSize: '14px', paddingRight: '10px', color: 'rgba(0, 0, 0, 0.65)', }, sourceList: { margin: '10px 10px 10px 0', border: '1px dashed rgb(217, 217, 217)', borderRadius: '4px', }, configTitle: { display: 'block', fontSize: '14px', margin: '10px 0', paddingRight: '10px', color: 'rgba(0, 0, 0, 0.65)', }, warnInfo: { display: 'inline-block', width: '100%', margin: '5px', textAlign: 'center', fontSize: '12px', color: '#f04134', }, } const linkRegx = /^https?:\/\/(([a-zA-Z0-9_-])+(\.)?)*(:\d+)?(\/((\.)?(\?)?=?&?[a-zA-Z0-9,_-](\?)?)*)*$/i let timeoutInstance = null class UploadModal extends React.PureComponent { state = { sources: [], currentSource: '', width: 400, height: 400, controls: 'true', autoplay: 'false', muted: 'false', loop: 'false', poster: '', name: '', author: '', errorMsg: '', errorMsgVisible: false, } updateCurrentSource = e => { this.setState({currentSource: e.target.value}) } addSource = () => { let {sources, currentSource} = this.state let newsources = sources.concat([currentSource]) if (currentSource === '') { this.showErrorMsg('链接不能为空') } else if (!linkRegx.test(currentSource)) { this.showErrorMsg('非法的链接') } else if (sources.indexOf(currentSource) !== -1) { this.showErrorMsg('链接已存在') } else { this.setState({ sources: newsources, currentSource: '', }, () => { this.props.onChange && this.props.onChange(this.generateHtml()) }) } } removeSource = index => { let sourcesCopy = this.state.sources.concat([]) sourcesCopy.splice(index, 1) this.setState({sources: sourcesCopy}) } upload = e => { let {upload} = this.props if (!upload) return upload(e).then(url => { this.setState({currentSource: url}) }).catch(e => { e.constructor === Error ? this.showErrorMsg(e.message) : this.showErrorMsg(e) }) } showErrorMsg = msg => { this.setState({errorMsg: msg, errorMsgVisible: true}) clearTimeout(timeoutInstance) timeoutInstance = setTimeout(() => { this.setState({errorMsg: '', errorMsgVisible: false}) }, 4000) } getFileType = (fileUrl, mediaType) => { let type = fileUrl.match(/\.(\w+)$/, 'i') return type ? type[1].toLowerCase() : 'mp3' } generateHtml = () => { let {sources, controls, autoplay, loop, poster, name, author} = this.state let dataExtra = JSON.stringify({'poster': poster, 'name': name, 'author': author}) let len = sources.length if (len > 0) { let html = '' let attr = '' attr += controls === 'false' ? '' : ' controls="true" ' attr += autoplay === 'false' ? '' : ' autoplay="true" ' attr += loop === 'false' ? '' : ' loop="true" ' if (len === 1) { html = `` } else { html = `' } return html + '

' } } closeModal = () => { this.props.closeModal() } changeConfig = (e, type) => { let value = e.target.value let boolType = ['controls', 'autoplay', 'muted', 'loop'] if (type === 'width' || type === 'height') { if (isNaN(parseInt(value))) { value = parseInt(value) } } else if (boolType.indexOf(type) !== -1) { value = !!value } this.setState({[type]: value}, () => { this.props.onChange && this.props.onChange(this.generateHtml()) }) } renderSourceList = () => { let {sources} = this.state if (sources.length > 0) { let list = sources.map((source, index) => { return }) return list } else { return 至少添加一个链接 } } renderAudioConfig = () => { let {controls, autoplay, loop, poster, name, author} = this.state return (
) } render() { let {currentSource, errorMsg, errorMsgVisible} = this.state let {progress} = this.props return (
插入链接
{progress}% {errorMsg}
{this.renderSourceList()}
参数配置 {this.renderAudioConfig()}
{ }
) } } export default UploadModal