yapi/client/containers/AddInterface/ResParams/ResParams.js

121 lines
3.2 KiB
JavaScript
Raw Normal View History

2017-07-18 12:53:53 +08:00
import React, { Component } from 'react'
2017-07-19 19:29:18 +08:00
import PropTypes from 'prop-types'
import { connect } from 'react-redux'
2017-07-27 20:35:19 +08:00
// import wangEditor from 'wangeditor'
2017-07-27 20:06:49 +08:00
import { Card } from 'antd'
2017-07-20 14:48:29 +08:00
import { getResParams } from '../../../actions/addInterface.js'
2017-07-19 19:29:18 +08:00
2017-07-27 19:32:08 +08:00
//const editor = new wangEditor('#res-cover')
2017-07-24 10:59:17 +08:00
2017-07-19 19:29:18 +08:00
@connect(
state => {
return {
resParams: state.addInterface.resParams
}
},
{
2017-07-20 14:48:29 +08:00
getResParams
2017-07-19 19:29:18 +08:00
}
)
2017-07-18 12:53:53 +08:00
class ResParams extends Component {
2017-07-19 19:29:18 +08:00
static propTypes = {
2017-07-20 14:48:29 +08:00
resParams: PropTypes.string,
getResParams: PropTypes.func
2017-07-19 19:29:18 +08:00
}
2017-07-18 12:53:53 +08:00
constructor(props) {
super(props)
}
2017-07-27 19:32:08 +08:00
// initResParams () {
// const { resParams } = this.props
// if (resParams) {
// editor.txt.html(resParams)
// }
// }
2017-07-24 10:59:17 +08:00
2017-07-31 21:07:24 +08:00
componentDidMount() {
2017-07-27 19:32:08 +08:00
//const reg = /(<p>)|(<\/p>)|&nbsp;|(<br>)|\s+|<div>|<\/div>/g
//editor.customConfig.menus = []
// editor.customConfig.onchange = html => {
// html = html.replace(reg, '')
// this.props.getResParams(html)
// }
// setTimeout(() => {
// this.initResParams()
// }, 400)
//editor.create()
2017-07-31 21:07:24 +08:00
function json_parse(json) {
try {
2017-07-27 19:32:08 +08:00
return JSON.stringify(JSON.parse(json), null, "\t");
2017-07-31 21:07:24 +08:00
} catch (e) {
2017-07-27 19:32:08 +08:00
return json
}
2017-07-19 19:29:18 +08:00
}
2017-07-31 21:07:24 +08:00
var langTools = window.ace.require("ace/ext/language_tools");
let editor = this.editor = window.ace.edit("res-cover")
editor.getSession().setMode("ace/mode/json");
editor.setOptions({
enableBasicAutocompletion: true,
enableSnippets: true,
enableLiveAutocompletion: true
});
2017-07-27 19:32:08 +08:00
2017-07-31 21:07:24 +08:00
var rhymeCompleter = {
identifierRegexps: [/[@]/],
getCompletions: function (editor, session, pos, prefix, callback) {
if (prefix.length === 0) { callback(null, []); return }
var wordList = [
{ name: '字符串', mock: '@string' },
{ name: '自然数', mock: '@natural' },
{ name: '布尔', mock: '@boolean' },
{ name: '标题', mock: '@title' },
{ name: '姓名', mock: '@name' },
{ name: 'url', mock: '@url' },
{ name: '域名', mock: '@domain' },
{ name: 'ip地址', mock: '@ip' },
{ name: 'id', mock: '@id' },
{ name: 'guid', mock: '@guid' },
{ name: '当前时间', mock: '@now' },
{ name: '整数', mock: '@integer' },
{ name: '浮点数', mock: '@float' },
{ name: 'email', mock: '@email' },
{ name: '大段文本', mock: '@text' },
{ name: '句子', mock: '@sentence' },
{ name: '单词', mock: '@word' },
{ name: '地址', mock: '@region' }
]
callback(null, wordList.map(function (ea) {
return { name: ea.mock, value: ea.mock, score: ea.mock, meta: ea.name }
}));
}
}
langTools.addCompleter(rhymeCompleter);
2017-08-01 09:49:11 +08:00
2017-07-31 21:07:24 +08:00
editor.getSession().on('change', () => {
this.props.getResParams(editor.getValue())
2017-07-27 19:32:08 +08:00
});
2017-08-01 09:49:11 +08:00
2017-07-31 21:07:24 +08:00
setTimeout(() => {
2017-08-01 09:49:11 +08:00
2017-07-31 21:07:24 +08:00
editor.setValue(json_parse(this.props.resParams))
}, 400)
2017-07-19 19:29:18 +08:00
}
2017-07-31 21:07:24 +08:00
render() {
2017-07-18 12:53:53 +08:00
return (
2017-07-26 12:31:42 +08:00
<section className="res-params-box">
2017-07-27 20:06:49 +08:00
<Card title="返回 Mock" style={{ width: 500 }}>
<div id="res-cover"></div>
</Card>
2017-07-18 12:53:53 +08:00
</section>
)
}
}
2017-07-20 19:26:04 +08:00
export default ResParams