gradio-pr-bot commited on
Commit
be65766
·
verified ·
1 Parent(s): 1be41e2

Upload folder using huggingface_hub

Browse files
5.49.1/imageslider/Example.svelte ADDED
@@ -0,0 +1,69 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ export let value: [string, string];
3
+ export let samples_dir: string;
4
+ export let type: "gallery" | "table";
5
+ export let selected = false;
6
+ </script>
7
+
8
+ <!-- TODO: fix -->
9
+ <!-- svelte-ignore a11y-missing-attribute -->
10
+ <div
11
+ class="wrap"
12
+ class:table={type === "table"}
13
+ class:gallery={type === "gallery"}
14
+ class:selected
15
+ >
16
+ <img src={samples_dir + value[0]} />
17
+
18
+ <img src={samples_dir + value[1]} />
19
+ <span></span>
20
+ </div>
21
+
22
+ <style>
23
+ .wrap {
24
+ position: relative;
25
+ height: var(--size-64);
26
+ width: var(--size-40);
27
+ overflow: hidden;
28
+ border-radius: var(--radius-lg);
29
+ }
30
+ img {
31
+ height: var(--size-64);
32
+ width: var(--size-40);
33
+ position: absolute;
34
+ /* border-radius: var(--radius-lg); */
35
+ /* max-width: none; */
36
+ object-fit: cover;
37
+ }
38
+
39
+ .wrap.selected {
40
+ border-color: var(--color-accent);
41
+ }
42
+ .wrap img:first-child {
43
+ clip-path: inset(0 50% 0 0%);
44
+ }
45
+
46
+ .wrap img:nth-of-type(2) {
47
+ clip-path: inset(0 0 0 50%);
48
+ }
49
+ span {
50
+ position: absolute;
51
+ top: 0;
52
+ left: calc(50% - 0.75px);
53
+ height: var(--size-64);
54
+ width: 1.5px;
55
+ background: var(--border-color-primary);
56
+ }
57
+
58
+ .table {
59
+ margin: 0 auto;
60
+ border: 2px solid var(--border-color-primary);
61
+ border-radius: var(--radius-lg);
62
+ }
63
+
64
+ .gallery {
65
+ border: 2px solid var(--border-color-primary);
66
+ /* max-height: var(--size-20); */
67
+ object-fit: cover;
68
+ }
69
+ </style>
5.49.1/imageslider/Index.svelte ADDED
@@ -0,0 +1,212 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <svelte:options accessors={true} />
2
+
3
+ <script lang="ts">
4
+ import type { Gradio, SelectData, ValueData } from "@gradio/utils";
5
+ import StaticImage from "./shared/SliderPreview.svelte";
6
+ import ImageUploader from "./shared/SliderUpload.svelte";
7
+ import { afterUpdate } from "svelte";
8
+
9
+ import { Block, Empty, UploadText } from "@gradio/atoms";
10
+ import { Image } from "@gradio/icons";
11
+ import { StatusTracker } from "@gradio/statustracker";
12
+ import { type FileData } from "@gradio/client";
13
+ import type { LoadingStatus } from "@gradio/statustracker";
14
+
15
+ type sources = "upload" | "webcam" | "clipboard" | null;
16
+
17
+ export let value_is_output = false;
18
+ export let elem_id = "";
19
+ export let elem_classes: string[] = [];
20
+ export let visible: boolean | "hidden" = true;
21
+ export let value: [FileData | null, FileData | null] = [null, null];
22
+ let old_value: [FileData | null, FileData | null] = [null, null];
23
+ export let label: string;
24
+ export let show_label: boolean;
25
+ export let show_download_button: boolean;
26
+ export let root: string;
27
+ export let height: number | undefined;
28
+ export let width: number | undefined;
29
+ export let container = true;
30
+ export let scale: number | null = null;
31
+ export let min_width: number | undefined = undefined;
32
+ export let loading_status: LoadingStatus;
33
+ export let interactive: boolean;
34
+ export let placeholder: string | undefined = undefined;
35
+ export let show_fullscreen_button: boolean;
36
+ let fullscreen = false;
37
+ export let input_ready: boolean;
38
+ export let slider_position: number;
39
+ export let upload_count = 1;
40
+ export let slider_color = "var(--border-color-primary)";
41
+ export let max_height: number;
42
+ let uploading = false;
43
+
44
+ $: normalised_slider_position =
45
+ Math.max(0, Math.min(100, slider_position)) / 100;
46
+
47
+ $: input_ready = !uploading;
48
+
49
+ export let gradio: Gradio<{
50
+ input: never;
51
+ change: never;
52
+ error: string;
53
+ edit: never;
54
+ stream: ValueData;
55
+ drag: never;
56
+ upload: never;
57
+ clear: never;
58
+ select: SelectData;
59
+ share: ShareData;
60
+ clear_status: LoadingStatus;
61
+ close_stream: string;
62
+ }>;
63
+
64
+ $: {
65
+ if (JSON.stringify(value) !== JSON.stringify(old_value)) {
66
+ old_value = value;
67
+ gradio.dispatch("change");
68
+ if (!value_is_output) {
69
+ gradio.dispatch("input");
70
+ }
71
+ }
72
+ }
73
+
74
+ afterUpdate(() => {
75
+ value_is_output = false;
76
+ });
77
+
78
+ let dragging: boolean;
79
+ let active_source: sources = null;
80
+ let upload_component: ImageUploader;
81
+
82
+ const handle_drag_event = (event: Event): void => {
83
+ const drag_event = event as DragEvent;
84
+ drag_event.preventDefault();
85
+ drag_event.stopPropagation();
86
+ if (drag_event.type === "dragenter" || drag_event.type === "dragover") {
87
+ dragging = true;
88
+ } else if (drag_event.type === "dragleave") {
89
+ dragging = false;
90
+ }
91
+ };
92
+
93
+ const handle_drop = (event: Event): void => {
94
+ if (interactive) {
95
+ const drop_event = event as DragEvent;
96
+ drop_event.preventDefault();
97
+ drop_event.stopPropagation();
98
+ dragging = false;
99
+
100
+ if (upload_component) {
101
+ upload_component.loadFilesFromDrop(drop_event);
102
+ }
103
+ }
104
+ };
105
+ </script>
106
+
107
+ {#if !interactive || (value?.[1] && value?.[0])}
108
+ <Block
109
+ {visible}
110
+ variant={"solid"}
111
+ border_mode={dragging ? "focus" : "base"}
112
+ padding={false}
113
+ {elem_id}
114
+ {elem_classes}
115
+ height={height || undefined}
116
+ {width}
117
+ allow_overflow={false}
118
+ {container}
119
+ {scale}
120
+ {min_width}
121
+ bind:fullscreen
122
+ >
123
+ <StatusTracker
124
+ autoscroll={gradio.autoscroll}
125
+ i18n={gradio.i18n}
126
+ {...loading_status}
127
+ />
128
+ <StaticImage
129
+ on:select={({ detail }) => gradio.dispatch("select", detail)}
130
+ on:share={({ detail }) => gradio.dispatch("share", detail)}
131
+ on:error={({ detail }) => gradio.dispatch("error", detail)}
132
+ on:clear={() => gradio.dispatch("clear")}
133
+ on:fullscreen={({ detail }) => {
134
+ fullscreen = detail;
135
+ }}
136
+ {fullscreen}
137
+ {interactive}
138
+ bind:value
139
+ {label}
140
+ {show_label}
141
+ {show_download_button}
142
+ i18n={gradio.i18n}
143
+ {show_fullscreen_button}
144
+ position={normalised_slider_position}
145
+ {slider_color}
146
+ {max_height}
147
+ />
148
+ </Block>
149
+ {:else}
150
+ <Block
151
+ {visible}
152
+ variant={value === null ? "dashed" : "solid"}
153
+ border_mode={dragging ? "focus" : "base"}
154
+ padding={false}
155
+ {elem_id}
156
+ {elem_classes}
157
+ height={height || undefined}
158
+ {width}
159
+ allow_overflow={false}
160
+ {container}
161
+ {scale}
162
+ {min_width}
163
+ on:dragenter={handle_drag_event}
164
+ on:dragleave={handle_drag_event}
165
+ on:dragover={handle_drag_event}
166
+ on:drop={handle_drop}
167
+ >
168
+ <StatusTracker
169
+ autoscroll={gradio.autoscroll}
170
+ i18n={gradio.i18n}
171
+ {...loading_status}
172
+ on:clear_status={() => gradio.dispatch("clear_status", loading_status)}
173
+ />
174
+
175
+ <ImageUploader
176
+ bind:this={upload_component}
177
+ bind:value
178
+ bind:dragging
179
+ {root}
180
+ on:edit={() => gradio.dispatch("edit")}
181
+ on:clear={() => {
182
+ gradio.dispatch("clear");
183
+ }}
184
+ on:drag={({ detail }) => (dragging = detail)}
185
+ on:upload={() => gradio.dispatch("upload")}
186
+ on:error={({ detail }) => {
187
+ loading_status = loading_status || {};
188
+ loading_status.status = "error";
189
+ gradio.dispatch("error", detail);
190
+ }}
191
+ on:close_stream={() => {
192
+ gradio.dispatch("close_stream", "stream");
193
+ }}
194
+ {label}
195
+ {show_label}
196
+ {upload_count}
197
+ max_file_size={gradio.max_file_size}
198
+ i18n={gradio.i18n}
199
+ upload={(...args) => gradio.client.upload(...args)}
200
+ stream_handler={gradio.client?.stream}
201
+ {max_height}
202
+ >
203
+ {#if active_source === "upload" || !active_source}
204
+ <UploadText i18n={gradio.i18n} type="image" {placeholder} />
205
+ {:else if active_source === "clipboard"}
206
+ <UploadText i18n={gradio.i18n} type="clipboard" mode="short" />
207
+ {:else}
208
+ <Empty unpadded_box={true} size="large"><Image /></Empty>
209
+ {/if}
210
+ </ImageUploader>
211
+ </Block>
212
+ {/if}
5.49.1/imageslider/package.json ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "name": "@gradio/imageslider",
3
+ "version": "0.3.0",
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": "^4.0.0"
40
+ },
41
+ "repository": {
42
+ "type": "git",
43
+ "url": "git+https://github.com/gradio-app/gradio.git",
44
+ "directory": "js/imageslider"
45
+ }
46
+ }
5.49.1/imageslider/shared/ClearImage.svelte ADDED
@@ -0,0 +1,30 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { createEventDispatcher } from "svelte";
3
+ import { IconButton } from "@gradio/atoms";
4
+ import { Clear } from "@gradio/icons";
5
+
6
+ const dispatch = createEventDispatcher();
7
+ </script>
8
+
9
+ <div>
10
+ <IconButton
11
+ Icon={Clear}
12
+ label="Remove Image"
13
+ on:click={(event) => {
14
+ dispatch("remove_image");
15
+ event.stopPropagation();
16
+ }}
17
+ />
18
+ </div>
19
+
20
+ <style>
21
+ div {
22
+ display: flex;
23
+ position: absolute;
24
+ top: var(--size-2);
25
+ right: var(--size-2);
26
+ justify-content: flex-end;
27
+ gap: var(--spacing-sm);
28
+ z-index: var(--layer-5);
29
+ }
30
+ </style>
5.49.1/imageslider/shared/Image.svelte ADDED
@@ -0,0 +1,234 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import Slider from "./Slider.svelte";
3
+ import { createEventDispatcher, tick } 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 SelectData, 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
+ export let value: [FileData | null, FileData | null];
16
+
17
+ export let label: string | undefined = undefined;
18
+ export let show_label: boolean;
19
+ export let root: string;
20
+ export let position: number;
21
+ export let upload_count = 2;
22
+
23
+ export let show_download_button = true;
24
+ export let slider_color: string;
25
+ export let upload: Client["upload"];
26
+ export let stream_handler: Client["stream"];
27
+ export let max_file_size: number | null = null;
28
+ export let i18n: I18nFormatter;
29
+ export let max_height: number;
30
+
31
+ let value_: [FileData | null, FileData | null] = value || [null, null];
32
+
33
+ let img: HTMLImageElement;
34
+ let el_width: number;
35
+ let el_height: number;
36
+
37
+ async function handle_upload(
38
+ { detail }: CustomEvent<FileData[]>,
39
+ n: number
40
+ ): Promise<void> {
41
+ const new_value = [value[0], value[1]] as [
42
+ FileData | null,
43
+ FileData | null
44
+ ];
45
+ if (detail.length > 1) {
46
+ new_value[n] = detail[0];
47
+ } else {
48
+ new_value[n] = detail[n];
49
+ }
50
+ value = new_value;
51
+ await tick();
52
+
53
+ dispatch("upload", new_value);
54
+ }
55
+
56
+ let old_value = "";
57
+
58
+ $: if (JSON.stringify(value) !== old_value) {
59
+ old_value = JSON.stringify(value);
60
+ value_ = value;
61
+ }
62
+
63
+ const dispatch = createEventDispatcher<{
64
+ change: string | null;
65
+ stream: string | null;
66
+ edit: undefined;
67
+ clear: undefined;
68
+ drag: boolean;
69
+ upload: [FileData | null, FileData | null];
70
+ select: SelectData;
71
+ }>();
72
+
73
+ export let dragging = false;
74
+
75
+ $: dispatch("drag", dragging);
76
+ </script>
77
+
78
+ <BlockLabel {show_label} Icon={Image} label={label || i18n("image.image")} />
79
+
80
+ <div
81
+ data-testid="image"
82
+ class="image-container"
83
+ bind:clientWidth={el_width}
84
+ bind:clientHeight={el_height}
85
+ >
86
+ {#if value?.[0]?.url || value?.[1]?.url}
87
+ <ClearImage
88
+ on:remove_image={() => {
89
+ position = 0.5;
90
+ value = [null, null];
91
+ dispatch("clear");
92
+ }}
93
+ />
94
+ {/if}
95
+ {#if value?.[1]?.url}
96
+ <div class="icon-buttons">
97
+ {#if show_download_button}
98
+ <DownloadLink
99
+ href={value[1].url}
100
+ download={value[1].orig_name || "image"}
101
+ >
102
+ <IconButton Icon={Download} />
103
+ </DownloadLink>
104
+ {/if}
105
+ </div>
106
+ {/if}
107
+ <Slider
108
+ bind:position
109
+ disabled={upload_count == 2 || !value?.[0]}
110
+ {slider_color}
111
+ >
112
+ <div
113
+ class="upload-wrap"
114
+ style:display={upload_count === 2 ? "flex" : "block"}
115
+ class:side-by-side={upload_count === 2}
116
+ >
117
+ {#if !value_?.[0]}
118
+ <div class="wrap" class:half-wrap={upload_count === 1}>
119
+ <Upload
120
+ bind:dragging
121
+ filetype="image/*"
122
+ on:load={(e) => handle_upload(e, 0)}
123
+ disable_click={!!value?.[0]}
124
+ {root}
125
+ file_count="multiple"
126
+ {upload}
127
+ {stream_handler}
128
+ {max_file_size}
129
+ >
130
+ <slot />
131
+ </Upload>
132
+ </div>
133
+ {:else}
134
+ <ImageEl
135
+ variant="upload"
136
+ src={value_[0]?.url}
137
+ alt=""
138
+ bind:img_el={img}
139
+ {max_height}
140
+ />
141
+ {/if}
142
+
143
+ {#if !value_?.[1] && upload_count === 2}
144
+ <Upload
145
+ bind:dragging
146
+ filetype="image/*"
147
+ on:load={(e) => handle_upload(e, 1)}
148
+ disable_click={!!value?.[1]}
149
+ {root}
150
+ file_count="multiple"
151
+ {upload}
152
+ {stream_handler}
153
+ {max_file_size}
154
+ >
155
+ <slot />
156
+ </Upload>
157
+ {:else if !value_?.[1] && upload_count === 1}
158
+ <div
159
+ class="empty-wrap fixed"
160
+ style:width="{el_width * (1 - position)}px"
161
+ style:transform="translateX({el_width * position}px)"
162
+ class:white-icon={!value?.[0]?.url}
163
+ >
164
+ <Empty unpadded_box={true} size="large"><Image /></Empty>
165
+ </div>
166
+ {:else if value_?.[1]}
167
+ <ImageEl
168
+ variant="upload"
169
+ src={value_[1].url}
170
+ alt=""
171
+ fixed={upload_count === 1}
172
+ transform="translate(0px, 0px) scale(1)"
173
+ {max_height}
174
+ />
175
+ {/if}
176
+ </div>
177
+ </Slider>
178
+ </div>
179
+
180
+ <style>
181
+ .upload-wrap {
182
+ display: flex;
183
+ justify-content: center;
184
+ align-items: center;
185
+ height: 100%;
186
+ width: 100%;
187
+ }
188
+
189
+ .wrap {
190
+ width: 100%;
191
+ }
192
+
193
+ .half-wrap {
194
+ width: 50%;
195
+ }
196
+ .image-container,
197
+ .empty-wrap {
198
+ width: var(--size-full);
199
+ height: var(--size-full);
200
+ }
201
+
202
+ .fixed {
203
+ --anim-block-background-fill: 255, 255, 255;
204
+ position: absolute;
205
+ top: 0;
206
+ left: 0;
207
+ background-color: rgba(var(--anim-block-background-fill), 0.8);
208
+ z-index: 0;
209
+ }
210
+
211
+ @media (prefers-color-scheme: dark) {
212
+ .fixed {
213
+ --anim-block-background-fill: 31, 41, 55;
214
+ }
215
+ }
216
+
217
+ .side-by-side :global(img) {
218
+ /* width: 100%; */
219
+ width: 50%;
220
+ object-fit: contain;
221
+ }
222
+
223
+ .empty-wrap {
224
+ pointer-events: none;
225
+ }
226
+
227
+ .icon-buttons {
228
+ display: flex;
229
+ position: absolute;
230
+ right: 8px;
231
+ z-index: var(--layer-top);
232
+ top: 8px;
233
+ }
234
+ </style>
5.49.1/imageslider/shared/ImageEl.svelte ADDED
@@ -0,0 +1,141 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import type { HTMLImgAttributes } from "svelte/elements";
3
+ import { createEventDispatcher, onMount, tick } from "svelte";
4
+ interface Props extends HTMLImgAttributes {
5
+ "data-testid"?: string;
6
+ fixed?: boolean;
7
+ transform?: string;
8
+ img_el?: HTMLImageElement;
9
+ hidden?: boolean;
10
+ variant?: "preview" | "upload";
11
+ max_height?: number;
12
+ fullscreen?: boolean;
13
+ }
14
+ type $$Props = Props;
15
+
16
+ export let src: HTMLImgAttributes["src"] = undefined;
17
+ export let fullscreen = false;
18
+
19
+ export let fixed = false;
20
+ export let transform = "translate(0px, 0px) scale(1)";
21
+ export let img_el: HTMLImageElement | null = null;
22
+ export let hidden = false;
23
+ export let variant = "upload";
24
+ export let max_height = 500;
25
+
26
+ const dispatch = createEventDispatcher<{
27
+ load: {
28
+ top: number;
29
+ left: number;
30
+ width: number;
31
+ height: number;
32
+ };
33
+ }>();
34
+
35
+ function get_image_size(img: HTMLImageElement | null): {
36
+ top: number;
37
+ left: number;
38
+ width: number;
39
+ height: number;
40
+ } {
41
+ if (!img) return { top: 0, left: 0, width: 0, height: 0 };
42
+ const container = img.parentElement?.getBoundingClientRect();
43
+
44
+ if (!container) return { top: 0, left: 0, width: 0, height: 0 };
45
+
46
+ const naturalAspect = img.naturalWidth / img.naturalHeight;
47
+ const containerAspect = container.width / container.height;
48
+ let displayedWidth, displayedHeight;
49
+
50
+ if (naturalAspect > containerAspect) {
51
+ displayedWidth = container.width;
52
+ displayedHeight = container.width / naturalAspect;
53
+ } else {
54
+ displayedHeight = container.height;
55
+ displayedWidth = container.height * naturalAspect;
56
+ }
57
+
58
+ const offsetX = (container.width - displayedWidth) / 2;
59
+ const offsetY = (container.height - displayedHeight) / 2;
60
+
61
+ return {
62
+ top: offsetY,
63
+ left: offsetX,
64
+ width: displayedWidth,
65
+ height: displayedHeight
66
+ };
67
+ }
68
+
69
+ onMount(() => {
70
+ const resizer = new ResizeObserver(async (entries) => {
71
+ for (const entry of entries) {
72
+ await tick();
73
+ dispatch("load", get_image_size(img_el));
74
+ }
75
+ });
76
+
77
+ resizer.observe(img_el!);
78
+
79
+ return () => {
80
+ resizer.disconnect();
81
+ };
82
+ });
83
+ </script>
84
+
85
+ <!-- svelte-ignore a11y-missing-attribute -->
86
+ <img
87
+ {src}
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
+ on:load={() => dispatch("load", 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>
5.49.1/imageslider/shared/Slider.svelte ADDED
@@ -0,0 +1,200 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <script lang="ts">
2
+ import { onMount } 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
+ export let position = 0.5;
11
+ export let disabled = false;
12
+
13
+ export let slider_color = "var(--border-color-primary)";
14
+ export let image_size: {
15
+ top: number;
16
+ left: number;
17
+ width: number;
18
+ height: number;
19
+ } = { top: 0, left: 0, width: 0, height: 0 };
20
+ export let el: HTMLDivElement | undefined = undefined;
21
+ export let parent_el: HTMLDivElement | undefined = undefined;
22
+ let inner: Element;
23
+ let px = 0;
24
+ let active = false;
25
+ let container_width = 0;
26
+
27
+ function set_position(width: number): void {
28
+ container_width = parent_el?.getBoundingClientRect().width || 0;
29
+ if (width === 0) {
30
+ image_size.width = el?.getBoundingClientRect().width || 0;
31
+ }
32
+
33
+ px = clamp(
34
+ image_size.width * position + image_size.left,
35
+ 0,
36
+ container_width
37
+ );
38
+ }
39
+
40
+ function round(n: number, points: number): number {
41
+ const mod = Math.pow(10, points);
42
+ return Math.round((n + Number.EPSILON) * mod) / mod;
43
+ }
44
+
45
+ function update_position(x: number): void {
46
+ px = clamp(x, 0, container_width);
47
+ position = round((x - image_size.left) / image_size.width, 5);
48
+ }
49
+
50
+ function drag_start(event: any): void {
51
+ if (disabled) return;
52
+ active = true;
53
+ update_position(event.x);
54
+ }
55
+
56
+ function drag_move(event: any): void {
57
+ if (disabled) return;
58
+ update_position(event.x);
59
+ }
60
+
61
+ function drag_end(): void {
62
+ if (disabled) return;
63
+ active = false;
64
+ }
65
+
66
+ function update_position_from_pc(pc: number): void {
67
+ px = clamp(image_size.width * pc + image_size.left, 0, container_width);
68
+ }
69
+
70
+ $: set_position(image_size.width);
71
+ $: update_position_from_pc(position);
72
+
73
+ onMount(() => {
74
+ set_position(image_size.width);
75
+ const drag_handler = drag()
76
+ .on("start", drag_start)
77
+ .on("drag", drag_move)
78
+ .on("end", drag_end);
79
+ select(inner).call(drag_handler);
80
+ });
81
+ </script>
82
+
83
+ <svelte:window on:resize={() => set_position(image_size.width)} />
84
+
85
+ <div class="wrap" role="none" bind:this={parent_el}>
86
+ <div class="content" bind:this={el}>
87
+ <slot />
88
+ </div>
89
+ <div
90
+ class="outer"
91
+ class:disabled
92
+ bind:this={inner}
93
+ role="none"
94
+ style="transform: translateX({px}px)"
95
+ class:grab={active}
96
+ >
97
+ <span class="icon-wrap" class:active class:disabled
98
+ ><span class="icon left">◢</span><span
99
+ class="icon center"
100
+ style:--color={slider_color}
101
+ ></span><span class="icon right">◢</span></span
102
+ >
103
+ <div class="inner" style:--color={slider_color}></div>
104
+ </div>
105
+ </div>
106
+
107
+ <style>
108
+ .wrap {
109
+ position: relative;
110
+ width: 100%;
111
+ height: 100%;
112
+ z-index: var(--layer-1);
113
+ overflow: hidden;
114
+ }
115
+
116
+ .icon-wrap {
117
+ display: block;
118
+ position: absolute;
119
+ top: 50%;
120
+ transform: translate(-20.5px, -50%);
121
+ left: 10px;
122
+ width: 40px;
123
+ transition: 0.2s;
124
+ color: var(--body-text-color);
125
+ height: 30px;
126
+ border-radius: 5px;
127
+ background-color: var(--color-accent);
128
+ display: flex;
129
+ align-items: center;
130
+ justify-content: center;
131
+ z-index: var(--layer-3);
132
+ box-shadow: 0px 0px 5px 2px rgba(0, 0, 0, 0.3);
133
+ font-size: 12px;
134
+ }
135
+
136
+ .icon.left {
137
+ transform: rotate(135deg);
138
+ text-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1);
139
+ }
140
+
141
+ .icon.right {
142
+ transform: rotate(-45deg);
143
+ text-shadow: -1px -1px 1px rgba(0, 0, 0, 0.1);
144
+ }
145
+
146
+ .icon.center {
147
+ display: block;
148
+ width: 1px;
149
+ height: 100%;
150
+ background-color: var(--color);
151
+ opacity: 0.1;
152
+ }
153
+
154
+ .icon-wrap.active {
155
+ opacity: 0;
156
+ }
157
+
158
+ .icon-wrap.disabled {
159
+ opacity: 0;
160
+ }
161
+
162
+ .outer {
163
+ width: 20px;
164
+ height: 100%;
165
+ position: absolute;
166
+ cursor: grab;
167
+ position: absolute;
168
+ top: 0;
169
+ left: -10px;
170
+ pointer-events: auto;
171
+ z-index: var(--layer-2);
172
+ }
173
+ .grab {
174
+ cursor: grabbing;
175
+ }
176
+
177
+ .inner {
178
+ width: 1px;
179
+ height: 100%;
180
+ background: var(--color);
181
+ position: absolute;
182
+ left: calc((100% - 2px) / 2);
183
+ }
184
+
185
+ .disabled {
186
+ cursor: auto;
187
+ }
188
+
189
+ .disabled .inner {
190
+ box-shadow: none;
191
+ }
192
+
193
+ .content {
194
+ width: 100%;
195
+ height: 100%;
196
+ display: flex;
197
+ justify-content: center;
198
+ align-items: center;
199
+ }
200
+ </style>
5.49.1/imageslider/shared/SliderPreview.svelte ADDED
@@ -0,0 +1,229 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 { Image, Download, Undo, Clear } from "@gradio/icons";
13
+ import { type FileData } from "@gradio/client";
14
+ import type { I18nFormatter } from "@gradio/utils";
15
+ import { ZoomableImage } from "./zoom";
16
+ import { onMount } from "svelte";
17
+ import { tweened, type Tweened } from "svelte/motion";
18
+ import { createEventDispatcher } from "svelte";
19
+
20
+ export let value: [null | FileData, null | FileData] = [null, null];
21
+ export let label: string | undefined = undefined;
22
+ export let show_download_button = true;
23
+ export let show_label: boolean;
24
+ export let i18n: I18nFormatter;
25
+ export let position: number;
26
+ export let layer_images = true;
27
+ export let show_single = false;
28
+ export let slider_color: string;
29
+ export let show_fullscreen_button = true;
30
+ export let fullscreen = false;
31
+ export let el_width = 0;
32
+ export let max_height: number;
33
+ export let interactive = true;
34
+ const dispatch = createEventDispatcher<{ clear: void }>();
35
+
36
+ let img: HTMLImageElement;
37
+ let slider_wrap: HTMLDivElement;
38
+ let image_container: HTMLDivElement;
39
+
40
+ let transform: Tweened<{ x: number; y: number; z: number }> = tweened(
41
+ { x: 0, y: 0, z: 1 },
42
+ {
43
+ duration: 75
44
+ }
45
+ );
46
+ let parent_el: HTMLDivElement;
47
+
48
+ $: coords_at_viewport = get_coords_at_viewport(
49
+ position,
50
+ viewport_width,
51
+ image_size.width,
52
+ image_size.left,
53
+ $transform.x,
54
+ $transform.z
55
+ );
56
+ $: style = layer_images
57
+ ? `clip-path: inset(0 0 0 ${coords_at_viewport * 100}%)`
58
+ : "";
59
+
60
+ function get_coords_at_viewport(
61
+ viewport_percent_x: number, // 0-1
62
+ viewportWidth: number,
63
+ image_width: number,
64
+ img_offset_x: number,
65
+ tx: number, // image translation x (in pixels)
66
+ scale: number // image scale (uniform)
67
+ ): number {
68
+ const px_relative_to_image = viewport_percent_x * image_width;
69
+ const pixel_position = px_relative_to_image + img_offset_x;
70
+
71
+ const normalised_position = (pixel_position - tx) / scale;
72
+ const percent_position = normalised_position / viewportWidth;
73
+
74
+ return percent_position;
75
+ }
76
+
77
+ let img_width = 0;
78
+ let viewport_width = 0;
79
+
80
+ let zoomable_image: ZoomableImage | null = null;
81
+ let observer: ResizeObserver | null = null;
82
+
83
+ function init_image(
84
+ img: HTMLImageElement,
85
+ slider_wrap: HTMLDivElement
86
+ ): void {
87
+ if (!img || !slider_wrap) return;
88
+ zoomable_image?.destroy();
89
+ observer?.disconnect();
90
+ img_width = img?.getBoundingClientRect().width || 0;
91
+ viewport_width = slider_wrap?.getBoundingClientRect().width || 0;
92
+ zoomable_image = new ZoomableImage(slider_wrap, img);
93
+ zoomable_image.subscribe(({ x, y, scale }) => {
94
+ transform.set({ x, y, z: scale });
95
+ });
96
+
97
+ observer = new ResizeObserver((entries) => {
98
+ for (const entry of entries) {
99
+ if (entry.target === slider_wrap) {
100
+ viewport_width = entry.contentRect.width;
101
+ }
102
+
103
+ if (entry.target === img) {
104
+ img_width = entry.contentRect.width;
105
+ }
106
+ }
107
+ });
108
+ observer.observe(slider_wrap);
109
+ observer.observe(img);
110
+ }
111
+
112
+ $: init_image(img, slider_wrap);
113
+
114
+ onMount(() => {
115
+ return () => {
116
+ zoomable_image?.destroy();
117
+ observer?.disconnect();
118
+ };
119
+ });
120
+
121
+ let slider_wrap_parent: HTMLDivElement;
122
+
123
+ let image_size: { top: number; left: number; width: number; height: number } =
124
+ { top: 0, left: 0, width: 0, height: 0 };
125
+
126
+ function handle_image_load(event: CustomEvent): void {
127
+ image_size = event.detail;
128
+ }
129
+ </script>
130
+
131
+ <BlockLabel {show_label} Icon={Image} label={label || i18n("image.image")} />
132
+ {#if (value === null || value[0] === null || value[1] === null) && !show_single}
133
+ <Empty unpadded_box={true} size="large"><Image /></Empty>
134
+ {:else}
135
+ <div class="image-container" bind:this={image_container}>
136
+ <IconButtonWrapper>
137
+ <IconButton
138
+ Icon={Undo}
139
+ label={i18n("common.undo")}
140
+ disabled={$transform.z === 1}
141
+ on:click={() => zoomable_image?.reset_zoom()}
142
+ />
143
+ {#if show_fullscreen_button}
144
+ <FullscreenButton {fullscreen} on:fullscreen />
145
+ {/if}
146
+
147
+ {#if show_download_button}
148
+ <DownloadLink
149
+ href={value[1]?.url}
150
+ download={value[1]?.orig_name || "image"}
151
+ >
152
+ <IconButton Icon={Download} label={i18n("common.download")} />
153
+ </DownloadLink>
154
+ {/if}
155
+ {#if interactive}
156
+ <IconButton
157
+ Icon={Clear}
158
+ label="Remove Image"
159
+ on:click={(event) => {
160
+ value = [null, null];
161
+ dispatch("clear");
162
+ event.stopPropagation();
163
+ }}
164
+ />
165
+ {/if}
166
+ </IconButtonWrapper>
167
+ <div
168
+ class="slider-wrap"
169
+ bind:this={slider_wrap_parent}
170
+ bind:clientWidth={el_width}
171
+ class:limit_height={!fullscreen}
172
+ >
173
+ <Slider
174
+ bind:position
175
+ {slider_color}
176
+ bind:el={slider_wrap}
177
+ bind:parent_el
178
+ {image_size}
179
+ >
180
+ <ImageEl
181
+ src={value?.[0]?.url}
182
+ alt=""
183
+ loading="lazy"
184
+ bind:img_el={img}
185
+ variant="preview"
186
+ transform="translate({$transform.x}px, {$transform.y}px) scale({$transform.z})"
187
+ {fullscreen}
188
+ {max_height}
189
+ on:load={handle_image_load}
190
+ />
191
+ <ImageEl
192
+ variant="preview"
193
+ fixed={layer_images}
194
+ hidden={!value?.[1]?.url}
195
+ src={value?.[1]?.url}
196
+ alt=""
197
+ loading="lazy"
198
+ style="{style}; background: var(--block-background-fill);"
199
+ transform="translate({$transform.x}px, {$transform.y}px) scale({$transform.z})"
200
+ {fullscreen}
201
+ {max_height}
202
+ on:load={handle_image_load}
203
+ />
204
+ </Slider>
205
+ </div>
206
+ </div>
207
+ {/if}
208
+
209
+ <style>
210
+ .slider-wrap {
211
+ user-select: none;
212
+ height: 100%;
213
+ width: 100%;
214
+ position: relative;
215
+ display: flex;
216
+ align-items: center;
217
+ justify-content: center;
218
+ }
219
+
220
+ .limit_height :global(img) {
221
+ max-height: 500px;
222
+ }
223
+
224
+ .image-container {
225
+ height: 100%;
226
+ position: relative;
227
+ min-width: var(--size-20);
228
+ }
229
+ </style>
5.49.1/imageslider/shared/SliderUpload.svelte ADDED
@@ -0,0 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <svelte:options accessors={true} />
2
+
3
+ <script lang="ts">
4
+ import type { I18nFormatter } from "@gradio/utils";
5
+ import Image from "./Image.svelte";
6
+ import { type Client } from "@gradio/client";
7
+
8
+ import type { FileData } from "@gradio/client";
9
+
10
+ export let value: [FileData | null, FileData | null] = [null, null];
11
+ export let upload: Client["upload"];
12
+ export let stream_handler: Client["stream"];
13
+ export let label: string;
14
+ export let show_label: boolean;
15
+ export let i18n: I18nFormatter;
16
+ export let root: string;
17
+ export let upload_count = 1;
18
+ export let dragging: boolean;
19
+ export let max_height: number;
20
+ export let max_file_size: number | null = null;
21
+ </script>
22
+
23
+ <Image
24
+ slider_color="var(--border-color-primary)"
25
+ position={0.5}
26
+ bind:value
27
+ bind:dragging
28
+ {root}
29
+ on:edit
30
+ on:clear
31
+ on:stream
32
+ on:drag={({ detail }) => (dragging = detail)}
33
+ on:upload
34
+ on:select
35
+ on:share
36
+ {label}
37
+ {show_label}
38
+ {upload_count}
39
+ {stream_handler}
40
+ {upload}
41
+ {max_file_size}
42
+ {max_height}
43
+ {i18n}
44
+ >
45
+ <slot />
46
+ </Image>
5.49.1/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
+ }