<commit_before>import React from 'react'
import _ from 'underscore'
import handleFocus from './handleFocus.js'
import {EditorState, ContentState, convertFromHTML, convertToRaw, convertFromRaw, getDefaultKeyBinding, KeyBindingUtil} from 'draft-js';
const {hasCommandModifier} = KeyBindingUtil;
import Editor from 'draft-js-plugins-editor';
import DraftPasteProcessor from 'draft-js/lib/DraftPasteProcessor';
import {stateToHTML} from 'draft-js-export-html';
import createRichButtonsPlugin from 'draft-js-richbuttons-plugin';
import {generateStyleObjects} from '../../libs/markupUserResponses';
var C = require("../../constants").default

const noUnderlineErrors = []

const feedbackStrings = C.FEEDBACK_STRINGS

export default React.createClass({
  getInitialState: function () {
    return {
      text: EditorState.createWithContent(ContentState.createFromBlockArray(convertFromHTML(this.props.value || "")))
    }
  },

  getErrorsForAttempt: function (attempt) {
    return _.pick(attempt, ...C.ERROR_TYPES)
  },

  componentWillReceiveProps: function (nextProps) {
    if (nextProps.latestAttempt !== this.props.latestAttempt) {
      if (nextProps.latestAttempt && nextProps.latestAttempt.found) {
        const parentID = nextProps.latestAttempt.response.parentID;
        const errorKeys = _.keys(this.getErrorsForAttempt(nextProps.latestAttempt))
        const nErrors = errorKeys.length;
        var targetText;
        if (parentID) {
          const parentResponse = this.props.getResponse(nextProps.latestAttempt.response.parentID)
          targetText = parentResponse.text
        } else if (nErrors > 0) {
          targetText = nextProps.latestAttempt.response.text
        } else {
          var state = convertToRaw(this.state.text.getCurrentContent());
          state.blocks[0].inlineStyleRanges = []
          this.setState({
            text: EditorState.createWithContent(convertFromRaw(state))
          }, () => {
            this.props.handleChange(stateToHTML(this.state.text.getCurrentContent()))
          });
          return
        }
        const newStyle = this.getUnderliningFunction(errorKeys[0], targetText, nextProps.latestAttempt.submitted)
        if (newStyle) {
          var state = convertToRaw(this.state.text.getCurrentContent());
          state.blocks[0].text = newStyle.text;
          state.blocks[0].inlineStyleRanges = newStyle.inlineStyleRanges
          this.setState({
            text: EditorState.createWithContent(convertFromRaw(state))
          }, () => {
            this.props.handleChange(stateToHTML(this.state.text.getCurrentContent()).blocks[0].text)
          });
        }
      }
    }
  },

  getUnderliningFunction: function (errorType, targetString, userString) {
    switch (errorType) {
      case "punctuationError":
      case "typingError":
      case "caseError":
      case "modifiedWordError":
      case "additionalWordError":
      case "missingWordError":
        return generateStyleObjects(targetString, userString)
      case "flexibleModifiedWordError":
      case "flexibleAdditionalWordError":
      case "flexibleMissingWordError":
        return generateStyleObjects(targetString, userString, true)
      default:
        return undefined
    }
  },

  // getState: function () {
  //   return EditorState.createWithContent(ContentState.createFromBlockArray(convertFromHTML(this.props.text || "")))
  // },

  handleKeyCommand: function (command) {
    if (command === 'student-editor-submit') {
      // Perform a request to save your contents, set<commit_msg>Refactor applying editor styles, and correctly underline prematched answers<commit_after>import React from 'react'
import _ from 'underscore'
import handleFocus from './handleFocus.js'
import {EditorState, ContentState, convertFromHTML, convertToRaw, convertFromRaw, getDefaultKeyBinding, KeyBindingUtil} from 'draft-js';
const {hasCommandModifier} = KeyBindingUtil;
import Editor from 'draft-js-plugins-editor';
import DraftPasteProcessor from 'draft-js/lib/DraftPasteProcessor';
import {stateToHTML} from 'draft-js-export-html';
import createRichButtonsPlugin from 'draft-js-richbuttons-plugin';
import {generateStyleObjects} from '../../libs/markupUserResponses';
var C = require("../../constants").default

const noUnderlineErrors = []

const feedbackStrings = C.FEEDBACK_STRINGS

export default React.createClass({
  getInitialState: function () {
    return {
      text: EditorState.createWithContent(ContentState.createFromBlockArray(convertFromHTML(this.props.value || "")))
    }
  },

  getErrorsForAttempt: function (attempt) {
    return _.pick(attempt, ...C.ERROR_TYPES)
  },

  componentWillReceiveProps: function (nextProps) {
    if (nextProps.latestAttempt !== this.props.latestAttempt) {
      if (nextProps.latestAttempt && nextProps.latestAttempt.found) {
        const parentID = nextProps.latestAttempt.response.parentID;
        const errorKeys = _.keys(this.getErrorsForAttempt(nextProps.latestAttempt))
        const nErrors = errorKeys.length;
        var targetText;
        if (parentID) {
          const parentResponse = this.props.getResponse(nextProps.latestAttempt.response.parentID)
          targetText = parentResponse.text
          const newStyle = this.getUnderliningFunctionFromAuthor(nextProps.latestAttempt.response.author, targetText, nextProps.latestAttempt.submitted)
          if (newStyle) {
            this.applyNewStyle(newStyle)
          }
          return
        } else if (nErrors > 0) {
          targetText = nextProps.latestAttempt.response.text
        } else {
          this.clearStyle()
          return
        }
        const newStyle = this.getUnderliningFunction(errorKeys[0], targetText, nextProps.latestAttempt.submitted)
        if (newStyle) {
          this.applyNewStyle(newStyle)
        }
      }
    }
  },

  getUnderliningFunction: function (errorType, targetString, userString) {
    switch (errorType) {
      case "punctuationError":
      case "typingError":
      case "caseError":
      case "modifiedWordError":
      case "additionalWordError":
      case "missingWordError":
        return generateStyleObjects(targetString, userString)
      case "flexibleModifiedWordError":
      case "flexibleAdditionalWordError":
      case "flexibleMissingWordError":
        return generateStyleObjects(targetString, userString, true)
      default:
        return undefined
    }
  },

  getUnderliningFunctionFromAuthor: function (author, targetString, userString) {
    switch (author) {
      case "Punctuation Hint":
      case "Capitalization Hint":
      case "Modified Word Hint":
      case "Additional Word Hint":
      case "Missing Word Hint":
        return generateStyleObjects(targetString, userString)
      case "Flexible Modified Word Hint":
      case "Flexible Additional Word Hint":
      case "Flexible Missing Word Hint":
        return generateStyleObjects(targetString, userString, true)
      default:
        return undefined
    }
  },

  applyNewStyle: function (newStyle) {
    var state = convertToRaw(this.state.text.getCurrentContent());
    state.blocks[0].text = newStyle.text;
    state.blocks[0].inlineStyleRanges = newStyle.inlineStyleRanges
    this.setState({
      text: EditorState.createWithContent(convertFromRaw(state))
    }, () => {
      this.props.handleChange(stateToHTML(this.state.text.getCurrentContent()).blocks[0].text)
    });
  },

  clearStyle: function () {
    var state = convertToRaw(this.state.text.getCurrentContent());
    state.blocks[0].inlineStyleRanges = []
    this.setState({
      text: EditorState.createWithContent(convertFromRaw(state))
    }, () => {
      this.props.handleChange(stateToHTML(this.state.text.getCurrentContent()))
    });
  },

  // getState: function () {
  //   return EditorState.createWithContent(ContentState.createFromBlockArray(convertFromHTML(this.props.text || "")))
  // },

  handleKeyCommand: function (command) {
    if (command === 'student-editor-submit') {
      // Perform a request to save your contents, set
{'fraction_non_alphanumeric': 0.10846067834513604, 'fraction_numerical': 0.0014908684308609765, 'mean_word_length': 3.4255085211654754, 'pattern_counts': {'":': 26, '<': 3, '<?xml version=': 0, '>': 9, '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': '9537', 'n_tokens_mistral': 2243, 'n_tokens_neox': 2193, 'n_words': 433}