@gradio/image 0.26.3 → 0.28.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.
@@ -1,26 +1,12 @@
1
+ import type { HTMLImgAttributes } from "svelte/elements";
1
2
  type $$ComponentProps = {
2
- src: string;
3
- restProps: object;
4
- data_testid: string;
5
- class_names: string[];
3
+ src?: string;
4
+ restProps?: Record<string, any>;
5
+ data_testid?: string;
6
+ class_names?: string[];
7
+ onload?: HTMLImgAttributes["onload"];
8
+ [key: string]: any;
6
9
  };
7
- interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
8
- new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
9
- $$bindings?: Bindings;
10
- } & Exports;
11
- (internal: unknown, props: Props & {
12
- $$events?: Events;
13
- $$slots?: Slots;
14
- }): Exports & {
15
- $set?: any;
16
- $on?: any;
17
- };
18
- z_$$bindings?: Bindings;
19
- }
20
- declare const Image: $$__sveltets_2_IsomorphicComponent<$$ComponentProps, {
21
- load: Event;
22
- } & {
23
- [evt: string]: CustomEvent<any>;
24
- }, {}, {}, "">;
25
- type Image = InstanceType<typeof Image>;
10
+ declare const Image: import("svelte").Component<$$ComponentProps, {}, "">;
11
+ type Image = ReturnType<typeof Image>;
26
12
  export default Image;
@@ -1,5 +1,4 @@
1
1
  <script lang="ts">
2
- import { createEventDispatcher, onMount } from "svelte";
3
2
  import type { SelectData } from "@gradio/utils";
4
3
  import { uploadToHuggingFace } from "@gradio/utils";
