image-exporter 1.0.6 → 1.0.8

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.6",
3
+ "version": "1.0.8",
4
4
  "description": "Easily download one or more DOM elements as images",
5
5
  "main": "src/index.ts",
6
6
  "type": "module",
@@ -41,8 +41,8 @@
41
41
  "dependencies": {
42
42
  "@types/downloadjs": "^1.4.6",
43
43
  "downloadjs": "^1.4.7",
44
- "html-to-image": "^1.11.13",
45
- "jszip": "^3.10.1"
44
+ "jszip": "^3.10.1",
45
+ "modern-screenshot": "^4.6.0"
46
46
  },
47
47
  "devDependencies": {
48
48
  "concurrently": "^9.1.2",
@@ -1,7 +1,7 @@
1
- import * as htmlToImage from "html-to-image";
2
1
  import { Image, ParsedImageOptions } from "../types";
3
2
  import { handleFileNames } from "./handle-filenames";
4
- import { Options } from "html-to-image/lib/types";
3
+ import * as modernScreenshot from "modern-screenshot";
4
+
5
5
  /**
6
6
  * captureElement
7
7
  *
@@ -15,11 +15,11 @@ export async function captureElement(
15
15
  try {
16
16
  let dataURL = "";
17
17
  // Final settings for capturing images.
18
- let htmlToImageOptions: Options = {
18
+ let htmlToImageOptions: modernScreenshot.Options = {
19
19
  // Ensure quality is a number
20
20
  quality: imageOptions.quality,
21
21
  // Ensure scale is a number
22
- pixelRatio: imageOptions.scale,
22
+ scale: imageOptions.scale,
23
23
  // Ignores elements with data-ignore-capture attribute
24
24
  filter: filter,
25
25
  };
@@ -41,13 +41,16 @@ export async function captureElement(
41
41
  // Captures image based on format
42
42
  switch (imageOptions.format) {
43
43
  case "jpg":
44
- dataURL = await htmlToImage.toJpeg(element, htmlToImageOptions);
44
+ dataURL = await modernScreenshot.domToJpeg(element, htmlToImageOptions);
45
45
  break;
46
46
  case "png":
47
- dataURL = await htmlToImage.toPng(element, htmlToImageOptions);
47
+ dataURL = await modernScreenshot.domToPng(element, htmlToImageOptions);
48
48
  break;
49
49
  case "svg":
50
- dataURL = await htmlToImage.toSvg(element, htmlToImageOptions);
50
+ dataURL = await modernScreenshot.domToSvg(element, htmlToImageOptions);
51
+ break;
52
+ case "webp":
53
+ dataURL = await modernScreenshot.domToWebp(element, htmlToImageOptions);
51
54
  break;
52
55
  }
53
56
 
@@ -9,12 +9,13 @@ import { ImageOptions, Label } from "../types";
9
9
  * If it doesn't, the function will start with "-2" and increment the number until a unique filename is found.
10
10
  */
11
11
  export function handleFileNames(imageOptions: ImageOptions, filenames: string[]): Label {
12
- // Finish alterting filenames before checking for uniqueness
12
+ // Finish altering filenames before checking for uniqueness
13
13
  let proposedFilename = imageOptions.label;
14
14
  // Add scale to filename if includeScaleInLabel is true
15
15
  if (imageOptions.includeScaleInLabel) proposedFilename += `_@${imageOptions.scale}x`;
16
16
  // Add format to filename last
17
- proposedFilename += `.${imageOptions.format}`;
17
+ const extension = `.${imageOptions.format}`;
18
+ proposedFilename += extension;
18
19
 
19
20
  // If filename is unique, add it to array and return as-is
20
21
  if (!filenames.includes(proposedFilename)) {
@@ -31,21 +32,22 @@ export function handleFileNames(imageOptions: ImageOptions, filenames: string[])
31
32
  const baseFilename = proposedFilename.replace(numberPattern, "");
32
33
  let counter = parseInt(match[1], 10);
33
34
 
34
- while (filenames.includes(`${baseFilename}-${counter}`)) {
35
+ while (filenames.includes(`${baseFilename}-${counter}${extension}`)) {
35
36
  counter++;
36
37
  }
37
38
 
38
- const newFilename = `${baseFilename}-${counter}`;
39
+ const newFilename = `${baseFilename}-${counter}${extension}`;
39
40
  filenames.push(newFilename);
40
41
  return newFilename;
41
42
  } else {
42
- // File doesn't end with -n, start with -1 and increment if needed
43
+ // File doesn't end with -n, start with -2 and increment if needed
44
+ const baseFilename = proposedFilename.replace(extension, "");
43
45
  let counter = 2;
44
- while (filenames.includes(`${proposedFilename}-${counter}`)) {
46
+ while (filenames.includes(`${baseFilename}-${counter}${extension}`)) {
45
47
  counter++;
46
48
  }
47
49
 
48
- const newFilename = `${proposedFilename}-${counter}`;
50
+ const newFilename = `${baseFilename}-${counter}${extension}`;
49
51
  filenames.push(newFilename);
50
52
  return newFilename;
51
53
  }
package/src/types.d.ts CHANGED
@@ -44,7 +44,7 @@ export interface Image {
44
44
  }
45
45
 
46
46
  export type Label = string;
47
- export type Format = "jpg" | "png" | "svg";
47
+ export type Format = "jpg" | "png" | "svg" | "webp";
48
48
  export type Scale = number | number[];
49
49
  export type Quality = number;
50
50
  export type IncludeScaleInLabel = boolean;