editor.jsx 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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. Agent.on('search', (kw) => {
  25. this.kw = kw
  26. this.highlightKeyword()
  27. })
  28. }
  29. highlightKeyword () {
  30. while (this.marks.length > 0) {
  31. this.marks.shift().clear()
  32. }
  33. let code = this.props.code
  34. let pos = m_kw.findPositions(this.kw, code) || []
  35. // this.codemirror.markText({line: 6, ch: 16}, {line: 6, ch: 22}, {className: 'cm-hl'});
  36. pos.map((p) => {
  37. this.marks.push(this.codemirror.markText(p[0], p[1], {className: 'cm-hl'}))
  38. })
  39. }
  40. setValue (v) {
  41. this.props.setValue(v)
  42. }
  43. toComment () {
  44. let doc = this.codemirror.getDoc()
  45. let cur = doc.getCursor()
  46. let line = cur.line
  47. let info = doc.lineInfo(line)
  48. this.codemirror.toggleComment({
  49. line: line,
  50. cur: 0
  51. }, {
  52. line: line,
  53. cur: info.text.length
  54. })
  55. }
  56. componentDidMount () {
  57. // console.log(this.cnt_node, this.cnt_node.value);
  58. this.codemirror = CodeMirror.fromTextArea(this.cnt_node, {
  59. lineNumbers: true,
  60. readOnly: true,
  61. mode: 'hosts'
  62. })
  63. this.codemirror.setSize('100%', '100%')
  64. this.codemirror.on('change', (a) => {
  65. let v = a.getDoc().getValue()
  66. this.setValue(v)
  67. })
  68. this.codemirror.on('gutterClick', (cm, n) => {
  69. if (this.props.readonly === true) return
  70. let info = cm.lineInfo(n)
  71. //cm.setGutterMarker(n, "breakpoints", info.gutterMarkers ? null : makeMarker());
  72. let ln = info.text
  73. if (/^\s*$/.test(ln)) return
  74. let new_ln
  75. if (/^#/.test(ln)) {
  76. new_ln = ln.replace(/^#\s*/, '')
  77. } else {
  78. new_ln = '# ' + ln
  79. }
  80. this.codemirror.getDoc()
  81. .replaceRange(new_ln, {line: info.line, ch: 0}, {
  82. line: info.line,
  83. ch: ln.length
  84. })
  85. //app.caculateHosts();
  86. })
  87. Agent.on('to_comment', () => {
  88. this.toComment()
  89. })
  90. }
  91. componentWillReceiveProps (next_props) {
  92. // console.log(next_props);
  93. let cm = this.codemirror
  94. let v = cm.getDoc().getValue()
  95. if (v !== next_props.code) {
  96. cm.getDoc().setValue(next_props.code)
  97. }
  98. cm.setOption('readOnly', next_props.readonly)
  99. setTimeout(() => {
  100. this.highlightKeyword()
  101. }, 100)
  102. }
  103. render () {
  104. return (
  105. <div
  106. id="sh-editor"
  107. className={classnames({
  108. readonly: this.props.readonly
  109. })}>
  110. <textarea
  111. ref={(c) => this.cnt_node = c}
  112. defaultValue={this.props.code || ''}
  113. />
  114. </div>
  115. )
  116. }
  117. }