gradio-pr-bot commited on
Commit
61e978a
·
verified ·
1 Parent(s): e1f579a

Upload folder using huggingface_hub

Browse files
6.25.0/imageslider/Example.svelte ADDED
@@ -0,0 +1,76 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ let {
3
+ value,
4
+ samples_dir,
5
+ type,
6
+ selected = false
7
+ }: {
8
+ value: [string, string];
9
+ samples_dir: string;
10
+ type: "gallery" | "table";
11
+ selected?: boolean;
12
+ } = $props();
13
+ </script>
14
+
15
+ <!-- TODO: fix -->
16
+ <!-- svelte-ignore a11y-missing-attribute -->
17
+ <div
18
+ class="wrap"
19
+ class:table={type === "table"}
20
+ class:gallery={type === "gallery"}
21
+ class:selected
22
+ >
23
+ <img src={samples_dir + value[0]} />
24
+
25
+ <img src={samples_dir + value[1]} />
26
+ <span></span>
27
+ </div>
28
+
29
+ <style>
30
+ .wrap {
31
+ position: relative;
32
+ height: var(--size-64);
33
+ width: var(--size-40);
34
+ overflow: hidden;
35
+ border-radius: var(--radius-lg);
36
+ }
37
+ img {
38
+ height: var(--size-64);
39
+ width: var(--size-40);
40
+ position: absolute;
41
+ /* border-radius: var(--radius-lg); */
42
+ /* max-width: none; */
43
+ object-fit: cover;
44
+ }
45
+
46
+ .wrap.selected {
47
+ border-color: var(--color-accent);
48
+ }
49
+ .wrap img:first-child {
50
+ clip-path: inset(0 50% 0 0%);
51
+ }
52
+
53
+ .wrap img:nth-of-type(2) {
54
+ clip-path: inset(0 0 0 50%);
55
+ }
56
+ span {
57
+ position: absolute;
58
+ top: 0;
59
+ left: calc(50% - 0.75px);
60
+ height: var(--size-64);
61
+ width: 1.5px;
62
+ background: var(--border-color-primary);
63
+ }
64
+
65
+ .table {
66
+ margin: 0 auto;
67
+ border: 2px solid var(--border-color-primary);
68
+ border-radius: var(--radius-lg);
69
+ }
70
+
71
+ .gallery {
72
+ border: 2px solid var(--border-color-primary);
73
+ /* max-height: var(--size-20); */
74
+ object-fit: cover;
75
+ }
76
+ </style>
6.25.0/imageslider/Index.svelte ADDED
@@ -0,0 +1,160 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { tick } from "svelte";
3
+ import type { ImageSliderProps, ImageSliderEvents } from "./types";
4
+ import { Gradio } from "@gradio/utils";
5
+ import StaticImage from "./shared/SliderPreview.svelte";
6
+ import ImageUploader from "./shared/SliderUpload.svelte";
7
+
8
+ import { Block, Empty, UploadText } from "@gradio/atoms";
9
+ import { Image } from "@gradio/icons";
10
+ import { StatusTracker } from "@gradio/statustracker";
11
+
12
+ type sources = "upload" | "webcam" | "clipboard" | null;
13
+
14
+ let upload_promise = $state<Promise<any>>();
15
+
16
+ class ImageSliderGradio extends Gradio<ImageSliderEvents, ImageSliderProps> {
17
+ async get_data() {
18
+ if (upload_promise) {
19
+ await upload_promise;
20
+ await tick();
21
+ }
22
+
23
+ const data = await super.get_data();
24
+ return data;
25
+ }
26
+ }
27
+
28
+ const props = $props();
29
+ const gradio = new ImageSliderGradio(props);
30
+
31
+ let fullscreen = $state(false);
32
+ let dragging = $state(false);
33
+ let active_source: sources = $state(null);
34
+ let value = $state(gradio.props.value ?? [null, null]);
35
+
36
+ let normalised_slider_position = $derived(
37
+ Math.max(0, Math.min(100, gradio.props.slider_position)) / 100
38
+ );
39
+
40
+ gradio.watch_for_change();
41
+
42
+ $effect(() => {
43
+ value = gradio.props.value ?? [null, null];
44
+ });
45
+
46
+ $effect(() => {
47
+ gradio.props.value = value;
48
+ });
49
+ </script>
50
+
51
+ {#if !gradio.shared.interactive || (value?.[1] && value?.[0])}
52
+ <Block
53
+ visible={gradio.shared.visible}
54
+ variant={"solid"}
55
+ border_mode={dragging ? "focus" : "base"}
56
+ padding={false}
57
+ elem_id={gradio.shared.elem_id}
58
+ elem_classes={gradio.shared.elem_classes}
59
+ height={gradio.props.height || undefined}
60
+ width={gradio.props.width}
61
+ allow_overflow={false}
62
+ container={gradio.shared.container}
63
+ scale={gradio.shared.scale}
64
+ min_width={gradio.shared.min_width}
65
+ bind:fullscreen
66
+ >
67
+ <StatusTracker
68
+ autoscroll={gradio.shared.autoscroll}
69
+ i18n={gradio.i18n}
70
+ {...gradio.shared.loading_status}
71
+ />
72
+ <StaticImage
73
+ onclear={() => {
74
+ gradio.dispatch("clear");
75
+ gradio.dispatch("input");
76
+ }}
77
+ onfullscreen={(detail) => {
78
+ fullscreen = detail;
79
+ }}
80
+ {fullscreen}
81
+ interactive={gradio.shared.interactive}
82
+ bind:value
83
+ label={gradio.shared.label}
84
+ show_label={gradio.shared.show_label}
85
+ show_download_button={gradio.props.buttons.some(
86
+ (btn) => typeof btn === "string" && btn === "download"
87
+ )}
88
+ i18n={gradio.i18n}
89
+ show_fullscreen_button={gradio.props.buttons.some(
90
+ (btn) => typeof btn === "string" && btn === "fullscreen"
91
+ )}
92
+ buttons={gradio.props.buttons}
93
+ on_custom_button_click={(id) => {
94
+ gradio.dispatch("custom_button_click", { id });
95
+ }}
96
+ position={normalised_slider_position}
97
+ slider_color={gradio.props.slider_color}
98
+ max_height={gradio.props.max_height}
99
+ />
100
+ </Block>
101
+ {:else}
102
+ <Block
103
+ visible={gradio.shared.visible}
104
+ variant={value?.[0] || value?.[1] ? "solid" : "dashed"}
105
+ border_mode={dragging ? "focus" : "base"}
106
+ padding={false}
107
+ elem_id={gradio.shared.elem_id}
108
+ elem_classes={gradio.shared.elem_classes}
109
+ height={gradio.props.height || undefined}
110
+ width={gradio.props.width}
111
+ allow_overflow={false}
112
+ container={gradio.shared.container}
113
+ scale={gradio.shared.scale}
114
+ min_width={gradio.shared.min_width}
115
+ >
116
+ <StatusTracker
117
+ autoscroll={gradio.shared.autoscroll}
118
+ i18n={gradio.i18n}
119
+ {...gradio.shared.loading_status}
120
+ on_clear_status={() =>
121
+ gradio.dispatch("clear_status", gradio.shared.loading_status)}
122
+ />
123
+
124
+ <ImageUploader
125
+ bind:upload_promise
126
+ bind:value
127
+ bind:dragging
128
+ root={gradio.shared.root}
129
+ onclear={() => {
130
+ gradio.dispatch("clear");
131
+ gradio.dispatch("input");
132
+ }}
133
+ ondrag={(detail) => (dragging = detail)}
134
+ onupload={() => {
135
+ gradio.dispatch("upload");
136
+ gradio.dispatch("input");
137
+ }}
138
+ label={gradio.shared.label}
139
+ show_label={gradio.shared.show_label}
140
+ upload_count={gradio.props.upload_count}
141
+ max_file_size={gradio.shared.max_file_size}
142
+ i18n={gradio.i18n}
143
+ upload={(...args) => gradio.shared.client.upload(...args)}
144
+ stream_handler={gradio.shared.client?.stream}
145
+ max_height={gradio.props.max_height}
146
+ >
147
+ {#if active_source === "upload" || !active_source}
148
+ <UploadText
149
+ i18n={gradio.i18n}
150
+ type="image"
151
+ placeholder={gradio.props.placeholder}
152
+ />
153
+ {:else if active_source === "clipboard"}
154
+ <UploadText i18n={gradio.i18n} type="clipboard" mode="short" />
155
+ {:else}
156
+ <Empty unpadded_box={true} size="large"><Image /></Empty>
157
+ {/if}
158
+ </ImageUploader>
159
+ </Block>
160
+ {/if}
6.25.0/imageslider/package.json ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/imageslider",
3
+ "version": "0.7.1",
4
+ "description": "Gradio UI packages",
5
+ "type": "module",
6
+ "author": "",
7
+ "license": "ISC",
8
+ "private": false,
9
+ "dependencies": {
10
+ "@gradio/atoms": "workspace:^",
11
+ "@gradio/client": "workspace:^",
12
+ "@gradio/icons": "workspace:^",
13
+ "@gradio/statustracker": "workspace:^",
14
+ "@gradio/upload": "workspace:^",
15
+ "@gradio/utils": "workspace:^",
16
+ "@types/d3-drag": "^3.0.7",
17
+ "@types/d3-selection": "^3.0.11",
18
+ "d3-drag": "^3.0.0",
19
+ "d3-selection": "^3.0.0"
20
+ },
21
+ "exports": {
22
+ ".": {
23
+ "gradio": "./Index.svelte",
24
+ "svelte": "./dist/Index.svelte",
25
+ "types": "./dist/Index.svelte.d.ts"
26
+ },
27
+ "./example": {
28
+ "gradio": "./Example.svelte",
29
+ "svelte": "./dist/Example.svelte",
30
+ "types": "./dist/Example.svelte.d.ts"
31
+ },
32
+ "./package.json": "./package.json"
33
+ },
34
+ "devDependencies": {
35
+ "@gradio/preview": "workspace:^"
36
+ },
37
+ "main_changeset": true,
38
+ "peerDependencies": {
39
+ "svelte": "^5.48.0"
40
+ },
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/gradio-app/gradio.git",
44
+ "directory": "js/imageslider"
45
+ }
46
+ }
6.25.0/imageslider/shared/ClearImage.svelte ADDED
@@ -0,0 +1,29 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { IconButton } from "@gradio/atoms";
3
+ import { Clear } from "@gradio/icons";
4
+
5
+ let { onremove_image }: { onremove_image?: () => void } = $props();
6
+ </script>
7
+
8
+ <div>
9
+ <IconButton
10
+ Icon={Clear}
11
+ label="Remove Image"
12
+ onclick={(event) => {
13
+ onremove_image?.();
14
+ event.stopPropagation();
15
+ }}
16
+ />
17
+ </div>
18
+
19
+ <style>
20
+ div {
21
+ display: flex;
22
+ position: absolute;
23
+ top: var(--size-2);
24
+ right: var(--size-2);
25
+ justify-content: flex-end;
26
+ gap: var(--spacing-sm);
27
+ z-index: var(--layer-5);
28
+ }
29
+ </style>
6.25.0/imageslider/shared/Image.svelte ADDED
@@ -0,0 +1,256 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import Slider from "./Slider.svelte";
3
+ import { tick, type Snippet } from "svelte";
4
+ import { BlockLabel, Empty, IconButton, DownloadLink } from "@gradio/atoms";
5
+ import { Download } from "@gradio/icons";
6
+ import { Image } from "@gradio/icons";
7
+ import { type I18nFormatter } from "@gradio/utils";
8
+ import ClearImage from "./ClearImage.svelte";
9
+ import ImageEl from "./ImageEl.svelte";
10
+
11
+ import { Upload } from "@gradio/upload";
12
+
13
+ import { type FileData, type Client } from "@gradio/client";
14
+
15
+ let {
16
+ value = $bindable<[FileData | null, FileData | null]>([null, null]),
17
+ label = undefined,
18
+ show_label,
19
+ root,
20
+ position = $bindable(0.5),
21
+ upload_count = 2,
22
+ show_download_button = true,
23
+ slider_color,
24
+ upload,
25
+ stream_handler,
26
+ max_file_size = null,
27
+ i18n,
28
+ max_height,
29
+ upload_promise = $bindable(),
30
+ dragging = $bindable(false),
31
+ onclear,
32
+ ondrag,
33
+ onupload,
34
+ children
35
+ }: {
36
+ value?: [FileData | null, FileData | null];
37
+ label?: string;
38
+ show_label: boolean;
39
+ root: string;
40
+ position?: number;
41
+ upload_count?: number;
42
+ show_download_button?: boolean;
43
+ slider_color: string;
44
+ upload: Client["upload"];
45
+ stream_handler: Client["stream"];
46
+ max_file_size?: number | null;
47
+ i18n: I18nFormatter;
48
+ max_height: number;
49
+ upload_promise?: Promise<any>;
50
+ dragging?: boolean;
51
+ onclear?: () => void;
52
+ ondrag?: (dragging: boolean) => void;
53
+ onupload?: (value: [FileData | null, FileData | null]) => void;
54
+ children?: Snippet;
55
+ } = $props();
56
+
57
+ let value_ = $state<[FileData | null, FileData | null]>(
58
+ value || [null, null]
59
+ );
60
+
61
+ let img: HTMLImageElement;
62
+ let el_width = $state(0);
63
+ let el_height = $state(0);
64
+
65
+ async function handle_upload(
66
+ detail: Blob | File | FileData | FileData[],
67
+ n: number
68
+ ): Promise<void> {
69
+ const file_data = Array.isArray(detail) ? detail : [detail as FileData];
70
+ const new_value = [value[0], value[1]] as [
71
+ FileData | null,
72
+ FileData | null
73
+ ];
74
+ if (file_data.length > 1) {
75
+ new_value[n] = file_data[0];
76
+ } else {
77
+ new_value[n] = file_data[n];
78
+ }
79
+ value = new_value;
80
+ await tick();
81
+
82
+ onupload?.(new_value);
83
+ }
84
+
85
+ let old_value = "";
86
+
87
+ $effect(() => {
88
+ if (JSON.stringify(value) === old_value) return;
89
+ old_value = JSON.stringify(value);
90
+ value_ = value;
91
+ });
92
+
93
+ $effect(() => {
94
+ ondrag?.(dragging);
95
+ });
96
+ </script>
97
+
98
+ <BlockLabel {show_label} Icon={Image} label={label || i18n("image.image")} />
99
+
100
+ <div
101
+ data-testid="image"
102
+ class="image-container"
103
+ bind:clientWidth={el_width}
104
+ bind:clientHeight={el_height}
105
+ >
106
+ {#if value?.[0]?.url || value?.[1]?.url}
107
+ <ClearImage
108
+ onremove_image={() => {
109
+ position = 0.5;
110
+ value = [null, null];
111
+ onclear?.();
112
+ }}
113
+ />
114
+ {/if}
115
+ {#if value?.[1]?.url}
116
+ <div class="icon-buttons">
117
+ {#if show_download_button}
118
+ <DownloadLink
119
+ href={value[1].url}
120
+ download={value[1].orig_name || "image"}
121
+ >
122
+ <IconButton Icon={Download} />
123
+ </DownloadLink>
124
+ {/if}
125
+ </div>
126
+ {/if}
127
+ <Slider
128
+ bind:position
129
+ disabled={upload_count == 2 || !value?.[0]}
130
+ {slider_color}
131
+ >
132
+ <div
133
+ class="upload-wrap"
134
+ style:display={upload_count === 2 ? "flex" : "block"}
135
+ class:side-by-side={upload_count === 2}
136
+ >
137
+ {#if !value_?.[0]}
138
+ <div class="wrap" class:half-wrap={upload_count === 1}>
139
+ <Upload
140
+ bind:upload_promise
141
+ bind:dragging
142
+ filetype="image/*"
143
+ onload={(e) => handle_upload(e, 0)}
144
+ disable_click={!!value?.[0]}
145
+ {root}
146
+ file_count="multiple"
147
+ {upload}
148
+ {stream_handler}
149
+ {max_file_size}
150
+ >
151
+ {#if children}{@render children()}{/if}
152
+ </Upload>
153
+ </div>
154
+ {:else}
155
+ <ImageEl
156
+ variant="upload"
157
+ src={value_[0]?.url}
158
+ alt=""
159
+ bind:img_el={img}
160
+ {max_height}
161
+ />
162
+ {/if}
163
+
164
+ {#if !value_?.[1] && upload_count === 2}
165
+ <Upload
166
+ bind:upload_promise
167
+ bind:dragging
168
+ filetype="image/*"
169
+ onload={(e) => handle_upload(e, 1)}
170
+ disable_click={!!value?.[1]}
171
+ {root}
172
+ file_count="multiple"
173
+ {upload}
174
+ {stream_handler}
175
+ {max_file_size}
176
+ >
177
+ {#if children}{@render children()}{/if}
178
+ </Upload>
179
+ {:else if !value_?.[1] && upload_count === 1}
180
+ <div
181
+ class="empty-wrap fixed"
182
+ style:width="{el_width * (1 - position)}px"
183
+ style:transform="translateX({el_width * position}px)"
184
+ class:white-icon={!value?.[0]?.url}
185
+ >
186
+ <Empty unpadded_box={true} size="large"><Image /></Empty>
187
+ </div>
188
+ {:else if value_?.[1]}
189
+ <ImageEl
190
+ variant="upload"
191
+ src={value_[1].url}
192
+ alt=""
193
+ fixed={upload_count === 1}
194
+ transform="translate(0px, 0px) scale(1)"
195
+ {max_height}
196
+ />
197
+ {/if}
198
+ </div>
199
+ </Slider>
200
+ </div>
201
+
202
+ <style>
203
+ .upload-wrap {
204
+ display: flex;
205
+ justify-content: center;
206
+ align-items: center;
207
+ height: 100%;
208
+ width: 100%;
209
+ }
210
+
211
+ .wrap {
212
+ width: 100%;
213
+ }
214
+
215
+ .half-wrap {
216
+ width: 50%;
217
+ }
218
+ .image-container,
219
+ .empty-wrap {
220
+ width: var(--size-full);
221
+ height: var(--size-full);
222
+ }
223
+
224
+ .fixed {
225
+ --anim-block-background-fill: 255, 255, 255;
226
+ position: absolute;
227
+ top: 0;
228
+ left: 0;
229
+ background-color: rgba(var(--anim-block-background-fill), 0.8);
230
+ z-index: 0;
231
+ }
232
+
233
+ @media (prefers-color-scheme: dark) {
234
+ .fixed {
235
+ --anim-block-background-fill: 31, 41, 55;
236
+ }
237
+ }
238
+
239
+ .side-by-side :global(img) {
240
+ /* width: 100%; */
241
+ width: 50%;
242
+ object-fit: contain;
243
+ }
244
+
245
+ .empty-wrap {
246
+ pointer-events: none;
247
+ }
248
+
249
+ .icon-buttons {
250
+ display: flex;
251
+ position: absolute;
252
+ right: 8px;
253
+ z-index: var(--layer-top);
254
+ top: 8px;
255
+ }
256
+ </style>
6.25.0/imageslider/shared/ImageEl.svelte ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import type { HTMLImgAttributes } from "svelte/elements";
3
+ import { onMount, tick } from "svelte";
4
+ interface Props extends Omit<HTMLImgAttributes, "onload"> {
5
+ "data-testid"?: string;
6
+ fixed?: boolean;
7
+ transform?: string;
8
+ img_el?: HTMLImageElement | null;
9
+ hidden?: boolean;
10
+ variant?: "preview" | "upload";
11
+ max_height?: number;
12
+ fullscreen?: boolean;
13
+ onload?: (size: {
14
+ top: number;
15
+ left: number;
16
+ width: number;
17
+ height: number;
18
+ }) => void;
19
+ }
20
+
21
+ let {
22
+ src = undefined,
23
+ fullscreen = false,
24
+ fixed = false,
25
+ transform = "translate(0px, 0px) scale(1)",
26
+ img_el = $bindable<HTMLImageElement | null>(),
27
+ hidden = false,
28
+ variant = "upload",
29
+ max_height = 500,
30
+ onload,
31
+ ...restProps
32
+ }: Props = $props();
33
+
34
+ function get_image_size(img: HTMLImageElement | null): {
35
+ top: number;
36
+ left: number;
37
+ width: number;
38
+ height: number;
39
+ } {
40
+ if (!img) return { top: 0, left: 0, width: 0, height: 0 };
41
+ const container = img.parentElement?.getBoundingClientRect();
42
+
43
+ if (!container) return { top: 0, left: 0, width: 0, height: 0 };
44
+
45
+ const naturalAspect = img.naturalWidth / img.naturalHeight;
46
+ const containerAspect = container.width / container.height;
47
+ let displayedWidth, displayedHeight;
48
+
49
+ if (naturalAspect > containerAspect) {
50
+ displayedWidth = container.width;
51
+ displayedHeight = container.width / naturalAspect;
52
+ } else {
53
+ displayedHeight = container.height;
54
+ displayedWidth = container.height * naturalAspect;
55
+ }
56
+
57
+ const offsetX = (container.width - displayedWidth) / 2;
58
+ const offsetY = (container.height - displayedHeight) / 2;
59
+
60
+ return {
61
+ top: offsetY,
62
+ left: offsetX,
63
+ width: displayedWidth,
64
+ height: displayedHeight
65
+ };
66
+ }
67
+
68
+ onMount(() => {
69
+ const resizer = new ResizeObserver(async (entries) => {
70
+ for (const entry of entries) {
71
+ await tick();
72
+ onload?.(get_image_size(img_el));
73
+ }
74
+ });
75
+
76
+ resizer.observe(img_el!);
77
+
78
+ return () => {
79
+ resizer.disconnect();
80
+ };
81
+ });
82
+ </script>
83
+
84
+ <!-- svelte-ignore a11y-missing-attribute -->
85
+ <img
86
+ {src}
87
+ data-testid="imageslider-image"
88
+ {...restProps}
89
+ class:fixed
90
+ style:transform
91
+ bind:this={img_el}
92
+ class:hidden
93
+ class:preview={variant === "preview"}
94
+ class:slider={variant === "upload"}
95
+ style:max-height={max_height && !fullscreen ? `${max_height}px` : null}
96
+ class:fullscreen
97
+ class:small={!fullscreen}
98
+ onload={() => onload?.(get_image_size(img_el))}
99
+ />
100
+
101
+ <style>
102
+ .preview {
103
+ object-fit: contain;
104
+ width: 100%;
105
+ transform-origin: top left;
106
+ margin: auto;
107
+ }
108
+
109
+ .small {
110
+ max-height: 500px;
111
+ }
112
+
113
+ .upload {
114
+ object-fit: contain;
115
+ max-height: 500px;
116
+ }
117
+
118
+ .fixed {
119
+ position: absolute;
120
+ top: 0;
121
+ left: 0;
122
+ right: 0;
123
+ bottom: 0;
124
+ }
125
+
126
+ .fullscreen {
127
+ width: 100%;
128
+ height: 100%;
129
+ }
130
+
131
+ :global(.image-container:fullscreen) img {
132
+ width: 100%;
133
+ height: 100%;
134
+ max-height: none;
135
+ max-width: none;
136
+ }
137
+
138
+ .hidden {
139
+ opacity: 0;
140
+ }
141
+ </style>
6.25.0/imageslider/shared/Slider.svelte ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { onMount, type Snippet } from "svelte";
3
+ import { drag } from "d3-drag";
4
+ import { select } from "d3-selection";
5
+
6
+ function clamp(value: number, min: number, max: number): number {
7
+ return Math.min(Math.max(value, min), max);
8
+ }
9
+
10
+ let {
11
+ position = $bindable(0.5),
12
+ disabled = false,
13
+ slider_color = "var(--border-color-primary)",
14
+ image_size = { top: 0, left: 0, width: 0, height: 0 },
15
+ el = $bindable<HTMLDivElement | undefined>(undefined),
16
+ parent_el = $bindable<HTMLDivElement | undefined>(undefined),
17
+ children
18
+ }: {
19
+ position?: number;
20
+ disabled?: boolean;
21
+ slider_color?: string;
22
+ image_size?: {
23
+ top: number;
24
+ left: number;
25
+ width: number;
26
+ height: number;
27
+ };
28
+ el?: HTMLDivElement;
29
+ parent_el?: HTMLDivElement;
30
+ children?: Snippet;
31
+ } = $props();
32
+
33
+ let inner: Element;
34
+ let px = $state(0);
35
+ let active = $state(false);
36
+ let container_width = $state(0);
37
+
38
+ function set_position(width: number): void {
39
+ container_width = parent_el?.getBoundingClientRect().width || 0;
40
+ if (width === 0) {
41
+ image_size.width = el?.getBoundingClientRect().width || 0;
42
+ }
43
+
44
+ px = clamp(
45
+ image_size.width * position + image_size.left,
46
+ 0,
47
+ container_width
48
+ );
49
+ }
50
+
51
+ function round(n: number, points: number): number {
52
+ const mod = Math.pow(10, points);
53
+ return Math.round((n + Number.EPSILON) * mod) / mod;
54
+ }
55
+
56
+ function update_position(x: number): void {
57
+ px = clamp(x, 0, container_width);
58
+ position = round((x - image_size.left) / image_size.width, 5);
59
+ }
60
+
61
+ function drag_start(event: any): void {
62
+ if (disabled) return;
63
+ active = true;
64
+ update_position(event.x);
65
+ }
66
+
67
+ function drag_move(event: any): void {
68
+ if (disabled) return;
69
+ update_position(event.x);
70
+ }
71
+
72
+ function drag_end(): void {
73
+ if (disabled) return;
74
+ active = false;
75
+ }
76
+
77
+ function update_position_from_pc(pc: number): void {
78
+ px = clamp(image_size.width * pc + image_size.left, 0, container_width);
79
+ }
80
+
81
+ $effect(() => {
82
+ set_position(image_size.width);
83
+ });
84
+ $effect(() => {
85
+ update_position_from_pc(position);
86
+ });
87
+
88
+ onMount(() => {
89
+ set_position(image_size.width);
90
+ const drag_handler = drag()
91
+ .on("start", drag_start)
92
+ .on("drag", drag_move)
93
+ .on("end", drag_end);
94
+ select(inner).call(drag_handler);
95
+ });
96
+ </script>
97
+
98
+ <svelte:window onresize={() => set_position(image_size.width)} />
99
+
100
+ <div class="wrap" role="none" bind:this={parent_el}>
101
+ <div class="content" bind:this={el}>
102
+ {#if children}{@render children()}{/if}
103
+ </div>
104
+ <div
105
+ class="outer"
106
+ class:disabled
107
+ data-testid="slider"
108
+ bind:this={inner}
109
+ role="none"
110
+ style="transform: translateX({px}px)"
111
+ class:grab={active}
112
+ >
113
+ <span class="icon-wrap" class:active class:disabled
114
+ ><span class="icon left">◢</span><span
115
+ class="icon center"
116
+ style:--color={slider_color}
117
+ ></span><span class="icon right">◢</span></span
118
+ >
119
+ <div class="inner" style:--color={slider_color}></div>
120
+ </div>
121
+ </div>
122
+
123
+ <style>
124
+ .wrap {
125
+ position: relative;
126
+ width: 100%;
127
+ height: 100%;
128
+ z-index: var(--layer-1);
129
+ overflow: hidden;
130
+ }
131
+
132
+ .icon-wrap {
133
+ display: block;
134
+ position: absolute;
135
+ top: 50%;
136
+ transform: translate(-20.5px, -50%);
137
+ left: 10px;
138
+ width: 40px;
139
+ transition: 0.2s;
140
+ color: var(--body-text-color);
141
+ height: 30px;
142
+ border-radius: 5px;
143
+ background-color: var(--color-accent);
144
+ display: flex;
145
+ align-items: center;
146
+ justify-content: center;
147
+ z-index: var(--layer-3);
148
+ box-shadow: 0px 0px 5px 2px rgba(0, 0, 0, 0.3);
149
+ font-size: 12px;
150
+ }
151
+
152
+ .icon.left {
153
+ transform: rotate(135deg);
154
+ text-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1);
155
+ }
156
+
157
+ .icon.right {
158
+ transform: rotate(-45deg);
159
+ text-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1);
160
+ }
161
+
162
+ .icon.center {
163
+ display: block;
164
+ width: 1px;
165
+ height: 100%;
166
+ background-color: var(--color);
167
+ opacity: 0.1;
168
+ }
169
+
170
+ .icon-wrap.active {
171
+ opacity: 0;
172
+ }
173
+
174
+ .icon-wrap.disabled {
175
+ opacity: 0;
176
+ }
177
+
178
+ .outer {
179
+ width: 20px;
180
+ height: 100%;
181
+ position: absolute;
182
+ cursor: grab;
183
+ position: absolute;
184
+ top: 0;
185
+ left: -10px;
186
+ pointer-events: auto;
187
+ z-index: var(--layer-2);
188
+ }
189
+ .grab {
190
+ cursor: grabbing;
191
+ }
192
+
193
+ .inner {
194
+ width: 1px;
195
+ height: 100%;
196
+ background: var(--color);
197
+ position: absolute;
198
+ left: calc((100% - 2px) / 2);
199
+ }
200
+
201
+ .disabled {
202
+ cursor: auto;
203
+ }
204
+
205
+ .disabled .inner {
206
+ box-shadow: none;
207
+ }
208
+
209
+ .content {
210
+ width: 100%;
211
+ height: 100%;
212
+ display: flex;
213
+ justify-content: center;
214
+ align-items: center;
215
+ }
216
+ </style>
6.25.0/imageslider/shared/SliderPreview.svelte ADDED
@@ -0,0 +1,270 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import Slider from "./Slider.svelte";
3
+ import ImageEl from "./ImageEl.svelte";
4
+ import {
5
+ BlockLabel,
6
+ Empty,
7
+ IconButton,
8
+ IconButtonWrapper,
9
+ FullscreenButton,
10
+ DownloadLink
11
+ } from "@gradio/atoms";
12
+ import type { CustomButton as CustomButtonType } from "@gradio/utils";
13
+ import { Image, Download, Undo, Clear } from "@gradio/icons";
14
+ import { type FileData } from "@gradio/client";
15
+ import type { I18nFormatter } from "@gradio/utils";
16
+ import { ZoomableImage } from "./zoom";
17
+ import { onMount } from "svelte";
18
+ import { tweened, type Tweened } from "svelte/motion";
19
+
20
+ let {
21
+ value = $bindable<[null | FileData, null | FileData]>([null, null]),
22
+ label = undefined,
23
+ show_download_button = true,
24
+ show_label,
25
+ i18n,
26
+ position = $bindable(0.5),
27
+ layer_images = true,
28
+ show_single = false,
29
+ slider_color,
30
+ show_fullscreen_button = true,
31
+ fullscreen = $bindable(false),
32
+ buttons = null,
33
+ on_custom_button_click = null,
34
+ el_width = $bindable(0),
35
+ max_height,
36
+ interactive = true,
37
+ onclear,
38
+ onfullscreen
39
+ }: {
40
+ value?: [null | FileData, null | FileData];
41
+ label?: string;
42
+ show_download_button?: boolean;
43
+ show_label: boolean;
44
+ i18n: I18nFormatter;
45
+ position?: number;
46
+ layer_images?: boolean;
47
+ show_single?: boolean;
48
+ slider_color: string;
49
+ show_fullscreen_button?: boolean;
50
+ fullscreen?: boolean;
51
+ buttons?: (string | CustomButtonType)[] | null;
52
+ on_custom_button_click?: ((id: number) => void) | null;
53
+ el_width?: number;
54
+ max_height: number;
55
+ interactive?: boolean;
56
+ onclear?: () => void;
57
+ onfullscreen?: (fullscreen: boolean) => void;
58
+ } = $props();
59
+
60
+ let img = $state<HTMLImageElement>();
61
+ let slider_wrap = $state<HTMLDivElement>();
62
+ let image_container: HTMLDivElement;
63
+
64
+ let transform: Tweened<{ x: number; y: number; z: number }> = tweened(
65
+ { x: 0, y: 0, z: 1 },
66
+ {
67
+ duration: 75
68
+ }
69
+ );
70
+ let parent_el: HTMLDivElement;
71
+ let img_width = $state(0);
72
+ let viewport_width = $state(0);
73
+ let image_size = $state<{
74
+ top: number;
75
+ left: number;
76
+ width: number;
77
+ height: number;
78
+ }>({ top: 0, left: 0, width: 0, height: 0 });
79
+
80
+ let coords_at_viewport = $derived(
81
+ get_coords_at_viewport(
82
+ position,
83
+ viewport_width,
84
+ image_size.width,
85
+ image_size.left,
86
+ $transform.x,
87
+ $transform.z
88
+ )
89
+ );
90
+ let style = $derived(
91
+ layer_images ? `clip-path: inset(0 0 0 ${coords_at_viewport * 100}%)` : ""
92
+ );
93
+
94
+ function get_coords_at_viewport(
95
+ viewport_percent_x: number, // 0-1
96
+ viewportWidth: number,
97
+ image_width: number,
98
+ img_offset_x: number,
99
+ tx: number, // image translation x (in pixels)
100
+ scale: number // image scale (uniform)
101
+ ): number {
102
+ const px_relative_to_image = viewport_percent_x * image_width;
103
+ const pixel_position = px_relative_to_image + img_offset_x;
104
+
105
+ const normalised_position = (pixel_position - tx) / scale;
106
+ const percent_position = normalised_position / viewportWidth;
107
+
108
+ return percent_position;
109
+ }
110
+
111
+ let zoomable_image: ZoomableImage | null = null;
112
+ let observer: ResizeObserver | null = null;
113
+
114
+ function init_image(
115
+ img: HTMLImageElement | undefined,
116
+ slider_wrap: HTMLDivElement | undefined
117
+ ): void {
118
+ if (!img || !slider_wrap) return;
119
+ zoomable_image?.destroy();
120
+ observer?.disconnect();
121
+ img_width = img?.getBoundingClientRect().width || 0;
122
+ viewport_width = slider_wrap?.getBoundingClientRect().width || 0;
123
+ zoomable_image = new ZoomableImage(slider_wrap, img);
124
+ zoomable_image.subscribe(({ x, y, scale }) => {
125
+ transform.set({ x, y, z: scale });
126
+ });
127
+
128
+ observer = new ResizeObserver((entries) => {
129
+ for (const entry of entries) {
130
+ if (entry.target === slider_wrap) {
131
+ viewport_width = entry.contentRect.width;
132
+ }
133
+
134
+ if (entry.target === img) {
135
+ img_width = entry.contentRect.width;
136
+ }
137
+ }
138
+ });
139
+ observer.observe(slider_wrap);
140
+ observer.observe(img);
141
+ }
142
+
143
+ $effect(() => {
144
+ init_image(img, slider_wrap);
145
+ });
146
+
147
+ onMount(() => {
148
+ return () => {
149
+ zoomable_image?.destroy();
150
+ observer?.disconnect();
151
+ };
152
+ });
153
+
154
+ let slider_wrap_parent: HTMLDivElement;
155
+
156
+ function handle_image_load(size: {
157
+ top: number;
158
+ left: number;
159
+ width: number;
160
+ height: number;
161
+ }): void {
162
+ image_size = size;
163
+ }
164
+ </script>
165
+
166
+ <BlockLabel {show_label} Icon={Image} label={label || i18n("image.image")} />
167
+ {#if (value === null || value[0] === null || value[1] === null) && !show_single}
168
+ <Empty unpadded_box={true} size="large"><Image /></Empty>
169
+ {:else}
170
+ <div class="image-container" bind:this={image_container}>
171
+ <IconButtonWrapper {buttons} {on_custom_button_click}>
172
+ <IconButton
173
+ Icon={Undo}
174
+ label={i18n("common.undo")}
175
+ disabled={$transform.z === 1}
176
+ onclick={() => zoomable_image?.reset_zoom()}
177
+ />
178
+ {#if show_fullscreen_button}
179
+ <FullscreenButton
180
+ {fullscreen}
181
+ onclick={(is_fullscreen) => {
182
+ fullscreen = is_fullscreen;
183
+ onfullscreen?.(is_fullscreen);
184
+ }}
185
+ />
186
+ {/if}
187
+
188
+ {#if show_download_button}
189
+ <DownloadLink
190
+ href={value[1]?.url}
191
+ download={value[1]?.orig_name || "image"}
192
+ >
193
+ <IconButton Icon={Download} label={i18n("common.download")} />
194
+ </DownloadLink>
195
+ {/if}
196
+ {#if interactive}
197
+ <IconButton
198
+ Icon={Clear}
199
+ label="Remove Image"
200
+ onclick={(event) => {
201
+ value = [null, null];
202
+ onclear?.();
203
+ event.stopPropagation();
204
+ }}
205
+ />
206
+ {/if}
207
+ </IconButtonWrapper>
208
+ <div
209
+ class="slider-wrap"
210
+ bind:this={slider_wrap_parent}
211
+ bind:clientWidth={el_width}
212
+ class:limit_height={!fullscreen}
213
+ >
214
+ <Slider
215
+ bind:position
216
+ {slider_color}
217
+ bind:el={slider_wrap}
218
+ bind:parent_el
219
+ {image_size}
220
+ >
221
+ <ImageEl
222
+ src={value?.[0]?.url}
223
+ alt=""
224
+ loading="lazy"
225
+ bind:img_el={img}
226
+ variant="preview"
227
+ transform="translate({$transform.x}px, {$transform.y}px) scale({$transform.z})"
228
+ {fullscreen}
229
+ {max_height}
230
+ onload={handle_image_load}
231
+ />
232
+ <ImageEl
233
+ variant="preview"
234
+ fixed={layer_images}
235
+ hidden={!value?.[1]?.url}
236
+ src={value?.[1]?.url}
237
+ alt=""
238
+ loading="lazy"
239
+ style="{style}; background: var(--block-background-fill);"
240
+ transform="translate({$transform.x}px, {$transform.y}px) scale({$transform.z})"
241
+ {fullscreen}
242
+ {max_height}
243
+ onload={handle_image_load}
244
+ />
245
+ </Slider>
246
+ </div>
247
+ </div>
248
+ {/if}
249
+
250
+ <style>
251
+ .slider-wrap {
252
+ user-select: none;
253
+ height: 100%;
254
+ width: 100%;
255
+ position: relative;
256
+ display: flex;
257
+ align-items: center;
258
+ justify-content: center;
259
+ }
260
+
261
+ .limit_height :global(img) {
262
+ max-height: 500px;
263
+ }
264
+
265
+ .image-container {
266
+ height: 100%;
267
+ position: relative;
268
+ min-width: var(--size-20);
269
+ }
270
+ </style>
6.25.0/imageslider/shared/SliderUpload.svelte ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import type { Snippet } from "svelte";
3
+ import type { I18nFormatter } from "@gradio/utils";
4
+ import Image from "./Image.svelte";
5
+ import { type Client } from "@gradio/client";
6
+
7
+ import type { FileData } from "@gradio/client";
8
+
9
+ let {
10
+ value = $bindable<[FileData | null, FileData | null]>([null, null]),
11
+ upload,
12
+ stream_handler,
13
+ label,
14
+ show_label,
15
+ i18n,
16
+ root,
17
+ upload_count = 1,
18
+ dragging = $bindable(false),
19
+ max_height,
20
+ max_file_size = null,
21
+ upload_promise = $bindable(),
22
+ onclear,
23
+ ondrag,
24
+ onupload,
25
+ children
26
+ }: {
27
+ value?: [FileData | null, FileData | null];
28
+ upload: Client["upload"];
29
+ stream_handler: Client["stream"];
30
+ label: string;
31
+ show_label: boolean;
32
+ i18n: I18nFormatter;
33
+ root: string;
34
+ upload_count?: number;
35
+ dragging?: boolean;
36
+ max_height: number;
37
+ max_file_size?: number | null;
38
+ upload_promise?: Promise<any>;
39
+ onclear?: () => void;
40
+ ondrag?: (dragging: boolean) => void;
41
+ onupload?: (value: [FileData | null, FileData | null]) => void;
42
+ children?: Snippet;
43
+ } = $props();
44
+ </script>
45
+
46
+ <Image
47
+ bind:upload_promise
48
+ slider_color="var(--border-color-primary)"
49
+ position={0.5}
50
+ bind:value
51
+ bind:dragging
52
+ {root}
53
+ {onclear}
54
+ ondrag={(detail) => {
55
+ dragging = detail;
56
+ ondrag?.(detail);
57
+ }}
58
+ {onupload}
59
+ {label}
60
+ {show_label}
61
+ {upload_count}
62
+ {stream_handler}
63
+ {upload}
64
+ {max_file_size}
65
+ {max_height}
66
+ {i18n}
67
+ >
68
+ {#if children}{@render children()}{/if}
69
+ </Image>
6.25.0/imageslider/shared/zoom.ts ADDED
@@ -0,0 +1,487 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ export class ZoomableImage {
2
+ container: HTMLDivElement;
3
+ image: HTMLImageElement;
4
+ scale: number;
5
+ offsetX: number;
6
+ offsetY: number;
7
+ isDragging: boolean;
8
+ lastX: number;
9
+ lastY: number;
10
+ initial_left_padding: number;
11
+ initial_top_padding: number;
12
+ initial_width: number;
13
+ initial_height: number;
14
+ subscribers: (({
15
+ x,
16
+ y,
17
+ scale
18
+ }: {
19
+ x: number;
20
+ y: number;
21
+ scale: number;
22
+ }) => void)[];
23
+ handleImageLoad: () => void;
24
+ real_image_size: {
25
+ top: number;
26
+ left: number;
27
+ width: number;
28
+ height: number;
29
+ } = { top: 0, left: 0, width: 0, height: 0 };
30
+
31
+ last_touch_distance: number;
32
+
33
+ constructor(container: HTMLDivElement, image: HTMLImageElement) {
34
+ this.container = container;
35
+ this.image = image;
36
+
37
+ this.scale = 1;
38
+ this.offsetX = 0;
39
+ this.offsetY = 0;
40
+ this.isDragging = false;
41
+ this.lastX = 0;
42
+ this.lastY = 0;
43
+ this.initial_left_padding = 0;
44
+ this.initial_top_padding = 0;
45
+ this.initial_width = 0;
46
+ this.initial_height = 0;
47
+ this.subscribers = [];
48
+ this.last_touch_distance = 0;
49
+
50
+ this.handleWheel = this.handleWheel.bind(this);
51
+ this.handleMouseDown = this.handleMouseDown.bind(this);
52
+ this.handleMouseMove = this.handleMouseMove.bind(this);
53
+ this.handleMouseUp = this.handleMouseUp.bind(this);
54
+ this.handleImageLoad = this.init.bind(this);
55
+ this.handleTouchStart = this.handleTouchStart.bind(this);
56
+ this.handleTouchMove = this.handleTouchMove.bind(this);
57
+ this.handleTouchEnd = this.handleTouchEnd.bind(this);
58
+
59
+ this.image.addEventListener("load", this.handleImageLoad);
60
+
61
+ this.container.addEventListener("wheel", this.handleWheel);
62
+ this.container.addEventListener("mousedown", this.handleMouseDown);
63
+ document.addEventListener("mousemove", this.handleMouseMove);
64
+ document.addEventListener("mouseup", this.handleMouseUp);
65
+
66
+ this.container.addEventListener("touchstart", this.handleTouchStart);
67
+ document.addEventListener("touchmove", this.handleTouchMove);
68
+ document.addEventListener("touchend", this.handleTouchEnd);
69
+
70
+ const observer = new ResizeObserver((entries) => {
71
+ for (const entry of entries) {
72
+ if (entry.target === this.container) {
73
+ this.handleResize();
74
+ this.get_image_size(this.image);
75
+ }
76
+ }
77
+ });
78
+ observer.observe(this.container);
79
+ }
80
+
81
+ handleResize(): void {
82
+ this.init();
83
+ }
84
+
85
+ init(): void {
86
+ const containerRect = this.container.getBoundingClientRect();
87
+
88
+ const imageRect = this.image.getBoundingClientRect();
89
+ this.initial_left_padding = imageRect.left - containerRect.left;
90
+ this.initial_top_padding = imageRect.top - containerRect.top;
91
+ this.initial_width = imageRect.width;
92
+ this.initial_height = imageRect.height;
93
+
94
+ this.reset_zoom();
95
+
96
+ this.updateTransform();
97
+ }
98
+
99
+ reset_zoom(): void {
100
+ this.scale = 1;
101
+ this.offsetX = 0;
102
+ this.offsetY = 0;
103
+ this.updateTransform();
104
+ }
105
+
106
+ handleMouseDown(e: MouseEvent): void {
107
+ const imageRect = this.image.getBoundingClientRect();
108
+
109
+ if (
110
+ e.clientX >= imageRect.left &&
111
+ e.clientX <= imageRect.right &&
112
+ e.clientY >= imageRect.top &&
113
+ e.clientY <= imageRect.bottom
114
+ ) {
115
+ e.preventDefault();
116
+ if (this.scale === 1) return;
117
+ this.isDragging = true;
118
+ this.lastX = e.clientX;
119
+ this.lastY = e.clientY;
120
+ this.image.style.cursor = "grabbing";
121
+ }
122
+ }
123
+
124
+ handleMouseMove(e: MouseEvent): void {
125
+ if (!this.isDragging) return;
126
+
127
+ const deltaX = e.clientX - this.lastX;
128
+ const deltaY = e.clientY - this.lastY;
129
+
130
+ this.offsetX += deltaX;
131
+ this.offsetY += deltaY;
132
+
133
+ this.lastX = e.clientX;
134
+ this.lastY = e.clientY;
135
+
136
+ this.updateTransform();
137
+
138
+ this.updateTransform();
139
+ }
140
+
141
+ handleMouseUp(): void {
142
+ if (this.isDragging) {
143
+ this.constrain_to_bounds(true);
144
+ this.updateTransform();
145
+ this.isDragging = false;
146
+ this.image.style.cursor = this.scale > 1 ? "grab" : "zoom-in";
147
+ }
148
+ }
149
+
150
+ async handleWheel(e: WheelEvent): Promise<void> {
151
+ e.preventDefault();
152
+
153
+ const containerRect = this.container.getBoundingClientRect();
154
+ const imageRect = this.image.getBoundingClientRect();
155
+
156
+ if (
157
+ e.clientX < imageRect.left ||
158
+ e.clientX > imageRect.right ||
159
+ e.clientY < imageRect.top ||
160
+ e.clientY > imageRect.bottom
161
+ ) {
162
+ return;
163
+ }
164
+
165
+ const zoomFactor = 1.05;
166
+ const oldScale = this.scale;
167
+ const newScale =
168
+ -Math.sign(e.deltaY) > 0
169
+ ? Math.min(15, oldScale * zoomFactor) // in
170
+ : Math.max(1, oldScale / zoomFactor); // out
171
+
172
+ if (newScale === oldScale) return;
173
+
174
+ const cursorX = e.clientX - containerRect.left - this.initial_left_padding;
175
+ const cursorY = e.clientY - containerRect.top - this.initial_top_padding;
176
+
177
+ this.scale = newScale;
178
+ this.offsetX = this.compute_new_offset({
179
+ cursor_position: cursorX,
180
+ current_offset: this.offsetX,
181
+ new_scale: newScale,
182
+ old_scale: oldScale
183
+ });
184
+ this.offsetY = this.compute_new_offset({
185
+ cursor_position: cursorY,
186
+ current_offset: this.offsetY,
187
+ new_scale: newScale,
188
+ old_scale: oldScale
189
+ });
190
+
191
+ this.updateTransform(); // apply before constraints
192
+
193
+ this.constrain_to_bounds();
194
+ this.updateTransform(); // apply again after constraints
195
+
196
+ this.image.style.cursor = this.scale > 1 ? "grab" : "zoom-in";
197
+ }
198
+
199
+ // compute_offset_for_positions({ position: number, scale: number }) {
200
+ // return position - (scale / this.scale) * (position - this.offset);
201
+ // }
202
+
203
+ compute_new_position({
204
+ position,
205
+ scale,
206
+ anchor_position
207
+ }: {
208
+ position: number;
209
+ scale: number;
210
+ anchor_position: number;
211
+ }): number {
212
+ return position - (position - anchor_position) * (scale / this.scale);
213
+ }
214
+
215
+ compute_new_offset({
216
+ cursor_position,
217
+ current_offset,
218
+ new_scale,
219
+ old_scale
220
+ }: {
221
+ cursor_position: number;
222
+ current_offset: number;
223
+ new_scale: number;
224
+ old_scale: number;
225
+ }): number {
226
+ return (
227
+ cursor_position -
228
+ (new_scale / old_scale) * (cursor_position - current_offset)
229
+ );
230
+ }
231
+
232
+ constrain_to_bounds(pan = false): void {
233
+ if (this.scale === 1) {
234
+ this.offsetX = 0;
235
+ this.offsetY = 0;
236
+ return;
237
+ }
238
+ const onscreen = {
239
+ top: this.real_image_size.top * this.scale + this.offsetY,
240
+ left: this.real_image_size.left * this.scale + this.offsetX,
241
+ width: this.real_image_size.width * this.scale,
242
+ height: this.real_image_size.height * this.scale,
243
+
244
+ bottom:
245
+ this.real_image_size.top * this.scale +
246
+ this.offsetY +
247
+ this.real_image_size.height * this.scale,
248
+ right:
249
+ this.real_image_size.left * this.scale +
250
+ this.offsetX +
251
+ this.real_image_size.width * this.scale
252
+ };
253
+
254
+ const real_image_size_right =
255
+ this.real_image_size.left + this.real_image_size.width;
256
+ const real_image_size_bottom =
257
+ this.real_image_size.top + this.real_image_size.height;
258
+
259
+ if (pan) {
260
+ if (onscreen.top > this.real_image_size.top) {
261
+ this.offsetY = this.calculate_position(
262
+ this.real_image_size.top,
263
+ 0,
264
+ "y"
265
+ );
266
+ } else if (onscreen.bottom < real_image_size_bottom) {
267
+ this.offsetY = this.calculate_position(real_image_size_bottom, 1, "y");
268
+ }
269
+
270
+ if (onscreen.left > this.real_image_size.left) {
271
+ this.offsetX = this.calculate_position(
272
+ this.real_image_size.left,
273
+ 0,
274
+ "x"
275
+ );
276
+ } else if (onscreen.right < real_image_size_right) {
277
+ this.offsetX = this.calculate_position(real_image_size_right, 1, "x");
278
+ }
279
+ }
280
+ }
281
+
282
+ updateTransform(): void {
283
+ this.notify({ x: this.offsetX, y: this.offsetY, scale: this.scale });
284
+ }
285
+
286
+ destroy(): void {
287
+ this.container.removeEventListener("wheel", this.handleWheel);
288
+ this.container.removeEventListener("mousedown", this.handleMouseDown);
289
+ document.removeEventListener("mousemove", this.handleMouseMove);
290
+ document.removeEventListener("mouseup", this.handleMouseUp);
291
+ this.container.removeEventListener("touchstart", this.handleTouchStart);
292
+ document.removeEventListener("touchmove", this.handleTouchMove);
293
+ document.removeEventListener("touchend", this.handleTouchEnd);
294
+ this.image.removeEventListener("load", this.handleImageLoad);
295
+ }
296
+
297
+ subscribe(
298
+ cb: ({ x, y, scale }: { x: number; y: number; scale: number }) => void
299
+ ): void {
300
+ this.subscribers.push(cb);
301
+ }
302
+
303
+ unsubscribe(
304
+ cb: ({ x, y, scale }: { x: number; y: number; scale: number }) => void
305
+ ): void {
306
+ this.subscribers = this.subscribers.filter(
307
+ (subscriber) => subscriber !== cb
308
+ );
309
+ }
310
+
311
+ notify({ x, y, scale }: { x: number; y: number; scale: number }): void {
312
+ this.subscribers.forEach((subscriber) => subscriber({ x, y, scale }));
313
+ }
314
+
315
+ handleTouchStart(e: TouchEvent): void {
316
+ e.preventDefault();
317
+ const imageRect = this.image.getBoundingClientRect();
318
+ const touch = e.touches[0];
319
+
320
+ if (
321
+ touch.clientX >= imageRect.left &&
322
+ touch.clientX <= imageRect.right &&
323
+ touch.clientY >= imageRect.top &&
324
+ touch.clientY <= imageRect.bottom
325
+ ) {
326
+ if (e.touches.length === 1 && this.scale > 1) {
327
+ // one finger == prepare pan
328
+ this.isDragging = true;
329
+ this.lastX = touch.clientX;
330
+ this.lastY = touch.clientY;
331
+ } else if (e.touches.length === 2) {
332
+ // two fingers == prepare pinch zoom
333
+ const touch1 = e.touches[0];
334
+ const touch2 = e.touches[1];
335
+ this.last_touch_distance = Math.hypot(
336
+ touch2.clientX - touch1.clientX,
337
+ touch2.clientY - touch1.clientY
338
+ );
339
+ }
340
+ }
341
+ }
342
+
343
+ get_image_size(img: HTMLImageElement | null): void {
344
+ if (!img) return;
345
+ const container = img.parentElement?.getBoundingClientRect();
346
+
347
+ if (!container) return;
348
+
349
+ const naturalAspect = img.naturalWidth / img.naturalHeight;
350
+ const containerAspect = container.width / container.height;
351
+ let displayedWidth, displayedHeight;
352
+
353
+ if (naturalAspect > containerAspect) {
354
+ displayedWidth = container.width;
355
+ displayedHeight = container.width / naturalAspect;
356
+ } else {
357
+ displayedHeight = container.height;
358
+ displayedWidth = container.height * naturalAspect;
359
+ }
360
+
361
+ const offsetX = (container.width - displayedWidth) / 2;
362
+ const offsetY = (container.height - displayedHeight) / 2;
363
+
364
+ this.real_image_size = {
365
+ top: offsetY,
366
+ left: offsetX,
367
+ width: displayedWidth,
368
+ height: displayedHeight
369
+ };
370
+ }
371
+
372
+ handleTouchMove(e: TouchEvent): void {
373
+ if (e.touches.length === 1 && this.isDragging) {
374
+ // one finger == pan
375
+ e.preventDefault();
376
+ const touch = e.touches[0];
377
+
378
+ const deltaX = touch.clientX - this.lastX;
379
+ const deltaY = touch.clientY - this.lastY;
380
+
381
+ this.offsetX += deltaX;
382
+ this.offsetY += deltaY;
383
+
384
+ this.lastX = touch.clientX;
385
+ this.lastY = touch.clientY;
386
+
387
+ this.updateTransform();
388
+ } else if (e.touches.length === 2) {
389
+ // two fingers == pinch zoom
390
+ e.preventDefault();
391
+
392
+ const touch1 = e.touches[0];
393
+ const touch2 = e.touches[1];
394
+
395
+ const current_distance = Math.hypot(
396
+ touch2.clientX - touch1.clientX,
397
+ touch2.clientY - touch1.clientY
398
+ );
399
+
400
+ if (this.last_touch_distance === 0) {
401
+ this.last_touch_distance = current_distance;
402
+ return;
403
+ }
404
+
405
+ const zoomFactor = current_distance / this.last_touch_distance;
406
+
407
+ const oldScale = this.scale;
408
+ const newScale = Math.min(15, Math.max(1, oldScale * zoomFactor));
409
+
410
+ if (newScale === oldScale) {
411
+ this.last_touch_distance = current_distance;
412
+ return;
413
+ }
414
+
415
+ // midpoint of touches relative to image
416
+ const containerRect = this.container.getBoundingClientRect();
417
+ const midX =
418
+ (touch1.clientX + touch2.clientX) / 2 -
419
+ containerRect.left -
420
+ this.initial_left_padding;
421
+ const midY =
422
+ (touch1.clientY + touch2.clientY) / 2 -
423
+ containerRect.top -
424
+ this.initial_top_padding;
425
+
426
+ this.scale = newScale;
427
+ this.offsetX = this.compute_new_offset({
428
+ cursor_position: midX,
429
+ current_offset: this.offsetX,
430
+ new_scale: newScale,
431
+ old_scale: oldScale
432
+ });
433
+ this.offsetY = this.compute_new_offset({
434
+ cursor_position: midY,
435
+ current_offset: this.offsetY,
436
+ new_scale: newScale,
437
+ old_scale: oldScale
438
+ });
439
+
440
+ this.updateTransform();
441
+ this.constrain_to_bounds();
442
+ this.updateTransform();
443
+
444
+ this.last_touch_distance = current_distance;
445
+
446
+ this.image.style.cursor = this.scale > 1 ? "grab" : "zoom-in";
447
+ }
448
+ }
449
+
450
+ handleTouchEnd(e: TouchEvent): void {
451
+ if (this.isDragging) {
452
+ this.constrain_to_bounds(true);
453
+ this.updateTransform();
454
+ this.isDragging = false;
455
+ }
456
+
457
+ if (e.touches.length === 0) {
458
+ this.last_touch_distance = 0;
459
+ }
460
+ }
461
+
462
+ calculate_position(
463
+ screen_coord: number,
464
+ image_anchor: number,
465
+ axis: "x" | "y"
466
+ ): number {
467
+ const containerRect = this.container.getBoundingClientRect();
468
+
469
+ // Calculate X offset if requested
470
+ if (axis === "x") {
471
+ const relative_screen_x = screen_coord;
472
+ const anchor_x =
473
+ this.real_image_size.left + image_anchor * this.real_image_size.width;
474
+ return relative_screen_x - anchor_x * this.scale;
475
+ }
476
+
477
+ // Calculate Y offset if requested
478
+ if (axis === "y") {
479
+ const relative_screen_y = screen_coord;
480
+ const anchor_y =
481
+ this.real_image_size.top + image_anchor * this.real_image_size.height;
482
+ return relative_screen_y - anchor_y * this.scale;
483
+ }
484
+
485
+ return 0;
486
+ }
487
+ }
6.25.0/imageslider/types.ts ADDED
@@ -0,0 +1,37 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import type {
2
+ SelectData,
3
+ ValueData,
4
+ ShareData,
5
+ CustomButton
6
+ } from "@gradio/utils";
7
+ import type { ILoadingStatus as LoadingStatus } from "@gradio/statustracker";
8
+ import type { FileData } from "@gradio/client";
9
+
10
+ export interface ImageSliderEvents {
11
+ input: never;
12
+ change: never;
13
+ error: string;
14
+ edit: never;
15
+ stream: ValueData;
16
+ drag: never;
17
+ upload: never;
18
+ clear: never;
19
+ select: SelectData;
20
+ share: ShareData;
21
+ clear_status: LoadingStatus;
22
+ close_stream: string;
23
+ custom_button_click: { id: number };
24
+ }
25
+
26
+ export interface ImageSliderProps {
27
+ value: [FileData | null, FileData | null];
28
+ height: number | undefined;
29
+ width: number | undefined;
30
+ placeholder: string | undefined;
31
+ buttons: (string | CustomButton)[];
32
+ input_ready: boolean;
33
+ slider_position: number;
34
+ upload_count: number;
35
+ slider_color: string;
36
+ max_height: number;
37
+ }