Repository navigation
Expand file tree
/
Copy pathApp.js
More file actions
217 lines (193 loc) · 6.66 KB
/
Copy pathApp.js
File metadata and controls
217 lines (193 loc) · 6.66 KB
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import React, { useRef, useEffect, useState } from "react";
import {
StyleSheet,
Image,
Text,
View,
Platform,
TouchableOpacity,
} from "react-native";
import { useVideoPlayer, VideoView } from "expo-video";
import PinturaEditor from "@pqina/react-native-expo-pintura";
import { useAssets } from "expo-asset";
import * as ImagePicker from "expo-image-picker";
import {
createMarkupEditorToolStyle,
createMarkupEditorToolStyles,
} from "@pqina/pintura";
const isAssetOfType = (asset, types) => {
const extension = `${asset}`.split(".").pop();
if (!extension) {
return false;
}
return types.some((type) => extension.includes(type));
};
const isImage = (asset) => isAssetOfType(asset, ["jpg", "jpeg", "png", "heic"]);
const isVideo = (asset) => isAssetOfType(asset, ["mov", "mp4"]);
export default function App() {
const [assets, error] = useAssets([require("./assets/image.jpeg")]);
const [editorEnabled, setEditorEnabled] = useState(true);
const [editorSource, setEditorSource] = useState(undefined);
const [editorResult, setEditorResult] = useState(undefined);
const editorRef = useRef(null);
const editorResultPlayer = useVideoPlayer(
isVideo(editorResult) ? editorResult : null,
(player) => {
// set player options
},
);
// This requests permission to select media from camera roll
useEffect(() => {
(async () => {
// Not needed on Web
if (Platform.OS === "web") {
return;
}
// All fine when access granted
const { status } =
await ImagePicker.requestMediaLibraryPermissionsAsync();
if (status === "granted") {
return;
}
// Show message if not allowed
alert("Sorry, we need camera roll permissions to make this work!");
})();
}, []);
return (
<View style={styles.container}>
{isImage(editorResult) && (
<Image style={styles.output} source={{ uri: editorResult }} />
)}
{isVideo(editorResult) && (
<VideoView style={styles.output} player={editorResultPlayer} />
)}
{/* The Pintura Editor component */}
{editorEnabled && (
<PinturaEditor
ref={editorRef}
style={styles.editor}
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 uri
console.log("onProcess", dest);
// preview
setEditorResult(dest);
}}
/>
)}
<View style={styles.buttonRow}>
{/* Example removing and adding the editor */}
<TouchableOpacity
style={styles.button}
onPress={() => {
setEditorEnabled(!editorEnabled);
}}
>
<Text style={styles.buttonText}>Toggle</Text>
</TouchableOpacity>
{/* Example updating editor image source */}
<TouchableOpacity
style={styles.button}
onPress={() => {
// load local asset
const [image] = assets;
setEditorSource(image.localUri);
}}
>
<Text style={styles.buttonText}>Test image</Text>
</TouchableOpacity>
{/* Example running an editor function */}
<TouchableOpacity
style={styles.button}
onPress={() => {
// Run editor function
editorRef.current.editor.history.undo();
}}
>
<Text style={styles.buttonText}>Undo</Text>
</TouchableOpacity>
{/* Example selecting a library image */}
<TouchableOpacity
style={styles.button}
onPress={async () => {
// Use ImagePicker to get a base64 image string
const res = await ImagePicker.launchImageLibraryAsync({
mediaTypes: ["images", "videos"],
allowsMultipleSelection: false,
quality: 1,
});
const { canceled, assets } = res;
if (canceled) {
return;
}
// send first selected asset
const [asset] = assets;
setEditorSource(asset.uri);
}}
>
<Text style={styles.buttonText}>Browse...</Text>
</TouchableOpacity>
</View>
</View>
);
}
const styles = StyleSheet.create({
container: {
padding: 15,
paddingTop: 65,
paddingBottom: 25,
gap: 15,
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "space-between",
},
output: {
width: "100%",
height: 100,
objectFit: "contain",
borderWidth: 1,
borderColor: "#eee",
borderRadius: 15,
},
editor: {
width: "100%",
flex: 1,
borderWidth: 1,
borderColor: "#eee",
borderRadius: 15,
overflow: "hidden",
},
buttonRow: {
flexDirection: "row",
gap: 1,
borderRadius: 15,
overflow: "hidden",
},
button: {
backgroundColor: "#222",
paddingTop: 5,
paddingBottom: 5,
paddingHorizontal: 15,
},
buttonText: {
fontSize: 14,
color: "#fff",
},
});