@onekeyfe/react-native-image-crop-picker 6.7.0-20 → 6.7.0-22
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 +12 -3
- package/lib/module/index.js +2 -14
- package/lib/module/index.js.map +1 -1
- package/lib/module/index.web.js +203 -0
- package/lib/module/index.web.js.map +1 -0
- package/lib/module/shared.js +19 -0
- package/lib/module/shared.js.map +1 -0
- package/lib/module/web/CropperView.js +570 -0
- package/lib/module/web/CropperView.js.map +1 -0
- package/lib/module/web/geometry.js +189 -0
- package/lib/module/web/geometry.js.map +1 -0
- package/lib/module/web/imageFormat.js +60 -0
- package/lib/module/web/imageFormat.js.map +1 -0
- package/lib/module/web/imageIO.js +153 -0
- package/lib/module/web/imageIO.js.map +1 -0
- package/lib/module/web/theme.js +98 -0
- package/lib/module/web/theme.js.map +1 -0
- package/lib/typescript/src/index.d.ts +4 -17
- package/lib/typescript/src/index.d.ts.map +1 -1
- package/lib/typescript/src/index.web.d.ts +18 -0
- package/lib/typescript/src/index.web.d.ts.map +1 -0
- package/lib/typescript/src/shared.d.ts +18 -0
- package/lib/typescript/src/shared.d.ts.map +1 -0
- package/lib/typescript/src/web/CropperView.d.ts +16 -0
- package/lib/typescript/src/web/CropperView.d.ts.map +1 -0
- package/lib/typescript/src/web/geometry.d.ts +53 -0
- package/lib/typescript/src/web/geometry.d.ts.map +1 -0
- package/lib/typescript/src/web/imageFormat.d.ts +3 -0
- package/lib/typescript/src/web/imageFormat.d.ts.map +1 -0
- package/lib/typescript/src/web/imageIO.d.ts +15 -0
- package/lib/typescript/src/web/imageIO.d.ts.map +1 -0
- package/lib/typescript/src/web/theme.d.ts +37 -0
- package/lib/typescript/src/web/theme.d.ts.map +1 -0
- package/package.json +12 -8
- package/src/index.tsx +16 -49
- package/src/index.web.tsx +288 -0
- package/src/shared.ts +51 -0
- package/src/web/CropperView.ts +725 -0
- package/src/web/geometry.ts +285 -0
- package/src/web/imageFormat.ts +63 -0
- package/src/web/imageIO.ts +178 -0
- package/src/web/theme.ts +130 -0
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"CropperView.d.ts","sourceRoot":"","sources":["../../../../src/web/CropperView.ts"],"names":[],"mappings":"AAiBA,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,qCAAqC,CAAC;AAClF,OAAO,KAAK,EAAE,SAAS,EAAmB,IAAI,EAAE,MAAM,YAAY,CAAC;AAOnE,MAAM,WAAW,kBAAkB;IACjC,KAAK,EAAE,gBAAgB,CAAC;IAGxB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,UAAU,CAAC,EAAE,MAAM,CAAC;IACpB,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,kBAAkB,CAAC,EAAE,OAAO,CAAC;IAC7B,cAAc,CAAC,EAAE,OAAO,CAAC;IACzB,QAAQ,CAAC,EAAE,OAAO,CAAC;IACnB,UAAU,CAAC,EAAE,sBAAsB,CAAC;CACrC;AAED,MAAM,MAAM,qBAAqB,CAAC,CAAC,IAAI,CACrC,KAAK,EAAE,SAAS,EAChB,GAAG,EAAE,IAAI,KACN,OAAO,CAAC,CAAC,CAAC,CAAC;AA6HhB,wBAAgB,cAAc,CAAC,CAAC,EAC9B,OAAO,EAAE,kBAAkB,EAC3B,SAAS,EAAE,qBAAqB,CAAC,CAAC,CAAC,GAClC,OAAO,CAAC,CAAC,CAAC,CA0iBZ"}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
export interface Size {
|
|
2
|
+
width: number;
|
|
3
|
+
height: number;
|
|
4
|
+
}
|
|
5
|
+
export interface Point {
|
|
6
|
+
x: number;
|
|
7
|
+
y: number;
|
|
8
|
+
}
|
|
9
|
+
export type Rotation = 0 | 90 | 180 | 270;
|
|
10
|
+
export interface CropState {
|
|
11
|
+
rotation: Rotation;
|
|
12
|
+
zoom: number;
|
|
13
|
+
offsetX: number;
|
|
14
|
+
offsetY: number;
|
|
15
|
+
}
|
|
16
|
+
export interface Rect {
|
|
17
|
+
x: number;
|
|
18
|
+
y: number;
|
|
19
|
+
width: number;
|
|
20
|
+
height: number;
|
|
21
|
+
}
|
|
22
|
+
export declare const MIN_ZOOM = 1;
|
|
23
|
+
export declare const MAX_ZOOM = 8;
|
|
24
|
+
export declare const CROP_BOX_PADDING = 20;
|
|
25
|
+
export declare const MAX_OUTPUT_LONG_SIDE = 4096;
|
|
26
|
+
export declare const INITIAL_STATE: CropState;
|
|
27
|
+
export declare function clamp(value: number, min: number, max: number): number;
|
|
28
|
+
export declare function rotatedSize(image: Size, rotation: Rotation): Size;
|
|
29
|
+
export interface CropAspectOptions {
|
|
30
|
+
image: Size;
|
|
31
|
+
rotation: Rotation;
|
|
32
|
+
targetAspectRatio?: number;
|
|
33
|
+
circular?: boolean;
|
|
34
|
+
}
|
|
35
|
+
export declare function cropAspectRatio({ image, rotation, targetAspectRatio, circular, }: CropAspectOptions): number;
|
|
36
|
+
export declare function fitCropBox(area: Size, aspectRatio: number): Size;
|
|
37
|
+
export declare function coverScale(image: Size, box: Size, rotation: Rotation): number;
|
|
38
|
+
export declare function scaleOf(state: CropState, image: Size, box: Size): number;
|
|
39
|
+
export declare function constrain(state: CropState, image: Size, box: Size): CropState;
|
|
40
|
+
export declare function pan(state: CropState, dx: number, dy: number, image: Size, box: Size): CropState;
|
|
41
|
+
export declare function zoomAt(state: CropState, zoom: number, anchor: Point, image: Size, box: Size): CropState;
|
|
42
|
+
export declare function rotateCounterclockwise(state: CropState, image: Size, box: Size): CropState;
|
|
43
|
+
export declare function rescale(state: CropState, previousBox: Size, box: Size, image: Size): CropState;
|
|
44
|
+
export declare function cropRectOf(state: CropState, image: Size, box: Size): Rect;
|
|
45
|
+
export interface OutputLimits {
|
|
46
|
+
targetSize?: Size;
|
|
47
|
+
maxWidth?: number;
|
|
48
|
+
maxHeight?: number;
|
|
49
|
+
}
|
|
50
|
+
export declare function outputSizeOf(crop: Size, limits: OutputLimits): Size;
|
|
51
|
+
export declare function shrinkToFit(size: Size, maxWidth?: number, maxHeight?: number): Size;
|
|
52
|
+
export declare function isSameState(a: CropState, b: CropState): boolean;
|
|
53
|
+
//# sourceMappingURL=geometry.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"geometry.d.ts","sourceRoot":"","sources":["../../../../src/web/geometry.ts"],"names":[],"mappings":"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"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"imageFormat.d.ts","sourceRoot":"","sources":["../../../../src/web/imageFormat.ts"],"names":[],"mappings":"AAKA,eAAO,MAAM,mBAAmB,KAAK,CAAC;AAkBtC,wBAAgB,eAAe,CAAC,KAAK,EAAE,UAAU,GAAG,MAAM,GAAG,SAAS,CAuCrE"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { CropState, Size } from './geometry';
|
|
2
|
+
export declare const RESULT_MIME = "image/jpeg";
|
|
3
|
+
export declare const DEFAULT_QUALITY = 0.8;
|
|
4
|
+
export declare const MAX_INPUT_BYTES: number;
|
|
5
|
+
export declare function createResultUrl(blob: Blob): string;
|
|
6
|
+
export declare function releaseResultUrl(url: string): void;
|
|
7
|
+
export declare function releaseAllResultUrls(): void;
|
|
8
|
+
export declare function imageSizeOf(image: HTMLImageElement): Size;
|
|
9
|
+
export declare function loadImage(src: string): Promise<HTMLImageElement>;
|
|
10
|
+
export declare function renderCrop(image: HTMLImageElement, state: CropState, box: Size, output: Size, quality: number): Promise<Blob>;
|
|
11
|
+
export declare function renderWhole(image: HTMLImageElement, output: Size, quality: number): Promise<Blob>;
|
|
12
|
+
export declare function readImageHeader(blob: Blob): Promise<Uint8Array>;
|
|
13
|
+
export declare function blobToBase64(blob: Blob): Promise<string>;
|
|
14
|
+
export declare function normalizeQuality(quality: number | undefined): number;
|
|
15
|
+
//# sourceMappingURL=imageIO.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"imageIO.d.ts","sourceRoot":"","sources":["../../../../src/web/imageIO.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,SAAS,EAAE,IAAI,EAAE,MAAM,YAAY,CAAC;AAElD,eAAO,MAAM,WAAW,eAAe,CAAC;AAExC,eAAO,MAAM,eAAe,MAAM,CAAC;AAEnC,eAAO,MAAM,eAAe,QAAmB,CAAC;AAMhD,wBAAgB,eAAe,CAAC,IAAI,EAAE,IAAI,GAAG,MAAM,CAIlD;AAED,wBAAgB,gBAAgB,CAAC,GAAG,EAAE,MAAM,GAAG,IAAI,CAIlD;AAED,wBAAgB,oBAAoB,IAAI,IAAI,CAG3C;AAED,wBAAgB,WAAW,CAAC,KAAK,EAAE,gBAAgB,GAAG,IAAI,CAEzD;AAID,wBAAgB,SAAS,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,gBAAgB,CAAC,CAkBhE;AA6DD,wBAAgB,UAAU,CACxB,KAAK,EAAE,gBAAgB,EACvB,KAAK,EAAE,SAAS,EAChB,GAAG,EAAE,IAAI,EACT,MAAM,EAAE,IAAI,EACZ,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,IAAI,CAAC,CAWf;AAGD,wBAAgB,WAAW,CACzB,KAAK,EAAE,gBAAgB,EACvB,MAAM,EAAE,IAAI,EACZ,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,IAAI,CAAC,CAIf;AAGD,wBAAgB,eAAe,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,UAAU,CAAC,CAO/D;AAGD,wBAAgB,YAAY,CAAC,IAAI,EAAE,IAAI,GAAG,OAAO,CAAC,MAAM,CAAC,CAUxD;AAED,wBAAgB,gBAAgB,CAAC,OAAO,EAAE,MAAM,GAAG,SAAS,GAAG,MAAM,CAKpE"}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import type { ImageCropperAppearance, ImageCropperColorScheme } from '../ReactNativeImageCropPicker.nitro';
|
|
2
|
+
declare const PALETTES: {
|
|
3
|
+
readonly light: {
|
|
4
|
+
readonly backgroundColor: "#FFFFFF";
|
|
5
|
+
readonly titleColor: "#000000DF";
|
|
6
|
+
readonly iconColor: "#0000009B";
|
|
7
|
+
readonly cancelButtonColor: "#0000000F";
|
|
8
|
+
readonly cancelButtonPressedColor: "#0000001F";
|
|
9
|
+
readonly cancelButtonTextColor: "#000000DF";
|
|
10
|
+
readonly confirmButtonColor: "#000000DF";
|
|
11
|
+
readonly confirmButtonPressedColor: "#0000009B";
|
|
12
|
+
readonly confirmButtonTextColor: "#FFFFFFED";
|
|
13
|
+
};
|
|
14
|
+
readonly dark: {
|
|
15
|
+
readonly backgroundColor: "#0F0F0F";
|
|
16
|
+
readonly titleColor: "#FFFFFFED";
|
|
17
|
+
readonly iconColor: "#FFFFFFAF";
|
|
18
|
+
readonly cancelButtonColor: "#FFFFFF12";
|
|
19
|
+
readonly cancelButtonPressedColor: "#FFFFFF22";
|
|
20
|
+
readonly cancelButtonTextColor: "#FFFFFFED";
|
|
21
|
+
readonly confirmButtonColor: "#FFFFFFED";
|
|
22
|
+
readonly confirmButtonPressedColor: "#FFFFFF72";
|
|
23
|
+
readonly confirmButtonTextColor: "#000000DF";
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
type PaletteKey = keyof (typeof PALETTES)['light'];
|
|
27
|
+
export type WebCropperTheme = Record<PaletteKey, string> & {
|
|
28
|
+
colorScheme: ImageCropperColorScheme;
|
|
29
|
+
dimmedColor: string;
|
|
30
|
+
titleFontFamily?: string;
|
|
31
|
+
buttonFontFamily?: string;
|
|
32
|
+
scale: number;
|
|
33
|
+
};
|
|
34
|
+
export declare function parseHexColor(value: string): [number, number, number, number] | undefined;
|
|
35
|
+
export declare function resolveTheme(appearance: ImageCropperAppearance | undefined): WebCropperTheme;
|
|
36
|
+
export {};
|
|
37
|
+
//# sourceMappingURL=theme.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"theme.d.ts","sourceRoot":"","sources":["../../../../src/web/theme.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,sBAAsB,EACtB,uBAAuB,EACxB,MAAM,qCAAqC,CAAC;AAI7C,QAAA,MAAM,QAAQ;;;;;;;;;;;;;;;;;;;;;;;CAuBJ,CAAC;AAEX,KAAK,UAAU,GAAG,MAAM,CAAC,OAAO,QAAQ,CAAC,CAAC,OAAO,CAAC,CAAC;AAEnD,MAAM,MAAM,eAAe,GAAG,MAAM,CAAC,UAAU,EAAE,MAAM,CAAC,GAAG;IACzD,WAAW,EAAE,uBAAuB,CAAC;IAErC,WAAW,EAAE,MAAM,CAAC;IACpB,eAAe,CAAC,EAAE,MAAM,CAAC;IACzB,gBAAgB,CAAC,EAAE,MAAM,CAAC;IAC1B,KAAK,EAAE,MAAM,CAAC;CACf,CAAC;AAgBF,wBAAgB,aAAa,CAC3B,KAAK,EAAE,MAAM,GACZ,CAAC,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,CAAC,GAAG,SAAS,CAgB9C;AAqBD,wBAAgB,YAAY,CAC1B,UAAU,EAAE,sBAAsB,GAAG,SAAS,GAC7C,eAAe,CA8BjB"}
|
package/package.json
CHANGED
|
@@ -1,13 +1,16 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@onekeyfe/react-native-image-crop-picker",
|
|
3
|
-
"version": "6.7.0-
|
|
3
|
+
"version": "6.7.0-22",
|
|
4
4
|
"description": "Single photo picker and cropper Nitro module for OneKey, replacing react-native-image-crop-picker",
|
|
5
5
|
"main": "./lib/module/index.js",
|
|
6
|
+
"react-native": "./lib/module/index.js",
|
|
7
|
+
"browser": "./lib/module/index.web.js",
|
|
6
8
|
"types": "./lib/typescript/src/index.d.ts",
|
|
7
9
|
"exports": {
|
|
8
10
|
".": {
|
|
9
11
|
"source": "./src/index.tsx",
|
|
10
12
|
"types": "./lib/typescript/src/index.d.ts",
|
|
13
|
+
"browser": "./lib/module/index.web.js",
|
|
11
14
|
"default": "./lib/module/index.js"
|
|
12
15
|
},
|
|
13
16
|
"./package.json": "./package.json"
|
|
@@ -49,7 +52,8 @@
|
|
|
49
52
|
"image",
|
|
50
53
|
"picker",
|
|
51
54
|
"crop",
|
|
52
|
-
"cropping"
|
|
55
|
+
"cropping",
|
|
56
|
+
"web"
|
|
53
57
|
],
|
|
54
58
|
"repository": {
|
|
55
59
|
"type": "git",
|
|
@@ -70,11 +74,11 @@
|
|
|
70
74
|
"@eslint/compat": "^1.3.2",
|
|
71
75
|
"@eslint/eslintrc": "^3.3.1",
|
|
72
76
|
"@eslint/js": "^9.35.0",
|
|
73
|
-
"@react-native/babel-preset": "0.
|
|
74
|
-
"@react-native/eslint-config": "0.
|
|
77
|
+
"@react-native/babel-preset": "0.88.0",
|
|
78
|
+
"@react-native/eslint-config": "0.88.0",
|
|
75
79
|
"@release-it/conventional-changelog": "^10.0.1",
|
|
76
80
|
"@types/jest": "^29.5.14",
|
|
77
|
-
"@types/react": "^19.
|
|
81
|
+
"@types/react": "^19.3.0",
|
|
78
82
|
"commitlint": "^19.8.1",
|
|
79
83
|
"del-cli": "^6.0.0",
|
|
80
84
|
"eslint": "^9.35.0",
|
|
@@ -84,8 +88,8 @@
|
|
|
84
88
|
"lefthook": "^2.0.3",
|
|
85
89
|
"nitrogen": "0.37.0",
|
|
86
90
|
"prettier": "^2.8.8",
|
|
87
|
-
"react": "19.
|
|
88
|
-
"react-native": "patch:react-native@npm%3A0.
|
|
91
|
+
"react": "19.3.0",
|
|
92
|
+
"react-native": "patch:react-native@npm%3A0.88.0#~/.yarn/patches/react-native-npm-0.88.0-60c6201dd2.patch",
|
|
89
93
|
"react-native-builder-bob": "^0.40.13",
|
|
90
94
|
"react-native-nitro-modules": "0.37.0",
|
|
91
95
|
"release-it": "^19.0.4",
|
|
@@ -130,7 +134,7 @@
|
|
|
130
134
|
"useTabs": false
|
|
131
135
|
},
|
|
132
136
|
"jest": {
|
|
133
|
-
"preset": "react-native",
|
|
137
|
+
"preset": "@react-native/jest-preset",
|
|
134
138
|
"modulePathIgnorePatterns": [
|
|
135
139
|
"<rootDir>/example/node_modules",
|
|
136
140
|
"<rootDir>/lib/"
|
package/src/index.tsx
CHANGED
|
@@ -1,12 +1,23 @@
|
|
|
1
1
|
import { NitroModules } from 'react-native-nitro-modules';
|
|
2
2
|
|
|
3
|
-
import type {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
3
|
+
import type { ReactNativeImageCropPicker as ReactNativeImageCropPickerSpec } from './ReactNativeImageCropPicker.nitro';
|
|
4
|
+
|
|
5
|
+
import {
|
|
6
|
+
ERROR_CODES,
|
|
7
|
+
ImageCropPickerError,
|
|
8
|
+
type CropperOptions,
|
|
9
|
+
type Image,
|
|
10
|
+
type Options,
|
|
11
|
+
} from './shared';
|
|
8
12
|
|
|
9
13
|
export type * from './ReactNativeImageCropPicker.nitro';
|
|
14
|
+
export { ImageCropPickerError } from './shared';
|
|
15
|
+
export type {
|
|
16
|
+
CropperOptions,
|
|
17
|
+
Image,
|
|
18
|
+
ImageCropPickerErrorCode,
|
|
19
|
+
Options,
|
|
20
|
+
} from './shared';
|
|
10
21
|
|
|
11
22
|
// Created on first use so importing this module costs nothing at startup.
|
|
12
23
|
let hybridObject: ReactNativeImageCropPickerSpec | undefined;
|
|
@@ -21,34 +32,6 @@ export function getReactNativeImageCropPicker(): ReactNativeImageCropPickerSpec
|
|
|
21
32
|
return hybridObject;
|
|
22
33
|
}
|
|
23
34
|
|
|
24
|
-
const ERROR_CODES = [
|
|
25
|
-
'E_PICKER_CANCELLED',
|
|
26
|
-
'E_PICKER_IN_PROGRESS',
|
|
27
|
-
'E_ACTIVITY_DOES_NOT_EXIST',
|
|
28
|
-
'E_FAILED_TO_SHOW_PICKER',
|
|
29
|
-
'E_NO_IMAGE_DATA_FOUND',
|
|
30
|
-
'E_CROPPER_IMAGE_NOT_FOUND',
|
|
31
|
-
'E_CANNOT_SAVE_IMAGE',
|
|
32
|
-
'E_LOW_MEMORY_ERROR',
|
|
33
|
-
'E_ERROR_WHILE_CLEANING_FILES',
|
|
34
|
-
] as const;
|
|
35
|
-
|
|
36
|
-
export type ImageCropPickerErrorCode =
|
|
37
|
-
| (typeof ERROR_CODES)[number]
|
|
38
|
-
| 'E_UNKNOWN';
|
|
39
|
-
|
|
40
|
-
// Rejections carry the same `code` values as react-native-image-crop-picker,
|
|
41
|
-
// e.g. `E_PICKER_CANCELLED` when the user dismisses the picker or cropper.
|
|
42
|
-
export class ImageCropPickerError extends Error {
|
|
43
|
-
readonly code: ImageCropPickerErrorCode;
|
|
44
|
-
|
|
45
|
-
constructor(code: ImageCropPickerErrorCode, message: string) {
|
|
46
|
-
super(message);
|
|
47
|
-
this.name = 'ImageCropPickerError';
|
|
48
|
-
this.code = code;
|
|
49
|
-
}
|
|
50
|
-
}
|
|
51
|
-
|
|
52
35
|
function isErrorCode(value: string): value is (typeof ERROR_CODES)[number] {
|
|
53
36
|
return (ERROR_CODES as readonly string[]).includes(value);
|
|
54
37
|
}
|
|
@@ -73,22 +56,6 @@ function toImageCropPickerError(error: unknown): ImageCropPickerError {
|
|
|
73
56
|
);
|
|
74
57
|
}
|
|
75
58
|
|
|
76
|
-
// Options accepted for source compatibility with react-native-image-crop-picker.
|
|
77
|
-
// Only single photos are supported; processed results are JPEG.
|
|
78
|
-
export interface Options extends ImageCropPickerOptions {
|
|
79
|
-
mediaType?: 'photo';
|
|
80
|
-
multiple?: false;
|
|
81
|
-
forceJpg?: boolean;
|
|
82
|
-
// The system photo picker controls ordering.
|
|
83
|
-
sortOrder?: 'none' | 'asc' | 'desc';
|
|
84
|
-
}
|
|
85
|
-
|
|
86
|
-
export interface CropperOptions extends Options {
|
|
87
|
-
path: string;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
export type Image = PickedImage;
|
|
91
|
-
|
|
92
59
|
export async function openPicker(options: Options = {}): Promise<Image> {
|
|
93
60
|
try {
|
|
94
61
|
return await getReactNativeImageCropPicker().openPicker(options);
|
|
@@ -0,0 +1,288 @@
|
|
|
1
|
+
/// <reference lib="dom" />
|
|
2
|
+
import { ImageCropPickerError } from './shared';
|
|
3
|
+
import { cropRectOf, outputSizeOf, shrinkToFit } from './web/geometry';
|
|
4
|
+
import {
|
|
5
|
+
MAX_INPUT_BYTES,
|
|
6
|
+
RESULT_MIME,
|
|
7
|
+
blobToBase64,
|
|
8
|
+
createResultUrl,
|
|
9
|
+
imageSizeOf,
|
|
10
|
+
loadImage,
|
|
11
|
+
normalizeQuality,
|
|
12
|
+
readImageHeader,
|
|
13
|
+
releaseAllResultUrls,
|
|
14
|
+
releaseResultUrl,
|
|
15
|
+
renderCrop,
|
|
16
|
+
renderWhole,
|
|
17
|
+
} from './web/imageIO';
|
|
18
|
+
import { presentCropper } from './web/CropperView';
|
|
19
|
+
import { detectImageMime } from './web/imageFormat';
|
|
20
|
+
|
|
21
|
+
import type { ReactNativeImageCropPicker as ReactNativeImageCropPickerSpec } from './ReactNativeImageCropPicker.nitro';
|
|
22
|
+
import type { CropperOptions, Image, Options } from './shared';
|
|
23
|
+
import type { Size } from './web/geometry';
|
|
24
|
+
|
|
25
|
+
export type * from './ReactNativeImageCropPicker.nitro';
|
|
26
|
+
export { ImageCropPickerError } from './shared';
|
|
27
|
+
export type {
|
|
28
|
+
CropperOptions,
|
|
29
|
+
Image,
|
|
30
|
+
ImageCropPickerErrorCode,
|
|
31
|
+
Options,
|
|
32
|
+
} from './shared';
|
|
33
|
+
|
|
34
|
+
// Web entry: the same API and results as the native module. The picker is a
|
|
35
|
+
// file input, the cropper is drawn with the DOM (web/CropperView.ts), and
|
|
36
|
+
// result paths are blob: URLs that clean/cleanSingle revoke.
|
|
37
|
+
|
|
38
|
+
export function getReactNativeImageCropPicker(): ReactNativeImageCropPickerSpec {
|
|
39
|
+
throw new ImageCropPickerError(
|
|
40
|
+
'E_UNKNOWN',
|
|
41
|
+
'The Nitro hybrid object is not available on the web'
|
|
42
|
+
);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// One picker or cropper at a time, like native.
|
|
46
|
+
let busy = false;
|
|
47
|
+
|
|
48
|
+
async function exclusive<T>(task: () => Promise<T>): Promise<T> {
|
|
49
|
+
if (busy) {
|
|
50
|
+
throw new ImageCropPickerError(
|
|
51
|
+
'E_PICKER_IN_PROGRESS',
|
|
52
|
+
'Another picker or cropper is still open'
|
|
53
|
+
);
|
|
54
|
+
}
|
|
55
|
+
busy = true;
|
|
56
|
+
try {
|
|
57
|
+
return await task();
|
|
58
|
+
} finally {
|
|
59
|
+
busy = false;
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function assertDocument(): void {
|
|
64
|
+
if (typeof document === 'undefined' || !document.body) {
|
|
65
|
+
throw new ImageCropPickerError(
|
|
66
|
+
'E_FAILED_TO_SHOW_PICKER',
|
|
67
|
+
'No document to show the picker in'
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function cancelledError(): ImageCropPickerError {
|
|
73
|
+
return new ImageCropPickerError(
|
|
74
|
+
'E_PICKER_CANCELLED',
|
|
75
|
+
'User cancelled image selection'
|
|
76
|
+
);
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
function targetSizeOf(options: Options): Size | undefined {
|
|
80
|
+
const { width, height } = options;
|
|
81
|
+
if (width && height && width > 0 && height > 0) {
|
|
82
|
+
return { width, height };
|
|
83
|
+
}
|
|
84
|
+
return undefined;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function chooseFile(): Promise<File> {
|
|
88
|
+
return new Promise((resolve, reject) => {
|
|
89
|
+
const input = document.createElement('input');
|
|
90
|
+
input.type = 'file';
|
|
91
|
+
input.accept = 'image/*';
|
|
92
|
+
input.style.display = 'none';
|
|
93
|
+
let settled = false;
|
|
94
|
+
const finish = (file: File | undefined) => {
|
|
95
|
+
if (settled) {
|
|
96
|
+
return;
|
|
97
|
+
}
|
|
98
|
+
settled = true;
|
|
99
|
+
window.removeEventListener('focus', onWindowFocus);
|
|
100
|
+
input.remove();
|
|
101
|
+
if (file) {
|
|
102
|
+
resolve(file);
|
|
103
|
+
} else {
|
|
104
|
+
reject(cancelledError());
|
|
105
|
+
}
|
|
106
|
+
};
|
|
107
|
+
// Browsers without the input `cancel` event only report a dismissed
|
|
108
|
+
// chooser by giving focus back; `change` arrives first when a file was
|
|
109
|
+
// picked.
|
|
110
|
+
const onWindowFocus = () => {
|
|
111
|
+
setTimeout(() => finish(input.files?.[0]), 1000);
|
|
112
|
+
};
|
|
113
|
+
input.addEventListener('change', () => finish(input.files?.[0]));
|
|
114
|
+
input.addEventListener('cancel', () => finish(undefined));
|
|
115
|
+
if (!('oncancel' in input)) {
|
|
116
|
+
window.addEventListener('focus', onWindowFocus);
|
|
117
|
+
}
|
|
118
|
+
document.body.appendChild(input);
|
|
119
|
+
input.click();
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
async function decodeFile(file: File): Promise<HTMLImageElement> {
|
|
124
|
+
if (file.size > MAX_INPUT_BYTES) {
|
|
125
|
+
throw new ImageCropPickerError(
|
|
126
|
+
'E_NO_IMAGE_DATA_FOUND',
|
|
127
|
+
'The selected image is larger than 64 MiB'
|
|
128
|
+
);
|
|
129
|
+
}
|
|
130
|
+
const url = URL.createObjectURL(file);
|
|
131
|
+
try {
|
|
132
|
+
return await loadImage(url);
|
|
133
|
+
} catch {
|
|
134
|
+
URL.revokeObjectURL(url);
|
|
135
|
+
throw new ImageCropPickerError(
|
|
136
|
+
'E_NO_IMAGE_DATA_FOUND',
|
|
137
|
+
'The selected file is not a readable image'
|
|
138
|
+
);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
// `mime` always describes the bytes in `blob`: RESULT_MIME for everything this
|
|
143
|
+
// module encodes, the sniffed format for a preserved original.
|
|
144
|
+
async function makeResult(
|
|
145
|
+
blob: Blob,
|
|
146
|
+
size: Size,
|
|
147
|
+
mime: string,
|
|
148
|
+
options: Options,
|
|
149
|
+
extra: Pick<Image, 'cropRect' | 'filename'>
|
|
150
|
+
): Promise<Image> {
|
|
151
|
+
return {
|
|
152
|
+
path: createResultUrl(blob),
|
|
153
|
+
size: blob.size,
|
|
154
|
+
width: size.width,
|
|
155
|
+
height: size.height,
|
|
156
|
+
mime,
|
|
157
|
+
data: options.includeBase64 ? await blobToBase64(blob) : undefined,
|
|
158
|
+
...extra,
|
|
159
|
+
};
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
function crop(
|
|
163
|
+
image: HTMLImageElement,
|
|
164
|
+
options: Options,
|
|
165
|
+
filename: string | undefined
|
|
166
|
+
): Promise<Image> {
|
|
167
|
+
const targetSize = targetSizeOf(options);
|
|
168
|
+
const quality = normalizeQuality(options.compressImageQuality);
|
|
169
|
+
return presentCropper(
|
|
170
|
+
{
|
|
171
|
+
image,
|
|
172
|
+
aspectRatio: targetSize
|
|
173
|
+
? targetSize.width / targetSize.height
|
|
174
|
+
: undefined,
|
|
175
|
+
title: options.cropperToolbarTitle,
|
|
176
|
+
cancelText: options.cropperCancelText,
|
|
177
|
+
confirmText: options.cropperChooseText,
|
|
178
|
+
rotateButtonHidden: options.cropperRotateButtonsHidden,
|
|
179
|
+
showGuidelines: options.showCropGuidelines,
|
|
180
|
+
circular: options.cropperCircleOverlay,
|
|
181
|
+
appearance: options.cropperAppearance,
|
|
182
|
+
},
|
|
183
|
+
async (state, box) => {
|
|
184
|
+
const cropRect = cropRectOf(state, imageSizeOf(image), box);
|
|
185
|
+
const output = outputSizeOf(cropRect, {
|
|
186
|
+
targetSize,
|
|
187
|
+
maxWidth: options.compressImageMaxWidth,
|
|
188
|
+
maxHeight: options.compressImageMaxHeight,
|
|
189
|
+
});
|
|
190
|
+
const blob = await renderCrop(image, state, box, output, quality);
|
|
191
|
+
return makeResult(blob, output, RESULT_MIME, options, {
|
|
192
|
+
cropRect,
|
|
193
|
+
filename,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async function pickWithoutCropping(
|
|
200
|
+
file: File,
|
|
201
|
+
image: HTMLImageElement,
|
|
202
|
+
options: Options
|
|
203
|
+
): Promise<Image> {
|
|
204
|
+
const size = imageSizeOf(image);
|
|
205
|
+
if (options.preserveOriginal) {
|
|
206
|
+
// The selected file as is; compression options are ignored. Its type
|
|
207
|
+
// comes from its bytes: `File.type` is only a guess from the name and can
|
|
208
|
+
// be empty or wrong.
|
|
209
|
+
const mime = detectImageMime(await readImageHeader(file));
|
|
210
|
+
if (!mime) {
|
|
211
|
+
throw new ImageCropPickerError(
|
|
212
|
+
'E_NO_IMAGE_DATA_FOUND',
|
|
213
|
+
'The selected file is not a recognized image format'
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
return makeResult(file, size, mime, options, { filename: file.name });
|
|
217
|
+
}
|
|
218
|
+
const output = shrinkToFit(
|
|
219
|
+
outputSizeOf(size, {}),
|
|
220
|
+
options.compressImageMaxWidth,
|
|
221
|
+
options.compressImageMaxHeight
|
|
222
|
+
);
|
|
223
|
+
const blob = await renderWhole(
|
|
224
|
+
image,
|
|
225
|
+
output,
|
|
226
|
+
normalizeQuality(options.compressImageQuality)
|
|
227
|
+
);
|
|
228
|
+
return makeResult(blob, output, RESULT_MIME, options, {
|
|
229
|
+
filename: file.name,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
export function openPicker(options: Options = {}): Promise<Image> {
|
|
234
|
+
return exclusive(async () => {
|
|
235
|
+
assertDocument();
|
|
236
|
+
const file = await chooseFile();
|
|
237
|
+
const image = await decodeFile(file);
|
|
238
|
+
try {
|
|
239
|
+
return options.cropping
|
|
240
|
+
? await crop(image, options, file.name)
|
|
241
|
+
: await pickWithoutCropping(file, image, options);
|
|
242
|
+
} finally {
|
|
243
|
+
URL.revokeObjectURL(image.src);
|
|
244
|
+
}
|
|
245
|
+
});
|
|
246
|
+
}
|
|
247
|
+
|
|
248
|
+
export function openCropper({
|
|
249
|
+
path,
|
|
250
|
+
...options
|
|
251
|
+
}: CropperOptions): Promise<Image> {
|
|
252
|
+
return exclusive(async () => {
|
|
253
|
+
assertDocument();
|
|
254
|
+
if (!path) {
|
|
255
|
+
throw new ImageCropPickerError(
|
|
256
|
+
'E_CROPPER_IMAGE_NOT_FOUND',
|
|
257
|
+
'No image path to crop'
|
|
258
|
+
);
|
|
259
|
+
}
|
|
260
|
+
let image: HTMLImageElement;
|
|
261
|
+
try {
|
|
262
|
+
image = await loadImage(path);
|
|
263
|
+
} catch {
|
|
264
|
+
throw new ImageCropPickerError(
|
|
265
|
+
'E_CROPPER_IMAGE_NOT_FOUND',
|
|
266
|
+
'Cannot load the image to crop'
|
|
267
|
+
);
|
|
268
|
+
}
|
|
269
|
+
return crop(image, options, undefined);
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
export async function clean(): Promise<void> {
|
|
274
|
+
releaseAllResultUrls();
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
export async function cleanSingle(path: string): Promise<void> {
|
|
278
|
+
releaseResultUrl(path);
|
|
279
|
+
}
|
|
280
|
+
|
|
281
|
+
const ImageCropPicker = {
|
|
282
|
+
openPicker,
|
|
283
|
+
openCropper,
|
|
284
|
+
clean,
|
|
285
|
+
cleanSingle,
|
|
286
|
+
};
|
|
287
|
+
|
|
288
|
+
export default ImageCropPicker;
|
package/src/shared.ts
ADDED
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ImageCropPickerOptions,
|
|
3
|
+
PickedImage,
|
|
4
|
+
} from './ReactNativeImageCropPicker.nitro';
|
|
5
|
+
|
|
6
|
+
// Shared by the native and web entry points, so both reject with the same
|
|
7
|
+
// error class and accept the same options.
|
|
8
|
+
|
|
9
|
+
export const ERROR_CODES = [
|
|
10
|
+
'E_PICKER_CANCELLED',
|
|
11
|
+
'E_PICKER_IN_PROGRESS',
|
|
12
|
+
'E_ACTIVITY_DOES_NOT_EXIST',
|
|
13
|
+
'E_FAILED_TO_SHOW_PICKER',
|
|
14
|
+
'E_NO_IMAGE_DATA_FOUND',
|
|
15
|
+
'E_CROPPER_IMAGE_NOT_FOUND',
|
|
16
|
+
'E_CANNOT_SAVE_IMAGE',
|
|
17
|
+
'E_LOW_MEMORY_ERROR',
|
|
18
|
+
'E_ERROR_WHILE_CLEANING_FILES',
|
|
19
|
+
] as const;
|
|
20
|
+
|
|
21
|
+
export type ImageCropPickerErrorCode =
|
|
22
|
+
| (typeof ERROR_CODES)[number]
|
|
23
|
+
| 'E_UNKNOWN';
|
|
24
|
+
|
|
25
|
+
// Rejections carry the same `code` values as react-native-image-crop-picker,
|
|
26
|
+
// e.g. `E_PICKER_CANCELLED` when the user dismisses the picker or cropper.
|
|
27
|
+
export class ImageCropPickerError extends Error {
|
|
28
|
+
readonly code: ImageCropPickerErrorCode;
|
|
29
|
+
|
|
30
|
+
constructor(code: ImageCropPickerErrorCode, message: string) {
|
|
31
|
+
super(message);
|
|
32
|
+
this.name = 'ImageCropPickerError';
|
|
33
|
+
this.code = code;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
// Options accepted for source compatibility with react-native-image-crop-picker.
|
|
38
|
+
// Only single photos are supported; processed results are JPEG.
|
|
39
|
+
export interface Options extends ImageCropPickerOptions {
|
|
40
|
+
mediaType?: 'photo';
|
|
41
|
+
multiple?: false;
|
|
42
|
+
forceJpg?: boolean;
|
|
43
|
+
// The system photo picker controls ordering.
|
|
44
|
+
sortOrder?: 'none' | 'asc' | 'desc';
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface CropperOptions extends Options {
|
|
48
|
+
path: string;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type Image = PickedImage;
|