2020-03-16 14:12:11 +08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
|
|
2020-03-29 18:16:58 +08:00
|
|
|
import CodeMirror from 'react-codemirror'
|
2020-03-16 14:12:11 +08:00
|
|
|
|
|
|
|
|
require('codemirror/lib/codemirror.css');
|
|
|
|
|
require('codemirror/mode/javascript/javascript');
|
|
|
|
|
require('codemirror/mode/xml/xml');
|
|
|
|
|
require('codemirror/mode/markdown/markdown');
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
class TestCodeMirror extends Component {
|
|
|
|
|
constructor(props) {
|
|
|
|
|
super(props)
|
|
|
|
|
|
|
|
|
|
this.updateCode = this.updateCode.bind(this)
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
code: '// Code\n //2 \n //3 \n //4 \n //5 \n\n\n\n\n\n\n'
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateCode(newCode) {
|
|
|
|
|
this.setState({
|
|
|
|
|
code: newCode,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
var readOnlyLines = [0,1,2,3,8];
|
2020-03-29 18:16:58 +08:00
|
|
|
|
2020-03-16 14:12:11 +08:00
|
|
|
this.refs.editor.getCodeMirror().on('beforeChange',function(cm,change) {
|
|
|
|
|
console.log('change.from.line', change.from.line)
|
2020-03-29 18:16:58 +08:00
|
|
|
if ( readOnlyLines.indexOf(change.from.line) !== -1
|
2020-03-16 14:12:11 +08:00
|
|
|
// 有问题:如果用回车将有内容的行挤到不可编辑的行,那么无法删除掉不可编辑行里的内容了
|
|
|
|
|
// 解决办法:将所需的行数固定写好,禁用掉回车键。
|
2020-03-29 18:16:58 +08:00
|
|
|
// || change.from.line > ( readOnlyLines[readOnlyLines.length -1] + 1 )
|
2020-03-16 14:12:11 +08:00
|
|
|
) {
|
|
|
|
|
change.cancel();
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
setCode() {
|
|
|
|
|
this.setState({
|
|
|
|
|
code: 'test'
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
var options = {
|
|
|
|
|
lineNumbers: true,
|
|
|
|
|
mode: 'javascript',
|
|
|
|
|
};
|
|
|
|
|
return (
|
|
|
|
|
<div>
|
|
|
|
|
<button onClick={this.setCode.bind(this)}>set</button>
|
|
|
|
|
<CodeMirror ref="editor" value={this.state.code} onChange={this.updateCode} options={options} />
|
|
|
|
|
</div>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-29 18:16:58 +08:00
|
|
|
export default TestCodeMirror;
|