image-exporter 1.2.3 → 1.3.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.
Files changed (45) hide show
  1. package/README.md +32 -3
  2. package/dist/capture/capture-element.d.ts +2 -1
  3. package/dist/capture/capture-element.d.ts.map +1 -1
  4. package/dist/capture/capture.dom.test.d.ts +2 -0
  5. package/dist/capture/capture.dom.test.d.ts.map +1 -0
  6. package/dist/capture/copy-to-clipboard.d.ts +13 -0
  7. package/dist/capture/copy-to-clipboard.d.ts.map +1 -0
  8. package/dist/capture/copy-to-clipboard.dom.test.d.ts +2 -0
  9. package/dist/capture/copy-to-clipboard.dom.test.d.ts.map +1 -0
  10. package/dist/capture/cors-cleanup.dom.test.d.ts +2 -0
  11. package/dist/capture/cors-cleanup.dom.test.d.ts.map +1 -0
  12. package/dist/capture/data-url-to-blob.d.ts +9 -0
  13. package/dist/capture/data-url-to-blob.d.ts.map +1 -0
  14. package/dist/capture/data-url-to-blob.node.test.d.ts +2 -0
  15. package/dist/capture/data-url-to-blob.node.test.d.ts.map +1 -0
  16. package/dist/capture/dedup-consistency.node.test.d.ts +2 -0
  17. package/dist/capture/dedup-consistency.node.test.d.ts.map +1 -0
  18. package/dist/capture/determine-total-elements.d.ts +7 -3
  19. package/dist/capture/determine-total-elements.d.ts.map +1 -1
  20. package/dist/capture/determine-total-elements.dom.test.d.ts +2 -0
  21. package/dist/capture/determine-total-elements.dom.test.d.ts.map +1 -0
  22. package/dist/capture/download-images.d.ts +8 -0
  23. package/dist/capture/download-images.d.ts.map +1 -1
  24. package/dist/capture/get-image-options.dom.test.d.ts +2 -0
  25. package/dist/capture/get-image-options.dom.test.d.ts.map +1 -0
  26. package/dist/capture/handle-filenames.d.ts +5 -6
  27. package/dist/capture/handle-filenames.d.ts.map +1 -1
  28. package/dist/capture/index.d.ts.map +1 -1
  29. package/dist/capture/make-unique.d.ts +14 -0
  30. package/dist/capture/make-unique.d.ts.map +1 -0
  31. package/dist/capture/make-unique.node.test.d.ts +2 -0
  32. package/dist/capture/make-unique.node.test.d.ts.map +1 -0
  33. package/dist/index.browser.js +16 -16
  34. package/dist/index.browser.js.map +20 -17
  35. package/dist/index.cjs +195 -122
  36. package/dist/index.cjs.map +20 -17
  37. package/dist/index.d.ts +10 -2
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +195 -122
  40. package/dist/index.js.map +20 -17
  41. package/dist/logger.d.ts +5 -5
  42. package/dist/logger.d.ts.map +1 -1
  43. package/dist/types.d.ts +33 -0
  44. package/dist/types.d.ts.map +1 -1
  45. package/package.json +27 -3
package/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  # image-exporter
2
2
 
