2014-11-25 16:24:54 +01:00
|
|
|
/**
|
|
|
|
* Created by avim on 11/25/2014.
|
|
|
|
*/
|
2014-11-27 17:53:37 +01:00
|
|
|
'use strict';
|
2014-11-25 16:24:54 +01:00
|
|
|
var React = require('react/addons');
|
|
|
|
var _ = require('lodash');
|
|
|
|
var brace = require('brace');
|
|
|
|
|
|
|
|
require('brace/mode/javascript');
|
|
|
|
require('brace/mode/html');
|
|
|
|
require('brace/theme/monokai');
|
|
|
|
|
|
|
|
var editor = React.createClass({
|
2014-11-27 17:53:37 +01:00
|
|
|
displayName: 'BraceEditor',
|
2014-11-25 16:24:54 +01:00
|
|
|
getInitialState: function () {
|
|
|
|
return {
|
|
|
|
editorId: _.uniqueId()
|
2014-11-27 17:53:37 +01:00
|
|
|
};
|
2014-11-25 16:24:54 +01:00
|
|
|
},
|
|
|
|
componentWillMount: function () {
|
|
|
|
|
|
|
|
},
|
|
|
|
render: function () {
|
2014-11-27 17:53:37 +01:00
|
|
|
var props = _.omit(this.props, ['id', 'ref', 'key', 'value', 'valueLink', 'onChange']);
|
2014-11-25 16:24:54 +01:00
|
|
|
props.id = this.state.editorId;
|
2014-11-27 17:53:37 +01:00
|
|
|
return React.DOM.div(props);
|
2014-11-25 16:24:54 +01:00
|
|
|
},
|
2014-11-27 17:53:37 +01:00
|
|
|
componentWillUpdate: function (nextProps/*, nextState*/) {
|
|
|
|
var value = nextProps.valueLink ? nextProps.valueLink() : nextProps.value;
|
|
|
|
if (this.editor && this.editor.getValue() != value) {
|
|
|
|
this.editor.setValue(value, 0);
|
2014-11-25 16:24:54 +01:00
|
|
|
}
|
|
|
|
},
|
|
|
|
componentDidMount: function () {
|
|
|
|
this.editor = brace.edit(this.state.editorId);
|
2014-11-27 17:53:37 +01:00
|
|
|
if (this.props.mode !== 'html') {
|
2014-11-25 16:24:54 +01:00
|
|
|
this.editor.getSession().setMode('ace/mode/javascript');
|
|
|
|
} else {
|
|
|
|
this.editor.getSession().setMode('ace/mode/html');
|
|
|
|
}
|
|
|
|
this.editor.getSession().setUseWorker(false);
|
|
|
|
this.editor.setTheme('ace/theme/monokai');
|
|
|
|
|
2014-11-27 17:53:37 +01:00
|
|
|
var value = this.props.valueLink ? this.props.valueLink() : this.props.value;
|
|
|
|
this.editor.setValue(value, 0);
|
2014-11-25 16:24:54 +01:00
|
|
|
if (this.props.readOnly) {
|
|
|
|
this.editor.setReadOnly(true);
|
|
|
|
} else {
|
|
|
|
this.editor.setReadOnly(false);
|
2014-11-27 17:53:37 +01:00
|
|
|
this.editor.on('change', function (/*e*/) {
|
2014-11-25 16:24:54 +01:00
|
|
|
if (this.props.valueLink) {
|
|
|
|
this.props.valueLink(this.editor.getValue());
|
|
|
|
} else if (this.props.onChange) {
|
2014-11-27 17:53:37 +01:00
|
|
|
this.props.onChange({target: {value: this.editor.getValue()}});
|
2014-11-25 16:24:54 +01:00
|
|
|
}
|
|
|
|
}.bind(this));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
componentWillUnmount: function () {
|
|
|
|
this.editor.destroy();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
module.exports = editor;
|