image-exporter 1.0.9 → 1.1.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "image-exporter",
3
- "version": "1.0.9",
3
+ "version": "1.1.1",
4
4
  "description": "Easily download one or more DOM elements as images",
5
5
  "main": "src/index.ts",
6
6
  "type": "module",
@@ -48,6 +48,6 @@
48
48
  "concurrently": "^9.1.2",
49
49
  "http-server": "^14.1.1",
50
50
  "typescript": "^5.7.3",
51
- "vite": "^5.4.14"
51
+ "vite": "^6.2.0"
52
52
  }
53
53
  }
@@ -1,6 +1,7 @@
1
1
  import { Config, Image, Label } from "../types";
2
2
  import download from "downloadjs";
3
3
  import JSZip from "jszip";
4
+ import { defaultConfig } from "../config";
4
5
 
5
6
  /**
6
7
  * downloadImages
@@ -9,13 +10,20 @@ import JSZip from "jszip";
9
10
  *
10
11
  * If multiple images are provided, they will be zipped and downloaded as a file.
11
12
  */
12
- export async function downloadImages(images: Image[], config: Config) {
13
- if (images.length === 1) {
14
- const image = images[0];
13
+ export async function downloadImages(
14
+ images: Image[],
15
+ userConfig: Config = defaultConfig
16
+ ) {
17
+ const config = userConfig ? { ...defaultConfig, ...userConfig } : defaultConfig;
15
18
 
19
+ // Ensure unique filenames before downloading
20
+ const uniqueImages = ensureUniqueFileNames(images);
21
+
22
+ if (uniqueImages.length === 1) {
23
+ const image = uniqueImages[0];
16
24
  await download(image.dataURL, image.fileName);
17
- } else if (images.length > 1) {
18
- const imagesBlob = await zipUpImages(images);
25
+ } else if (uniqueImages.length > 1) {
26
+ const imagesBlob = await zipUpImages(uniqueImages);
19
27
  if (imagesBlob) await download(imagesBlob, parseLabel(config));
20
28
  }
21
29
  }
@@ -67,3 +75,41 @@ function parseLabel(config: Config): Label {
67
75
  return "images";
68
76
  }
69
77
  }
78
+
79
+ /**
80
+ * ensureUniqueFileNames
81
+ *
82
+ * Ensures all image filenames are unique by adding -2, -3, etc. to duplicates
83
+ * before the file extension.
84
+ */
85
+ function ensureUniqueFileNames(images: Image[]): Image[] {
86
+ const fileNameMap = new Map<string, number>();
87
+
88
+ return images.map((image) => {
89
+ const { fileName } = image;
90
+
91
+ // Split the filename into base and extension
92
+ const lastDotIndex = fileName.lastIndexOf(".");
93
+ const baseName = lastDotIndex !== -1 ? fileName.substring(0, lastDotIndex) : fileName;
94
+ const extension = lastDotIndex !== -1 ? fileName.substring(lastDotIndex) : "";
95
+
96
+ // Check if this base filename has been seen before
97
+ if (!fileNameMap.has(fileName)) {
98
+ fileNameMap.set(fileName, 1);
99
+ return image;
100
+ }
101
+
102
+ // If it's a duplicate, increment the counter and create a new filename
103
+ const count = fileNameMap.get(fileName)! + 1;
104
+ fileNameMap.set(fileName, count);
105
+
106
+ // Create new filename with -2, -3, etc. before the extension
107
+ const newFileName = `${baseName}-${count}${extension}`;
108
+
109
+ // Return a new image object with the updated filename
110
+ return {
111
+ ...image,
112
+ fileName: newFileName,
113
+ };
114
+ });
115
+ }
package/src/index.ts CHANGED
@@ -4,11 +4,13 @@
4
4
  /* by briantuckerdesign */
5
5
  /* -------------------------------------------------------------------------- */
6
6
  import { capture } from "./capture";
7
+ import { downloadImages } from "./capture/download-images";
7
8
 
8
9
  /** Exports for use in browser */
9
10
  if (typeof window !== "undefined") {
10
11
  (window as any).imageExporter = capture;
12
+ (window as any).imageExporterDownload = downloadImages;
11
13
  }
12
14
 
13
15
  /** Exports for use as an imported package */
14
- export { capture };
16
+ export { capture, downloadImages };