5
4
  import {
@@ -19,27 +18,44 @@
19
18
  import type { I18nFormatter } from "@gradio/utils";
20
19
  import type { FileData } from "@gradio/client";
21
20
 
22
- export let value: null | FileData;
23
- export let label: string | undefined = undefined;
24
- export let show_label: boolean;
25
- export let buttons: (string | CustomButtonType)[] = [];
26
- export let on_custom_button_click: ((id: number) => void) | null = null;
27
- export let selectable = false;
28
- export let i18n: I18nFormatter;
29
- export let display_icon_button_wrapper_top_corner = false;
30
- export let fullscreen = false;
31
- export let show_button_background = true;
32
-
33
- const dispatch = createEventDispatcher<{
34
- change: string;
35
- select: SelectData;
36
- fullscreen: boolean;
37
- }>();
21
+ let {
22
+ value,
23
+ label = undefined,
24
+ show_label,
25
+ buttons = [],
26
+ on_custom_button_click = null,
27
+ selectable = false,
28
+ i18n,
29
+ display_icon_button_wrapper_top_corner = false,
30
+ fullscreen = $bindable(false),
31
+ show_button_background = true,
32
+ onselect,
33
+ onfullscreen,
34
+ onshare,
35
+ onerror,
36
+ onload
37
+ }: {
38
+ value: null | FileData;
39
+ label?: string;
40
+ show_label: boolean;
41
+ buttons?: (string | CustomButtonType)[];
42
+ on_custom_button_click?: ((id: number) => void) | null;
43
+ selectable?: boolean;
44
+ i18n: I18nFormatter;
45
+ display_icon_button_wrapper_top_corner?: boolean;
46
+ fullscreen?: boolean;
47
+ show_button_background?: boolean;
48
+ onselect?: (value: SelectData) => void;
49
+ onfullscreen?: (fullscreen: boolean) => void;
50
+ onshare?: (detail: unknown) => void;
51
+ onerror?: (error: string) => void;
52
+ onload?: () => void;
53
+ } = $props();
38
54
 
39
55
  const handle_click = (evt: MouseEvent): void => {
40
56
  let coordinates = get_coordinates_of_clicked_image(evt);
41
57
  if (coordinates) {
42
- dispatch("select", { index: coordinates, value: null });
58
+ onselect?.({ index: coordinates, value: null });
43
59
  }
44
60
  };
45
61
 
@@ -66,7 +82,7 @@
66
82
  {fullscreen}
67
83
  onclick={(is_fullscreen) => {
68
84
  fullscreen = is_fullscreen;
69
- dispatch("fullscreen", is_fullscreen);
85
+ onfullscreen?.(is_fullscreen);
70
86
  }}
71
87
  />
72
88
  {/if}
@@ -78,8 +94,8 @@
78
94
  {#if buttons.some((btn) => typeof btn === "string" && btn === "share")}
79
95
  <ShareButton
80
96
  {i18n}
81
- on:share
82
- on:error
97
+ onshare={(detail) => onshare?.(detail)}
98
+ onerror={(detail) => onerror?.(detail)}
83
99
  formatter={async (value) => {
84
100
  if (!value) return "";
85
101
  let url = await uploadToHuggingFace(value, "url");
@@ -89,12 +105,12 @@
89
105
  />
90
106
  {/if}
91
107
  </IconButtonWrapper>
92
- <button on:click={handle_click}>
108
+ <button onclick={handle_click}>
93
109
  <div class:selectable class="image-frame">
94
110
  <Image
95
111
  src={value.url}
96
112
  restProps={{ loading: "lazy", alt: "" }}
97
- on:load
113
+ {onload}
98
114
  />
99
115
  </div>
100
116
  </button>
@@ -2,22 +2,9 @@ import type { SelectData } from "@gradio/utils";
2
2
  import type { CustomButton as CustomButtonType } from "@gradio/utils";
3
3
  import type { I18nFormatter } from "@gradio/utils";
4
4
  import type { FileData } from "@gradio/client";
5
- interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
6
- new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
7
- $$bindings?: Bindings;
8
- } & Exports;
9
- (internal: unknown, props: Props & {
10
- $$events?: Events;
11
- $$slots?: Slots;
12
- }): Exports & {
13
- $set?: any;
14
- $on?: any;
15
- };
16
- z_$$bindings?: Bindings;
17
- }
18
- declare const ImagePreview: $$__sveltets_2_IsomorphicComponent<{
5
+ type $$ComponentProps = {
19
6
  value: null | FileData;
20
- label?: string | undefined;
7
+ label?: string;
21
8
  show_label: boolean;
22
9
  buttons?: (string | CustomButtonType)[];
23
10
  on_custom_button_click?: ((id: number) => void) | null;
@@ -26,15 +13,12 @@ declare const ImagePreview: $$__sveltets_2_IsomorphicComponent<{
26
13
  display_icon_button_wrapper_top_corner?: boolean;
27
14
  fullscreen?: boolean;
28
15
  show_button_background?: boolean;
29
- }, {
30
- share: CustomEvent<import("@gradio/utils").ShareData>;
31
- error: CustomEvent<string>;
32
- load: Event;
33
- change: CustomEvent<string>;
34
- select: CustomEvent<SelectData>;
35
- fullscreen: CustomEvent<boolean>;
36
- } & {
37
- [evt: string]: CustomEvent<any>;
38
- }, {}, {}, string>;
39
- type ImagePreview = InstanceType<typeof ImagePreview>;
16
+ onselect?: (value: SelectData) => void;
17
+ onfullscreen?: (fullscreen: boolean) => void;
18
+ onshare?: (detail: unknown) => void;
19
+ onerror?: (error: string) => void;
20
+ onload?: () => void;
21
+ };
22
+ declare const ImagePreview: import("svelte").Component<$$ComponentProps, {}, "fullscreen">;
23
+ type ImagePreview = ReturnType<typeof ImagePreview>;
40
24
  export default ImagePreview;
@@ -1,5 +1,5 @@
1
1
  <script lang="ts">
2
- import { createEventDispatcher, tick } from "svelte";
2
+ import { tick, type Snippet } from "svelte";
3
3
  import { BlockLabel, IconButtonWrapper, IconButton } from "@gradio/atoms";
4
4
  import { Clear, Image as ImageIcon } from "@gradio/icons";
5
5
  import { FullscreenButton } from "@gradio/atoms";
@@ -17,36 +17,80 @@
17
17
  import Image from "./Image.svelte";
18
18
  import type { Base64File, WebcamOptions } from "./types";
19
19
 
20
- export let value: null | FileData | Base64File = null;
21
- export let label: string | undefined = undefined;
22
- export let show_label: boolean;
23
-
24
20
  type source_type = "upload" | "webcam" | "clipboard" | "microphone" | null;
25
21
 
26
- export let sources: source_type[] = ["upload", "clipboard", "webcam"];
27
- export let streaming = false;
28
- export let pending = false;
29
- export let webcam_options: WebcamOptions;
30
- export let selectable = false;
31
- export let root: string;
32
- export let i18n: I18nFormatter;
33
- export let max_file_size: number | null = null;
34
- export let upload: Client["upload"];
35
- export let stream_handler: Client["stream"];
36
- export let stream_every: number;
37
- export let time_limit: number;
38
- export let show_fullscreen_button = true;
39
- export let stream_state: "open" | "waiting" | "closed" = "closed";
40
- export let upload_promise: Promise<any> | null = null;
41
- export let onerror: ((error: string) => void) | undefined = undefined;
22
+ let {
23
+ value = $bindable<null | FileData | Base64File>(null),
24
+ label = undefined,
25
+ show_label,
26
+ sources = ["upload", "clipboard", "webcam"],
27
+ streaming = false,
28
+ pending = $bindable(false),
29
+ webcam_options,
30
+ selectable = false,
31
+ root,
32
+ i18n,
33
+ max_file_size = null,
34
+ upload,
35
+ stream_handler,
36
+ stream_every,
37
+ time_limit,
38
+ show_fullscreen_button = true,
39
+ stream_state = "closed",
40
+ upload_promise = $bindable<Promise<any> | null>(null),
41
+ onerror,
42
+ uploading = $bindable(false),
43
+ active_source = $bindable<source_type>(null),
44
+ fullscreen = $bindable(false),
45
+ dragging = $bindable(false),
46
+ onchange,
47
+ onstream,
48
+ onclear,
49
+ ondrag,
50
+ onupload,
51
+ onselect,
52
+ onfullscreen,
53
+ onclose_stream,
54
+ children
55
+ }: {
56
+ value?: null | FileData | Base64File;
57
+ label?: string;
58
+ show_label: boolean;
59
+ sources?: source_type[];
60
+ streaming?: boolean;
61
+ pending?: boolean;
62
+ webcam_options: WebcamOptions;
63
+ selectable?: boolean;
64
+ root: string;
65
+ i18n: I18nFormatter;
66
+ max_file_size?: number | null;
67
+ upload: Client["upload"];
68
+ stream_handler: Client["stream"];
69
+ stream_every: number;
70
+ time_limit: number;
71
+ show_fullscreen_button?: boolean;
72
+ stream_state?: "open" | "waiting" | "closed";
73
+ upload_promise?: Promise<any> | null;
74
+ onerror?: (error: string) => void;
75
+ uploading?: boolean;
76
+ active_source?: source_type;
77
+ fullscreen?: boolean;
78
+ dragging?: boolean;
79
+ onchange?: (value?: null | FileData | Base64File) => void;
80
+ onstream?: (value: ValueData) => void;
81
+ onclear?: () => void;
82
+ ondrag?: (dragging: boolean) => void;
83
+ onupload?: () => void;
84
+ onselect?: (value: SelectData) => void;
85
+ onfullscreen?: (fullscreen: boolean) => void;
86
+ onclose_stream?: () => void;
87
+ children?: Snippet;
88
+ } = $props();
42
89
 
43
90
  let upload_input: Upload;
44
- export let uploading = false;
45
- export let active_source: source_type = null;
46
- export let fullscreen = false;
47
91
 
48
- let files: FileData[] = [];
49
- let upload_id: string;
92
+ let files = $state<FileData[]>([]);
93
+ let upload_id = $state("");
50
94
 
51
95
  async function handle_upload(detail: FileData): Promise<void> {
52
96
  if (!streaming) {
@@ -62,14 +106,14 @@
62
106
  }
63
107
 
64
108
  await tick();
65
- dispatch("upload");
109
+ onupload?.();
66
110
  }
67
111
  }
68
112
 
69
113
  function handle_clear(): void {
70
114
  value = null;
71
- dispatch("clear");
72
- dispatch("change", null);
115
+ onclear?.();
116
+ onchange?.(null);
73
117
  }
74
118
 
75
119
  function handle_remove_image_click(event: MouseEvent): void {
@@ -82,7 +126,7 @@
82
126
  event: "change" | "stream" | "upload"
83
127
  ): Promise<void> {
84
128
  if (event === "stream") {
85
- dispatch("stream", {
129
+ onstream?.({
86
130
  value: { url: img_blob } as Base64File,
87
131
  is_value_data: true
88
132
  });
@@ -105,38 +149,31 @@
105
149
  if (event === "change" || event === "upload") {
106
150
  value = f?.[0] || null;
107
151
  await tick();
108
- dispatch("change");
152
+ onchange?.();
109
153
  }
110
154
  pending = false;
111
155
  }
112
156
 
113
- $: active_streaming = streaming && active_source === "webcam";
114
- $: if (uploading && !active_streaming) value = null;
115
-
116
- const dispatch = createEventDispatcher<{
117
- change?: never;
118
- stream: ValueData;
119
- clear?: never;
120
- drag: boolean;
121
- upload?: never;
122
- select: SelectData;
123
- end_stream: never;
124
- }>();
125
-
126
- export let dragging = false;
127
-
128
- $: dispatch("drag", dragging);
157
+ let active_streaming = $derived(streaming && active_source === "webcam");
158
+ $effect(() => {
159
+ if (uploading && !active_streaming) value = null;
160
+ });
161
+ $effect(() => {
162
+ ondrag?.(dragging);
163
+ });
129
164
 
130
165
  function handle_click(evt: MouseEvent): void {
131
166
  let coordinates = get_coordinates_of_clicked_image(evt);
132
167
  if (coordinates) {
133
- dispatch("select", { index: coordinates, value: null });
168
+ onselect?.({ index: coordinates, value: null });
134
169
  }
135
170
  }
136
171
 
137
- $: if (!active_source && sources) {
138
- active_source = sources[0];
139
- }
172
+ $effect(() => {
173
+ if (!active_source && sources) {
174
+ active_source = sources[0];
175
+ }
176
+ });
140
177
 
141
178
  async function handle_select_source(
142
179
  source: (typeof sources)[number]
@@ -184,7 +221,13 @@
184
221
  <IconButtonWrapper>
185
222
  {#if value?.url && !active_streaming}
186
223
  {#if show_fullscreen_button}
187
- <FullscreenButton {fullscreen} on:fullscreen />
224
+ <FullscreenButton
225
+ {fullscreen}
226
+ onclick={(is_fullscreen) => {
227
+ fullscreen = is_fullscreen;
228
+ onfullscreen?.(is_fullscreen);
229
+ }}
230
+ />
188
231
  {/if}
189
232
  <IconButton
190
233
  Icon={Clear}
@@ -198,8 +241,8 @@
198
241
  class="upload-container"
199
242
  class:reduced-height={sources.length > 1}
200
243
  style:width={value ? "auto" : "100%"}
201
- on:dragover={on_drag_over}
202
- on:drop={on_drop}
244
+ ondragover={on_drag_over}
245
+ ondrop={on_drop}
203
246
  >
204
247
  <Upload
205
248
  bind:upload_promise
@@ -218,7 +261,7 @@
218
261
  aria_label={i18n("image.drop_to_upload")}
219
262
  >
220
263
  {#if value === null}
221
- <slot />
264
+ {#if children}{@render children()}{/if}
222
265
  {/if}
223
266
  </Upload>
224
267
  {#if active_source === "webcam" && !streaming && pending}
@@ -227,12 +270,10 @@
227
270
  <Webcam
228
271
  {root}
229
272
  {value}
230
- on:capture={(e) => handle_save(e.detail, "change")}
231
- on:stream={(e) => handle_save(e.detail, "stream")}
232
- on:error
233
- on:drag
234
- on:upload={(e) => handle_save(e.detail, "upload")}
235
- on:close_stream
273
+ oncapture={(detail) => handle_save(detail, "change")}
274
+ onstream={(detail) => handle_save(detail, "stream")}
275
+ {onerror}
276
+ {onclose_stream}
236
277
  {stream_state}
237
278
  mirror_webcam={webcam_options.mirror}
238
279
  {stream_every}
@@ -247,7 +288,7 @@
247
288
  {:else if value !== null && !streaming}
248
289
  <!-- svelte-ignore a11y-click-events-have-key-events-->
249
290
  <!-- svelte-ignore a11y-no-static-element-interactions-->
250
- <div class:selectable class="image-frame" on:click={handle_click}>
291
+ <div class:selectable class="image-frame" onclick={handle_click}>
251
292
  <Image src={value.url} restProps={{ alt: value.alt_text }} />
252
293
  </div>
253
294
  {/if}
@@ -257,7 +298,7 @@
257
298
  {sources}
258
299
  bind:active_source
259
300
  {handle_clear}
260
- handle_select={handle_select_source}
301
+ handle_select={(source) => handle_select_source(source as source_type)}
261
302
  />
262
303
  {/if}
263
304
  </div>
@@ -1,29 +1,13 @@
1
+ import { type Snippet } from "svelte";
1
2
  import { type SelectData, type I18nFormatter, type ValueData } from "@gradio/utils";
2
3
  import { FileData, type Client } from "@gradio/client";
3
4
  import type { Base64File, WebcamOptions } from "./types";
4
- interface $$__sveltets_2_IsomorphicComponent<Props extends Record<string, any> = any, Events extends Record<string, any> = any, Slots extends Record<string, any> = any, Exports = {}, Bindings = string> {
5
- new (options: import('svelte').ComponentConstructorOptions<Props>): import('svelte').SvelteComponent<Props, Events, Slots> & {
6
- $$bindings?: Bindings;
7
- } & Exports;
8
- (internal: unknown, props: Props & {
9
- $$events?: Events;
10
- $$slots?: Slots;
11
- }): Exports & {
12
- $set?: any;
13
- $on?: any;
14
- };
15
- z_$$bindings?: Bindings;
16
- }
17
- type $$__sveltets_2_PropsWithChildren<Props, Slots> = Props & (Slots extends {
18
- default: any;
19
- } ? Props extends Record<string, never> ? any : {
20
- children?: any;
21
- } : {});
22
- declare const ImageUploader: $$__sveltets_2_IsomorphicComponent<$$__sveltets_2_PropsWithChildren<{
5
+ type source_type = "upload" | "webcam" | "clipboard" | "microphone" | null;
6
+ type $$ComponentProps = {
23
7
  value?: null | FileData | Base64File;
24
- label?: string | undefined;
8
+ label?: string;
25
9
  show_label: boolean;
26
- sources?: ("upload" | "clipboard" | "microphone" | "webcam" | null)[];
10
+ sources?: source_type[];
27
11
  streaming?: boolean;
28
12
  pending?: boolean;
29
13
  webcam_options: WebcamOptions;
@@ -38,28 +22,21 @@ declare const ImageUploader: $$__sveltets_2_IsomorphicComponent<$$__sveltets_2_P
38
22
  show_fullscreen_button?: boolean;
39
23
  stream_state?: "open" | "waiting" | "closed";
40
24
  upload_promise?: Promise<any> | null;
41
- onerror?: ((error: string) => void) | undefined;
25
+ onerror?: (error: string) => void;
42
26
  uploading?: boolean;
43
- active_source?: "upload" | "clipboard" | "microphone" | "webcam" | null;
27
+ active_source?: source_type;
44
28
  fullscreen?: boolean;
45
29
  dragging?: boolean;
46
- }, {
47
- default: {};
48
- }>, {
49
- fullscreen: any;
50
- error: CustomEvent<string>;
51
- drag: CustomEvent<any>;
52
- close_stream: CustomEvent<undefined>;
53
- change?: CustomEvent<undefined> | undefined;
54
- stream: CustomEvent<ValueData>;
55
- clear?: CustomEvent<undefined> | undefined;
56
- upload?: CustomEvent<undefined> | undefined;
57
- select: CustomEvent<SelectData>;
58
- end_stream: CustomEvent<never>;
59
- } & {
60
- [evt: string]: CustomEvent<any>;
61
- }, {
62
- default: {};
63
- }, {}, string>;
64
- type ImageUploader = InstanceType<typeof ImageUploader>;
30
+ onchange?: (value?: null | FileData | Base64File) => void;
31
+ onstream?: (value: ValueData) => void;
32
+ onclear?: () => void;
33
+ ondrag?: (dragging: boolean) => void;
34
+ onupload?: () => void;
35
+ onselect?: (value: SelectData) => void;
36
+ onfullscreen?: (fullscreen: boolean) => void;
37
+ onclose_stream?: () => void;
38
+ children?: Snippet;
39
+ };
40
+ declare const ImageUploader: import("svelte").Component<$$ComponentProps, {}, "pending" | "value" | "active_source" | "fullscreen" | "uploading" | "dragging" | "upload_promise">;
41
+ type ImageUploader = ReturnType<typeof ImageUploader>;
65
42
  export default ImageUploader;