panelui-native 0.110.0 → 0.112.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/module/components/avatar-cropper/avatar-cropper-math.js +68 -28
- package/lib/module/components/avatar-cropper/avatar-cropper-math.js.map +1 -1
- package/lib/module/components/avatar-cropper/index.js +190 -24
- package/lib/module/components/avatar-cropper/index.js.map +1 -1
- package/lib/module/components/file-upload/file-upload-core.js +317 -0
- package/lib/module/components/file-upload/file-upload-core.js.map +1 -0
- package/lib/module/components/file-upload/index.js +1214 -0
- package/lib/module/components/file-upload/index.js.map +1 -0
- package/lib/module/icons/index.js +10 -0
- package/lib/module/icons/index.js.map +1 -1
- package/lib/module/index.js +2 -1
- package/lib/module/index.js.map +1 -1
- package/lib/typescript/src/components/avatar-cropper/avatar-cropper-math.d.ts +32 -10
- package/lib/typescript/src/components/avatar-cropper/avatar-cropper-math.d.ts.map +1 -1
- package/lib/typescript/src/components/avatar-cropper/index.d.ts +30 -3
- package/lib/typescript/src/components/avatar-cropper/index.d.ts.map +1 -1
- package/lib/typescript/src/components/file-upload/file-upload-core.d.ts +136 -0
- package/lib/typescript/src/components/file-upload/file-upload-core.d.ts.map +1 -0
- package/lib/typescript/src/components/file-upload/index.d.ts +265 -0
- package/lib/typescript/src/components/file-upload/index.d.ts.map +1 -0
- package/lib/typescript/src/icons/index.d.ts +2 -0
- package/lib/typescript/src/icons/index.d.ts.map +1 -1
- package/lib/typescript/src/index.d.ts +2 -1
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/package.json +5 -1
- package/src/components/avatar-cropper/avatar-cropper-math.ts +78 -42
- package/src/components/avatar-cropper/index.tsx +251 -23
- package/src/components/file-upload/file-upload-core.ts +359 -0
- package/src/components/file-upload/index.tsx +1435 -0
- package/src/icons/index.tsx +4 -0
- package/src/index.ts +24 -0
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "panelui-native",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.112.0",
|
|
4
4
|
"description": "High-performance React Native UI components for Expo. Semantic design tokens, powered by Uniwind (Tailwind v4) and Reanimated.",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
6
|
"module": "./lib/module/index.js",
|
|
@@ -142,6 +142,7 @@
|
|
|
142
142
|
"@react-native-masked-view/masked-view": ">=0.3.0",
|
|
143
143
|
"expo-blur": ">=13.0.0",
|
|
144
144
|
"expo-clipboard": ">=7.0.0",
|
|
145
|
+
"expo-document-picker": ">=13.0.0",
|
|
145
146
|
"expo-file-system": ">=17.0.0",
|
|
146
147
|
"expo-glass-effect": ">=57.0.0",
|
|
147
148
|
"expo-haptics": ">=13.0.0",
|
|
@@ -202,6 +203,9 @@
|
|
|
202
203
|
"expo-clipboard": {
|
|
203
204
|
"optional": true
|
|
204
205
|
},
|
|
206
|
+
"expo-document-picker": {
|
|
207
|
+
"optional": true
|
|
208
|
+
},
|
|
205
209
|
"expo-file-system": {
|
|
206
210
|
"optional": true
|
|
207
211
|
},
|
|
@@ -4,8 +4,9 @@
|
|
|
4
4
|
* The mask stays fixed in the middle of the canvas and the photo moves under
|
|
5
5
|
* it, so everything here is one question asked different ways: where is the
|
|
6
6
|
* mask, measured in the photo's own frame? Clamping asks it to keep the mask
|
|
7
|
-
* on the photo,
|
|
8
|
-
*
|
|
7
|
+
* on the photo — or, when the photo may be smaller than the mask, the photo
|
|
8
|
+
* inside the mask — and the export asks it to find the same square in the
|
|
9
|
+
* photo's pixels.
|
|
9
10
|
*
|
|
10
11
|
* The photo is drawn at a base size whose short side equals the mask, then
|
|
11
12
|
* moved by `x`/`y`, turned by `rotation` and scaled by `scale` about its own
|
|
@@ -106,12 +107,31 @@ export function maskReach(shape: CropShape, mask: number, rotation: number): num
|
|
|
106
107
|
return half * (Math.abs(cos) + Math.abs(sin));
|
|
107
108
|
}
|
|
108
109
|
|
|
109
|
-
/**
|
|
110
|
-
|
|
110
|
+
/**
|
|
111
|
+
* The limits a crop is held to.
|
|
112
|
+
*
|
|
113
|
+
* With `cover` on, the photo always fills the mask. With it off, the photo may
|
|
114
|
+
* also be smaller than the mask, down to `minZoom` — its long side as a
|
|
115
|
+
* fraction of the mask's — and a smaller photo is kept inside the mask rather
|
|
116
|
+
* than on it.
|
|
117
|
+
*/
|
|
118
|
+
export interface CropBounds {
|
|
119
|
+
shape: CropShape;
|
|
120
|
+
maxZoom: number;
|
|
121
|
+
cover: boolean;
|
|
122
|
+
minZoom: number;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** The smallest scale the photo may settle at. */
|
|
126
|
+
export function minScale(bounds: CropBounds, frame: CropFrame, rotation: number): number {
|
|
111
127
|
'worklet';
|
|
112
128
|
// The base size's short side is the mask, so covering the reach on the
|
|
113
129
|
// short side covers it on the long one too.
|
|
114
|
-
|
|
130
|
+
const covering = maskReach(bounds.shape, 2, rotation);
|
|
131
|
+
if (bounds.cover) return covering;
|
|
132
|
+
const longSide = Math.max(frame.image.width, frame.image.height);
|
|
133
|
+
if (longSide <= 0) return covering;
|
|
134
|
+
return Math.min(covering, (bounds.minZoom * frame.mask) / longSide);
|
|
115
135
|
}
|
|
116
136
|
|
|
117
137
|
/** Screen offset → the same offset along the photo's axes, and back. */
|
|
@@ -128,26 +148,31 @@ function fromImageAxes(u: number, v: number, rotation: number): { x: number; y:
|
|
|
128
148
|
}
|
|
129
149
|
|
|
130
150
|
/**
|
|
131
|
-
* How far the photo can move along each of its own axes
|
|
132
|
-
*
|
|
151
|
+
* How far the photo can move along each of its own axes.
|
|
152
|
+
*
|
|
153
|
+
* On an axis where the photo is larger than the mask's reach, the mask has to
|
|
154
|
+
* stay on the photo. On one where it is smaller — only possible with `cover`
|
|
155
|
+
* off — the photo has to stay inside the reach instead. Both are the gap
|
|
156
|
+
* between the two half-lengths, which is why one expression covers both.
|
|
133
157
|
*/
|
|
134
|
-
function travel(state: CropState,
|
|
158
|
+
function travel(state: CropState, bounds: CropBounds, frame: CropFrame): { u: number; v: number } {
|
|
135
159
|
'worklet';
|
|
136
|
-
const reach = maskReach(shape, frame.mask, state.rotation);
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
160
|
+
const reach = maskReach(bounds.shape, frame.mask, state.rotation);
|
|
161
|
+
const u = (state.scale * frame.image.width) / 2 - reach;
|
|
162
|
+
const v = (state.scale * frame.image.height) / 2 - reach;
|
|
163
|
+
return bounds.cover
|
|
164
|
+
? { u: Math.max(0, u), v: Math.max(0, v) }
|
|
165
|
+
: { u: Math.abs(u), v: Math.abs(v) };
|
|
141
166
|
}
|
|
142
167
|
|
|
143
|
-
/** The nearest translation
|
|
168
|
+
/** The nearest translation inside the bounds. */
|
|
144
169
|
export function clampTranslation(
|
|
145
170
|
state: CropState,
|
|
146
|
-
|
|
171
|
+
bounds: CropBounds,
|
|
147
172
|
frame: CropFrame
|
|
148
173
|
): { x: number; y: number } {
|
|
149
174
|
'worklet';
|
|
150
|
-
const limit = travel(state,
|
|
175
|
+
const limit = travel(state, bounds, frame);
|
|
151
176
|
const along = toImageAxes(state.x, state.y, state.rotation);
|
|
152
177
|
return fromImageAxes(
|
|
153
178
|
clamp(along.u, -limit.u, limit.u),
|
|
@@ -156,18 +181,28 @@ export function clampTranslation(
|
|
|
156
181
|
);
|
|
157
182
|
}
|
|
158
183
|
|
|
159
|
-
/** The state a gesture settles into: in range, and
|
|
160
|
-
export function settle(
|
|
161
|
-
state: CropState,
|
|
162
|
-
shape: CropShape,
|
|
163
|
-
frame: CropFrame,
|
|
164
|
-
maxZoom: number
|
|
165
|
-
): CropState {
|
|
184
|
+
/** The state a gesture settles into: in range, and inside the bounds. */
|
|
185
|
+
export function settle(state: CropState, bounds: CropBounds, frame: CropFrame): CropState {
|
|
166
186
|
'worklet';
|
|
167
|
-
const lowest = minScale(
|
|
168
|
-
const scale = clamp(state.scale, lowest, Math.max(lowest, maxZoom));
|
|
187
|
+
const lowest = minScale(bounds, frame, state.rotation);
|
|
188
|
+
const scale = clamp(state.scale, lowest, Math.max(lowest, bounds.maxZoom));
|
|
169
189
|
const next = { ...state, scale };
|
|
170
|
-
return { ...next, ...clampTranslation(next,
|
|
190
|
+
return { ...next, ...clampTranslation(next, bounds, frame) };
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
/**
|
|
194
|
+
* True when every point of the mask is on the photo — when a crop needs no
|
|
195
|
+
* background at all. Always true with `cover` on, once settled.
|
|
196
|
+
*/
|
|
197
|
+
export function coversMask(state: CropState, shape: CropShape, frame: CropFrame): boolean {
|
|
198
|
+
'worklet';
|
|
199
|
+
const reach = maskReach(shape, frame.mask, state.rotation);
|
|
200
|
+
const along = toImageAxes(state.x, state.y, state.rotation);
|
|
201
|
+
const slack = 0.5;
|
|
202
|
+
return (
|
|
203
|
+
Math.abs(along.u) <= (state.scale * frame.image.width) / 2 - reach + slack &&
|
|
204
|
+
Math.abs(along.v) <= (state.scale * frame.image.height) / 2 - reach + slack
|
|
205
|
+
);
|
|
171
206
|
}
|
|
172
207
|
|
|
173
208
|
/** An overshoot past a bound, compressed so it gives but never runs away. */
|
|
@@ -184,21 +219,16 @@ export function rubberBand(overshoot: number, dimension: number): number {
|
|
|
184
219
|
* past a bound rubber-banded back towards it. At rest the raw state is already
|
|
185
220
|
* settled and this returns it unchanged.
|
|
186
221
|
*/
|
|
187
|
-
export function present(
|
|
188
|
-
state: CropState,
|
|
189
|
-
shape: CropShape,
|
|
190
|
-
frame: CropFrame,
|
|
191
|
-
maxZoom: number
|
|
192
|
-
): CropState {
|
|
222
|
+
export function present(state: CropState, bounds: CropBounds, frame: CropFrame): CropState {
|
|
193
223
|
'worklet';
|
|
194
|
-
const lowest = minScale(
|
|
195
|
-
const highest = Math.max(lowest, maxZoom);
|
|
224
|
+
const lowest = minScale(bounds, frame, state.rotation);
|
|
225
|
+
const highest = Math.max(lowest, bounds.maxZoom);
|
|
196
226
|
let scale = state.scale;
|
|
197
227
|
if (scale < lowest) scale = lowest - rubberBand(lowest - scale, lowest / 2);
|
|
198
228
|
else if (scale > highest) scale = highest + rubberBand(scale - highest, highest / 2);
|
|
199
229
|
|
|
200
230
|
const next = { ...state, scale };
|
|
201
|
-
const limit = travel(next,
|
|
231
|
+
const limit = travel(next, bounds, frame);
|
|
202
232
|
const along = toImageAxes(state.x, state.y, state.rotation);
|
|
203
233
|
const u = clamp(along.u, -limit.u, limit.u);
|
|
204
234
|
const v = clamp(along.v, -limit.v, limit.v);
|
|
@@ -257,7 +287,13 @@ export function nextQuarterTurn(rotation: number, direction: 1 | -1): number {
|
|
|
257
287
|
* after the turn it lands at `−(x, y) / scale` — the rotation cancels, and the
|
|
258
288
|
* crop is the mask's offset scaled into pixels.
|
|
259
289
|
*/
|
|
260
|
-
export function sourceCrop(
|
|
290
|
+
export function sourceCrop(
|
|
291
|
+
state: CropState,
|
|
292
|
+
frame: CropFrame,
|
|
293
|
+
source: Size,
|
|
294
|
+
/** Keep the rectangle on the turned image. Off, it can reach past the edges. */
|
|
295
|
+
onImage = true
|
|
296
|
+
): SourceCrop {
|
|
261
297
|
'worklet';
|
|
262
298
|
const rotation = normalizeDegrees(state.rotation);
|
|
263
299
|
const { cos, sin } = trig(rotation);
|
|
@@ -268,15 +304,15 @@ export function sourceCrop(state: CropState, frame: CropFrame, source: Size): So
|
|
|
268
304
|
};
|
|
269
305
|
// Screen points per source pixel, at the current scale.
|
|
270
306
|
const pointsPerPixel = (frame.image.width / source.width) * state.scale;
|
|
271
|
-
const
|
|
272
|
-
|
|
273
|
-
Math.min(Math.floor(frame.mask / pointsPerPixel), canvas.width, canvas.height)
|
|
274
|
-
);
|
|
307
|
+
const full = Math.max(1, Math.floor(frame.mask / pointsPerPixel));
|
|
308
|
+
const side = onImage ? Math.max(1, Math.min(full, canvas.width, canvas.height)) : full;
|
|
275
309
|
const centreX = canvas.width / 2 - state.x / pointsPerPixel;
|
|
276
310
|
const centreY = canvas.height / 2 - state.y / pointsPerPixel;
|
|
311
|
+
const originX = Math.round(centreX - side / 2);
|
|
312
|
+
const originY = Math.round(centreY - side / 2);
|
|
277
313
|
return {
|
|
278
|
-
originX: clamp(
|
|
279
|
-
originY: clamp(
|
|
314
|
+
originX: onImage ? clamp(originX, 0, canvas.width - side) : originX,
|
|
315
|
+
originY: onImage ? clamp(originY, 0, canvas.height - side) : originY,
|
|
280
316
|
width: side,
|
|
281
317
|
height: side,
|
|
282
318
|
rotation,
|
|
@@ -51,6 +51,7 @@ import {
|
|
|
51
51
|
useEffect,
|
|
52
52
|
useImperativeHandle,
|
|
53
53
|
useMemo,
|
|
54
|
+
useRef,
|
|
54
55
|
useState,
|
|
55
56
|
type ReactNode,
|
|
56
57
|
type Ref,
|
|
@@ -81,6 +82,7 @@ import { Button, type ButtonProps } from '../button';
|
|
|
81
82
|
import {
|
|
82
83
|
IDENTITY,
|
|
83
84
|
baseSize,
|
|
85
|
+
coversMask,
|
|
84
86
|
maskPath,
|
|
85
87
|
nextQuarterTurn,
|
|
86
88
|
normalizeDegrees,
|
|
@@ -89,6 +91,7 @@ import {
|
|
|
89
91
|
settle,
|
|
90
92
|
sourceCrop,
|
|
91
93
|
zoomAbout,
|
|
94
|
+
type CropBounds,
|
|
92
95
|
type CropFrame,
|
|
93
96
|
type CropShape,
|
|
94
97
|
type CropState,
|
|
@@ -165,6 +168,17 @@ function loadPicker(): PickerModule | null {
|
|
|
165
168
|
}
|
|
166
169
|
}
|
|
167
170
|
|
|
171
|
+
/** `react-native-view-shot`'s capture function, or null when it is not installed. */
|
|
172
|
+
function loadCapture(): ((view: unknown, options: unknown) => Promise<string>) | null {
|
|
173
|
+
try {
|
|
174
|
+
// eslint-disable-next-line @typescript-eslint/no-require-imports
|
|
175
|
+
const mod = require('react-native-view-shot');
|
|
176
|
+
return (mod?.captureRef as (view: unknown, options: unknown) => Promise<string>) ?? null;
|
|
177
|
+
} catch {
|
|
178
|
+
return null;
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
|
|
168
182
|
/** True when `crop()` can write an image, because `expo-image-manipulator` is installed. */
|
|
169
183
|
export const hasAvatarCropperExport = loadManipulator()?.ImageManipulator != null;
|
|
170
184
|
|
|
@@ -237,7 +251,11 @@ export interface AvatarCropOptions {
|
|
|
237
251
|
* pixels than this is written at its own size rather than scaled up.
|
|
238
252
|
*/
|
|
239
253
|
size?: number;
|
|
240
|
-
/**
|
|
254
|
+
/**
|
|
255
|
+
* Default `'jpeg'`, or `'png'` when `background` is `'transparent'` so the
|
|
256
|
+
* transparency survives. A crop with a background is written as PNG or
|
|
257
|
+
* JPEG, so `'webp'` is written as PNG there.
|
|
258
|
+
*/
|
|
241
259
|
format?: 'jpeg' | 'png' | 'webp';
|
|
242
260
|
/** Quality, 0–1, for JPEG and WebP. Default 0.9. */
|
|
243
261
|
compress?: number;
|
|
@@ -249,13 +267,16 @@ export interface AvatarCropperHandle {
|
|
|
249
267
|
* every shape; draw it in a round `Avatar` to show it as a circle.
|
|
250
268
|
*
|
|
251
269
|
* Needs the optional `expo-image-manipulator`, and throws if it is not
|
|
252
|
-
* installed — use `getCrop()` then.
|
|
270
|
+
* installed — use `getCrop()` then. A crop with part of `background` in it
|
|
271
|
+
* (only possible with `cover` off) is drawn and captured instead, and needs
|
|
272
|
+
* the optional `react-native-view-shot`.
|
|
253
273
|
*/
|
|
254
274
|
crop(options?: AvatarCropOptions): Promise<AvatarCropResult>;
|
|
255
275
|
/**
|
|
256
276
|
* The rectangle the mask covers, in the source's pixels, after turning the
|
|
257
277
|
* source by `rotation` degrees clockwise. Null until the photo has been
|
|
258
|
-
* measured and laid out.
|
|
278
|
+
* measured and laid out. With `cover` off it can reach past the turned
|
|
279
|
+
* image's edges; whatever lies outside is `background`.
|
|
259
280
|
*/
|
|
260
281
|
getCrop(): SourceCrop | null;
|
|
261
282
|
/** Turn a quarter clockwise, or a quarter back with `-1`. */
|
|
@@ -274,6 +295,26 @@ export interface AvatarCropperProps extends ViewProps {
|
|
|
274
295
|
shape?: CropShape;
|
|
275
296
|
/** How far the photo can be zoomed past filling the mask. Default 5. */
|
|
276
297
|
maxZoom?: number;
|
|
298
|
+
/**
|
|
299
|
+
* Keep the photo covering the whole outline. Default true: let go past an
|
|
300
|
+
* edge, or zoom out too far, and it springs back to fill it.
|
|
301
|
+
*
|
|
302
|
+
* Set it to false to let the photo be zoomed out smaller than the outline,
|
|
303
|
+
* down to `minZoom`. The space around it is filled with `background`, on
|
|
304
|
+
* screen and in the image `crop()` writes.
|
|
305
|
+
*/
|
|
306
|
+
cover?: boolean;
|
|
307
|
+
/**
|
|
308
|
+
* With `cover` off, how small the photo can get: its long side as a
|
|
309
|
+
* fraction of the outline's width. Default 0.5.
|
|
310
|
+
*/
|
|
311
|
+
minZoom?: number;
|
|
312
|
+
/**
|
|
313
|
+
* What fills the outline around a photo smaller than it, with `cover` off.
|
|
314
|
+
* Any colour React Native accepts, or `'transparent'` — written out as PNG
|
|
315
|
+
* so it stays transparent. Default: the theme's background colour.
|
|
316
|
+
*/
|
|
317
|
+
background?: string;
|
|
277
318
|
/** Space between the mask and the canvas's shorter side, in points. Default 24. */
|
|
278
319
|
inset?: number;
|
|
279
320
|
className?: string;
|
|
@@ -283,7 +324,11 @@ export interface AvatarCropperProps extends ViewProps {
|
|
|
283
324
|
|
|
284
325
|
interface CropperContextValue {
|
|
285
326
|
shape: CropShape;
|
|
286
|
-
|
|
327
|
+
bounds: CropBounds;
|
|
328
|
+
/** The resolved fill around a photo smaller than the outline. */
|
|
329
|
+
fill: string;
|
|
330
|
+
/** A crop being drawn for capture, while `crop()` is waiting on it. */
|
|
331
|
+
stage: ExportJob | null;
|
|
287
332
|
inset: number;
|
|
288
333
|
source: AvatarCropperSource;
|
|
289
334
|
/** The source's upright size, once known. */
|
|
@@ -322,6 +367,9 @@ function AvatarCropperRoot(
|
|
|
322
367
|
source,
|
|
323
368
|
shape = 'circle',
|
|
324
369
|
maxZoom = 5,
|
|
370
|
+
cover = true,
|
|
371
|
+
minZoom = 0.5,
|
|
372
|
+
background,
|
|
325
373
|
inset = 24,
|
|
326
374
|
className,
|
|
327
375
|
children,
|
|
@@ -336,6 +384,14 @@ function AvatarCropperRoot(
|
|
|
336
384
|
const [canvas, setCanvasState] = useState<Size | null>(null);
|
|
337
385
|
const [measured, setMeasured] = useState<Size | null>(null);
|
|
338
386
|
const [summary, setSummary] = useState(describe(IDENTITY));
|
|
387
|
+
const [stage, setStage] = useState<ExportJob | null>(null);
|
|
388
|
+
const backgroundToken = useCSSVariable('--color-background');
|
|
389
|
+
const fill =
|
|
390
|
+
background ?? (typeof backgroundToken === 'string' ? backgroundToken : '#ffffff');
|
|
391
|
+
const bounds = useMemo<CropBounds>(
|
|
392
|
+
() => ({ shape, maxZoom, cover, minZoom }),
|
|
393
|
+
[shape, maxZoom, cover, minZoom]
|
|
394
|
+
);
|
|
339
395
|
|
|
340
396
|
const { uri, width, height } = source;
|
|
341
397
|
const sourceSize =
|
|
@@ -388,7 +444,7 @@ function AvatarCropperRoot(
|
|
|
388
444
|
const animateTo = useCallback(
|
|
389
445
|
(target: CropState, timed: boolean) => {
|
|
390
446
|
if (!frame) return;
|
|
391
|
-
const next = settle(target,
|
|
447
|
+
const next = settle(target, bounds, frame);
|
|
392
448
|
const run = (value: number) =>
|
|
393
449
|
timed ? withTiming(value, { duration: TURN_DURATION }) : withSpring(value, SETTLE_SPRING);
|
|
394
450
|
x.value = run(next.x);
|
|
@@ -397,7 +453,7 @@ function AvatarCropperRoot(
|
|
|
397
453
|
rotation.value = run(next.rotation);
|
|
398
454
|
setSummary(describe(next));
|
|
399
455
|
},
|
|
400
|
-
[frame,
|
|
456
|
+
[frame, bounds, x, y, scale, rotation]
|
|
401
457
|
);
|
|
402
458
|
|
|
403
459
|
const settleNow = useCallback(() => animateTo(read(), false), [animateTo, read]);
|
|
@@ -424,10 +480,16 @@ function AvatarCropperRoot(
|
|
|
424
480
|
[animateTo, read]
|
|
425
481
|
);
|
|
426
482
|
|
|
483
|
+
const settled = useCallback(
|
|
484
|
+
(): CropState | null => (frame ? settle(read(), bounds, frame) : null),
|
|
485
|
+
[frame, bounds, read]
|
|
486
|
+
);
|
|
487
|
+
|
|
427
488
|
const getCrop = useCallback((): SourceCrop | null => {
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
489
|
+
const state = settled();
|
|
490
|
+
if (!state || !frame || !sourceSize) return null;
|
|
491
|
+
return sourceCrop(state, frame, sourceSize, coversMask(state, shape, frame));
|
|
492
|
+
}, [settled, frame, sourceSize, shape]);
|
|
431
493
|
|
|
432
494
|
useImperativeHandle(
|
|
433
495
|
ref,
|
|
@@ -435,7 +497,46 @@ function AvatarCropperRoot(
|
|
|
435
497
|
getCrop,
|
|
436
498
|
rotate,
|
|
437
499
|
reset,
|
|
438
|
-
async crop({ size = 1024, format
|
|
500
|
+
async crop({ size = 1024, format, compress = 0.9 } = {}) {
|
|
501
|
+
const state = settled();
|
|
502
|
+
const rect = getCrop();
|
|
503
|
+
if (!state || !frame || !rect || !sourceSize) {
|
|
504
|
+
throw new Error('AvatarCropper.crop: the photo has not been measured and laid out yet.');
|
|
505
|
+
}
|
|
506
|
+
const transparent = fill === 'transparent';
|
|
507
|
+
const output = format ?? (transparent ? 'png' : 'jpeg');
|
|
508
|
+
const side = Math.min(size, rect.width);
|
|
509
|
+
|
|
510
|
+
if (!coversMask(state, shape, frame)) {
|
|
511
|
+
// Part of the outline is background, which the manipulator cannot
|
|
512
|
+
// paint on a phone. Draw the crop as the screen does, off to one
|
|
513
|
+
// side, and capture that instead.
|
|
514
|
+
const capture = loadCapture();
|
|
515
|
+
if (!capture) {
|
|
516
|
+
throw new Error(
|
|
517
|
+
'AvatarCropper.crop: a photo smaller than the outline is written with ' +
|
|
518
|
+
'react-native-view-shot. Install it with: npx expo install react-native-view-shot'
|
|
519
|
+
);
|
|
520
|
+
}
|
|
521
|
+
return new Promise<AvatarCropResult>((resolve, reject) => {
|
|
522
|
+
setStage({
|
|
523
|
+
state,
|
|
524
|
+
side,
|
|
525
|
+
format: output === 'jpeg' ? 'jpg' : 'png',
|
|
526
|
+
compress,
|
|
527
|
+
capture,
|
|
528
|
+
resolve: (result) => {
|
|
529
|
+
setStage(null);
|
|
530
|
+
resolve(result);
|
|
531
|
+
},
|
|
532
|
+
reject: (error) => {
|
|
533
|
+
setStage(null);
|
|
534
|
+
reject(error);
|
|
535
|
+
},
|
|
536
|
+
});
|
|
537
|
+
});
|
|
538
|
+
}
|
|
539
|
+
|
|
439
540
|
const manipulator = loadManipulator()?.ImageManipulator;
|
|
440
541
|
if (!manipulator) {
|
|
441
542
|
throw new Error(
|
|
@@ -443,10 +544,6 @@ function AvatarCropperRoot(
|
|
|
443
544
|
'npx expo install expo-image-manipulator — or use getCrop() to crop elsewhere.'
|
|
444
545
|
);
|
|
445
546
|
}
|
|
446
|
-
const rect = getCrop();
|
|
447
|
-
if (!rect || !sourceSize) {
|
|
448
|
-
throw new Error('AvatarCropper.crop: the photo has not been measured and laid out yet.');
|
|
449
|
-
}
|
|
450
547
|
// Redraw upright first. The turn and the cut both read raw pixels,
|
|
451
548
|
// and a camera photo often keeps its orientation in a flag instead.
|
|
452
549
|
const context = manipulator.manipulate(uri).resize({ width: sourceSize.width });
|
|
@@ -457,17 +554,19 @@ function AvatarCropperRoot(
|
|
|
457
554
|
width: rect.width,
|
|
458
555
|
height: rect.height,
|
|
459
556
|
});
|
|
460
|
-
if (
|
|
557
|
+
if (side < rect.width) context.resize({ width: side, height: side });
|
|
461
558
|
const rendered = await context.renderAsync();
|
|
462
|
-
return rendered.saveAsync({ format, compress });
|
|
559
|
+
return rendered.saveAsync({ format: output, compress });
|
|
463
560
|
},
|
|
464
561
|
}),
|
|
465
|
-
[getCrop, rotate, reset, sourceSize, uri]
|
|
562
|
+
[getCrop, settled, rotate, reset, sourceSize, uri, frame, shape, fill]
|
|
466
563
|
);
|
|
467
564
|
|
|
468
565
|
const value: CropperContextValue = {
|
|
469
566
|
shape,
|
|
470
|
-
|
|
567
|
+
bounds,
|
|
568
|
+
fill,
|
|
569
|
+
stage,
|
|
471
570
|
inset,
|
|
472
571
|
source,
|
|
473
572
|
sourceSize,
|
|
@@ -499,6 +598,106 @@ const AvatarCropperForwarded = forwardRef<AvatarCropperHandle, AvatarCropperProp
|
|
|
499
598
|
);
|
|
500
599
|
AvatarCropperForwarded.displayName = 'AvatarCropper';
|
|
501
600
|
|
|
601
|
+
interface ExportJob {
|
|
602
|
+
state: CropState;
|
|
603
|
+
/** Side of the image to write, in pixels. */
|
|
604
|
+
side: number;
|
|
605
|
+
format: 'png' | 'jpg';
|
|
606
|
+
compress: number;
|
|
607
|
+
capture: (view: unknown, options: unknown) => Promise<string>;
|
|
608
|
+
resolve: (result: AvatarCropResult) => void;
|
|
609
|
+
reject: (error: unknown) => void;
|
|
610
|
+
}
|
|
611
|
+
|
|
612
|
+
/**
|
|
613
|
+
* The crop drawn on its own, for capture: a square of the background with the
|
|
614
|
+
* photo placed in it exactly as the canvas places it under the outline.
|
|
615
|
+
*
|
|
616
|
+
* It sits directly under the outline, at the outline's size, beneath the
|
|
617
|
+
* canvas's own drawing of the same thing — so it is never seen, whatever
|
|
618
|
+
* surrounds the cropper. The capture reads the view's own drawing, not what is
|
|
619
|
+
* on screen above it, and scales it to the size asked for.
|
|
620
|
+
*/
|
|
621
|
+
function ExportStage({
|
|
622
|
+
job,
|
|
623
|
+
frame,
|
|
624
|
+
uri,
|
|
625
|
+
fill,
|
|
626
|
+
left,
|
|
627
|
+
top,
|
|
628
|
+
}: {
|
|
629
|
+
job: ExportJob;
|
|
630
|
+
frame: CropFrame;
|
|
631
|
+
uri: string;
|
|
632
|
+
fill: string;
|
|
633
|
+
left: number;
|
|
634
|
+
top: number;
|
|
635
|
+
}) {
|
|
636
|
+
const ref = useRef<View>(null);
|
|
637
|
+
const started = useRef(false);
|
|
638
|
+
const side = frame.mask;
|
|
639
|
+
const { width, height } = frame.image;
|
|
640
|
+
const { state } = job;
|
|
641
|
+
|
|
642
|
+
const capture = () => {
|
|
643
|
+
if (started.current) return;
|
|
644
|
+
started.current = true;
|
|
645
|
+
job
|
|
646
|
+
.capture(ref.current, {
|
|
647
|
+
format: job.format,
|
|
648
|
+
quality: job.compress,
|
|
649
|
+
result: 'tmpfile',
|
|
650
|
+
width: job.side,
|
|
651
|
+
height: job.side,
|
|
652
|
+
})
|
|
653
|
+
.then((uri) => job.resolve({ uri, width: job.side, height: job.side }))
|
|
654
|
+
.catch(job.reject);
|
|
655
|
+
};
|
|
656
|
+
|
|
657
|
+
return (
|
|
658
|
+
<View
|
|
659
|
+
ref={ref}
|
|
660
|
+
collapsable={false}
|
|
661
|
+
pointerEvents="none"
|
|
662
|
+
importantForAccessibility="no-hide-descendants"
|
|
663
|
+
accessibilityElementsHidden
|
|
664
|
+
style={{
|
|
665
|
+
position: 'absolute',
|
|
666
|
+
left,
|
|
667
|
+
top,
|
|
668
|
+
width: side,
|
|
669
|
+
height: side,
|
|
670
|
+
overflow: 'hidden',
|
|
671
|
+
backgroundColor: fill,
|
|
672
|
+
}}
|
|
673
|
+
>
|
|
674
|
+
<View
|
|
675
|
+
style={{
|
|
676
|
+
position: 'absolute',
|
|
677
|
+
left: (side - width) / 2,
|
|
678
|
+
top: (side - height) / 2,
|
|
679
|
+
width,
|
|
680
|
+
height,
|
|
681
|
+
transform: [
|
|
682
|
+
{ translateX: state.x },
|
|
683
|
+
{ translateY: state.y },
|
|
684
|
+
{ rotate: `${state.rotation}deg` },
|
|
685
|
+
{ scale: state.scale },
|
|
686
|
+
],
|
|
687
|
+
}}
|
|
688
|
+
>
|
|
689
|
+
<Image
|
|
690
|
+
source={{ uri }}
|
|
691
|
+
style={{ width, height }}
|
|
692
|
+
resizeMode="cover"
|
|
693
|
+
onLoad={() => requestAnimationFrame(capture)}
|
|
694
|
+
onError={() => job.reject(new Error('AvatarCropper.crop: the photo failed to load.'))}
|
|
695
|
+
/>
|
|
696
|
+
</View>
|
|
697
|
+
</View>
|
|
698
|
+
);
|
|
699
|
+
}
|
|
700
|
+
|
|
502
701
|
// ---------------------------------------------------------------------------
|
|
503
702
|
// Canvas
|
|
504
703
|
// ---------------------------------------------------------------------------
|
|
@@ -522,7 +721,9 @@ function AvatarCropperCanvas({
|
|
|
522
721
|
}: AvatarCropperCanvasProps) {
|
|
523
722
|
const {
|
|
524
723
|
shape,
|
|
525
|
-
|
|
724
|
+
bounds,
|
|
725
|
+
fill,
|
|
726
|
+
stage,
|
|
526
727
|
source,
|
|
527
728
|
frame,
|
|
528
729
|
canvas,
|
|
@@ -552,7 +753,7 @@ function AvatarCropperCanvas({
|
|
|
552
753
|
|
|
553
754
|
const shown = useDerivedValue((): CropState => {
|
|
554
755
|
const raw = { x: x.value, y: y.value, scale: scale.value, rotation: rotation.value };
|
|
555
|
-
return frame ? present(raw,
|
|
756
|
+
return frame ? present(raw, bounds, frame) : raw;
|
|
556
757
|
});
|
|
557
758
|
|
|
558
759
|
const imageStyle = useAnimatedStyle(() => ({
|
|
@@ -584,8 +785,12 @@ function AvatarCropperCanvas({
|
|
|
584
785
|
const bound = (state: CropState) => {
|
|
585
786
|
'worklet';
|
|
586
787
|
if (!frame) return state;
|
|
587
|
-
|
|
588
|
-
const
|
|
788
|
+
// A guard against running away, not a bound: `present` does the bounding.
|
|
789
|
+
const lowest = 0.05;
|
|
790
|
+
const next = {
|
|
791
|
+
...state,
|
|
792
|
+
scale: Math.min(Math.max(state.scale, lowest), bounds.maxZoom * OVERSHOOT),
|
|
793
|
+
};
|
|
589
794
|
const limit = Math.max(image.width, image.height) * next.scale;
|
|
590
795
|
next.x = Math.min(Math.max(next.x, -limit), limit);
|
|
591
796
|
next.y = Math.min(Math.max(next.y, -limit), limit);
|
|
@@ -656,7 +861,7 @@ function AvatarCropperCanvas({
|
|
|
656
861
|
});
|
|
657
862
|
|
|
658
863
|
return Gesture.Simultaneous(pan, pinch, turn);
|
|
659
|
-
}, [active, x, y, scale, rotation, settleNow, frame, image.width, image.height,
|
|
864
|
+
}, [active, x, y, scale, rotation, settleNow, frame, image.width, image.height, bounds, W, H]);
|
|
660
865
|
|
|
661
866
|
const onAccessibilityAction = useCallback(
|
|
662
867
|
(event: AccessibilityActionEvent) => {
|
|
@@ -704,6 +909,29 @@ function AvatarCropperCanvas({
|
|
|
704
909
|
onAccessibilityAction={onAccessibilityAction}
|
|
705
910
|
{...props}
|
|
706
911
|
>
|
|
912
|
+
{stage && frame && W > 0 ? (
|
|
913
|
+
<ExportStage
|
|
914
|
+
job={stage}
|
|
915
|
+
frame={frame}
|
|
916
|
+
uri={source.uri}
|
|
917
|
+
fill={fill}
|
|
918
|
+
left={(W - mask) / 2}
|
|
919
|
+
top={(H - mask) / 2}
|
|
920
|
+
/>
|
|
921
|
+
) : null}
|
|
922
|
+
{!bounds.cover && frame && W > 0 && fill !== 'transparent' ? (
|
|
923
|
+
<View
|
|
924
|
+
pointerEvents="none"
|
|
925
|
+
style={{
|
|
926
|
+
position: 'absolute',
|
|
927
|
+
left: (W - mask) / 2,
|
|
928
|
+
top: (H - mask) / 2,
|
|
929
|
+
width: mask,
|
|
930
|
+
height: mask,
|
|
931
|
+
backgroundColor: fill,
|
|
932
|
+
}}
|
|
933
|
+
/>
|
|
934
|
+
) : null}
|
|
707
935
|
{frame && W > 0 ? (
|
|
708
936
|
<Animated.View
|
|
709
937
|
pointerEvents="none"
|