editor.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /**
  2. * @author oldj
  3. * @blog http://oldj.net
  4. */
  5. 'use strict'
  6. import React from 'react'
  7. import CodeMirror from 'codemirror'
  8. // import '../../../node_modules/codemirror/addon/comment/comment'
  9. import 'codemirror/addon/comment/comment'
  10. import classnames from 'classnames'
  11. import m_kw from './kw'
  12. import Agent from '../Agent'
  13. import 'codemirror/lib/codemirror.css'
  14. import './editor.less'
  15. import modeHosts from './cm_hl'
  16. modeHosts()
  17. export default class Editor extends React.Component {
  18. constructor (props) {
  19. super(props)
  20. this.codemirror = null
  21. this.marks = []
  22. this.kw = ''
  23. this.state = {
  24. code: this.props.code
  25. }
  26. Agent.on('search', (kw) => {
  27. this.kw = kw
  28. this.highlightKeyword()
  29. })
  30. }
  31. highlightKeyword () {
  32. while (this.marks.length > 0) {
  33. this.marks.shift().clear()
  34. }
  35. let code = this.props.code
  36. let pos = m_kw.findPositions(this.kw, code) || []
  37. // this.codemirror.markText({line: 6, ch: 16}, {line: 6, ch: 22}, {className: 'cm-hl'});
  38. pos.map((p) => {
  39. this.marks.push(this.codemirror.markText(p[0], p[1], {className: 'cm-hl'}))
  40. })
  41. }
  42. setValue (v) {
  43. //this.props.setValue(v)
  44. }
  45. toComment () {
  46. let doc = this.codemirror.getDoc()
  47. let cur = doc.getCursor()
  48. let line = cur.line
  49. let info = doc.lineInfo(line)
  50. this.codemirror.toggleComment({
  51. line: line,
  52. cur: 0
  53. }, {
  54. line: line,
  55. cur: info.text.length
  56. })
  57. }
  58. componentDidMount () {
  59. // console.log(this.cnt_node, this.cnt_node.value);
  60. this.codemirror = CodeMirror.fromTextArea(this.cnt_node, {
  61. lineNumbers: true,
  62. readOnly: true,
  63. mode: 'hosts'
  64. })
  65. this.codemirror.setSize('100%', '100%')
  66. this.codemirror.on('change', (a) => {
  67. let v = a.getDoc().getValue()
  68. this.setValue(v)
  69. })
  70. this.codemirror.on('gutterClick', (cm, n) => {
  71. if (this.props.readonly === true) return
  72. let info = cm.lineInfo(n)
  73. //cm.setGutterMarker(n, "breakpoints", info.gutterMarkers ? null : makeMarker());
  74. let ln = info.text
  75. if (/^\s*$/.test(ln)) return
  76. let new_ln
  77. if (/^#/.test(ln)) {
  78. new_ln = ln.replace(/^#\s*/, '')
  79. } else {
  80. new_ln = '# ' + ln
  81. }
  82. this.codemirror.getDoc()
  83. .replaceRange(new_ln, {line: info.line, ch: 0}, {
  84. line: info.line,
  85. ch: ln.length
  86. })
  87. //app.caculateHosts();
  88. })
  89. Agent.on('to_comment', () => {
  90. this.toComment()
  91. })
  92. }
  93. componentWillReceiveProps (next_props) {
  94. // console.log(next_props);
  95. this.codemirror.getDoc().setValue(next_props.code)
  96. this.codemirror.setOption('readOnly', next_props.readonly)
  97. setTimeout(() => {
  98. this.highlightKeyword()
  99. }, 100)
  100. }
  101. render () {
  102. return (
  103. <div
  104. id="sh-editor"
  105. className={classnames({
  106. readonly: this.props.readonly
  107. })}>
  108. <textarea
  109. ref={(c) => this.cnt_node = c}
  110. defaultValue={this.props.code || ''}
  111. />
  112. </div>
  113. )
  114. }
  115. }