@webstudio-is/image 0.0.0-588fe22 → 0.0.0-7a19155

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/lib/index.js CHANGED
@@ -1,9 +1,30 @@
1
- // src/image.tsx
2
- import {
3
- forwardRef
4
- } from "react";
1
+ // src/image-loaders.ts
2
+ import warnOnce from "warn-once";
5
3
 
6
4
  // src/image-optimize.ts
5
+ var imagePlaceholderDataUrl = `data:image/svg+xml;base64,${btoa(`<svg
6
+ width="140"
7
+ height="140"
8
+ viewBox="0 0 600 600"
9
+ fill="none"
10
+ xmlns="http://www.w3.org/2000/svg"
11
+ >
12
+ <rect width="600" height="600" fill="#DFE3E6" />
13
+ <path
14
+ fill-rule="evenodd"
15
+ clip-rule="evenodd"
16
+ d="M450 170H150C141.716 170 135 176.716 135 185V415C135 423.284 141.716 430 150 430H450C458.284 430 465 423.284 465 415V185C465 176.716 458.284 170 450 170ZM150 145C127.909 145 110 162.909 110 185V415C110 437.091 127.909 455 150 455H450C472.091 455 490 437.091 490 415V185C490 162.909 472.091 145 450 145H150Z"
17
+ fill="#C1C8CD"
18
+ />
19
+ <path
20
+ d="M237.135 235.012C237.135 255.723 220.345 272.512 199.635 272.512C178.924 272.512 162.135 255.723 162.135 235.012C162.135 214.301 178.924 197.512 199.635 197.512C220.345 197.512 237.135 214.301 237.135 235.012Z"
21
+ fill="#C1C8CD"
22
+ />
23
+ <path
24
+ d="M160 405V367.205L221.609 306.364L256.552 338.628L358.161 234L440 316.043V405H160Z"
25
+ fill="#C1C8CD"
26
+ />
27
+ </svg>`)}`;
7
28
  var imageSizes = [16, 32, 48, 64, 96, 128, 256, 384];
8
29
  var deviceSizes = [640, 750, 828, 1080, 1200, 1920, 2048, 3840];
9
30
  var allSizes = [...imageSizes, ...deviceSizes];
@@ -69,7 +90,7 @@ var getInt = (value) => {
69
90
  return Math.round(vNum);
70
91
  }
71
92
  }
72
- return void 0;
93
+ return;
73
94
  };
74
95
  var DEFAULT_SIZES = "(min-width: 1280px) 50vw, 100vw";
75
96
  var DEFAULT_QUALITY = 80;
@@ -81,13 +102,16 @@ var UrlCanParse = (url) => {
81
102
  return false;
82
103
  }
83
104
  };
