react-codemirror怎么添加代码折叠功能?
github下的example例子下进行添加代码行高亮成功,但代码折叠的功能没有效果
var React = require('react');
var ReactDOM = require('react-dom');
var Codemirror = require('../../src/Codemirror');
require('codemirror/mode/javascript/javascript');
require('codemirror/mode/xml/xml');
require('codemirror/mode/markdown/markdown');
// 添加高亮
require('codemirror/addon/selection/active-line');
require('codemirror/addon/fold/foldcode');
require('codemirror/addon/fold/foldgutter');
var defaults = {
markdown: '# Heading\n\nSome **bold** and _italic_ text\nBy [Jed Watson](https://github.com/JedWatson)',
javascript: 'var component = {\n\tname: "react-codemirror",\n\tauthor: "Jed Watson",\n\trepo: "https://github.com/JedWatson/react-codemirror"\n};'
};
var App = React.createClass({
getInitialState () {
return {
code: defaults.markdown,
readOnly: false,
mode: 'markdown',
};
},
updateCode (newCode) {
this.setState({
code: newCode
});
},
changeMode (e) {
var mode = e.target.value;
this.setState({
mode: mode,
code: defaults[mode]
});
},
toggleReadOnly () {
this.setState({
readOnly: !this.state.readOnly
}, () => this.refs.editor.focus());
},
interact (cm) {
console.log(cm.getValue());
},
render () {
var options = {
styleActiveLine: true,
lineNumbers: true,
lineWrapping: true,
mode: this.state.mode,
foldGutter: true,
gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"]
};
return (
<div>
<Codemirror ref="editor" value={this.state.code} onChange={this.updateCode} options={options} interact={this.interact} />
<div style={{ marginTop: 10 }}>
<select onChange={this.changeMode} value={this.state.mode}>
<option value="markdown">Markdown</option>
<option value="javascript">JavaScript</option>
</select>
<button onClick={this.toggleReadOnly}>Toggle read-only mode (currently {this.state.readOnly ? 'on' : 'off'})</button>
</div>
</div>
);
}
});
ReactDOM.render(<App />, document.getElementById('app'));
样式已经在example.css引进去了
.ReactCodeMirror {
border: 1px solid #ccc;
}
@import (inline) "../../node_modules/codemirror/lib/codemirror.css";
@import (inline) "../../node_modules/codemirror/addon/fold/foldgutter.css";
但效果如下:
并没有出现左侧的小三角,求各位大侠解答,谢谢!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
必须要放在option:{}里面
const options = {
};
需要在options里加入gutters的折叠类型
require('codemirror/addon/fold/brace-fold.js')