<commit_before>import React, { PropTypes } from 'react';
import Component from './AbstractComponent';
import {
  View,
  PanResponder,
  requireNativeComponent,
  NativeModules,
  Platform,
  PixelRatio
} from 'react-native';

const ratio = PixelRatio.get();

const {
  SitbRCTCanvasView
} = NativeModules;

/**
 * @author 田尘殇Sean(<EMAIL>)
 * @date 16/7/14
 */
class Canvas extends Component {

  static propTypes = {
    ...View.propTypes,
    lineWidth: PropTypes.number,
    lines: PropTypes.array,
    strokeColor: PropTypes.string
  };

  static defaultProps = {
    lineWidth: 2,
    strokeColor: 'black'
  };

  // refs
  canvas;

  panResponder;
  points:Array;
  lines = [];

  constructor(props) {
    super(props);
    this.state = {
      lineWidth: props.lineWidth * ratio
    };
  }

  componentWillMount() {
    this.panResponder = PanResponder.create({
      onStartShouldSetPanResponder: () => true,
      onStartShouldSetPanResponderCapture: () => true,
      onMoveShouldSetPanResponder: () => true,
      onMoveShouldSetPanResponderCapture: () => true,
      onPanResponderGrant: this.handlePanResponderGrant,
      onPanResponderMove: this.handlePanResponderMove,
      onPanResponderRelease: () => {}
    });
  }

  componentWillReceiveProps(nextProps) {
    this.setState({lineWidth: nextProps.lineWidth});
  }

  setNativeProps(props) {
    console.log(props);
    this.canvas && this.canvas.setNativeProps(props);
  }

  handlePanResponderGrant(event, gestureState) {
    const {touchStart} = this.props;
    if (touchStart) {
      touchStart(event, gestureState);
    } else {
      this.points = [];
      this.lines.push(this.points);
      this.points.push(this.handlePoint(event, gestureState));
      this.setNativeProps({lines: this.lines});
    }
  }

  handlePanResponderMove(event, gestureState) {
    const {touchMove} = this.props;
    if (touchMove) {
      touchMove(event, gestureState);
    } else {
      this.points.push(this.handlePoint(event, gestureState));
      this.setNativeProps({lines: this.lines});
    }
  }

  handlePoint(event, gestureState) {
    const {dx, dy} = gestureState;
    const {locationX, locationY} = event.nativeEvent;

    if (Platform.OS === 'android') {
      return {
        x: (locationX + dx ) * PixelRatio.get(),
        y: (locationY + dy) * PixelRatio.get()
      };
    }

    return {
      x: locationX,
      y: locationY
    };
  }

  capture() {
    return SitbRCTCanvasView.capture();
  }

  clearScreen() {
    this.lines = [];
    this.setNativeProps({lines: []});
  }

  render() {
    return (
      <RCTCanvas {...this.props}
        {...this.panResponder.panHandlers}
        lineWidth={this.state.lineWidth}
        ref={ref => this.canvas = ref}
      />
    );
  }

}

const RCTCanvas = requireNativeComponent('SitbRCTCanvasView', Canvas);

export default Canvas;
<commit_msg>设置安卓线宽BUG 修复<commit_after>import React, { PropTypes } from 'react';
import Component from './AbstractComponent';
import {
  View,
  PanResponder,
  requireNativeComponent,
  NativeModules,
  Platform,
  PixelRatio
} from 'react-native';

const ratio = PixelRatio.get();

const {
  SitbRCTCanvasView
} = NativeModules;

/**
 * @author 田尘殇Sean(<EMAIL>)
 * @date 16/7/14
 */
class Canvas extends Component {

  static propTypes = {
    ...View.propTypes,
    lineWidth: PropTypes.number,
    lines: PropTypes.array,
    strokeColor: PropTypes.string
  };

  static defaultProps = {
    lineWidth: 2,
    strokeColor: 'black'
  };

  // refs
  canvas;

  panResponder;
  points:Array;
  lines = [];

  constructor(props) {
    super(props);
    this.state = {
      lineWidth: this.getLineWidth(props.lineWidth)
    };
  }

  componentWillMount() {
    this.panResponder = PanResponder.create({
      onStartShouldSetPanResponder: () => true,
      onStartShouldSetPanResponderCapture: () => true,
      onMoveShouldSetPanResponder: () => true,
      onMoveShouldSetPanResponderCapture: () => true,
      onPanResponderGrant: this.handlePanResponderGrant,
      onPanResponderMove: this.handlePanResponderMove,
      onPanResponderRelease: () => {}
    });
  }

  componentWillReceiveProps(nextProps) {
    this.setState({lineWidth: this.getLineWidth(nextProps.lineWidth)});
  }

  getLineWidth(width) {
    return Platform.OS === 'android' ? width * ratio : width;
  }

  setNativeProps(props) {
    console.log(props);
    this.canvas && this.canvas.setNativeProps(props);
  }

  handlePanResponderGrant(event, gestureState) {
    const {touchStart} = this.props;
    if (touchStart) {
      touchStart(event, gestureState);
    } else {
      this.points = [];
      this.lines.push(this.points);
      this.points.push(this.handlePoint(event, gestureState));
      this.setNativeProps({lines: this.lines});
    }
  }

  handlePanResponderMove(event, gestureState) {
    const {touchMove} = this.props;
    if (touchMove) {
      touchMove(event, gestureState);
    } else {
      this.points.push(this.handlePoint(event, gestureState));
      this.setNativeProps({lines: this.lines});
    }
  }

  handlePoint(event, gestureState) {
    const {dx, dy} = gestureState;
    const {locationX, locationY} = event.nativeEvent;

    if (Platform.OS === 'android') {
      return {
        x: (locationX + dx ) * PixelRatio.get(),
        y: (locationY + dy) * PixelRatio.get()
      };
    }

    return {
      x: locationX,
      y: locationY
    };
  }

  capture() {
    return SitbRCTCanvasView.capture();
  }

  clearScreen() {
    this.lines = [];
    this.setNativeProps({lines: []});
  }

  render() {
    return (
      <RCTCanvas {...this.props}
        {...this.panResponder.panHandlers}
        lineWidth={this.state.lineWidth}
        ref={ref => this.canvas = ref}
      />
    );
  }

}

const RCTCanvas = requireNativeComponent('SitbRCTCanvasView', Canvas);

export default Canvas;

{'fraction_non_alphanumeric': 0.1345039209001023, 'fraction_numerical': 0.002045687009887487, 'mean_word_length': 2.993873383253914, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 19, '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': '1704907', 'n_tokens_mistral': 1978, 'n_tokens_neox': 1893, 'n_words': 327}