-
Notifications
You must be signed in to change notification settings - Fork 1
/
App.tsx
155 lines (137 loc) · 4.1 KB
/
App.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import React, {useRef, useState} from 'react';
import {StyleSheet, Text, View, TouchableOpacity} from 'react-native';
import {launchImageLibrary} from 'react-native-image-picker';
import RNFS from 'react-native-fs';
// PinturaEditor component import
import PinturaEditor from '@pqina/react-native-pintura';
// Pintura imports
import {
createMarkupEditorToolStyle,
createMarkupEditorToolStyles,
} from '@pqina/pintura';
function App(): JSX.Element {
const [editorEnabled, setEditorEnabled] = useState(true);
const [editorSource, setEditorSource] = useState<string | undefined>(
undefined,
);
const editorRef = useRef<PinturaEditor>(null);
return (
<View style={styles.container}>
{/* The Pintura Editor component */}
{editorEnabled && (
<PinturaEditor
ref={editorRef}
style={styles.pintura}
styleRules={`
.pintura-editor {
--color-background: 255, 255, 255;
--color-foreground: 0, 0, 0;
}
`}
markupEditorToolStyles={createMarkupEditorToolStyles({
text: createMarkupEditorToolStyle('text', {
fontSize: '10%',
}),
})}
imageCropAspectRatio={1}
src={editorSource}
onLoaderror={err => {
console.log('onLoaderror', err);
}}
onLoad={({size}) => {
console.log('onLoad', size);
}}
onProcess={({dest, imageState}) => {
// dest is output file in dataURI format
console.log('onProcess', imageState, dest);
}}
/>
)}
<View style={styles.buttonRow}>
{/* Example removing and adding the editor */}
<TouchableOpacity
style={styles.buttonStyle}
onPress={() => {
setEditorEnabled(!editorEnabled);
}}>
<Text style={styles.buttonTextStyle}>Toggle</Text>
</TouchableOpacity>
{/* Example running an editor function */}
<TouchableOpacity
style={styles.buttonStyle}
onPress={() => {
if (!editorRef.current) {
return;
}
// Run editor function
editorRef.current.editor.history.undo();
}}>
<Text style={styles.buttonTextStyle}>Undo</Text>
</TouchableOpacity>
{/* Example selecting a library image */}
<TouchableOpacity
style={styles.buttonStyle}
onPress={async () => {
// Use ImagePicker to get a base64 image string
let {assets, didCancel} = await launchImageLibrary({
mediaType: 'mixed',
selectionLimit: 1,
quality: 1,
includeBase64: true,
});
// user cancelled early
if (didCancel || !assets) {
return;
}
// get image reference
const [asset] = assets;
// Somehow the asset size can be 0
if (!asset.uri || asset.width === 0 || asset.height === 0) {
console.error('Failed to load', asset);
return;
}
// video to base64
let base64 = asset.base64;
if (!base64) {
base64 = await RNFS.readFile(asset.uri, 'base64');
}
// send data url to editor
setEditorSource(`data:${asset.type};base64,${base64}`);
}}>
<Text style={styles.buttonTextStyle}>Browse...</Text>
</TouchableOpacity>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#fff',
alignItems: 'center',
justifyContent: 'center',
},
pintura: {
width: '95%',
height: '80%',
borderWidth: 1,
borderColor: '#eee',
},
buttonRow: {
flexDirection: 'row',
marginTop: 20,
},
buttonStyle: {
backgroundColor: '#222',
paddingTop: 5,
paddingBottom: 5,
paddingHorizontal: 15,
borderRadius: 10,
marginHorizontal: 5,
},
buttonTextStyle: {
fontSize: 14,
color: '#fff',
},
});
export default App;