<commit_before>'use strict';

var React = require('react')
var {getSelection, setSelection} = require('react/lib/ReactInputSelection')

var MaskedInput = React.createClass({
  propTypes: {
    pattern: React.PropTypes.string.isRequired
  },

  getDefaultProps() {
    return {
      value: ''
    }
  },

  componentWillMount() {
    this.mask = new InputMask({
      pattern: this.props.pattern,
      value: this.props.value
    })
  },

  _updateMaskSelection() {
    this.mask.selection = getSelection(this.getDOMNode())
  },

  _updateInputSelection() {
    setSelection(this.getDOMNode(), this.mask.selection)
  },

  _onChange(e) {
    console.log('onChange', getSelection(this.getDOMNode()), e.target.value)

    var maskValue = this.mask.getValue()
    if (e.target.value != maskValue) {
      // Naive cut detection
      if (e.target.value.length < maskValue.length) {
        this.mask.backspace()
      }
      e.target.value = this.mask.getValue()
      this._updateInputSelection()
    }
    if (this.props.onChange) {
      this.props.onChange(e)
    }
  },

  _onKeyDown(e) {
    console.log('onKeyDown', getSelection(this.getDOMNode()), e.key, e.target.value)

    if (e.key == 'Backspace') {
      e.preventDefault()
      this._updateMaskSelection()
      if (this.mask.backspace()) {
        e.target.value = this.mask.getValue()
        this._updateInputSelection()
        this.props.onChange(e)
      }
    }
    else if (e.key == 'Delete') {
      // Not quite sure how to handle this, so just cancel it for now
      e.preventDefault()
    }
  },

  _onKeyPress(e) {
    console.log('onKeyPress', getSelection(this.getDOMNode()), e.key, e.target.value)

    // Ignore modified key presses
    if (e.metaKey || e.altKey || e.ctrlKey) { return }

    e.preventDefault()
    this._updateMaskSelection()
    if (this.mask.input(e.key)) {
      e.target.value = this.mask.getValue()
      this._updateInputSelection()
      this.props.onChange(e)
    }
  },

  _onPaste(e) {
    console.log('onPaste', getSelection(this.getDOMNode()), e.clipboardData.getData('Text'), e.target.value)

    e.preventDefault()
    this._updateMaskSelection()
    // getData value needed for IE also works in FF & Chrome
    if (this.mask.paste(e.clipboardData.getData('Text'))) {
      e.target.value = this.mask.getValue()<commit_msg>Use onChange to detect cut and delete operations<commit_after>'use strict';

var React = require('react')
var {getSelection, setSelection} = require('react/lib/ReactInputSelection')

var MaskedInput = React.createClass({
  propTypes: {
    pattern: React.PropTypes.string.isRequired
  },

  getDefaultProps() {
    return {
      value: ''
    }
  },

  componentWillMount() {
    this.mask = new InputMask({
      pattern: this.props.pattern,
      value: this.props.value
    })
  },

  _updateMaskSelection() {
    this.mask.selection = getSelection(this.getDOMNode())
  },

  _updateInputSelection() {
    setSelection(this.getDOMNode(), this.mask.selection)
  },

  _onChange(e) {
    console.log('onChange', getSelection(this.getDOMNode()), e.target.value)

    var maskValue = this.mask.getValue()
    if (e.target.value != maskValue) {
      // Cut or delete operations will have shortened the value
      if (e.target.value.length < maskValue.length) {
        var sizeDiff = maskValue.length - e.target.value.length
        // onSelect does weird things in IE - we can only be sure of the start
        // od the selection, so make sure it covers the difference in size
        // before blanking out the characters which were cut or deleted.
        this.mask.selection.end = this.mask.selection.start + sizeDiff
        this.mask.backspace()
      }
      e.target.value = this.mask.getValue()
      this._updateInputSelection()
    }
    if (this.props.onChange) {
      this.props.onChange(e)
    }
  },

  _onKeyDown(e) {
    console.log('onKeyDown', getSelection(this.getDOMNode()), e.key, e.target.value)

    if (e.key == 'Backspace') {
      e.preventDefault()
      this._updateMaskSelection()
      if (this.mask.backspace()) {
        e.target.value = this.mask.getValue()
        this._updateInputSelection()
        this.props.onChange(e)
      }
    }
  },

  _onKeyPress(e) {
    console.log('onKeyPress', getSelection(this.getDOMNode()), e.key, e.target.value)

    // Ignore modified key presses
    if (e.metaKey || e.altKey || e.ctrlKey) { return }

    e.preventDefault()
    this._updateMaskSelection()
    if (this.mask.input(e.key)) {
      e.target.value = this.mask.getValue()
      this._updateInputSelection()
      this.props.onChange(e)
    }
  },

  _onPaste(e) {
    console.log('onPaste', getSelection(this.getDOMNode()), e.clipboardData.getData('Text'), e.target.value)

    e.preventDefault()
    this._updateMaskSelection()
    // getData value needed for IE also works in FF & Chrome
    if (this.mask.paste(e.clipboardData.getData('Text'))) {
      e.target.value = this.mask.getValue()
{'fraction_non_alphanumeric': 0.1396097364715349, 'fraction_numerical': 0.0, 'mean_word_length': 2.8423493044822257, 'pattern_counts': {'":': 0, '<': 5, '<?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': '7096680', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1575, 'n_words': 301}