Skip to content

Commit

Permalink
typescript version
Browse files Browse the repository at this point in the history
  • Loading branch information
luutruong committed Jul 5, 2022
1 parent 7aba94c commit 5942666
Show file tree
Hide file tree
Showing 11 changed files with 269 additions and 310 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -46,3 +46,4 @@ yarn-error.log
.gitattributes
.watchmanconfig
.history
dist/
60 changes: 27 additions & 33 deletions DoubleTapView.js → DoubleTapView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,38 +10,30 @@
import React, {Component} from 'react';
import {
View,
PanResponder
PanResponder,
GestureResponderEvent,
PanResponderGestureState
} from 'react-native';
import PropTypes from 'prop-types';
import type {ViewProps} from 'react-native/Libraries/Components/View/ViewPropTypes';
export default class DoubleTapView extends Component {

static propTypes = {
...ViewProps,
delay: PropTypes.number,
radius: PropTypes.number,
onSingleTap: PropTypes.func,
onDoubleTap: PropTypes.func,
};
import { DoubleTapViewProps } from '.';

export default class DoubleTapView extends Component<DoubleTapViewProps> {
static defaultProps = {
delay: 300,
radius: 50,
onSingleTap: () => {
},
onDoubleTap: () => {
},
};
gestureHandlers: any;
prevTouchInfo: { prevTouchX: number; prevTouchY: number; prevTouchTimeStamp: number; };
timer: any;

constructor() {
super();
constructor(props: DoubleTapViewProps) {
super(props);

this.gestureHandlers = PanResponder.create({
onStartShouldSetPanResponder: (evt, gestureState) => (gestureState.numberActiveTouches === 1),
onStartShouldSetResponderCapture: (evt, gestureState) => (gestureState.numberActiveTouches === 1),
onMoveShouldSetPanResponder: (evt, gestureState) => (false),
onMoveShouldSetResponderCapture: (evt, gestureState) => (false),
onPanResponderTerminationRequest: (evt, gestureState) => false,
onStartShouldSetPanResponder: (_evt, gestureState) => (gestureState.numberActiveTouches === 1),
onStartShouldSetPanResponderCapture: (_evt, gestureState) => (gestureState.numberActiveTouches === 1),
onMoveShouldSetPanResponder: () => false,
onMoveShouldSetPanResponderCapture: () => false,
onPanResponderTerminationRequest: () => false,
onPanResponderRelease: this.handlePanResponderRelease,

});
Expand All @@ -53,39 +45,35 @@ export default class DoubleTapView extends Component {
};

this.timer = null;

}


distance = (x0, y0, x1, y1) => {
return Math.sqrt(Math.pow((x1 - x0), 2) + Math.pow((y1 - y0), 2)).toFixed(1);
distance = (x0: number, y0: number, x1: number, y1: number) => {
return Math.sqrt(Math.pow((x1 - x0), 2) + Math.pow((y1 - y0), 2));
};

isDoubleTap = (currentTouchTimeStamp, {x0, y0}) => {
isDoubleTap = (currentTouchTimeStamp: number, gestureState: PanResponderGestureState) => {
const {prevTouchX, prevTouchY, prevTouchTimeStamp} = this.prevTouchInfo;
const dt = currentTouchTimeStamp - prevTouchTimeStamp;
const {delay, radius} = this.props;

return (prevTouchTimeStamp > 0 && dt < delay && this.distance(prevTouchX, prevTouchY, x0, y0) < radius);
return (prevTouchTimeStamp > 0 && dt < delay && this.distance(prevTouchX, prevTouchY, gestureState.x0, gestureState.y0) < radius);
};

handlePanResponderRelease = (evt, gestureState) => {
handlePanResponderRelease = (evt: GestureResponderEvent, gestureState: PanResponderGestureState) => {

const currentTouchTimeStamp = Date.now();
const x = evt.nativeEvent.locationX;
const y = evt.nativeEvent.locationY;

if (this.timer) {

if (this.isDoubleTap(currentTouchTimeStamp, gestureState)) {

clearTimeout(this.timer);
this.timer = null;
this.props.onDoubleTap();

} else {

const {prevTouchX, prevTouchY, prevTouchTimeStamp} = this.prevTouchInfo;
const {prevTouchX, prevTouchY} = this.prevTouchInfo;
const {radius} = this.props;

// if not in radius, it's a move
Expand Down Expand Up @@ -115,6 +103,12 @@ export default class DoubleTapView extends Component {

};

componentWillUnmount() {
if (this.timer) {
clearTimeout(this.timer);
}
}

render() {
return (
<View {...this.props} {...this.gestureHandlers.panHandlers}>
Expand Down
3 changes: 1 addition & 2 deletions PdfManager.js → PdfManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,7 @@
const PdfManagerNative = require('react-native').NativeModules.PdfManager;

export default class PdfManager {

static loadFile(path, password) {
static loadFile(path: string, password?: string) {
if (typeof path !== 'string') {
throw new TypeError('path must be a valid string.');
}
Expand Down
25 changes: 8 additions & 17 deletions PdfPageView.js → PdfPageView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,21 @@
* LICENSE file in the root directory of this source tree.
*/


'use strict';

import React, {PureComponent} from 'react';
import PropTypes from 'prop-types';
import {
requireNativeComponent,
} from 'react-native';
import type {ViewProps} from 'react-native/Libraries/Components/View/ViewPropTypes';
export default class PdfPageView extends PureComponent {
import { PdfPageViewProps } from '.';

export default class PdfPageView extends PureComponent<PdfPageViewProps> {
_getStylePropsProps = () => {
const {width, height} = this.props;
if (width || height) {
return {width, height};
}

return {};
};

Expand All @@ -31,23 +32,13 @@ export default class PdfPageView extends PureComponent {
return (
<PdfPageViewCustom
{...restProps}
// @ts-ignore
style={[style, this._getStylePropsProps()]}
/>
);

}
}

PdfPageView.propTypes = {
...ViewProps,
fileNo: PropTypes.number,
page: PropTypes.number,
width: PropTypes.number,
height: PropTypes.number
};

PdfPageView.defaultProps = {
style: {}
};

let PdfPageViewCustom = requireNativeComponent('RCTPdfPageView', PdfPageView, {nativeOnly: {}});
// @ts-ignore
const PdfPageViewCustom = requireNativeComponent('RCTPdfPageView', PdfPageView, {nativeOnly: {}});
Loading

0 comments on commit 5942666

Please sign in to comment.