84
- var getImageAttributes = (props) => {
105
+ var getOptimizedImageAttributes = (props) => {
85
106
  const width = getInt(props.width);
86
107
  const quality = Math.max(
87
108
  Math.min(getInt(props.quality) ?? DEFAULT_QUALITY, 100),
88
109
  0
89
110
  );
90
111
  if (props.src != null && props.src !== "") {
112
+ if (props.src.startsWith("data:")) {
113
+ return { src: props.src };
114
+ }
91
115
  if (props.srcSet == null && props.optimize) {
92
116
  const sizes = props.sizes ?? (props.width == null ? DEFAULT_SIZES : void 0);
93
117
  return generateImgAttrs({
@@ -110,67 +134,35 @@ var getImageAttributes = (props) => {
110
134
  return resAttrs;
111
135
  }
112
136
  };
113
-
114
- // src/image.tsx
115
- import { jsx } from "react/jsx-runtime";
116
- var Image = forwardRef(
117
- ({
137
+ var getImageAttributes = ({
138
+ quality,
139
+ loader,
140
+ optimize = true,
141
+ loading = "lazy",
142
+ decoding = "async",
143
+ ...attributes
144
+ }) => {
145
+ const optimizedAttributes = getOptimizedImageAttributes({
146
+ src: attributes.src,
147
+ srcSet: attributes.srcSet,
148
+ sizes: attributes.sizes,
149
+ width: attributes.width,
118
150
  quality,
119
151
  loader,
120
- optimize = true,
121
- loading = "lazy",
122
- decoding = "async",
123
- ...imageProps
124
- }, ref) => {
125
- const imageAttributes = getImageAttributes({
126
- src: imageProps.src,
127
- srcSet: imageProps.srcSet,
128
- sizes: imageProps.sizes,
129
- width: imageProps.width,
130
- quality,
131
- loader,
132
- optimize
133
- }) ?? { src: imagePlaceholderDataUrl };
134
- return /* @__PURE__ */ jsx(
135
- "img",
136
- {
137
- alt: "",
138
- ...imageProps,
139
- ...imageAttributes,
140
- decoding,
141
- loading,
142
- ref
143
- }
144
- );
145
- }
146
- );
147
- Image.displayName = "Image";
148
- var imagePlaceholderDataUrl = `data:image/svg+xml;base64,${btoa(`<svg
149
- width="140"
150
- height="140"
151
- viewBox="0 0 600 600"
152
- fill="none"
153
- xmlns="http://www.w3.org/2000/svg"
154
- >
155
- <rect width="600" height="600" fill="#DFE3E6" />
156
- <path
157
- fill-rule="evenodd"
158
- clip-rule="evenodd"
159
- d="M450 170H150C141.716 170 135 176.716 135 185V415C135 423.284 141.716 430 150 430H450C458.284 430 465 423.284 465 415V185C465 176.716 458.284 170 450 170ZM150 145C127.909 145 110 162.909 110 185V415C110 437.091 127.909 455 150 455H450C472.091 455 490 437.091 490 415V185C490 162.909 472.091 145 450 145H150Z"
160
- fill="#C1C8CD"
161
- />
162
- <path
163
- d="M237.135 235.012C237.135 255.723 220.345 272.512 199.635 272.512C178.924 272.512 162.135 255.723 162.135 235.012C162.135 214.301 178.924 197.512 199.635 197.512C220.345 197.512 237.135 214.301 237.135 235.012Z"
164
- fill="#C1C8CD"
165
- />
166
- <path
167
- d="M160 405V367.205L221.609 306.364L256.552 338.628L358.161 234L440 316.043V405H160Z"
168
- fill="#C1C8CD"
169
- />
170
- </svg>`)}`;
152
+ optimize
153
+ }) ?? { src: imagePlaceholderDataUrl };
154
+ return {
155
+ alt: "",
156
+ ...attributes.alt !== void 0 ? { alt: attributes.alt } : {},
157
+ ...attributes.width !== void 0 ? { width: attributes.width } : {},
158
+ ...attributes.height !== void 0 ? { height: attributes.height } : {},
159
+ ...optimizedAttributes,
160
+ decoding,
161
+ loading
162
+ };
163
+ };
171
164
 
172
165
  // src/image-loaders.ts
173
- import warnOnce from "warn-once";
174
166
  var NON_EXISTING_DOMAIN = "https://a3cbcbec-cdb1-4ea4-ad60-43c795308ddc.ddc";
175
167
  var joinPath = (...segments) => {
176
168
  return segments.filter((segment) => segment !== "").map((segment) => segment.replace(/(^\/+|\/+$)/g, "")).join("/");
@@ -178,6 +170,15 @@ var joinPath = (...segments) => {
178
170
  var encodePathFragment = (fragment) => {
179
171
  return encodeURIComponent(fragment).replace(/%2F/g, "/");
180
172
  };
173
+ var getImageSource = (src) => {
174
+ if (src.startsWith("/cgi/asset")) {
175
+ return decodeURIComponent(src.slice("/cgi/asset".length).split("?")[0]);
176
+ }
177
+ if (src.startsWith("/cgi/image")) {
178
+ return decodeURIComponent(src.slice("/cgi/image".length).split("?")[0]);
179
+ }
180
+ return src;
181
+ };
181
182
  var wsImageLoader = (props) => {
182
183
  const width = props.format === "raw" ? 16 : props.width;
183
184
  const quality = props.format === "raw" ? 100 : props.quality;
@@ -187,10 +188,7 @@ var wsImageLoader = (props) => {
187
188
  "Width must be only from allowed values"
188
189
  );
189
190
  }
190
- let src = props.src;
191
- if (src.startsWith("/cgi/asset")) {
192
- src = src.slice("/cgi/asset".length);
193
- }
191
+ const src = getImageSource(props.src);
194
192
  const resultUrl = new URL("/cgi/image/", NON_EXISTING_DOMAIN);
195
193
  if (props.format !== "raw") {
196
194
  resultUrl.searchParams.set("width", width.toString());
@@ -209,10 +207,16 @@ var wsImageLoader = (props) => {
209
207
  }
210
208
  return resultUrl.href;
211
209
  };
210
+ var wsVideoLoader = ({ src }) => {
211
+ if (src.startsWith("/cgi/asset/")) {
212
+ src = src.slice("/cgi/asset/".length);
213
+ }
214
+ return `/cgi/video/${src}`;
215
+ };
212
216
  export {
213
- Image,
214
217
  allSizes,
215
218
  getImageAttributes,
216
219
  imagePlaceholderDataUrl,
217
- wsImageLoader
220
+ wsImageLoader,
221
+ wsVideoLoader
218
222
  };
@@ -4,3 +4,7 @@ import { type ImageLoader } from "./image-optimize";
4
4
  * https://developers.cloudflare.com/images/image-resizing/url-format/
5
5
  **/
6
6
  export declare const wsImageLoader: ImageLoader;
7
+ export type VideoLoader = (options: {
8
+ src: string;
9
+ }) => string;
10
+ export declare const wsVideoLoader: VideoLoader;
@@ -95,17 +95,29 @@ export type ImageLoader = (props: {
95
95
  src: string;
96
96
  format: "raw";
97
97
  }) => string;
98
+ export declare const imagePlaceholderDataUrl: string;
98
99
  export declare const allSizes: number[];
99
- export declare const getImageAttributes: (props: {
100
- src: string | undefined;
101
- srcSet: string | undefined;
102
- sizes: string | undefined;
103
- width: string | number | undefined;
104
- quality: string | number | undefined;
100
+ export type ImageAttributes = {
101
+ alt?: string;
102
+ decoding?: "async" | "auto" | "sync";
103
+ height?: number | string;
104
+ loading?: "eager" | "lazy";
105
+ sizes?: string;
106
+ src?: string;
107
+ srcSet?: string;
108
+ width?: number | string;
109
+ };
110
+ type ImageOptions = {
105
111
  loader: ImageLoader;
106
- optimize: boolean;
107
- }) => {
112
+ /** Optimize the image for enhanced performance. */
113
+ optimize?: boolean;
114
+ quality?: number;
115
+ };
116
+ export type ResolvedImageAttributes = ImageAttributes & {
117
+ alt: string | undefined;
118
+ decoding: "async" | "auto" | "sync";
119
+ loading: "eager" | "lazy";
108
120
  src: string;
109
- srcSet?: string;
110
- sizes?: string;
111
- } | undefined;
121
+ };
122
+ export declare const getImageAttributes: ({ quality, loader, optimize, loading, decoding, ...attributes }: ImageAttributes & ImageOptions) => ResolvedImageAttributes;
123
+ export {};
@@ -1,4 +1,3 @@
1
- export { Image, imagePlaceholderDataUrl } from "./image";
2
1
  export type { ImageLoader } from "./image-optimize";
3
2
  export * from "./image-loaders";
4
3
  export * from "./image-optimize";
package/package.json CHANGED
@@ -1,26 +1,23 @@
1
1
  {
2
2
  "name": "@webstudio-is/image",
3
- "version": "0.0.0-588fe22",
3
+ "version": "0.0.0-7a19155",
4
4
  "description": "Image optimization",
5
5
  "author": "Webstudio <github@webstudio.is>",
6
6
  "homepage": "https://webstudio.is",
7
7
  "type": "module",
8
8
  "dependencies": {
9
- "react": "18.3.0-canary-14898b6a9-20240318",
10
- "warn-once": "^0.1.1"
9
+ "warn-once": "^0.1.1",
10
+ "@webstudio-is/sdk": "0.0.0-7a19155"
11
11
  },
12
12
  "devDependencies": {
13
13
  "@types/react": "^18.2.70",
14
14
  "@types/react-dom": "^18.2.25",
15
15
  "react": "18.3.0-canary-14898b6a9-20240318",
16
16
  "react-dom": "18.3.0-canary-14898b6a9-20240318",
17
- "vitest": "^3.0.2",
17
+ "vitest": "^3.1.2",
18
+ "@webstudio-is/design-system": "0.0.0",
18
19
  "@webstudio-is/tsconfig": "1.0.7"
19
20
  },
20
- "peerDependencies": {
21
- "react": "18.3.0-canary-14898b6a9-20240318",
22
- "react-dom": "18.3.0-canary-14898b6a9-20240318"
23
- },
24
21
  "exports": {
25
22
  "webstudio": "./src/index.ts",
26
23
  "types": "./lib/types/index.d.ts",
@@ -34,7 +31,7 @@
34
31
  "private": false,
35
32
  "sideEffects": false,
36
33
  "scripts": {
37
- "typecheck": "tsc",
34
+ "typecheck": "tsc --noEmit",
38
35
  "test": "vitest run",
39
36
  "build": "rm -rf lib && esbuild src/index.ts --outdir=lib --bundle --format=esm --packages=external",
40
37
  "dts": "tsc --project tsconfig.dts.json"
@@ -1 +0,0 @@
1
- export {};
@@ -1 +0,0 @@
1
- export {};
@@ -1,12 +0,0 @@
1
- import { type ComponentProps, type ForwardRefExoticComponent } from "react";
2
- import { type ImageLoader } from "./image-optimize";
3
- declare const defaultTag = "img";
4
- type ImageProps = ComponentProps<typeof defaultTag> & {
5
- quality?: number;
6
- /** Optimize the image for enhanced performance. */
7
- optimize?: boolean;
8
- loader: ImageLoader;
9
- };
10
- export declare const Image: ForwardRefExoticComponent<ImageProps>;
11
- export declare const imagePlaceholderDataUrl: string;
12
- export {};