3
3
  [![npm version](https://img.shields.io/npm/v/image-exporter.svg)](https://www.npmjs.com/package/image-exporter)
4
- [![license](https://img.shields.io/npm/l/image-exporter.svg)](https://github.com/briantuckerdesign/image-exporter/blob/main/LICENSE)
4
+ [![license](https://img.shields.io/npm/l/image-exporter.svg)](https://github.com/briantuckerdesign/image-exporter/blob/main/LICENSE.md)
5
5
 
6
6
  A client-side JavaScript tool that downloads DOM elements as images. It can be imported using your favorite package manager or attached directly to the window.
7
7
 
@@ -34,7 +34,11 @@ const images = await capture(artboards, {
34
34
  ### Browser
35
35
 
36
36
  ```html
37
- <script src="your-path/image-exporter.umd.js" type="text/javascript"></script>
37
+ <!-- Self-hosted -->
38
+ <script src="your-path/index.browser.js" type="text/javascript"></script>
39
+
40
+ <!-- Or via CDN -->
41
+ <script src="https://unpkg.com/image-exporter" type="text/javascript"></script>
38
42
 
39
43
  <div class="artboard">I will be downloaded.</div>
40
44
 
@@ -64,8 +68,11 @@ Captures images from HTML elements.
64
68
 
65
69
  ```typescript
66
70
  interface Image {
71
+ /** base64 data URL. Empty when `output` is `"blob"`. */
67
72
  dataURL: string;
68
73
  fileName: string;
74
+ /** Populated when `config.output` is `"blob"` or `"both"`. */
75
+ blob?: Blob;
69
76
  }
70
77
  ```
71
78
 
@@ -83,6 +90,19 @@ await downloadImages(images);
83
90
 
84
91
  In the browser, this is available as `window.imageExporterDownload`.
85
92
 
93
+ ### `copyImageToClipboard(image)`
94
+
95
+ Copies a single captured image to the system clipboard. Must be called from a user gesture (e.g. a click). Use `format: "png"` for the best browser compatibility.
96
+
97
+ ```typescript
98
+ import { capture, copyImageToClipboard } from "image-exporter";
99
+
100
+ const images = await capture(element, { downloadImages: false, format: "png" });
101
+ await copyImageToClipboard(images[0]);
102
+ ```
103
+
104
+ In the browser, this is available as `window.imageExporterCopyToClipboard`.
105
+
86
106
  ## Config
87
107
 
88
108
  Config options are passed to the `capture` function. Image options (label, format, scale, quality, includeScaleInLabel) can also be set at the config level as defaults.
@@ -102,6 +122,15 @@ interface Config {
102
122
  /** Logging level for debugging. Default: "none" */
103
123
  loggingLevel: "none" | "info" | "error" | "verbose";
104
124
 
125
+ /** Called after each capture with (completed, total). */
126
+ onProgress?: (completed: number, total: number) => void;
127
+ /** Cancel an in-progress capture; returns the images captured so far. */
128
+ signal?: AbortSignal;
129
+ /** Extra options forwarded to modern-screenshot (fonts, width/height, pixelRatio, etc.). */
130
+ screenshotOptions?: Partial<import("modern-screenshot").Options>;
131
+ /** What the returned images carry. Default: "dataurl" */
132
+ output?: "dataurl" | "blob" | "both";
133
+
105
134
  // Default image options (can be overridden per-element)
106
135
  /** Default: "image" */
107
136
  label: string;
@@ -186,4 +215,4 @@ Bundled with Bun and written in TypeScript.
186
215
 
187
216
  ## License
188
217
 
189
- ISC License - see [LICENSE](LICENSE) for details.
218
+ Apache-2.0 License - see [LICENSE.md](LICENSE.md) for details.
@@ -1,8 +1,9 @@
1
1
  import { Image, ParsedImageOptions } from "../types";
2
+ import * as modernScreenshot from "modern-screenshot";
2
3
  /**
3
4
  * captureElement
4
5
  *
5
6
  * Captures an image from an HTML element and returns it.
6
7
  */
7
- export declare function captureElement(element: HTMLElement, imageOptions: ParsedImageOptions, filenames: string[]): Promise<Image>;
8
+ export declare function captureElement(element: HTMLElement, imageOptions: ParsedImageOptions, seen: Set<string>, screenshotOptions?: Partial<modernScreenshot.Options>): Promise<Image>;
8
9
  //# sourceMappingURL=capture-element.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"capture-element.d.ts","sourceRoot":"","sources":["../../src/capture/capture-element.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAIrD;;;;GAIG;AACH,wBAAsB,cAAc,CAClC,OAAO,EAAE,WAAW,EACpB,YAAY,EAAE,kBAAkB,EAChC,SAAS,EAAE,MAAM,EAAE,GAClB,OAAO,CAAC,KAAK,CAAC,CAwDhB"}
1
+ {"version":3,"file":"capture-element.d.ts","sourceRoot":"","sources":["../../src/capture/capture-element.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAErD,OAAO,KAAK,gBAAgB,MAAM,mBAAmB,CAAC;AAEtD;;;;GAIG;AACH,wBAAsB,cAAc,CAClC,OAAO,EAAE,WAAW,EACpB,YAAY,EAAE,kBAAkB,EAChC,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,EACjB,iBAAiB,CAAC,EAAE,OAAO,CAAC,gBAAgB,CAAC,OAAO,CAAC,GACpD,OAAO,CAAC,KAAK,CAAC,CAgEhB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=capture.dom.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"capture.dom.test.d.ts","sourceRoot":"","sources":["../../src/capture/capture.dom.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,13 @@
1
+ import { Image } from "../types";
2
+ /**
3
+ * copyImageToClipboard
4
+ *
5
+ * Copies a single captured image to the system clipboard. Must be called from a
6
+ * user gesture (click/keypress) due to browser security. The clipboard holds
7
+ * one image at a time, so pass the specific image you want to copy.
8
+ *
9
+ * Note: browsers most reliably support `image/png` on the clipboard — capture
10
+ * with `format: "png"` for best compatibility.
11
+ */
12
+ export declare function copyImageToClipboard(image: Image): Promise<void>;
13
+ //# sourceMappingURL=copy-to-clipboard.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"copy-to-clipboard.d.ts","sourceRoot":"","sources":["../../src/capture/copy-to-clipboard.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAGjC;;;;;;;;;GASG;AACH,wBAAsB,oBAAoB,CAAC,KAAK,EAAE,KAAK,GAAG,OAAO,CAAC,IAAI,CAAC,CAGtE"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=copy-to-clipboard.dom.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"copy-to-clipboard.dom.test.d.ts","sourceRoot":"","sources":["../../src/capture/copy-to-clipboard.dom.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=cors-cleanup.dom.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"cors-cleanup.dom.test.d.ts","sourceRoot":"","sources":["../../src/capture/cors-cleanup.dom.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,9 @@
1
+ /**
2
+ * dataUrlToBlob
3
+ *
4
+ * Converts a data URL (base64 or URL-encoded, e.g. SVG) into a Blob, preserving
5
+ * the MIME type. Lets callers work with binary Blobs instead of large base64
6
+ * strings.
7
+ */
8
+ export declare function dataUrlToBlob(dataURL: string): Blob;
9
+ //# sourceMappingURL=data-url-to-blob.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"data-url-to-blob.d.ts","sourceRoot":"","sources":["../../src/capture/data-url-to-blob.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAgB,aAAa,CAAC,OAAO,EAAE,MAAM,GAAG,IAAI,CAiBnD"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=data-url-to-blob.node.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"data-url-to-blob.node.test.d.ts","sourceRoot":"","sources":["../../src/capture/data-url-to-blob.node.test.ts"],"names":[],"mappings":""}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=dedup-consistency.node.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"dedup-consistency.node.test.d.ts","sourceRoot":"","sources":["../../src/capture/dedup-consistency.node.test.ts"],"names":[],"mappings":""}
@@ -1,9 +1,13 @@
1
+ import { Config } from "../types";
1
2
  /**
2
3
  * determineTotalElements
3
4
  *
4
- * Just used for progress logging to show progress of all element captures.
5
+ * Computes how many captures will occur across all elements, so progress
6
+ * logging reflects multi-scale captures (not just elements.length).
5
7
  *
6
- * This emcompasses multi-scale captures unlike elements.length.
8
+ * Each element produces at least one capture. A `data-scale` comma-list
9
+ * produces one capture per scale. An element with no (or an invalid)
10
+ * `data-scale` falls back to `config.scale`, mirroring `getImageOptions`.
7
11
  */
8
- export declare function determineTotalElements(elements: HTMLElement[] | NodeListOf<HTMLElement>): Promise<number>;
12
+ export declare function determineTotalElements(elements: HTMLElement[] | NodeListOf<HTMLElement>, config: Config): Promise<number>;
9
13
  //# sourceMappingURL=determine-total-elements.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"determine-total-elements.d.ts","sourceRoot":"","sources":["../../src/capture/determine-total-elements.ts"],"names":[],"mappings":"AAAA;;;;;;GAMG;AACH,wBAAsB,sBAAsB,CAC1C,QAAQ,EAAE,WAAW,EAAE,GAAG,UAAU,CAAC,WAAW,CAAC,GAChD,OAAO,CAAC,MAAM,CAAC,CA8BjB"}
1
+ {"version":3,"file":"determine-total-elements.d.ts","sourceRoot":"","sources":["../../src/capture/determine-total-elements.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,UAAU,CAAC;AAElC;;;;;;;;;GASG;AACH,wBAAsB,sBAAsB,CAC1C,QAAQ,EAAE,WAAW,EAAE,GAAG,UAAU,CAAC,WAAW,CAAC,EACjD,MAAM,EAAE,MAAM,GACb,OAAO,CAAC,MAAM,CAAC,CAUjB"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=determine-total-elements.dom.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"determine-total-elements.dom.test.d.ts","sourceRoot":"","sources":["../../src/capture/determine-total-elements.dom.test.ts"],"names":[],"mappings":""}
@@ -7,4 +7,12 @@ import { Config, Image } from "../types";
7
7
  * If multiple images are provided, they will be zipped and downloaded as a file.
8
8
  */
9
9
  export declare function downloadImages(images: Image[], userConfig?: Config): Promise<void>;
10
+ /**
11
+ * ensureUniqueFileNames
12
+ *
13
+ * Ensures all image filenames are unique by adding -2, -3, etc. to duplicates
14
+ * before the file extension. Uses the shared `makeUnique` helper so this public
15
+ * entry point dedups identically to capture-time naming.
16
+ */
17
+ export declare function ensureUniqueFileNames(images: Image[]): Image[];
10
18
  //# sourceMappingURL=download-images.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"download-images.d.ts","sourceRoot":"","sources":["../../src/capture/download-images.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,KAAK,EAAS,MAAM,UAAU,CAAC;AAIhD;;;;;;GAMG;AACH,wBAAsB,cAAc,CAClC,MAAM,EAAE,KAAK,EAAE,EACf,UAAU,GAAE,MAAsB,iBAcnC"}
1
+ {"version":3,"file":"download-images.d.ts","sourceRoot":"","sources":["../../src/capture/download-images.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,KAAK,EAAS,MAAM,UAAU,CAAC;AAKhD;;;;;;GAMG;AACH,wBAAsB,cAAc,CAClC,MAAM,EAAE,KAAK,EAAE,EACf,UAAU,GAAE,MAAsB,iBAenC;AAmED;;;;;;GAMG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,KAAK,EAAE,GAAG,KAAK,EAAE,CAO9D"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=get-image-options.dom.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"get-image-options.dom.test.d.ts","sourceRoot":"","sources":["../../src/capture/get-image-options.dom.test.ts"],"names":[],"mappings":""}
@@ -1,11 +1,10 @@
1
1
  import { ImageOptions, Label } from "../types";
2
2
  /**
3
- * Handles the generation of unique filenames based on a proposed filename and an array of existing filenames.
3
+ * handleFileNames
4
4
  *
5
- * If the proposed filename is unique, it is added to the filenames array and returned as-is.
6
- * If the proposed filename is not unique, the function will check if it already ends with a "-n" pattern.
7
- * If it does, the function will increment the number until a unique filename is found.
8
- * If it doesn't, the function will start with "-2" and increment the number until a unique filename is found.
5
+ * Builds the filename for an image from its options (label + optional scale +
6
+ * format extension), then ensures it is unique against `seen` via the shared
7
+ * `makeUnique` helper.
9
8
  */
10
- export declare function handleFileNames(imageOptions: ImageOptions, filenames: string[]): Label;
9
+ export declare function handleFileNames(imageOptions: ImageOptions, seen: Set<string>): Label;
11
10
  //# sourceMappingURL=handle-filenames.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"handle-filenames.d.ts","sourceRoot":"","sources":["../../src/capture/handle-filenames.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAE/C;;;;;;;GAOG;AACH,wBAAgB,eAAe,CAAC,YAAY,EAAE,YAAY,EAAE,SAAS,EAAE,MAAM,EAAE,GAAG,KAAK,CA2CtF"}
1
+ {"version":3,"file":"handle-filenames.d.ts","sourceRoot":"","sources":["../../src/capture/handle-filenames.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,YAAY,EAAE,KAAK,EAAE,MAAM,UAAU,CAAC;AAG/C;;;;;;GAMG;AACH,wBAAgB,eAAe,CAAC,YAAY,EAAE,YAAY,EAAE,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,KAAK,CAQpF"}
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/capture/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,KAAK,EAAsB,MAAM,UAAU,CAAC;AAO7D,eAAO,IAAI,aAAa,SAAO,CAAC;AAChC,eAAO,IAAI,YAAY,QAAS,CAAC;AAEjC;;;;GAIG;AACH,wBAAsB,OAAO,CAC3B,QAAQ,EAAE,WAAW,EAAE,GAAG,UAAU,CAAC,WAAW,CAAC,GAAG,WAAW,EAC/D,UAAU,GAAE,OAAO,CAAC,MAAM,CAAiB,GAC1C,OAAO,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,CAgFzB"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/capture/index.ts"],"names":[],"mappings":"AAGA,OAAO,EAAE,MAAM,EAAE,KAAK,EAA8B,MAAM,UAAU,CAAC;AAQrE,eAAO,IAAI,aAAa,SAAO,CAAC;AAChC,eAAO,IAAI,YAAY,QAAS,CAAC;AAEjC;;;;GAIG;AACH,wBAAsB,OAAO,CAC3B,QAAQ,EAAE,WAAW,EAAE,GAAG,UAAU,CAAC,WAAW,CAAC,GAAG,WAAW,EAC/D,UAAU,GAAE,OAAO,CAAC,MAAM,CAAiB,GAC1C,OAAO,CAAC,KAAK,EAAE,GAAG,IAAI,CAAC,CAyGzB"}
@@ -0,0 +1,14 @@
1
+ /**
2
+ * makeUnique
3
+ *
4
+ * Returns a filename guaranteed not to collide with any name in `seen`, and
5
+ * records the result in `seen`. Duplicates get a `-2`, `-3`, ... suffix before
6
+ * the extension. If the proposed name already ends with `-n`, numbering
7
+ * continues from there (e.g. `a-2.png` -> `a-3.png`) rather than nesting
8
+ * (`a-2-2.png`).
9
+ *
10
+ * Shared by `handleFileNames` (capture time) and `downloadImages` (public
11
+ * entry point) so both paths dedup identically.
12
+ */
13
+ export declare function makeUnique(fileName: string, seen: Set<string>): string;
14
+ //# sourceMappingURL=make-unique.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"make-unique.d.ts","sourceRoot":"","sources":["../../src/capture/make-unique.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;GAWG;AACH,wBAAgB,UAAU,CAAC,QAAQ,EAAE,MAAM,EAAE,IAAI,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,MAAM,CA2BtE"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=make-unique.node.test.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"make-unique.node.test.d.ts","sourceRoot":"","sources":["../../src/capture/make-unique.node.test.ts"],"names":[],"mappings":""}