panelui-native 0.108.0 → 0.110.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 +320 -0
- package/lib/module/components/avatar-cropper/avatar-cropper-math.js.map +1 -0
- package/lib/module/components/avatar-cropper/index.js +656 -0
- package/lib/module/components/avatar-cropper/index.js.map +1 -0
- package/lib/module/components/badge/index.js +85 -4
- package/lib/module/components/badge/index.js.map +1 -1
- package/lib/module/components/button/index.js +12 -0
- package/lib/module/components/button/index.js.map +1 -1
- package/lib/module/components/button-group/index.js +3 -0
- package/lib/module/components/button-group/index.js.map +1 -1
- package/lib/module/components/checkbox/index.js +54 -4
- package/lib/module/components/checkbox/index.js.map +1 -1
- package/lib/module/components/chip/index.js +23 -1
- package/lib/module/components/chip/index.js.map +1 -1
- package/lib/module/components/input/index.js +14 -0
- package/lib/module/components/input/index.js.map +1 -1
- package/lib/module/components/map/index.js +157 -5
- package/lib/module/components/map/index.js.map +1 -1
- package/lib/module/components/map/map-insets.js +174 -0
- package/lib/module/components/map/map-insets.js.map +1 -0
- package/lib/module/components/radio-group/index.js +49 -3
- package/lib/module/components/radio-group/index.js.map +1 -1
- package/lib/module/components/select/index.js +54 -2
- package/lib/module/components/select/index.js.map +1 -1
- package/lib/module/components/switch/index.js +42 -3
- package/lib/module/components/switch/index.js.map +1 -1
- package/lib/module/components/tabs/index.js +147 -1
- package/lib/module/components/tabs/index.js.map +1 -1
- package/lib/module/components/textarea/index.js +18 -0
- package/lib/module/components/textarea/index.js.map +1 -1
- package/lib/module/components/toggle-button/index.js +34 -2
- package/lib/module/components/toggle-button/index.js.map +1 -1
- package/lib/module/icons/index.js +6 -0
- package/lib/module/icons/index.js.map +1 -1
- package/lib/module/index.js +1 -0
- package/lib/module/index.js.map +1 -1
- package/lib/module/providers/panel-ui-provider.js +22 -10
- package/lib/module/providers/panel-ui-provider.js.map +1 -1
- package/lib/typescript/src/components/avatar-cropper/avatar-cropper-math.d.ts +98 -0
- package/lib/typescript/src/components/avatar-cropper/avatar-cropper-math.d.ts.map +1 -0
- package/lib/typescript/src/components/avatar-cropper/index.d.ts +168 -0
- package/lib/typescript/src/components/avatar-cropper/index.d.ts.map +1 -0
- package/lib/typescript/src/components/badge/index.d.ts +68 -0
- package/lib/typescript/src/components/badge/index.d.ts.map +1 -1
- package/lib/typescript/src/components/button/index.d.ts +12 -0
- package/lib/typescript/src/components/button/index.d.ts.map +1 -1
- package/lib/typescript/src/components/button-group/index.d.ts +9 -0
- package/lib/typescript/src/components/button-group/index.d.ts.map +1 -1
- package/lib/typescript/src/components/checkbox/index.d.ts +89 -0
- package/lib/typescript/src/components/checkbox/index.d.ts.map +1 -1
- package/lib/typescript/src/components/chip/index.d.ts +24 -0
- package/lib/typescript/src/components/chip/index.d.ts.map +1 -1
- package/lib/typescript/src/components/input/index.d.ts +30 -0
- package/lib/typescript/src/components/input/index.d.ts.map +1 -1
- package/lib/typescript/src/components/map/index.d.ts +67 -1
- package/lib/typescript/src/components/map/index.d.ts.map +1 -1
- package/lib/typescript/src/components/map/map-insets.d.ts +80 -0
- package/lib/typescript/src/components/map/map-insets.d.ts.map +1 -0
- package/lib/typescript/src/components/radio-group/index.d.ts +188 -1
- package/lib/typescript/src/components/radio-group/index.d.ts.map +1 -1
- package/lib/typescript/src/components/select/index.d.ts +9 -1
- package/lib/typescript/src/components/select/index.d.ts.map +1 -1
- package/lib/typescript/src/components/switch/index.d.ts +38 -0
- package/lib/typescript/src/components/switch/index.d.ts.map +1 -1
- package/lib/typescript/src/components/tabs/index.d.ts +10 -1
- package/lib/typescript/src/components/tabs/index.d.ts.map +1 -1
- package/lib/typescript/src/components/textarea/index.d.ts +18 -0
- package/lib/typescript/src/components/textarea/index.d.ts.map +1 -1
- package/lib/typescript/src/components/toggle-button/index.d.ts +24 -0
- package/lib/typescript/src/components/toggle-button/index.d.ts.map +1 -1
- 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 +8 -7
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/providers/panel-ui-provider.d.ts.map +1 -1
- package/package.json +9 -1
- package/src/components/avatar-cropper/avatar-cropper-math.ts +311 -0
- package/src/components/avatar-cropper/index.tsx +808 -0
- package/src/components/badge/index.tsx +30 -4
- package/src/components/button/index.tsx +9 -2
- package/src/components/button-group/index.tsx +1 -0
- package/src/components/checkbox/index.tsx +47 -3
- package/src/components/chip/index.tsx +5 -1
- package/src/components/input/index.tsx +6 -0
- package/src/components/map/index.tsx +215 -5
- package/src/components/map/map-insets.ts +169 -0
- package/src/components/radio-group/index.tsx +56 -4
- package/src/components/select/index.tsx +50 -1
- package/src/components/switch/index.tsx +16 -4
- package/src/components/tabs/index.tsx +61 -0
- package/src/components/textarea/index.tsx +6 -0
- package/src/components/toggle-button/index.tsx +16 -1
- package/src/icons/index.tsx +3 -0
- package/src/index.ts +24 -3
- package/src/providers/panel-ui-provider.tsx +21 -11
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The geometry behind the avatar cropper, kept apart from the component.
|
|
3
|
+
*
|
|
4
|
+
* The mask stays fixed in the middle of the canvas and the photo moves under
|
|
5
|
+
* it, so everything here is one question asked different ways: where is the
|
|
6
|
+
* mask, measured in the photo's own frame? Clamping asks it to keep the mask
|
|
7
|
+
* on the photo, and the export asks it to find the same square in the photo's
|
|
8
|
+
* pixels.
|
|
9
|
+
*
|
|
10
|
+
* The photo is drawn at a base size whose short side equals the mask, then
|
|
11
|
+
* moved by `x`/`y`, turned by `rotation` and scaled by `scale` about its own
|
|
12
|
+
* centre — the order React Native applies `translate`, `rotate`, `scale` in a
|
|
13
|
+
* transform. Every function runs on the UI thread inside a gesture or an
|
|
14
|
+
* animated style, so each is a worklet, and none touches React Native, which
|
|
15
|
+
* is what lets the contract tests run them in Node.
|
|
16
|
+
*/
|
|
17
|
+
|
|
18
|
+
export type CropShape = 'circle' | 'squircle' | 'square';
|
|
19
|
+
|
|
20
|
+
export interface Size {
|
|
21
|
+
width: number;
|
|
22
|
+
height: number;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/** Where the photo sits under the mask. Rotation is in degrees, clockwise. */
|
|
26
|
+
export interface CropState {
|
|
27
|
+
x: number;
|
|
28
|
+
y: number;
|
|
29
|
+
scale: number;
|
|
30
|
+
rotation: number;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** The fixed parts: the mask's side, and the photo's size at a scale of 1. */
|
|
34
|
+
export interface CropFrame {
|
|
35
|
+
mask: number;
|
|
36
|
+
image: Size;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/** A crop in the source's pixels, after the source is turned by `rotation`. */
|
|
40
|
+
export interface SourceCrop {
|
|
41
|
+
/** The rectangle to cut, in the turned image's pixels. Always square. */
|
|
42
|
+
originX: number;
|
|
43
|
+
originY: number;
|
|
44
|
+
width: number;
|
|
45
|
+
height: number;
|
|
46
|
+
/** Degrees to turn the source by first, clockwise, in (-180, 180]. */
|
|
47
|
+
rotation: number;
|
|
48
|
+
/** The size of the turned image the rectangle is measured in. */
|
|
49
|
+
canvas: Size;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export const IDENTITY: CropState = { x: 0, y: 0, scale: 1, rotation: 0 };
|
|
53
|
+
|
|
54
|
+
/** How much a rubber-banded overshoot gives. Lower is stiffer. */
|
|
55
|
+
const RUBBER_BAND = 0.55;
|
|
56
|
+
|
|
57
|
+
/** Below this a sine or cosine is taken as zero, so a quarter turn is exact. */
|
|
58
|
+
const EPSILON = 1e-9;
|
|
59
|
+
|
|
60
|
+
export function clamp(value: number, min: number, max: number): number {
|
|
61
|
+
'worklet';
|
|
62
|
+
return Math.min(Math.max(value, min), max);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function trig(degrees: number): { cos: number; sin: number } {
|
|
66
|
+
'worklet';
|
|
67
|
+
const radians = (degrees * Math.PI) / 180;
|
|
68
|
+
let cos = Math.cos(radians);
|
|
69
|
+
let sin = Math.sin(radians);
|
|
70
|
+
if (Math.abs(cos) < EPSILON) cos = 0;
|
|
71
|
+
if (Math.abs(sin) < EPSILON) sin = 0;
|
|
72
|
+
return { cos, sin };
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** An angle folded into (-180, 180]. */
|
|
76
|
+
export function normalizeDegrees(degrees: number): number {
|
|
77
|
+
'worklet';
|
|
78
|
+
let folded = degrees % 360;
|
|
79
|
+
if (folded <= -180) folded += 360;
|
|
80
|
+
if (folded > 180) folded -= 360;
|
|
81
|
+
return folded;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** The photo's size at a scale of 1: its short side exactly covers the mask. */
|
|
85
|
+
export function baseSize(source: Size, mask: number): Size {
|
|
86
|
+
'worklet';
|
|
87
|
+
const shortSide = Math.min(source.width, source.height);
|
|
88
|
+
if (shortSide <= 0 || mask <= 0) return { width: mask, height: mask };
|
|
89
|
+
const factor = mask / shortSide;
|
|
90
|
+
return { width: source.width * factor, height: source.height * factor };
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
/**
|
|
94
|
+
* Half the mask's extent along the photo's axes, in screen points.
|
|
95
|
+
*
|
|
96
|
+
* A circle reaches the same distance whichever way the photo is turned. A
|
|
97
|
+
* square turned against the photo reaches further — out to its corners — and
|
|
98
|
+
* the squircle is held to its square, which contains it, so its corners can
|
|
99
|
+
* never come off the photo either.
|
|
100
|
+
*/
|
|
101
|
+
export function maskReach(shape: CropShape, mask: number, rotation: number): number {
|
|
102
|
+
'worklet';
|
|
103
|
+
const half = mask / 2;
|
|
104
|
+
if (shape === 'circle') return half;
|
|
105
|
+
const { cos, sin } = trig(rotation);
|
|
106
|
+
return half * (Math.abs(cos) + Math.abs(sin));
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** The smallest scale at which the photo still covers the whole mask. */
|
|
110
|
+
export function minScale(shape: CropShape, rotation: number): number {
|
|
111
|
+
'worklet';
|
|
112
|
+
// The base size's short side is the mask, so covering the reach on the
|
|
113
|
+
// short side covers it on the long one too.
|
|
114
|
+
return maskReach(shape, 2, rotation);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/** Screen offset → the same offset along the photo's axes, and back. */
|
|
118
|
+
function toImageAxes(x: number, y: number, rotation: number): { u: number; v: number } {
|
|
119
|
+
'worklet';
|
|
120
|
+
const { cos, sin } = trig(rotation);
|
|
121
|
+
return { u: cos * x + sin * y, v: -sin * x + cos * y };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function fromImageAxes(u: number, v: number, rotation: number): { x: number; y: number } {
|
|
125
|
+
'worklet';
|
|
126
|
+
const { cos, sin } = trig(rotation);
|
|
127
|
+
return { x: cos * u - sin * v, y: sin * u + cos * v };
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
/**
|
|
131
|
+
* How far the photo can move along each of its own axes before the mask
|
|
132
|
+
* reaches its edge. Zero when the photo is too small to move at all.
|
|
133
|
+
*/
|
|
134
|
+
function travel(state: CropState, shape: CropShape, frame: CropFrame): { u: number; v: number } {
|
|
135
|
+
'worklet';
|
|
136
|
+
const reach = maskReach(shape, frame.mask, state.rotation);
|
|
137
|
+
return {
|
|
138
|
+
u: Math.max(0, (state.scale * frame.image.width) / 2 - reach),
|
|
139
|
+
v: Math.max(0, (state.scale * frame.image.height) / 2 - reach),
|
|
140
|
+
};
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/** The nearest translation that keeps the mask on the photo. */
|
|
144
|
+
export function clampTranslation(
|
|
145
|
+
state: CropState,
|
|
146
|
+
shape: CropShape,
|
|
147
|
+
frame: CropFrame
|
|
148
|
+
): { x: number; y: number } {
|
|
149
|
+
'worklet';
|
|
150
|
+
const limit = travel(state, shape, frame);
|
|
151
|
+
const along = toImageAxes(state.x, state.y, state.rotation);
|
|
152
|
+
return fromImageAxes(
|
|
153
|
+
clamp(along.u, -limit.u, limit.u),
|
|
154
|
+
clamp(along.v, -limit.v, limit.v),
|
|
155
|
+
state.rotation
|
|
156
|
+
);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
/** The state a gesture settles into: in range, and covering the mask. */
|
|
160
|
+
export function settle(
|
|
161
|
+
state: CropState,
|
|
162
|
+
shape: CropShape,
|
|
163
|
+
frame: CropFrame,
|
|
164
|
+
maxZoom: number
|
|
165
|
+
): CropState {
|
|
166
|
+
'worklet';
|
|
167
|
+
const lowest = minScale(shape, state.rotation);
|
|
168
|
+
const scale = clamp(state.scale, lowest, Math.max(lowest, maxZoom));
|
|
169
|
+
const next = { ...state, scale };
|
|
170
|
+
return { ...next, ...clampTranslation(next, shape, frame) };
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
/** An overshoot past a bound, compressed so it gives but never runs away. */
|
|
174
|
+
export function rubberBand(overshoot: number, dimension: number): number {
|
|
175
|
+
'worklet';
|
|
176
|
+
if (dimension <= 0) return 0;
|
|
177
|
+
const sign = overshoot < 0 ? -1 : 1;
|
|
178
|
+
const distance = Math.abs(overshoot);
|
|
179
|
+
return sign * (1 - 1 / ((distance * RUBBER_BAND) / dimension + 1)) * dimension;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* What to draw while a gesture is still going: the raw state, with anything
|
|
184
|
+
* past a bound rubber-banded back towards it. At rest the raw state is already
|
|
185
|
+
* settled and this returns it unchanged.
|
|
186
|
+
*/
|
|
187
|
+
export function present(
|
|
188
|
+
state: CropState,
|
|
189
|
+
shape: CropShape,
|
|
190
|
+
frame: CropFrame,
|
|
191
|
+
maxZoom: number
|
|
192
|
+
): CropState {
|
|
193
|
+
'worklet';
|
|
194
|
+
const lowest = minScale(shape, state.rotation);
|
|
195
|
+
const highest = Math.max(lowest, maxZoom);
|
|
196
|
+
let scale = state.scale;
|
|
197
|
+
if (scale < lowest) scale = lowest - rubberBand(lowest - scale, lowest / 2);
|
|
198
|
+
else if (scale > highest) scale = highest + rubberBand(scale - highest, highest / 2);
|
|
199
|
+
|
|
200
|
+
const next = { ...state, scale };
|
|
201
|
+
const limit = travel(next, shape, frame);
|
|
202
|
+
const along = toImageAxes(state.x, state.y, state.rotation);
|
|
203
|
+
const u = clamp(along.u, -limit.u, limit.u);
|
|
204
|
+
const v = clamp(along.v, -limit.v, limit.v);
|
|
205
|
+
const offset = fromImageAxes(
|
|
206
|
+
u + rubberBand(along.u - u, frame.mask),
|
|
207
|
+
v + rubberBand(along.v - v, frame.mask),
|
|
208
|
+
state.rotation
|
|
209
|
+
);
|
|
210
|
+
return { ...next, ...offset };
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
/** Scale by `factor` about a point, given relative to the canvas centre. */
|
|
214
|
+
export function zoomAbout(state: CropState, factor: number, fx: number, fy: number): CropState {
|
|
215
|
+
'worklet';
|
|
216
|
+
return {
|
|
217
|
+
...state,
|
|
218
|
+
scale: state.scale * factor,
|
|
219
|
+
x: fx + (state.x - fx) * factor,
|
|
220
|
+
y: fy + (state.y - fy) * factor,
|
|
221
|
+
};
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/** Turn by `degrees` about a point, given relative to the canvas centre. */
|
|
225
|
+
export function rotateAbout(state: CropState, degrees: number, ax: number, ay: number): CropState {
|
|
226
|
+
'worklet';
|
|
227
|
+
const { cos, sin } = trig(degrees);
|
|
228
|
+
const dx = state.x - ax;
|
|
229
|
+
const dy = state.y - ay;
|
|
230
|
+
return {
|
|
231
|
+
...state,
|
|
232
|
+
rotation: state.rotation + degrees,
|
|
233
|
+
x: ax + cos * dx - sin * dy,
|
|
234
|
+
y: ay + sin * dx + cos * dy,
|
|
235
|
+
};
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/** The next quarter turn from wherever the photo is, in either direction. */
|
|
239
|
+
export function nextQuarterTurn(rotation: number, direction: 1 | -1): number {
|
|
240
|
+
'worklet';
|
|
241
|
+
const quarters = rotation / 90;
|
|
242
|
+
const nearest = Math.round(quarters);
|
|
243
|
+
// Already on a quarter turn: step a whole one. Part way: finish this one.
|
|
244
|
+
const onQuarter = Math.abs(quarters - nearest) < 1e-6;
|
|
245
|
+
const base = onQuarter ? nearest : direction > 0 ? Math.floor(quarters) : Math.ceil(quarters);
|
|
246
|
+
return (base + direction) * 90;
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/**
|
|
250
|
+
* The square the mask covers, in the source's pixels.
|
|
251
|
+
*
|
|
252
|
+
* The export turns the source first and cuts an upright square from the
|
|
253
|
+
* result, because a turned square cannot be cut directly. Turning grows the
|
|
254
|
+
* canvas to the turned image's bounding box with the image still centred, and
|
|
255
|
+
* a point at `p` from the source's centre lands at `R(θ)·p` from the canvas
|
|
256
|
+
* centre. The mask's centre sits at `R(−θ)·(−x, −y) / scale` in the photo, so
|
|
257
|
+
* after the turn it lands at `−(x, y) / scale` — the rotation cancels, and the
|
|
258
|
+
* crop is the mask's offset scaled into pixels.
|
|
259
|
+
*/
|
|
260
|
+
export function sourceCrop(state: CropState, frame: CropFrame, source: Size): SourceCrop {
|
|
261
|
+
'worklet';
|
|
262
|
+
const rotation = normalizeDegrees(state.rotation);
|
|
263
|
+
const { cos, sin } = trig(rotation);
|
|
264
|
+
// Both platforms round the turned canvas down to whole pixels.
|
|
265
|
+
const canvas = {
|
|
266
|
+
width: Math.floor(Math.abs(source.width * cos) + Math.abs(source.height * sin) + 1e-6),
|
|
267
|
+
height: Math.floor(Math.abs(source.width * sin) + Math.abs(source.height * cos) + 1e-6),
|
|
268
|
+
};
|
|
269
|
+
// Screen points per source pixel, at the current scale.
|
|
270
|
+
const pointsPerPixel = (frame.image.width / source.width) * state.scale;
|
|
271
|
+
const side = Math.max(
|
|
272
|
+
1,
|
|
273
|
+
Math.min(Math.floor(frame.mask / pointsPerPixel), canvas.width, canvas.height)
|
|
274
|
+
);
|
|
275
|
+
const centreX = canvas.width / 2 - state.x / pointsPerPixel;
|
|
276
|
+
const centreY = canvas.height / 2 - state.y / pointsPerPixel;
|
|
277
|
+
return {
|
|
278
|
+
originX: clamp(Math.round(centreX - side / 2), 0, canvas.width - side),
|
|
279
|
+
originY: clamp(Math.round(centreY - side / 2), 0, canvas.height - side),
|
|
280
|
+
width: side,
|
|
281
|
+
height: side,
|
|
282
|
+
rotation,
|
|
283
|
+
canvas,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/** Superellipse exponent for the squircle. Four is soft; five reads as an app icon. */
|
|
288
|
+
const SQUIRCLE_EXPONENT = 5;
|
|
289
|
+
const SQUIRCLE_STEPS = 128;
|
|
290
|
+
|
|
291
|
+
/** The mask's outline as an SVG path, centred on (`cx`, `cy`). */
|
|
292
|
+
export function maskPath(shape: CropShape, cx: number, cy: number, mask: number): string {
|
|
293
|
+
const r = mask / 2;
|
|
294
|
+
if (shape === 'square') {
|
|
295
|
+
return `M${cx - r} ${cy - r}H${cx + r}V${cy + r}H${cx - r}Z`;
|
|
296
|
+
}
|
|
297
|
+
if (shape === 'circle') {
|
|
298
|
+
return `M${cx - r} ${cy}A${r} ${r} 0 1 0 ${cx + r} ${cy}A${r} ${r} 0 1 0 ${cx - r} ${cy}Z`;
|
|
299
|
+
}
|
|
300
|
+
const exponent = 2 / SQUIRCLE_EXPONENT;
|
|
301
|
+
let path = '';
|
|
302
|
+
for (let step = 0; step < SQUIRCLE_STEPS; step += 1) {
|
|
303
|
+
const t = (step / SQUIRCLE_STEPS) * Math.PI * 2;
|
|
304
|
+
const cos = Math.cos(t);
|
|
305
|
+
const sin = Math.sin(t);
|
|
306
|
+
const x = cx + r * Math.sign(cos) * Math.abs(cos) ** exponent;
|
|
307
|
+
const y = cy + r * Math.sign(sin) * Math.abs(sin) ** exponent;
|
|
308
|
+
path += `${step === 0 ? 'M' : 'L'}${x.toFixed(2)} ${y.toFixed(2)}`;
|
|
309
|
+
}
|
|
310
|
+
return `${path}Z`;
|
|
311
|
+
}
|