1- import React from 'react'
1+ import React , { PropTypes } from 'react'
22import ReactDOM from 'react-dom'
33import modes from '../lib/modes'
44import _ from 'lodash'
55
66const remote = require ( 'electron' ) . remote
77const ace = window . ace
88
9- module . exports = React . createClass ( {
10- propTypes : {
11- code : React . PropTypes . string ,
12- mode : React . PropTypes . string ,
13- className : React . PropTypes . string ,
14- onChange : React . PropTypes . func ,
15- readOnly : React . PropTypes . bool
16- } ,
17- getDefaultProps : function ( ) {
18- return {
19- readOnly : false
20- }
21- } ,
22- componentWillReceiveProps : function ( nextProps ) {
9+ export default class CodeEditor extends React . Component {
10+ componentWillReceiveProps ( nextProps ) {
2311 if ( nextProps . readOnly !== this . props . readOnly ) {
2412 this . editor . setReadOnly ( ! ! nextProps . readOnly )
2513 }
26- } ,
27- componentDidMount : function ( ) {
28- var el = ReactDOM . findDOMNode ( this . refs . target )
14+ }
15+
16+ componentDidMount ( ) {
17+ var el = ReactDOM . findDOMNode ( this )
2918 var editor = this . editor = ace . edit ( el )
3019 editor . $blockScrolling = Infinity
3120 editor . setValue ( this . props . code )
3221 editor . renderer . setShowGutter ( true )
3322 editor . setTheme ( 'ace/theme/xcode' )
3423 editor . clearSelection ( )
3524 editor . moveCursorTo ( 0 , 0 )
25+ editor . setReadOnly ( ! ! this . props . readOnly )
26+
3627 editor . commands . addCommand ( {
3728 name : 'Emacs cursor up' ,
3829 bindKey : { mac : 'Ctrl-P' } ,
@@ -46,13 +37,14 @@ module.exports = React.createClass({
4637 name : 'Focus title' ,
4738 bindKey : { win : 'Esc' , mac : 'Esc' } ,
4839 exec : function ( editor , e ) {
49- console . log ( e )
5040 remote . getCurrentWebContents ( ) . send ( 'detail-edit' )
5141 } ,
5242 readOnly : true
5343 } )
5444
55- editor . setReadOnly ( ! ! this . props . readOnly )
45+ editor . on ( 'blur' , ( ) => {
46+ if ( this . props . onBlur ) this . props . onBlur ( )
47+ } )
5648
5749 var session = editor . getSession ( )
5850 let mode = _ . findWhere ( modes , { name : this . props . mode } )
@@ -65,14 +57,15 @@ module.exports = React.createClass({
6557 session . setOption ( 'useWorker' , false )
6658 session . setUseWrapMode ( true )
6759
68- session . on ( 'change' , function ( e ) {
60+ session . on ( 'change' , e => {
6961 if ( this . props . onChange != null ) {
7062 var value = editor . getValue ( )
7163 this . props . onChange ( e , value )
7264 }
73- } . bind ( this ) )
74- } ,
75- componentDidUpdate : function ( prevProps ) {
65+ } )
66+ }
67+
68+ componentDidUpdate ( prevProps ) {
7669 if ( this . editor . getValue ( ) !== this . props . code ) {
7770 this . editor . setValue ( this . props . code )
7871 this . editor . clearSelection ( )
@@ -85,22 +78,42 @@ module.exports = React.createClass({
8578 : 'text'
8679 session . setMode ( 'ace/mode/' + syntaxMode )
8780 }
88- } ,
89- getFirstVisibleRow : function ( ) {
81+ }
82+
83+ getFirstVisibleRow ( ) {
9084 return this . editor . getFirstVisibleRow ( )
91- } ,
92- getCursorPosition : function ( ) {
85+ }
86+
87+ getCursorPosition ( ) {
9388 return this . editor . getCursorPosition ( )
94- } ,
95- moveCursorTo : function ( row , col ) {
89+ }
90+
91+ moveCursorTo ( row , col ) {
9692 this . editor . moveCursorTo ( row , col )
97- } ,
98- scrollToLine : function ( num ) {
93+ }
94+
95+ scrollToLine ( num ) {
9996 this . editor . scrollToLine ( num , false , false )
100- } ,
101- render : function ( ) {
97+ }
98+
99+ render ( ) {
102100 return (
103- < div ref = 'target' className = { this . props . className == null ? 'CodeEditor' : 'CodeEditor ' + this . props . className } > </ div >
101+ < div className = { this . props . className == null ? 'CodeEditor' : 'CodeEditor ' + this . props . className } > </ div >
104102 )
105103 }
106- } )
104+ }
105+
106+ CodeEditor . propTypes = {
107+ code : PropTypes . string ,
108+ mode : PropTypes . string ,
109+ className : PropTypes . string ,
110+ onChange : PropTypes . func ,
111+ onBlur : PropTypes . func ,
112+ readOnly : PropTypes . bool
113+ }
114+
115+ CodeEditor . defaultProps = {
116+ readOnly : false
117+ }
118+
119+ export default CodeEditor
0 commit comments