@molecule/app-image-crop-cropperjs 1.0.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/LICENSE ADDED
@@ -0,0 +1,115 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work.
38
+
39
+ "Derivative Works" shall mean any work, whether in Source or Object
40
+ form, that is based on (or derived from) the Work and for which the
41
+ editorial revisions, annotations, elaborations, or other modifications
42
+ represent, as a whole, an original work of authorship.
43
+
44
+ "Contribution" shall mean any work of authorship, including the
45
+ original version of the Work and any modifications or additions
46
+ to that Work, that is intentionally submitted to the Licensor for
47
+ inclusion in the Work by the copyright owner or by an individual or
48
+ Legal Entity authorized to submit on behalf of the copyright owner.
49
+
50
+ "Contributor" shall mean Licensor and any individual or Legal Entity
51
+ on behalf of whom a Contribution has been received by the Licensor and
52
+ subsequently incorporated within the Work.
53
+
54
+ 2. Grant of Copyright License. Subject to the terms and conditions of
55
+ this License, each Contributor hereby grants to You a perpetual,
56
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
57
+ copyright license to reproduce, prepare Derivative Works of,
58
+ publicly display, publicly perform, sublicense, and distribute the
59
+ Work and such Derivative Works in Source or Object form.
60
+
61
+ 3. Grant of Patent License. Subject to the terms and conditions of
62
+ this License, each Contributor hereby grants to You a perpetual,
63
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
64
+ patent license to make, have made, use, offer to sell, sell, import,
65
+ and otherwise transfer the Work.
66
+
67
+ 4. Redistribution. You may reproduce and distribute copies of the
68
+ Work or Derivative Works thereof in any medium, with or without
69
+ modifications, and in Source or Object form, provided that You
70
+ meet the following conditions:
71
+
72
+ (a) You must give any other recipients of the Work or
73
+ Derivative Works a copy of this License; and
74
+
75
+ (b) You must cause any modified files to carry prominent notices
76
+ stating that You changed the files; and
77
+
78
+ (c) You must retain, in the Source form of any Derivative Works
79
+ that You distribute, all copyright, patent, trademark, and
80
+ attribution notices from the Source form of the Work,
81
+ excluding those notices that do not pertain to any part of
82
+ the Derivative Works; and
83
+
84
+ (d) If the Work includes a "NOTICE" text file as part of its
85
+ distribution, then any Derivative Works that You distribute must
86
+ include a readable copy of the attribution notices contained
87
+ within such NOTICE file.
88
+
89
+ 5. Submission of Contributions.
90
+
91
+ 6. Trademarks. This License does not grant permission to use the trade
92
+ names, trademarks, service marks, or product names of the Licensor.
93
+
94
+ 7. Disclaimer of Warranty. Unless required by applicable law or
95
+ agreed to in writing, Licensor provides the Work on an "AS IS" BASIS,
96
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND.
97
+
98
+ 8. Limitation of Liability. In no event and under no legal theory shall
99
+ any Contributor be liable to You for damages.
100
+
101
+ 9. Accepting Warranty or Additional Liability.
102
+
103
+ Copyright 2026 Molecule Dev, Inc.
104
+
105
+ Licensed under the Apache License, Version 2.0 (the "License");
106
+ you may not use this file except in compliance with the License.
107
+ You may obtain a copy of the License at
108
+
109
+ http://www.apache.org/licenses/LICENSE-2.0
110
+
111
+ Unless required by applicable law or agreed to in writing, software
112
+ distributed under the License is distributed on an "AS IS" BASIS,
113
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
114
+ See the License for the specific language governing permissions and
115
+ limitations under the License.
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Cropper.js image-crop provider for `@molecule/app-image-crop` — a REAL
3
+ * implementation backed by cropperjs v1. `createCropper({ src })` mounts a live
4
+ * `Cropper` on an image element and every instance method delegates to the
5
+ * corresponding cropperjs call, so `getCroppedCanvas()` returns the actual
6
+ * cropped `<canvas>` (call `.toBlob()` / `.toDataURL()` on it to export) — not a
7
+ * placeholder object.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { provider } from '@molecule/app-image-crop-cropperjs'
12
+ * import { setProvider, requireProvider } from '@molecule/app-image-crop'
13
+ *
14
+ * setProvider(provider) // once, at app startup (bonds.ts)
15
+ *
16
+ * const cropper = requireProvider().createCropper({ src: '/avatar.jpg', aspectRatio: 1 })
17
+ * const canvas = cropper.getCroppedCanvas({ width: 200, height: 200 })
18
+ * canvas.toBlob((blob) => uploadAvatar(blob), 'image/png')
19
+ * ```
20
+ *
21
+ * @remarks
22
+ * - **Import cropperjs's stylesheet yourself** — this package does NOT:
23
+ * `import 'cropperjs/dist/cropper.css'`. Without it the crop box, handles, and
24
+ * drag guides render unstyled (an invisible/broken cropper), the same way Quill
25
+ * needs its theme CSS.
26
+ * - **Browser-only.** `createCropper()` calls `document.createElement('img')` and
27
+ * `new Cropper(...)`; construct it in a client-only effect under SSR.
28
+ * - **cropperjs initializes on the image's `load` event.** Reading `getCropData()`
29
+ * or `getCroppedCanvas()` before the source has loaded returns empty/degenerate
30
+ * data — drive them after load, or set the region explicitly with `setCropData()`
31
+ * in natural-image coordinates first.
32
+ * - **`circular` is a UI concern, not a pixel op.** cropperjs has no circular
33
+ * pixel output; for a round avatar, style the crop box round in CSS
34
+ * (`.cropper-view-box, .cropper-face { border-radius: 50% }`) and draw the
35
+ * returned canvas into a rounded canvas before upload.
36
+ * - **`OutputOptions.quality` applies at encode time**, not to canvas generation —
37
+ * pass it to `canvas.toBlob(cb, 'image/jpeg', quality)` / `toDataURL(type, quality)`.
38
+ * - Provider defaults (`guides`, `background`, `viewMode`) come from
39
+ * `createProvider(config)`; per-cropper `CropperOptions.guides` wins over the
40
+ * config default. `maxWidth`/`maxHeight` are enforced on output (cropperjs has no
41
+ * max-crop-box constructor option).
42
+ * - **Wire it** with `setProvider()` from `@molecule/app-image-crop` or
43
+ * `bond('image-crop', provider)` from `@molecule/app-bond` — both route through
44
+ * the shared registry; `requireProvider()` throws until one has run.
45
+ *
46
+ * @module
47
+ */
48
+ export * from './provider.js';
49
+ export * from './types.js';
50
+ export { default as Cropper } from 'cropperjs';
51
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA8CG;AAEH,cAAc,eAAe,CAAA;AAC7B,cAAc,YAAY,CAAA;AAG1B,OAAO,EAAE,OAAO,IAAI,OAAO,EAAE,MAAM,WAAW,CAAA"}
package/dist/index.js ADDED
@@ -0,0 +1,51 @@
1
+ /**
2
+ * Cropper.js image-crop provider for `@molecule/app-image-crop` — a REAL
3
+ * implementation backed by cropperjs v1. `createCropper({ src })` mounts a live
4
+ * `Cropper` on an image element and every instance method delegates to the
5
+ * corresponding cropperjs call, so `getCroppedCanvas()` returns the actual
6
+ * cropped `<canvas>` (call `.toBlob()` / `.toDataURL()` on it to export) — not a
7
+ * placeholder object.
8
+ *
9
+ * @example
10
+ * ```typescript
11
+ * import { provider } from '@molecule/app-image-crop-cropperjs'
12
+ * import { setProvider, requireProvider } from '@molecule/app-image-crop'
13
+ *
14
+ * setProvider(provider) // once, at app startup (bonds.ts)
15
+ *
16
+ * const cropper = requireProvider().createCropper({ src: '/avatar.jpg', aspectRatio: 1 })
17
+ * const canvas = cropper.getCroppedCanvas({ width: 200, height: 200 })
18
+ * canvas.toBlob((blob) => uploadAvatar(blob), 'image/png')
19
+ * ```
20
+ *
21
+ * @remarks
22
+ * - **Import cropperjs's stylesheet yourself** — this package does NOT:
23
+ * `import 'cropperjs/dist/cropper.css'`. Without it the crop box, handles, and
24
+ * drag guides render unstyled (an invisible/broken cropper), the same way Quill
25
+ * needs its theme CSS.
26
+ * - **Browser-only.** `createCropper()` calls `document.createElement('img')` and
27
+ * `new Cropper(...)`; construct it in a client-only effect under SSR.
28
+ * - **cropperjs initializes on the image's `load` event.** Reading `getCropData()`
29
+ * or `getCroppedCanvas()` before the source has loaded returns empty/degenerate
30
+ * data — drive them after load, or set the region explicitly with `setCropData()`
31
+ * in natural-image coordinates first.
32
+ * - **`circular` is a UI concern, not a pixel op.** cropperjs has no circular
33
+ * pixel output; for a round avatar, style the crop box round in CSS
34
+ * (`.cropper-view-box, .cropper-face { border-radius: 50% }`) and draw the
35
+ * returned canvas into a rounded canvas before upload.
36
+ * - **`OutputOptions.quality` applies at encode time**, not to canvas generation —
37
+ * pass it to `canvas.toBlob(cb, 'image/jpeg', quality)` / `toDataURL(type, quality)`.
38
+ * - Provider defaults (`guides`, `background`, `viewMode`) come from
39
+ * `createProvider(config)`; per-cropper `CropperOptions.guides` wins over the
40
+ * config default. `maxWidth`/`maxHeight` are enforced on output (cropperjs has no
41
+ * max-crop-box constructor option).
42
+ * - **Wire it** with `setProvider()` from `@molecule/app-image-crop` or
43
+ * `bond('image-crop', provider)` from `@molecule/app-bond` — both route through
44
+ * the shared registry; `requireProvider()` throws until one has run.
45
+ *
46
+ * @module
47
+ */
48
+ export * from './provider.js';
49
+ export * from './types.js';
50
+ // Re-export Cropper for advanced usage (custom cropperjs options / events).
51
+ export { default as Cropper } from 'cropperjs';
@@ -0,0 +1,22 @@
1
+ /**
2
+ * Cropper.js image crop provider implementation.
3
+ *
4
+ * Wraps cropperjs v1 (`new Cropper(imageElement, options)`) behind the molecule
5
+ * `ImageCropProvider` / `CropperInstance` contract. Every instance method
6
+ * delegates to the corresponding real cropperjs call, so `getCroppedCanvas()`
7
+ * returns the actual cropped `<canvas>` — not a placeholder.
8
+ *
9
+ * @module
10
+ */
11
+ import type { ImageCropProvider } from '@molecule/app-image-crop';
12
+ import type { CropperjsConfig } from './types.js';
13
+ /**
14
+ * Creates a Cropper.js-based image crop provider.
15
+ *
16
+ * @param config - Optional provider-level defaults (`guides`, `background`, `viewMode`).
17
+ * @returns A configured `ImageCropProvider` backed by real cropperjs instances.
18
+ */
19
+ export declare function createProvider(config?: CropperjsConfig): ImageCropProvider;
20
+ /** Default Cropper.js provider instance. */
21
+ export declare const provider: ImageCropProvider;
22
+ //# sourceMappingURL=provider.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"provider.d.ts","sourceRoot":"","sources":["../src/provider.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,OAAO,KAAK,EAIV,iBAAiB,EAElB,MAAM,0BAA0B,CAAA;AAEjC,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,YAAY,CAAA;AA2EjD;;;;;GAKG;AACH,wBAAgB,cAAc,CAAC,MAAM,GAAE,eAAoB,GAAG,iBAAiB,CA8C9E;AAED,4CAA4C;AAC5C,eAAO,MAAM,QAAQ,EAAE,iBAAoC,CAAA"}
@@ -0,0 +1,122 @@
1
+ /**
2
+ * Cropper.js image crop provider implementation.
3
+ *
4
+ * Wraps cropperjs v1 (`new Cropper(imageElement, options)`) behind the molecule
5
+ * `ImageCropProvider` / `CropperInstance` contract. Every instance method
6
+ * delegates to the corresponding real cropperjs call, so `getCroppedCanvas()`
7
+ * returns the actual cropped `<canvas>` — not a placeholder.
8
+ *
9
+ * @module
10
+ */
11
+ import Cropper from 'cropperjs';
12
+ /**
13
+ * Maps the core `CropperOptions` (plus provider-level config defaults) onto the
14
+ * cropperjs constructor `Options`. `aspectRatio` / `minCropBoxWidth` /
15
+ * `minCropBoxHeight` are only set when provided so cropperjs keeps its own
16
+ * free-form / unconstrained defaults otherwise. (Max crop size has no
17
+ * constructor equivalent in cropperjs — it is enforced on output via
18
+ * `getCroppedCanvas`; see `toGetCroppedCanvasOptions`.)
19
+ *
20
+ * @param options - The core cropper options for this instance.
21
+ * @param config - Provider-level defaults from `createProvider`.
22
+ * @returns cropperjs constructor options.
23
+ */
24
+ const toCropperOptions = (options, config) => {
25
+ const cropperOptions = {
26
+ viewMode: config.viewMode ?? 1,
27
+ guides: options.guides ?? config.guides ?? true,
28
+ background: config.background ?? true,
29
+ autoCrop: true,
30
+ };
31
+ if (options.aspectRatio !== undefined) {
32
+ cropperOptions.aspectRatio = options.aspectRatio;
33
+ }
34
+ if (options.minWidth !== undefined) {
35
+ cropperOptions.minCropBoxWidth = options.minWidth;
36
+ }
37
+ if (options.minHeight !== undefined) {
38
+ cropperOptions.minCropBoxHeight = options.minHeight;
39
+ }
40
+ return cropperOptions;
41
+ };
42
+ /**
43
+ * Maps the core `OutputOptions` and the cropper's min/max size constraints onto
44
+ * cropperjs `getCroppedCanvas` options. `quality` is intentionally NOT mapped:
45
+ * it is an encode-time setting for `canvas.toBlob`/`toDataURL`, not a
46
+ * canvas-generation parameter.
47
+ *
48
+ * @param options - The core cropper options (for min/max size constraints).
49
+ * @param output - Optional per-call output options.
50
+ * @returns cropperjs `getCroppedCanvas` options.
51
+ */
52
+ const toGetCroppedCanvasOptions = (options, output) => {
53
+ const canvasOptions = {};
54
+ if (output?.width !== undefined) {
55
+ canvasOptions.width = output.width;
56
+ }
57
+ if (output?.height !== undefined) {
58
+ canvasOptions.height = output.height;
59
+ }
60
+ if (output?.fillColor !== undefined) {
61
+ canvasOptions.fillColor = output.fillColor;
62
+ }
63
+ if (options.minWidth !== undefined) {
64
+ canvasOptions.minWidth = options.minWidth;
65
+ }
66
+ if (options.minHeight !== undefined) {
67
+ canvasOptions.minHeight = options.minHeight;
68
+ }
69
+ if (options.maxWidth !== undefined) {
70
+ canvasOptions.maxWidth = options.maxWidth;
71
+ }
72
+ if (options.maxHeight !== undefined) {
73
+ canvasOptions.maxHeight = options.maxHeight;
74
+ }
75
+ return canvasOptions;
76
+ };
77
+ /**
78
+ * Creates a Cropper.js-based image crop provider.
79
+ *
80
+ * @param config - Optional provider-level defaults (`guides`, `background`, `viewMode`).
81
+ * @returns A configured `ImageCropProvider` backed by real cropperjs instances.
82
+ */
83
+ export function createProvider(config = {}) {
84
+ return {
85
+ name: 'cropperjs',
86
+ createCropper(options) {
87
+ // cropperjs mounts on an <img>; the core contract only gives us `src`, so
88
+ // we create the image element here. cropperjs initializes on the image's
89
+ // `load` event — read crop data / export after load (or set the region
90
+ // explicitly via setCropData first).
91
+ const image = document.createElement('img');
92
+ image.src = options.src;
93
+ const cropper = new Cropper(image, toCropperOptions(options, config));
94
+ return {
95
+ getCroppedCanvas(outputOptions) {
96
+ return cropper.getCroppedCanvas(toGetCroppedCanvasOptions(options, outputOptions));
97
+ },
98
+ getCropData() {
99
+ return cropper.getData();
100
+ },
101
+ setCropData(data) {
102
+ cropper.setData(data);
103
+ },
104
+ reset() {
105
+ cropper.reset();
106
+ },
107
+ rotate(degrees) {
108
+ cropper.rotate(degrees);
109
+ },
110
+ zoom(ratio) {
111
+ cropper.zoom(ratio);
112
+ },
113
+ destroy() {
114
+ cropper.destroy();
115
+ image.remove();
116
+ },
117
+ };
118
+ },
119
+ };
120
+ }
121
+ /** Default Cropper.js provider instance. */
122
+ export const provider = createProvider();
@@ -0,0 +1,26 @@
1
+ /**
2
+ * Configuration for the Cropper.js provider.
3
+ *
4
+ * @module
5
+ */
6
+ /**
7
+ * Provider-level defaults applied to every cropper created by the provider.
8
+ *
9
+ * These map onto cropperjs constructor options. Per-cropper `CropperOptions`
10
+ * (e.g. `guides`) take precedence over the matching field here.
11
+ */
12
+ export interface CropperjsConfig {
13
+ /**
14
+ * Whether to show the dashed crop guide lines by default. Overridden per-cropper
15
+ * by `CropperOptions.guides`. Defaults to `true`.
16
+ */
17
+ guides?: boolean;
18
+ /** Whether to render the checkerboard background behind the image. Defaults to `true`. */
19
+ background?: boolean;
20
+ /**
21
+ * cropperjs view mode (0-3) constraining the crop box relative to the canvas /
22
+ * container. `1` restricts the crop box within the canvas. Defaults to `1`.
23
+ */
24
+ viewMode?: 0 | 1 | 2 | 3;
25
+ }
26
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA;;;;GAIG;AAEH;;;;;GAKG;AACH,MAAM,WAAW,eAAe;IAC9B;;;OAGG;IACH,MAAM,CAAC,EAAE,OAAO,CAAA;IAEhB,0FAA0F;IAC1F,UAAU,CAAC,EAAE,OAAO,CAAA;IAEpB;;;OAGG;IACH,QAAQ,CAAC,EAAE,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAA;CACzB"}
package/dist/types.js ADDED
@@ -0,0 +1,6 @@
1
+ /**
2
+ * Configuration for the Cropper.js provider.
3
+ *
4
+ * @module
5
+ */
6
+ export {};
package/package.json ADDED
@@ -0,0 +1,46 @@
1
+ {
2
+ "name": "@molecule/app-image-crop-cropperjs",
3
+ "version": "1.0.0",
4
+ "description": "Cropper.js provider for @molecule/app-image-crop",
5
+ "type": "module",
6
+ "main": "dist/index.js",
7
+ "types": "dist/index.d.ts",
8
+ "scripts": {
9
+ "build": "tsc",
10
+ "test": "vitest run",
11
+ "test:watch": "vitest"
12
+ },
13
+ "exports": {
14
+ ".": {
15
+ "types": "./dist/index.d.ts",
16
+ "import": "./dist/index.js"
17
+ }
18
+ },
19
+ "files": [
20
+ "dist"
21
+ ],
22
+ "keywords": [
23
+ "molecule",
24
+ "image",
25
+ "crop",
26
+ "cropperjs"
27
+ ],
28
+ "license": "Apache-2.0",
29
+ "repository": {
30
+ "type": "git",
31
+ "directory": "packages/app/bonds/image-crop/cropperjs"
32
+ },
33
+ "peerDependencies": {
34
+ "@molecule/app-image-crop": "^1.0.0"
35
+ },
36
+ "devDependencies": {
37
+ "@molecule/app-image-crop": "1.0.0",
38
+ "@types/cropperjs": "1.3.3",
39
+ "@types/node": "26.1.2",
40
+ "typescript": "6.0.3",
41
+ "vitest": "4.1.10"
42
+ },
43
+ "dependencies": {
44
+ "cropperjs": "1.6.2"
45
+ }
46
+ }