<commit_before>'use strict';
var React = require('react');
var isMobile = require('./utils/isMobile')();
var CodeMirror;
var jsLintAddon;

if (!isMobile) {
  CodeMirror = require('codemirror');
  require('./mode/javascript')(CodeMirror);
  require('./addons/lint')(CodeMirror);
  require('./addons/edit/matchBrackets')(CodeMirror);
  require('./addons/edit/closeBrackets')(CodeMirror);
  jsLintAddon = require('./addons/lint/javascript');
}

var CodeMirrorEditor = React.createClass({

  propTypes: {
    className: React.PropTypes.string,
    config: React.PropTypes.object,
    defaultValue: React.PropTypes.string,
    forceTextArea: React.PropTypes.bool,
    linter: React.PropTypes.func,
    onChange: React.PropTypes.func,
    readOnly: React.PropTypes.oneOfType([
      React.PropTypes.bool,
      React.PropTypes.string
    ]),
    style: React.PropTypes.object,
    textAreaStyle: React.PropTypes.object,
    textAreaClass: React.PropTypes.string,
    textAreaClassName: React.PropTypes.string,
    value: React.PropTypes.string
  },

  getInitialState: function() {
    return { isControlled: typeof this.props.value !== 'undefined' };
  },

  componentDidMount: function() {
    var isTextArea = this.props.forceTextArea || isMobile;
    var config = this.props.config;
    if (isTextArea) {
      return;
    }

    jsLintAddon(CodeMirror, this.props.linter);
    this._editor =
      CodeMirror.fromTextArea(this.refs.editor.getDOMNode(), this.props);
    this._editor.on('change', this._handleChange);
    if (config) {
      Object.keys(config).map(function(prop) {
        var args = config[prop];
        if (!Array.isArray(args)) {
          args = [args];
        }
        if (prop.indexOf('set') !== -1) {
          return this._editor[prop].apply(this._editor, args);
        }
        args.unshift(prop);
        this._editor.setOption.apply(this._editor, args);
      }.bind(this));
    }
  },

  _handleChange: function() {
    if (this._editor) {
      var value = this._editor.getValue();

      if (value !== this.props.value) {
        if (this.props.onChange) {
          this.props.onChange({target: {value: value}});
        }

        if (this._editor.getValue() !== this.props.value) {
          if (this.state.isControlled) {
            this._editor.setValue(this.props.value);
          } else {
            this.props.value = value;
          }
        }
      }
    }
  },

  render: function() {
    var editor = React.createElement('textarea', {
      ref: 'editor',
      value: this.props.value,
      readOnly: this.props.readOnly,
      defaultValue: this.props.defaultValue,
      onChange: this.props.onChange,
      style: this.props.textAreaStyle,
      className: this.props.textAreaClassName || this.props.textAreaClass
    });

    return React.createElement('div', {
      style: this.props.style,
      className: this.props.className
    }, editor);
  }
});<commit_msg>With readOnly active, value should be updated with prop changes.<commit_after>'use strict';
var React = require('react');
var isMobile = require('./utils/isMobile')();
var CodeMirror;
var jsLintAddon;

if (!isMobile) {
  CodeMirror = require('codemirror');
  require('./mode/javascript')(CodeMirror);
  require('./addons/lint')(CodeMirror);
  require('./addons/edit/matchBrackets')(CodeMirror);
  require('./addons/edit/closeBrackets')(CodeMirror);
  jsLintAddon = require('./addons/lint/javascript');
}

var CodeMirrorEditor = React.createClass({

  propTypes: {
    className: React.PropTypes.string,
    config: React.PropTypes.object,
    defaultValue: React.PropTypes.string,
    forceTextArea: React.PropTypes.bool,
    linter: React.PropTypes.func,
    onChange: React.PropTypes.func,
    readOnly: React.PropTypes.oneOfType([
      React.PropTypes.bool,
      React.PropTypes.string
    ]),
    style: React.PropTypes.object,
    textAreaStyle: React.PropTypes.object,
    textAreaClass: React.PropTypes.string,
    textAreaClassName: React.PropTypes.string,
    value: React.PropTypes.string
  },

  getInitialState: function() {
    return { isControlled: typeof this.props.value !== 'undefined' };
  },

  componentDidMount: function() {
    var isTextArea = this.props.forceTextArea || isMobile;
    var config = this.props.config;
    if (isTextArea) {
      return;
    }

    jsLintAddon(CodeMirror, this.props.linter);
    this._editor =
      CodeMirror.fromTextArea(this.refs.editor.getDOMNode(), this.props);
    this._editor.on('change', this._handleChange);
    if (config) {
      Object.keys(config).map(function(prop) {
        var args = config[prop];
        if (!Array.isArray(args)) {
          args = [args];
        }
        if (prop.indexOf('set') !== -1) {
          return this._editor[prop].apply(this._editor, args);
        }
        args.unshift(prop);
        this._editor.setOption.apply(this._editor, args);
      }.bind(this));
    }
  },

  componentWillReceiveProps: function(newProps) {
    // With read only option we want output to be updated
    // when ever new props detect a change in value.
    if (
      this.props.readOnly &&
      this._editor &&
      this._editor.getValue() !== newProps.value
    ) {
      this._editor.setValue(newProps.value);
    }
  },

  _handleChange: function() {
    if (!this._editor) {
      return;
    }

    var value = this._editor.getValue();
    if (value !== this.props.value) {
      if (this.props.onChange) {
        this.props.onChange({target: {value: value}});
      }

      if (value !== this.props.value) {
        if (this.state.isControlled) {
          this._editor.setValue(this.props.value);
        }
      }
    }
  },

  render: function() {
    var editor = React.createElement('textarea', {
      ref: 'editor',
      value: this.props.value,
      readOnly: this.props.readOnly,
      defaultValue: this.props.defaultValue,
      onChange: this.props.onChange,
      style: this.props.textAreaStyle,
      className: this.props.textAreaClassName || this.props.textAreaClass
    });

    return React.createElement('div', {
      style: this.props.style,
      className: this.props.className
    }, editor);
  }
});
{'fraction_non_alphanumeric': 0.14369310793237972, 'fraction_numerical': 0.00032509752925877764, 'mean_word_length': 2.8384279475982535, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3881377', 'n_tokens_mistral': 1928, 'n_tokens_neox': 1890, 'n_words': 332}