@panneau/media 5.0.0-alpha.4 → 5.0.0-alpha.41
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/README.md +106 -7
- package/dist/ImageItem.d.ts +23 -0
- package/dist/ImageItem.d.ts.map +1 -0
- package/dist/ImageItem.js +292 -0
- package/dist/ImageItem.js.map +1 -0
- package/dist/MediaField.css +127 -0
- package/dist/MediaField.d.ts +74 -15
- package/dist/MediaField.d.ts.map +1 -1
- package/dist/MediaField.js +790 -189
- package/dist/MediaField.js.map +1 -1
- package/dist/MediaField.module.js +28 -2
- package/dist/MediaField.module.js.map +1 -1
- package/dist/editor/ImageEditor.css +369 -0
- package/dist/editor/ImageEditor.d.ts +13 -0
- package/dist/editor/ImageEditor.d.ts.map +1 -0
- package/dist/editor/ImageEditor.js +692 -0
- package/dist/editor/ImageEditor.js.map +1 -0
- package/dist/editor/ImageEditor.module.js +86 -0
- package/dist/editor/ImageEditor.module.js.map +1 -0
- package/dist/editor/draw.d.ts +37 -0
- package/dist/editor/draw.d.ts.map +1 -0
- package/dist/editor/draw.js +130 -0
- package/dist/editor/draw.js.map +1 -0
- package/dist/editor/geometry.d.ts +54 -0
- package/dist/editor/geometry.d.ts.map +1 -0
- package/dist/editor/geometry.js +227 -0
- package/dist/editor/geometry.js.map +1 -0
- package/dist/editor/index.d.ts +5 -0
- package/dist/editor/index.d.ts.map +1 -0
- package/dist/editor/index.js +3 -0
- package/dist/editor/tools.d.ts +11 -0
- package/dist/editor/tools.d.ts.map +1 -0
- package/dist/editor/tools.js +1199 -0
- package/dist/editor/tools.js.map +1 -0
- package/dist/editor/types.d.ts +111 -0
- package/dist/editor/types.d.ts.map +1 -0
- package/dist/index.d.ts +4 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -2
- package/dist/picker/MediaDetails.d.ts +22 -0
- package/dist/picker/MediaDetails.d.ts.map +1 -0
- package/dist/picker/MediaDetails.js +368 -0
- package/dist/picker/MediaDetails.js.map +1 -0
- package/dist/picker/MediaGrid.d.ts +31 -0
- package/dist/picker/MediaGrid.d.ts.map +1 -0
- package/dist/picker/MediaGrid.js +281 -0
- package/dist/picker/MediaGrid.js.map +1 -0
- package/dist/picker/MediaLibrary.d.ts +9 -0
- package/dist/picker/MediaLibrary.d.ts.map +1 -0
- package/dist/picker/MediaLibrary.js +80 -0
- package/dist/picker/MediaLibrary.js.map +1 -0
- package/dist/picker/MediaPicker.css +627 -0
- package/dist/picker/MediaPicker.d.ts +9 -0
- package/dist/picker/MediaPicker.d.ts.map +1 -0
- package/dist/picker/MediaPicker.js +246 -0
- package/dist/picker/MediaPicker.js.map +1 -0
- package/dist/picker/MediaPicker.module.js +124 -0
- package/dist/picker/MediaPicker.module.js.map +1 -0
- package/dist/picker/PickerToolbar.d.ts +19 -0
- package/dist/picker/PickerToolbar.d.ts.map +1 -0
- package/dist/picker/PickerToolbar.js +266 -0
- package/dist/picker/PickerToolbar.js.map +1 -0
- package/dist/picker/UploadPanel.d.ts +22 -0
- package/dist/picker/UploadPanel.d.ts.map +1 -0
- package/dist/picker/UploadPanel.js +186 -0
- package/dist/picker/UploadPanel.js.map +1 -0
- package/dist/picker/format.d.ts +18 -0
- package/dist/picker/format.d.ts.map +1 -0
- package/dist/picker/format.js +64 -0
- package/dist/picker/format.js.map +1 -0
- package/dist/picker/index.d.ts +4 -0
- package/dist/picker/index.d.ts.map +1 -0
- package/dist/picker/index.js +3 -0
- package/dist/picker/types.d.ts +131 -0
- package/dist/picker/types.d.ts.map +1 -0
- package/dist/picker/useMediaBrowser.d.ts +50 -0
- package/dist/picker/useMediaBrowser.d.ts.map +1 -0
- package/dist/picker/useMediaBrowser.js +314 -0
- package/dist/picker/useMediaBrowser.js.map +1 -0
- package/dist/picker/useMediaLibrary.d.ts +51 -0
- package/dist/picker/useMediaLibrary.d.ts.map +1 -0
- package/dist/picker/useMediaLibrary.js +1051 -0
- package/dist/picker/useMediaLibrary.js.map +1 -0
- package/dist/plugin.d.ts +1 -1
- package/dist/plugin.js +5 -2
- package/dist/plugin.js.map +1 -1
- package/dist/resourceSource.d.ts +21 -0
- package/dist/resourceSource.d.ts.map +1 -0
- package/dist/resourceSource.js +108 -0
- package/dist/resourceSource.js.map +1 -0
- package/package.json +20 -9
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
//#region src/editor/geometry.ts
|
|
2
|
+
var clamp = (value, min, max) => Math.min(Math.max(value, min), max);
|
|
3
|
+
/** The size of an image once rotated. */
|
|
4
|
+
function viewSize(size, rotate = 0) {
|
|
5
|
+
return rotate === 90 || rotate === 270 ? {
|
|
6
|
+
width: size.height,
|
|
7
|
+
height: size.width
|
|
8
|
+
} : {
|
|
9
|
+
width: size.width,
|
|
10
|
+
height: size.height
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
function rotatePoint({ x, y }, { width, height }, rotate) {
|
|
14
|
+
switch (rotate) {
|
|
15
|
+
case 90: return {
|
|
16
|
+
x: height - y,
|
|
17
|
+
y: x
|
|
18
|
+
};
|
|
19
|
+
case 180: return {
|
|
20
|
+
x: width - x,
|
|
21
|
+
y: height - y
|
|
22
|
+
};
|
|
23
|
+
case 270: return {
|
|
24
|
+
x: y,
|
|
25
|
+
y: width - x
|
|
26
|
+
};
|
|
27
|
+
default: return {
|
|
28
|
+
x,
|
|
29
|
+
y
|
|
30
|
+
};
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
/** A point of the image, in the view: rotated, then flipped. */
|
|
34
|
+
function toView(point, size, edits) {
|
|
35
|
+
const view = viewSize(size, edits.rotate);
|
|
36
|
+
const rotated = rotatePoint(point, size, edits.rotate ?? 0);
|
|
37
|
+
return {
|
|
38
|
+
x: edits.flipX === true ? view.width - rotated.x : rotated.x,
|
|
39
|
+
y: edits.flipY === true ? view.height - rotated.y : rotated.y
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
/** A point of the view, in the image. */
|
|
43
|
+
function fromView(point, size, edits) {
|
|
44
|
+
const view = viewSize(size, edits.rotate);
|
|
45
|
+
return rotatePoint({
|
|
46
|
+
x: edits.flipX === true ? view.width - point.x : point.x,
|
|
47
|
+
y: edits.flipY === true ? view.height - point.y : point.y
|
|
48
|
+
}, view, (360 - (edits.rotate ?? 0)) % 360);
|
|
49
|
+
}
|
|
50
|
+
function rectBetween(a, b) {
|
|
51
|
+
return {
|
|
52
|
+
x: Math.min(a.x, b.x),
|
|
53
|
+
y: Math.min(a.y, b.y),
|
|
54
|
+
width: Math.abs(a.x - b.x),
|
|
55
|
+
height: Math.abs(a.y - b.y)
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
/** A rectangle of the image, in the view. */
|
|
59
|
+
function rectToView(rect, size, edits) {
|
|
60
|
+
return rectBetween(toView(rect, size, edits), toView({
|
|
61
|
+
x: rect.x + rect.width,
|
|
62
|
+
y: rect.y + rect.height
|
|
63
|
+
}, size, edits));
|
|
64
|
+
}
|
|
65
|
+
/** A rectangle of the view, in whole pixels of the image, as the protocol stores a crop. */
|
|
66
|
+
function rectFromView(rect, size, edits) {
|
|
67
|
+
const found = rectBetween(fromView(rect, size, edits), fromView({
|
|
68
|
+
x: rect.x + rect.width,
|
|
69
|
+
y: rect.y + rect.height
|
|
70
|
+
}, size, edits));
|
|
71
|
+
const x = clamp(Math.round(found.x), 0, size.width - 1);
|
|
72
|
+
const y = clamp(Math.round(found.y), 0, size.height - 1);
|
|
73
|
+
return {
|
|
74
|
+
x,
|
|
75
|
+
y,
|
|
76
|
+
width: clamp(Math.round(found.width), 1, size.width - x),
|
|
77
|
+
height: clamp(Math.round(found.height), 1, size.height - y)
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
/** The crop of the edits, in the view: the whole image without one. */
|
|
81
|
+
function cropInView(size, edits) {
|
|
82
|
+
return rectToView(edits.crop ?? {
|
|
83
|
+
x: 0,
|
|
84
|
+
y: 0,
|
|
85
|
+
width: size.width,
|
|
86
|
+
height: size.height
|
|
87
|
+
}, size, edits);
|
|
88
|
+
}
|
|
89
|
+
/** The SVG transform that draws the image as the view shows it. */
|
|
90
|
+
function svgTransform({ width, height }, edits) {
|
|
91
|
+
const view = viewSize({
|
|
92
|
+
width,
|
|
93
|
+
height
|
|
94
|
+
}, edits.rotate);
|
|
95
|
+
const parts = [];
|
|
96
|
+
if (edits.flipX === true) parts.push(`translate(${String(view.width)} 0) scale(-1 1)`);
|
|
97
|
+
if (edits.flipY === true) parts.push(`translate(0 ${String(view.height)}) scale(1 -1)`);
|
|
98
|
+
switch (edits.rotate) {
|
|
99
|
+
case 90:
|
|
100
|
+
parts.push(`translate(${String(height)} 0) rotate(90)`);
|
|
101
|
+
break;
|
|
102
|
+
case 180:
|
|
103
|
+
parts.push(`translate(${String(width)} ${String(height)}) rotate(180)`);
|
|
104
|
+
break;
|
|
105
|
+
case 270: parts.push(`translate(0 ${String(width)}) rotate(270)`);
|
|
106
|
+
}
|
|
107
|
+
return parts.join(" ");
|
|
108
|
+
}
|
|
109
|
+
/** The focal point in the view, in pixels: the center of the image without one. */
|
|
110
|
+
function focalInView(size, edits) {
|
|
111
|
+
const focal = edits.focalPoint ?? {
|
|
112
|
+
x: .5,
|
|
113
|
+
y: .5
|
|
114
|
+
};
|
|
115
|
+
return toView({
|
|
116
|
+
x: focal.x * size.width,
|
|
117
|
+
y: focal.y * size.height
|
|
118
|
+
}, size, edits);
|
|
119
|
+
}
|
|
120
|
+
/** A point of the view as a focal point: from 0 to 1, relative to the image. */
|
|
121
|
+
function focalFromView(point, size, edits) {
|
|
122
|
+
const found = fromView(point, size, edits);
|
|
123
|
+
const round = (value) => Math.round(clamp(value, 0, 1) * 1e3) / 1e3;
|
|
124
|
+
return {
|
|
125
|
+
x: round(found.x / size.width),
|
|
126
|
+
y: round(found.y / size.height)
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
/** The largest crop of a ratio inside a rectangle, centered on a point as far as it can be. */
|
|
130
|
+
function cropAround(within, ratio, center) {
|
|
131
|
+
const width = Math.min(within.width, within.height * ratio);
|
|
132
|
+
const height = width / ratio;
|
|
133
|
+
return {
|
|
134
|
+
x: clamp(center.x - width / 2, within.x, within.x + within.width - width),
|
|
135
|
+
y: clamp(center.y - height / 2, within.y, within.y + within.height - height),
|
|
136
|
+
width,
|
|
137
|
+
height
|
|
138
|
+
};
|
|
139
|
+
}
|
|
140
|
+
/**
|
|
141
|
+
* A crop inside an image, for a crop made on another version of it (decision 0031): what falls
|
|
142
|
+
* outside is cut off, and a crop wholly outside is dropped.
|
|
143
|
+
*/
|
|
144
|
+
function clampCrop(crop, size) {
|
|
145
|
+
const x = clamp(crop.x, 0, size.width);
|
|
146
|
+
const y = clamp(crop.y, 0, size.height);
|
|
147
|
+
const width = Math.min(crop.x + crop.width, size.width) - x;
|
|
148
|
+
const height = Math.min(crop.y + crop.height, size.height) - y;
|
|
149
|
+
return width >= 1 && height >= 1 ? {
|
|
150
|
+
x,
|
|
151
|
+
y,
|
|
152
|
+
width,
|
|
153
|
+
height
|
|
154
|
+
} : null;
|
|
155
|
+
}
|
|
156
|
+
/** Moves a rectangle within bounds. */
|
|
157
|
+
function moveRect(rect, dx, dy, bounds) {
|
|
158
|
+
return {
|
|
159
|
+
...rect,
|
|
160
|
+
x: clamp(rect.x + dx, 0, bounds.width - rect.width),
|
|
161
|
+
y: clamp(rect.y + dy, 0, bounds.height - rect.height)
|
|
162
|
+
};
|
|
163
|
+
}
|
|
164
|
+
/** Moves the edges of a rectangle that a handle holds, within bounds, keeping a ratio if any. */
|
|
165
|
+
function resizeRect(rect, handle, dx, dy, ratio, bounds) {
|
|
166
|
+
const min = Math.min(bounds.width, bounds.height) * .05;
|
|
167
|
+
const west = handle.includes("w");
|
|
168
|
+
const north = handle.includes("n");
|
|
169
|
+
if (ratio !== null) {
|
|
170
|
+
const anchor = {
|
|
171
|
+
x: west ? rect.x + rect.width : rect.x,
|
|
172
|
+
y: north ? rect.y + rect.height : rect.y
|
|
173
|
+
};
|
|
174
|
+
const roomX = west ? anchor.x : bounds.width - anchor.x;
|
|
175
|
+
const roomY = north ? anchor.y : bounds.height - anchor.y;
|
|
176
|
+
const largest = Math.min(roomX, roomY * ratio);
|
|
177
|
+
const width = clamp(west ? rect.width - dx : rect.width + dx, Math.min(min, largest), largest);
|
|
178
|
+
const height = width / ratio;
|
|
179
|
+
return {
|
|
180
|
+
x: west ? anchor.x - width : anchor.x,
|
|
181
|
+
y: north ? anchor.y - height : anchor.y,
|
|
182
|
+
width,
|
|
183
|
+
height
|
|
184
|
+
};
|
|
185
|
+
}
|
|
186
|
+
let left = rect.x;
|
|
187
|
+
let right = rect.x + rect.width;
|
|
188
|
+
let top = rect.y;
|
|
189
|
+
let bottom = rect.y + rect.height;
|
|
190
|
+
if (west) left = clamp(left + dx, 0, right - min);
|
|
191
|
+
else if (handle.includes("e")) right = clamp(right + dx, left + min, bounds.width);
|
|
192
|
+
if (north) top = clamp(top + dy, 0, bottom - min);
|
|
193
|
+
else if (handle.includes("s")) bottom = clamp(bottom + dy, top + min, bounds.height);
|
|
194
|
+
return {
|
|
195
|
+
x: left,
|
|
196
|
+
y: top,
|
|
197
|
+
width: right - left,
|
|
198
|
+
height: bottom - top
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
/** Where a handle sits on a rectangle. */
|
|
202
|
+
function handlePoint(rect, handle) {
|
|
203
|
+
return {
|
|
204
|
+
x: handle.includes("w") ? rect.x : handle.includes("e") ? rect.x + rect.width : rect.x + rect.width / 2,
|
|
205
|
+
y: handle.includes("n") ? rect.y : handle.includes("s") ? rect.y + rect.height : rect.y + rect.height / 2
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
/** Whether two transforms have the same edits, whatever the order of their keys. */
|
|
209
|
+
function sameEdits(a, b) {
|
|
210
|
+
const normalized = (edits) => JSON.stringify({
|
|
211
|
+
crop: edits?.crop ? [
|
|
212
|
+
edits.crop.x,
|
|
213
|
+
edits.crop.y,
|
|
214
|
+
edits.crop.width,
|
|
215
|
+
edits.crop.height
|
|
216
|
+
] : null,
|
|
217
|
+
rotate: edits?.rotate ?? 0,
|
|
218
|
+
flipX: edits?.flipX === true,
|
|
219
|
+
flipY: edits?.flipY === true,
|
|
220
|
+
focalPoint: edits?.focalPoint ? [edits.focalPoint.x, edits.focalPoint.y] : null
|
|
221
|
+
});
|
|
222
|
+
return normalized(a) === normalized(b);
|
|
223
|
+
}
|
|
224
|
+
//#endregion
|
|
225
|
+
export { clampCrop, cropAround, cropInView, focalFromView, focalInView, fromView, handlePoint, moveRect, rectFromView, rectToView, resizeRect, sameEdits, svgTransform, toView, viewSize };
|
|
226
|
+
|
|
227
|
+
//# sourceMappingURL=geometry.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"geometry.js","names":["MediaTransform","Size","width","height","Point","x","y","Rect","Rotation","Edits","Pick","clamp","value","min","max","Math","viewSize","size","rotate","rotatePoint","toView","point","edits","view","rotated","flipX","flipY","fromView","unflipped","rectBetween","a","b","abs","rectToView","rect","rectFromView","found","round","cropInView","crop","svgTransform","parts","push","String","join","focalInView","focal","focalPoint","focalFromView","cropAround","within","ratio","center","clampCrop","moveRect","dx","dy","bounds","Handle","resizeRect","handle","west","includes","north","anchor","roomX","roomY","largest","left","right","top","bottom","handlePoint","sameEdits","normalized","JSON","stringify"],"sources":["../../src/editor/geometry.ts"],"sourcesContent":["import type { MediaTransform } from '@panneau/protocol';\n\n/**\n * The geometry of the protocol's `MediaTransform`: a crop in pixels of the upright image, then a\n * clockwise rotation, then flips. The editor shows the image as it will be rendered (rotated and\n * flipped), the view, and converts between it and the pixels of the image.\n */\n\n// Type aliases rather than interfaces, so that they fit the protocol's loose objects.\nexport type Size = { width: number; height: number };\n\nexport type Point = { x: number; y: number };\n\nexport type Rect = Point & Size;\n\nexport type Rotation = 0 | 90 | 180 | 270;\n\n/** The edits the geometry reads: those of the protocol, without the base. */\nexport type Edits = Pick<MediaTransform, 'crop' | 'rotate' | 'flipX' | 'flipY' | 'focalPoint'>;\n\nconst clamp = (value: number, min: number, max: number) => Math.min(Math.max(value, min), max);\n\n/** The size of an image once rotated. */\nexport function viewSize(size: Size, rotate: Rotation = 0): Size {\n return rotate === 90 || rotate === 270\n ? { width: size.height, height: size.width }\n : { width: size.width, height: size.height };\n}\n\nfunction rotatePoint({ x, y }: Point, { width, height }: Size, rotate: Rotation): Point {\n switch (rotate) {\n case 90:\n return { x: height - y, y: x };\n case 180:\n return { x: width - x, y: height - y };\n case 270:\n return { x: y, y: width - x };\n default:\n return { x, y };\n }\n}\n\n/** A point of the image, in the view: rotated, then flipped. */\nexport function toView(point: Point, size: Size, edits: Edits): Point {\n const view = viewSize(size, edits.rotate);\n const rotated = rotatePoint(point, size, edits.rotate ?? 0);\n return {\n x: edits.flipX === true ? view.width - rotated.x : rotated.x,\n y: edits.flipY === true ? view.height - rotated.y : rotated.y,\n };\n}\n\n/** A point of the view, in the image. */\nexport function fromView(point: Point, size: Size, edits: Edits): Point {\n const view = viewSize(size, edits.rotate);\n const unflipped = {\n x: edits.flipX === true ? view.width - point.x : point.x,\n y: edits.flipY === true ? view.height - point.y : point.y,\n };\n return rotatePoint(unflipped, view, ((360 - (edits.rotate ?? 0)) % 360) as Rotation);\n}\n\nfunction rectBetween(a: Point, b: Point): Rect {\n return {\n x: Math.min(a.x, b.x),\n y: Math.min(a.y, b.y),\n width: Math.abs(a.x - b.x),\n height: Math.abs(a.y - b.y),\n };\n}\n\n/** A rectangle of the image, in the view. */\nexport function rectToView(rect: Rect, size: Size, edits: Edits): Rect {\n return rectBetween(\n toView(rect, size, edits),\n toView({ x: rect.x + rect.width, y: rect.y + rect.height }, size, edits),\n );\n}\n\n/** A rectangle of the view, in whole pixels of the image, as the protocol stores a crop. */\nexport function rectFromView(rect: Rect, size: Size, edits: Edits): Rect {\n const found = rectBetween(\n fromView(rect, size, edits),\n fromView({ x: rect.x + rect.width, y: rect.y + rect.height }, size, edits),\n );\n const x = clamp(Math.round(found.x), 0, size.width - 1);\n const y = clamp(Math.round(found.y), 0, size.height - 1);\n return {\n x,\n y,\n width: clamp(Math.round(found.width), 1, size.width - x),\n height: clamp(Math.round(found.height), 1, size.height - y),\n };\n}\n\n/** The crop of the edits, in the view: the whole image without one. */\nexport function cropInView(size: Size, edits: Edits): Rect {\n return rectToView(\n edits.crop ?? { x: 0, y: 0, width: size.width, height: size.height },\n size,\n edits,\n );\n}\n\n/** The SVG transform that draws the image as the view shows it. */\nexport function svgTransform({ width, height }: Size, edits: Edits): string {\n const view = viewSize({ width, height }, edits.rotate);\n const parts: string[] = [];\n if (edits.flipX === true) {\n parts.push(`translate(${String(view.width)} 0) scale(-1 1)`);\n }\n if (edits.flipY === true) {\n parts.push(`translate(0 ${String(view.height)}) scale(1 -1)`);\n }\n switch (edits.rotate) {\n case 90:\n parts.push(`translate(${String(height)} 0) rotate(90)`);\n break;\n case 180:\n parts.push(`translate(${String(width)} ${String(height)}) rotate(180)`);\n break;\n case 270:\n parts.push(`translate(0 ${String(width)}) rotate(270)`);\n break;\n default:\n break;\n }\n return parts.join(' ');\n}\n\n/** The focal point in the view, in pixels: the center of the image without one. */\nexport function focalInView(size: Size, edits: Edits): Point {\n const focal = edits.focalPoint ?? { x: 0.5, y: 0.5 };\n return toView({ x: focal.x * size.width, y: focal.y * size.height }, size, edits);\n}\n\n/** A point of the view as a focal point: from 0 to 1, relative to the image. */\nexport function focalFromView(point: Point, size: Size, edits: Edits): Point {\n const found = fromView(point, size, edits);\n const round = (value: number) => Math.round(clamp(value, 0, 1) * 1000) / 1000;\n return { x: round(found.x / size.width), y: round(found.y / size.height) };\n}\n\n/** The largest crop of a ratio inside a rectangle, centered on a point as far as it can be. */\nexport function cropAround(within: Rect, ratio: number, center: Point): Rect {\n const width = Math.min(within.width, within.height * ratio);\n const height = width / ratio;\n return {\n x: clamp(center.x - width / 2, within.x, within.x + within.width - width),\n y: clamp(center.y - height / 2, within.y, within.y + within.height - height),\n width,\n height,\n };\n}\n\n/**\n * A crop inside an image, for a crop made on another version of it (decision 0031): what falls\n * outside is cut off, and a crop wholly outside is dropped.\n */\nexport function clampCrop(crop: Rect, size: Size): Rect | null {\n const x = clamp(crop.x, 0, size.width);\n const y = clamp(crop.y, 0, size.height);\n const width = Math.min(crop.x + crop.width, size.width) - x;\n const height = Math.min(crop.y + crop.height, size.height) - y;\n return width >= 1 && height >= 1 ? { x, y, width, height } : null;\n}\n\n/** Moves a rectangle within bounds. */\nexport function moveRect(rect: Rect, dx: number, dy: number, bounds: Size): Rect {\n return {\n ...rect,\n x: clamp(rect.x + dx, 0, bounds.width - rect.width),\n y: clamp(rect.y + dy, 0, bounds.height - rect.height),\n };\n}\n\n/** A corner or an edge of a crop frame. */\nexport type Handle = 'nw' | 'n' | 'ne' | 'e' | 'se' | 's' | 'sw' | 'w';\n\n/** Moves the edges of a rectangle that a handle holds, within bounds, keeping a ratio if any. */\nexport function resizeRect(\n rect: Rect,\n handle: Handle,\n dx: number,\n dy: number,\n ratio: number | null,\n bounds: Size,\n): Rect {\n const min = Math.min(bounds.width, bounds.height) * 0.05;\n const west = handle.includes('w');\n const north = handle.includes('n');\n if (ratio !== null) {\n const anchor = {\n x: west ? rect.x + rect.width : rect.x,\n y: north ? rect.y + rect.height : rect.y,\n };\n const roomX = west ? anchor.x : bounds.width - anchor.x;\n const roomY = north ? anchor.y : bounds.height - anchor.y;\n const largest = Math.min(roomX, roomY * ratio);\n const width = clamp(\n west ? rect.width - dx : rect.width + dx,\n Math.min(min, largest),\n largest,\n );\n const height = width / ratio;\n return {\n x: west ? anchor.x - width : anchor.x,\n y: north ? anchor.y - height : anchor.y,\n width,\n height,\n };\n }\n let left = rect.x;\n let right = rect.x + rect.width;\n let top = rect.y;\n let bottom = rect.y + rect.height;\n if (west) {\n left = clamp(left + dx, 0, right - min);\n } else if (handle.includes('e')) {\n right = clamp(right + dx, left + min, bounds.width);\n }\n if (north) {\n top = clamp(top + dy, 0, bottom - min);\n } else if (handle.includes('s')) {\n bottom = clamp(bottom + dy, top + min, bounds.height);\n }\n return { x: left, y: top, width: right - left, height: bottom - top };\n}\n\n/** Where a handle sits on a rectangle. */\nexport function handlePoint(rect: Rect, handle: Handle): Point {\n const x = handle.includes('w')\n ? rect.x\n : handle.includes('e')\n ? rect.x + rect.width\n : rect.x + rect.width / 2;\n const y = handle.includes('n')\n ? rect.y\n : handle.includes('s')\n ? rect.y + rect.height\n : rect.y + rect.height / 2;\n return { x, y };\n}\n\n/** Whether two transforms have the same edits, whatever the order of their keys. */\nexport function sameEdits(a: Edits | null | undefined, b: Edits | null | undefined): boolean {\n const normalized = (edits: Edits | null | undefined) =>\n JSON.stringify({\n crop: edits?.crop\n ? [edits.crop.x, edits.crop.y, edits.crop.width, edits.crop.height]\n : null,\n rotate: edits?.rotate ?? 0,\n flipX: edits?.flipX === true,\n flipY: edits?.flipY === true,\n focalPoint: edits?.focalPoint ? [edits.focalPoint.x, edits.focalPoint.y] : null,\n });\n return normalized(a) === normalized(b);\n}\n"],"mappings":";AAoBA,IAAMW,SAASC,OAAeC,KAAaC,QAAgBC,KAAKF,IAAIE,KAAKD,IAAIF,OAAOC,GAAG,GAAGC,GAAG;;AAG7F,SAAgBE,SAASC,MAAYC,SAAmB,GAAS;CAC7D,OAAOA,WAAW,MAAMA,WAAW,MAC7B;EAAEhB,OAAOe,KAAKd;EAAQA,QAAQc,KAAKf;CAAM,IACzC;EAAEA,OAAOe,KAAKf;EAAOC,QAAQc,KAAKd;CAAO;AACnD;AAEA,SAASgB,YAAY,EAAEd,GAAGC,KAAY,EAAEJ,OAAOC,UAAgBe,QAAyB;CACpF,QAAQA,QAAR;EACI,KAAK,IACD,OAAO;GAAEb,GAAGF,SAASG;GAAGA,GAAGD;EAAE;EACjC,KAAK,KACD,OAAO;GAAEA,GAAGH,QAAQG;GAAGC,GAAGH,SAASG;EAAE;EACzC,KAAK,KACD,OAAO;GAAED,GAAGC;GAAGA,GAAGJ,QAAQG;EAAE;EAChC,SACI,OAAO;GAAEA;GAAGC;EAAE;CACtB;AACJ;;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+B,KAAK5B,IAAI4B,KAAK/B,SAAS;CACjB;AAClB;;AAGA,SAAgBsE,UAAU3C,GAA6BC,GAAsC;CACzF,MAAM2C,cAAcpD,UAChBqD,KAAKC,UAAU;EACXrC,MAAMjB,OAAOiB,OACP;GAACjB,MAAMiB,KAAKlC;GAAGiB,MAAMiB,KAAKjC;GAAGgB,MAAMiB,KAAKrC;GAAOoB,MAAMiB,KAAKpC;EAAM,IAChE;EACNe,QAAQI,OAAOJ,UAAU;EACzBO,OAAOH,OAAOG,UAAU;EACxBC,OAAOJ,OAAOI,UAAU;EACxBqB,YAAYzB,OAAOyB,aAAa,CAACzB,MAAMyB,WAAW1C,GAAGiB,MAAMyB,WAAWzC,CAAC,IAAI;CAC/E,CAAC;CACL,OAAOoE,WAAW5C,CAAC,MAAM4C,WAAW3C,CAAC;AACzC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { ImageEditor, ImageEditorDialog } from './ImageEditor.js';
|
|
2
|
+
export { useImageEditorTools } from './tools.js';
|
|
3
|
+
export type { EditedImage, ImageEditorDialogProps, ImageEditorProps, ImageEditorTool, ImageEditorToolContext, TransformBase, } from './types.js';
|
|
4
|
+
export type { Point, Rect, Size } from './geometry.js';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/editor/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,WAAW,EAAE,iBAAiB,EAAE,MAAM,eAAe,CAAC;AAC/D,OAAO,EAAE,mBAAmB,EAAE,MAAM,SAAS,CAAC;AAC9C,YAAY,EACR,WAAW,EACX,sBAAsB,EACtB,gBAAgB,EAChB,eAAe,EACf,sBAAsB,EACtB,aAAa,GAChB,MAAM,SAAS,CAAC;AACjB,YAAY,EAAE,KAAK,EAAE,IAAI,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC"}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { ImageEditorTool } from './types.js';
|
|
2
|
+
/**
|
|
3
|
+
* The tools of the editor by default: crop, rotate (with flips) and focal point. A product passes
|
|
4
|
+
* them to `ImageEditor`'s `tools` with its own, or without the ones a field doesn't offer.
|
|
5
|
+
*/
|
|
6
|
+
export declare function useImageEditorTools(): {
|
|
7
|
+
crop: ImageEditorTool;
|
|
8
|
+
rotate: ImageEditorTool;
|
|
9
|
+
focalPoint: ImageEditorTool;
|
|
10
|
+
};
|
|
11
|
+
//# sourceMappingURL=tools.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"tools.d.ts","sourceRoot":"","sources":["../../src/editor/tools.tsx"],"names":[],"mappings":"AAyBA,OAAO,KAAK,EAAE,eAAe,EAA0B,MAAM,SAAS,CAAC;AAmcvE;;;GAGG;AACH,wBAAgB,mBAAmB,IAAI;IACnC,IAAI,EAAE,eAAe,CAAC;IACtB,MAAM,EAAE,eAAe,CAAC;IACxB,UAAU,EAAE,eAAe,CAAC;CAC/B,CA2BA"}
|