-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpencil_kit.android.tsx
127 lines (106 loc) · 2.98 KB
/
pencil_kit.android.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import React, { ReactElement } from 'react'
import ReactNative, {
processColor,
requireNativeComponent,
StyleSheet,
SyntheticEvent,
UIManager,
} from 'react-native'
// import the native android pencil kit class
const RCTPencilKitView = requireNativeComponent('RCTPencilKitView')
const styles = StyleSheet.create({
container: {
justifyContent: 'center',
backgroundColor: 'white',
alignItems: 'baseline',
flex: 1,
},
})
interface Props {
style?: 'light' | 'dark'
color?: 'string'
strokeWidth?: number
}
interface State {
color: string
strokeWidth: number
}
type SaveCallback = ({ uri: string }) => void
const COLORS = [
'black',
'#FA3142',
'#157FFB',
'#FDD230',
'#51D727',
'#F17CFC',
'#835FF4',
'#72E1FD',
'#FF9800',
'#985213',
]
export class PencilKit extends React.Component<Props, State> {
private pencilKit: React.RefObject<PencilKit>
private onSaveCallback: SaveCallback = null
// Always true on android
public static isAvailable = (): boolean => {
return true
}
constructor(props: Props) {
super(props)
this.pencilKit = React.createRef()
this.state = {
color: props.color || 'black',
strokeWidth: props.strokeWidth || 5
}
}
static getDerivedStateFromProps(props:Props, state:State): State {
const color = props.color || state.color
const strokeWidth = props.strokeWidth || state.strokeWidth
return { ...state, color, strokeWidth }
}
public setColor = (color: string): void => {
this.setState({ color })
}
public undo = (): void => {
UIManager.dispatchViewManagerCommand(this.getTag(), 1, null)
}
public redo = (): void => {
UIManager.dispatchViewManagerCommand(this.getTag(), 2, null)
}
public erase = (): void => {
UIManager.dispatchViewManagerCommand(this.getTag(), 3, null)
}
public setDarkMode = (): void => {
UIManager.dispatchViewManagerCommand(this.getTag(), 5, null)
}
public setLightMode = (): void => {
UIManager.dispatchViewManagerCommand(this.getTag(), 6, null)
}
private draw = (): void => {
this.setState({ color: this.state.color })
}
private getTag = (): number => {
return ReactNative.findNodeHandle(this.pencilKit.current)
}
public getDrawing = (callback: SaveCallback): void => {
console.log('[Android] Pencil kit saved called!')
this.onSaveCallback = callback
UIManager.dispatchViewManagerCommand(this.getTag(), 4, null)
}
private onSaveEvent = (event: SyntheticEvent): void => {
console.log('[AndroidPencilKit] on saved event called!', event.nativeEvent)
if (this.onSaveCallback && event && event.nativeEvent && event.nativeEvent.uri)
this.onSaveCallback({ uri: `file://${event.nativeEvent.uri}` })
}
render(): ReactElement {
return (
<RCTPencilKitView
color={processColor(this.state.color)}
strokeWidth={+this.state.strokeWidth}
onSaveEvent={this.onSaveEvent}
style={styles.container}
ref={this.pencilKit}
/>
);
}
}