<commit_before>import PropTypes from 'prop-types'
import React from 'react'
import { ContentState, Editor, EditorState } from 'draft-js'

export class PlainText extends React.Component {
  static propTypes = {
    content: PropTypes.string,
    name: PropTypes.string,
    onChange: PropTypes.func.isRequired,
    placeholder: PropTypes.string
  }

  constructor (props) {
    super(props)

    const editorState = this.setEditorState()
    this.state = { editorState }
  }

  setEditorState () {
    if (this.props.content) {
      return this.setStateWithContent()
    } else {
      return EditorState.createEmpty()
    }
  }

  setStateWithContent () {
    const content = ContentState.createFromText(this.props.content)
    return EditorState.createWithContent(content)
  }

  onChange = (editorState) => {
    const currentContentState = this.state.editorState.getCurrentContent()
    const newContentState = editorState.getCurrentContent()
    let newContent = editorState.getCurrentContent().getPlainText()

    if (currentContentState !== newContentState) {
      // There was a change in the content
      this.onContentChange(newContent)
    }
    this.setState({ editorState })
  }

  onContentChange = (content) => {
    if (this.props.name) {
      this.props.onChange(this.props.name, content)
    } else {
      this.props.onChange(content)
    }
  }

  focus = () => {
    this.refs.editor.focus()
  }

  handleReturn = (e) => {
    return 'handled'
  }

  render () {
    return (
      <div
        className='plain-text'
        name={this.props.name}
        onClick={this.focus}
      >
        <Editor
          ref='editor'
          editorState={this.state.editorState}
          placeholder={this.props.placeholder || 'Start Typing...'}
          handleReturn={this.handleReturn}
          onChange={this.onChange}
          spellcheck<commit_msg>use debounce for plaintext onChange<commit_after>import { debounce } from 'lodash'
import PropTypes from 'prop-types'
import React from 'react'
import { ContentState, Editor, EditorState } from 'draft-js'

export class PlainText extends React.Component {
  static propTypes = {
    content: PropTypes.string,
    name: PropTypes.string,
    onChange: PropTypes.func.isRequired,
    placeholder: PropTypes.string
  }

  constructor (props) {
    super(props)

    const editorState = this.setEditorState()
    this.state = { editorState }
  }

  setEditorState () {
    if (this.props.content) {
      return this.setStateWithContent()
    } else {
      return EditorState.createEmpty()
    }
  }

  setStateWithContent () {
    const content = ContentState.createFromText(this.props.content)
    return EditorState.createWithContent(content)
  }

  onChange = (editorState) => {
    const currentContentState = this.state.editorState.getCurrentContent()
    const newContentState = editorState.getCurrentContent()
    let newContent = editorState.getCurrentContent().getPlainText()

    if (currentContentState !== newContentState) {
      // There was a change in the content
      this.debouncedOnContentChange(newContent)
    }
    this.setState({ editorState })
  }

  debouncedOnContentChange = debounce((content) => {
    this.onContentChange(content)
  }, 500)

  onContentChange = (content) => {
    if (this.props.name) {
      this.props.onChange(this.props.name, content)
    } else {
      this.props.onChange(content)
    }
  }

  focus = () => {
    this.refs.editor.focus()
  }

  handleReturn = (e) => {
    return 'handled'
  }

  render () {
    return (
      <div
        className='plain-text'
        name={this.props.name}
        onClick={this.focus}
      >
        <Editor
          ref='editor'
          editorState={this.state.editorState}
          placeholder={this.props.placeholder || 'Start Typing...'}
          handleReturn={this.handleReturn}
          onChange={this.onChange}
          spellcheck
{'fraction_non_alphanumeric': 0.11332821693527756, 'fraction_numerical': 0.0007674597083653108, 'mean_word_length': 2.852216748768473, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 14, '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': '2272884', 'n_tokens_mistral': 1142, 'n_tokens_neox': 1142, 'n_words': 219}