Upload folder using huggingface_hub
Browse files- 6.0.0/imageslider/Example.svelte +69 -0
- 6.0.0/imageslider/Index.svelte +190 -0
- 6.0.0/imageslider/package.json +46 -0
- 6.0.0/imageslider/shared/ClearImage.svelte +30 -0
- 6.0.0/imageslider/shared/Image.svelte +237 -0
- 6.0.0/imageslider/shared/ImageEl.svelte +141 -0
- 6.0.0/imageslider/shared/Slider.svelte +200 -0
- 6.0.0/imageslider/shared/SliderPreview.svelte +229 -0
- 6.0.0/imageslider/shared/SliderUpload.svelte +48 -0
- 6.0.0/imageslider/shared/zoom.ts +487 -0
- 6.0.0/imageslider/types.ts +31 -0
6.0.0/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>
|
6.0.0/imageslider/Index.svelte
ADDED
|
@@ -0,0 +1,190 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<svelte:options accessors={true} />
|
| 2 |
+
|
| 3 |
+
<script lang="ts">
|
| 4 |
+
import { tick } from "svelte";
|
| 5 |
+
import type { ImageSliderProps, ImageSliderEvents } from "./types";
|
| 6 |
+
import { Gradio } from "@gradio/utils";
|
| 7 |
+
import StaticImage from "./shared/SliderPreview.svelte";
|
| 8 |
+
import ImageUploader from "./shared/SliderUpload.svelte";
|
| 9 |
+
|
| 10 |
+
import { Block, Empty, UploadText } from "@gradio/atoms";
|
| 11 |
+
import { Image } from "@gradio/icons";
|
| 12 |
+
import { StatusTracker } from "@gradio/statustracker";
|
| 13 |
+
|
| 14 |
+
type sources = "upload" | "webcam" | "clipboard" | null;
|
| 15 |
+
|
| 16 |
+
let upload_promise = $state<Promise<any>>();
|
| 17 |
+
|
| 18 |
+
class ImageSliderGradio extends Gradio<ImageSliderEvents, ImageSliderProps> {
|
| 19 |
+
async get_data() {
|
| 20 |
+
if (upload_promise) {
|
| 21 |
+
await upload_promise;
|
| 22 |
+
await tick();
|
| 23 |
+
}
|
| 24 |
+
|
| 25 |
+
const data = await super.get_data();
|
| 26 |
+
return data;
|
| 27 |
+
}
|
| 28 |
+
}
|
| 29 |
+
|
| 30 |
+
const props = $props();
|
| 31 |
+
const gradio = new ImageSliderGradio(props);
|
| 32 |
+
|
| 33 |
+
let value_is_output = $state(false);
|
| 34 |
+
let old_value = $state(gradio.props.value);
|
| 35 |
+
let fullscreen = $state(false);
|
| 36 |
+
let dragging = $state(false);
|
| 37 |
+
let active_source: sources = $state(null);
|
| 38 |
+
let upload_component: ImageUploader;
|
| 39 |
+
|
| 40 |
+
let normalised_slider_position = $derived(
|
| 41 |
+
Math.max(0, Math.min(100, gradio.props.slider_position)) / 100
|
| 42 |
+
);
|
| 43 |
+
|
| 44 |
+
$effect(() => {
|
| 45 |
+
if (old_value != gradio.props.value) {
|
| 46 |
+
old_value = gradio.props.value;
|
| 47 |
+
gradio.dispatch("change");
|
| 48 |
+
if (!value_is_output) {
|
| 49 |
+
gradio.dispatch("input");
|
| 50 |
+
}
|
| 51 |
+
}
|
| 52 |
+
});
|
| 53 |
+
|
| 54 |
+
const handle_drag_event = (event: Event): void => {
|
| 55 |
+
const drag_event = event as DragEvent;
|
| 56 |
+
drag_event.preventDefault();
|
| 57 |
+
drag_event.stopPropagation();
|
| 58 |
+
if (drag_event.type === "dragenter" || drag_event.type === "dragover") {
|
| 59 |
+
dragging = true;
|
| 60 |
+
} else if (drag_event.type === "dragleave") {
|
| 61 |
+
dragging = false;
|
| 62 |
+
}
|
| 63 |
+
};
|
| 64 |
+
|
| 65 |
+
const handle_drop = (event: Event): void => {
|
| 66 |
+
if (gradio.shared.interactive) {
|
| 67 |
+
const drop_event = event as DragEvent;
|
| 68 |
+
drop_event.preventDefault();
|
| 69 |
+
drop_event.stopPropagation();
|
| 70 |
+
dragging = false;
|
| 71 |
+
|
| 72 |
+
if (upload_component) {
|
| 73 |
+
upload_component.loadFilesFromDrop(drop_event);
|
| 74 |
+
}
|
| 75 |
+
}
|
| 76 |
+
};
|
| 77 |
+
</script>
|
| 78 |
+
|
| 79 |
+
{#if !gradio.shared.interactive || (gradio.props.value?.[1] && gradio.props.value?.[0])}
|
| 80 |
+
<Block
|
| 81 |
+
visible={gradio.shared.visible}
|
| 82 |
+
variant={"solid"}
|
| 83 |
+
border_mode={dragging ? "focus" : "base"}
|
| 84 |
+
padding={false}
|
| 85 |
+
elem_id={gradio.shared.elem_id}
|
| 86 |
+
elem_classes={gradio.shared.elem_classes}
|
| 87 |
+
height={gradio.props.height || undefined}
|
| 88 |
+
width={gradio.props.width}
|
| 89 |
+
allow_overflow={false}
|
| 90 |
+
container={gradio.shared.container}
|
| 91 |
+
scale={gradio.shared.scale}
|
| 92 |
+
min_width={gradio.shared.min_width}
|
| 93 |
+
bind:fullscreen
|
| 94 |
+
>
|
| 95 |
+
<StatusTracker
|
| 96 |
+
autoscroll={gradio.shared.autoscroll}
|
| 97 |
+
i18n={gradio.i18n}
|
| 98 |
+
{...gradio.shared.loading_status}
|
| 99 |
+
/>
|
| 100 |
+
<StaticImage
|
| 101 |
+
on:select={({ detail }) => gradio.dispatch("select", detail)}
|
| 102 |
+
on:share={({ detail }) => gradio.dispatch("share", detail)}
|
| 103 |
+
on:error={({ detail }) => gradio.dispatch("error", detail)}
|
| 104 |
+
on:clear={() => gradio.dispatch("clear")}
|
| 105 |
+
on:fullscreen={({ detail }) => {
|
| 106 |
+
fullscreen = detail;
|
| 107 |
+
}}
|
| 108 |
+
{fullscreen}
|
| 109 |
+
interactive={gradio.shared.interactive}
|
| 110 |
+
bind:value={gradio.props.value}
|
| 111 |
+
label={gradio.shared.label}
|
| 112 |
+
show_label={gradio.shared.show_label}
|
| 113 |
+
show_download_button={gradio.props.buttons.includes("download")}
|
| 114 |
+
i18n={gradio.i18n}
|
| 115 |
+
show_fullscreen_button={gradio.props.buttons.includes("fullscreen")}
|
| 116 |
+
position={normalised_slider_position}
|
| 117 |
+
slider_color={gradio.props.slider_color}
|
| 118 |
+
max_height={gradio.props.max_height}
|
| 119 |
+
/>
|
| 120 |
+
</Block>
|
| 121 |
+
{:else}
|
| 122 |
+
<Block
|
| 123 |
+
visible={gradio.shared.visible}
|
| 124 |
+
variant={gradio.props.value === null ? "dashed" : "solid"}
|
| 125 |
+
border_mode={dragging ? "focus" : "base"}
|
| 126 |
+
padding={false}
|
| 127 |
+
elem_id={gradio.shared.elem_id}
|
| 128 |
+
elem_classes={gradio.shared.elem_classes}
|
| 129 |
+
height={gradio.props.height || undefined}
|
| 130 |
+
width={gradio.props.width}
|
| 131 |
+
allow_overflow={false}
|
| 132 |
+
container={gradio.shared.container}
|
| 133 |
+
scale={gradio.shared.scale}
|
| 134 |
+
min_width={gradio.shared.min_width}
|
| 135 |
+
on:dragenter={handle_drag_event}
|
| 136 |
+
on:dragleave={handle_drag_event}
|
| 137 |
+
on:dragover={handle_drag_event}
|
| 138 |
+
on:drop={handle_drop}
|
| 139 |
+
>
|
| 140 |
+
<StatusTracker
|
| 141 |
+
autoscroll={gradio.shared.autoscroll}
|
| 142 |
+
i18n={gradio.i18n}
|
| 143 |
+
{...gradio.shared.loading_status}
|
| 144 |
+
on_clear_status={() =>
|
| 145 |
+
gradio.dispatch("clear_status", gradio.shared.loading_status)}
|
| 146 |
+
/>
|
| 147 |
+
|
| 148 |
+
<ImageUploader
|
| 149 |
+
bind:upload_promise
|
| 150 |
+
bind:this={upload_component}
|
| 151 |
+
bind:value={gradio.props.value}
|
| 152 |
+
bind:dragging
|
| 153 |
+
root={gradio.shared.root}
|
| 154 |
+
on:edit={() => gradio.dispatch("edit")}
|
| 155 |
+
on:clear={() => {
|
| 156 |
+
gradio.dispatch("clear");
|
| 157 |
+
}}
|
| 158 |
+
on:drag={({ detail }) => (dragging = detail)}
|
| 159 |
+
on:upload={() => gradio.dispatch("upload")}
|
| 160 |
+
on:error={({ detail }) => {
|
| 161 |
+
if (gradio.shared.loading_status)
|
| 162 |
+
gradio.shared.loading_status.status = "error";
|
| 163 |
+
gradio.dispatch("error", detail);
|
| 164 |
+
}}
|
| 165 |
+
on:close_stream={() => {
|
| 166 |
+
gradio.dispatch("close_stream", "stream");
|
| 167 |
+
}}
|
| 168 |
+
label={gradio.shared.label}
|
| 169 |
+
show_label={gradio.shared.show_label}
|
| 170 |
+
upload_count={gradio.props.upload_count}
|
| 171 |
+
max_file_size={gradio.shared.max_file_size}
|
| 172 |
+
i18n={gradio.i18n}
|
| 173 |
+
upload={(...args) => gradio.shared.client.upload(...args)}
|
| 174 |
+
stream_handler={gradio.shared.client?.stream}
|
| 175 |
+
max_height={gradio.props.max_height}
|
| 176 |
+
>
|
| 177 |
+
{#if active_source === "upload" || !active_source}
|
| 178 |
+
<UploadText
|
| 179 |
+
i18n={gradio.i18n}
|
| 180 |
+
type="image"
|
| 181 |
+
placeholder={gradio.props.placeholder}
|
| 182 |
+
/>
|
| 183 |
+
{:else if active_source === "clipboard"}
|
| 184 |
+
<UploadText i18n={gradio.i18n} type="clipboard" mode="short" />
|
| 185 |
+
{:else}
|
| 186 |
+
<Empty unpadded_box={true} size="large"><Image /></Empty>
|
| 187 |
+
{/if}
|
| 188 |
+
</ImageUploader>
|
| 189 |
+
</Block>
|
| 190 |
+
{/if}
|
6.0.0/imageslider/package.json
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"name": "@gradio/imageslider",
|
| 3 |
+
"version": "0.3.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.43.4"
|
| 40 |
+
},
|
| 41 |
+
"repository": {
|
| 42 |
+
"type": "git",
|
| 43 |
+
"url": "git+https://github.com/gradio-app/gradio.git",
|
| 44 |
+
"directory": "js/imageslider"
|
| 45 |
+
}
|
| 46 |
+
}
|
6.0.0/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>
|
6.0.0/imageslider/shared/Image.svelte
ADDED
|
@@ -0,0 +1,237 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
export let upload_promise: Promise<any> | null = null;
|
| 31 |
+
|
| 32 |
+
let value_: [FileData | null, FileData | null] = value || [null, null];
|
| 33 |
+
|
| 34 |
+
let img: HTMLImageElement;
|
| 35 |
+
let el_width: number;
|
| 36 |
+
let el_height: number;
|
| 37 |
+
|
| 38 |
+
async function handle_upload(
|
| 39 |
+
{ detail }: CustomEvent<FileData[]>,
|
| 40 |
+
n: number
|
| 41 |
+
): Promise<void> {
|
| 42 |
+
const new_value = [value[0], value[1]] as [
|
| 43 |
+
FileData | null,
|
| 44 |
+
FileData | null
|
| 45 |
+
];
|
| 46 |
+
if (detail.length > 1) {
|
| 47 |
+
new_value[n] = detail[0];
|
| 48 |
+
} else {
|
| 49 |
+
new_value[n] = detail[n];
|
| 50 |
+
}
|
| 51 |
+
value = new_value;
|
| 52 |
+
await tick();
|
| 53 |
+
|
| 54 |
+
dispatch("upload", new_value);
|
| 55 |
+
}
|
| 56 |
+
|
| 57 |
+
let old_value = "";
|
| 58 |
+
|
| 59 |
+
$: if (JSON.stringify(value) !== old_value) {
|
| 60 |
+
old_value = JSON.stringify(value);
|
| 61 |
+
value_ = value;
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
const dispatch = createEventDispatcher<{
|
| 65 |
+
change: string | null;
|
| 66 |
+
stream: string | null;
|
| 67 |
+
edit: undefined;
|
| 68 |
+
clear: undefined;
|
| 69 |
+
drag: boolean;
|
| 70 |
+
upload: [FileData | null, FileData | null];
|
| 71 |
+
select: SelectData;
|
| 72 |
+
}>();
|
| 73 |
+
|
| 74 |
+
export let dragging = false;
|
| 75 |
+
|
| 76 |
+
$: dispatch("drag", dragging);
|
| 77 |
+
</script>
|
| 78 |
+
|
| 79 |
+
<BlockLabel {show_label} Icon={Image} label={label || i18n("image.image")} />
|
| 80 |
+
|
| 81 |
+
<div
|
| 82 |
+
data-testid="image"
|
| 83 |
+
class="image-container"
|
| 84 |
+
bind:clientWidth={el_width}
|
| 85 |
+
bind:clientHeight={el_height}
|
| 86 |
+
>
|
| 87 |
+
{#if value?.[0]?.url || value?.[1]?.url}
|
| 88 |
+
<ClearImage
|
| 89 |
+
on:remove_image={() => {
|
| 90 |
+
position = 0.5;
|
| 91 |
+
value = [null, null];
|
| 92 |
+
dispatch("clear");
|
| 93 |
+
}}
|
| 94 |
+
/>
|
| 95 |
+
{/if}
|
| 96 |
+
{#if value?.[1]?.url}
|
| 97 |
+
<div class="icon-buttons">
|
| 98 |
+
{#if show_download_button}
|
| 99 |
+
<DownloadLink
|
| 100 |
+
href={value[1].url}
|
| 101 |
+
download={value[1].orig_name || "image"}
|
| 102 |
+
>
|
| 103 |
+
<IconButton Icon={Download} />
|
| 104 |
+
</DownloadLink>
|
| 105 |
+
{/if}
|
| 106 |
+
</div>
|
| 107 |
+
{/if}
|
| 108 |
+
<Slider
|
| 109 |
+
bind:position
|
| 110 |
+
disabled={upload_count == 2 || !value?.[0]}
|
| 111 |
+
{slider_color}
|
| 112 |
+
>
|
| 113 |
+
<div
|
| 114 |
+
class="upload-wrap"
|
| 115 |
+
style:display={upload_count === 2 ? "flex" : "block"}
|
| 116 |
+
class:side-by-side={upload_count === 2}
|
| 117 |
+
>
|
| 118 |
+
{#if !value_?.[0]}
|
| 119 |
+
<div class="wrap" class:half-wrap={upload_count === 1}>
|
| 120 |
+
<Upload
|
| 121 |
+
bind:upload_promise
|
| 122 |
+
bind:dragging
|
| 123 |
+
filetype="image/*"
|
| 124 |
+
on:load={(e) => handle_upload(e, 0)}
|
| 125 |
+
disable_click={!!value?.[0]}
|
| 126 |
+
{root}
|
| 127 |
+
file_count="multiple"
|
| 128 |
+
{upload}
|
| 129 |
+
{stream_handler}
|
| 130 |
+
{max_file_size}
|
| 131 |
+
>
|
| 132 |
+
<slot />
|
| 133 |
+
</Upload>
|
| 134 |
+
</div>
|
| 135 |
+
{:else}
|
| 136 |
+
<ImageEl
|
| 137 |
+
variant="upload"
|
| 138 |
+
src={value_[0]?.url}
|
| 139 |
+
alt=""
|
| 140 |
+
bind:img_el={img}
|
| 141 |
+
{max_height}
|
| 142 |
+
/>
|
| 143 |
+
{/if}
|
| 144 |
+
|
| 145 |
+
{#if !value_?.[1] && upload_count === 2}
|
| 146 |
+
<Upload
|
| 147 |
+
bind:upload_promise
|
| 148 |
+
bind:dragging
|
| 149 |
+
filetype="image/*"
|
| 150 |
+
on:load={(e) => handle_upload(e, 1)}
|
| 151 |
+
disable_click={!!value?.[1]}
|
| 152 |
+
{root}
|
| 153 |
+
file_count="multiple"
|
| 154 |
+
{upload}
|
| 155 |
+
{stream_handler}
|
| 156 |
+
{max_file_size}
|
| 157 |
+
>
|
| 158 |
+
<slot />
|
| 159 |
+
</Upload>
|
| 160 |
+
{:else if !value_?.[1] && upload_count === 1}
|
| 161 |
+
<div
|
| 162 |
+
class="empty-wrap fixed"
|
| 163 |
+
style:width="{el_width * (1 - position)}px"
|
| 164 |
+
style:transform="translateX({el_width * position}px)"
|
| 165 |
+
class:white-icon={!value?.[0]?.url}
|
| 166 |
+
>
|
| 167 |
+
<Empty unpadded_box={true} size="large"><Image /></Empty>
|
| 168 |
+
</div>
|
| 169 |
+
{:else if value_?.[1]}
|
| 170 |
+
<ImageEl
|
| 171 |
+
variant="upload"
|
| 172 |
+
src={value_[1].url}
|
| 173 |
+
alt=""
|
| 174 |
+
fixed={upload_count === 1}
|
| 175 |
+
transform="translate(0px, 0px) scale(1)"
|
| 176 |
+
{max_height}
|
| 177 |
+
/>
|
| 178 |
+
{/if}
|
| 179 |
+
</div>
|
| 180 |
+
</Slider>
|
| 181 |
+
</div>
|
| 182 |
+
|
| 183 |
+
<style>
|
| 184 |
+
.upload-wrap {
|
| 185 |
+
display: flex;
|
| 186 |
+
justify-content: center;
|
| 187 |
+
align-items: center;
|
| 188 |
+
height: 100%;
|
| 189 |
+
width: 100%;
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
.wrap {
|
| 193 |
+
width: 100%;
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
.half-wrap {
|
| 197 |
+
width: 50%;
|
| 198 |
+
}
|
| 199 |
+
.image-container,
|
| 200 |
+
.empty-wrap {
|
| 201 |
+
width: var(--size-full);
|
| 202 |
+
height: var(--size-full);
|
| 203 |
+
}
|
| 204 |
+
|
| 205 |
+
.fixed {
|
| 206 |
+
--anim-block-background-fill: 255, 255, 255;
|
| 207 |
+
position: absolute;
|
| 208 |
+
top: 0;
|
| 209 |
+
left: 0;
|
| 210 |
+
background-color: rgba(var(--anim-block-background-fill), 0.8);
|
| 211 |
+
z-index: 0;
|
| 212 |
+
}
|
| 213 |
+
|
| 214 |
+
@media (prefers-color-scheme: dark) {
|
| 215 |
+
.fixed {
|
| 216 |
+
--anim-block-background-fill: 31, 41, 55;
|
| 217 |
+
}
|
| 218 |
+
}
|
| 219 |
+
|
| 220 |
+
.side-by-side :global(img) {
|
| 221 |
+
/* width: 100%; */
|
| 222 |
+
width: 50%;
|
| 223 |
+
object-fit: contain;
|
| 224 |
+
}
|
| 225 |
+
|
| 226 |
+
.empty-wrap {
|
| 227 |
+
pointer-events: none;
|
| 228 |
+
}
|
| 229 |
+
|
| 230 |
+
.icon-buttons {
|
| 231 |
+
display: flex;
|
| 232 |
+
position: absolute;
|
| 233 |
+
right: 8px;
|
| 234 |
+
z-index: var(--layer-top);
|
| 235 |
+
top: 8px;
|
| 236 |
+
}
|
| 237 |
+
</style>
|
6.0.0/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>
|
6.0.0/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>
|
6.0.0/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>
|
6.0.0/imageslider/shared/SliderUpload.svelte
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
+
export let upload_promise: Promise<any> | null = null;
|
| 22 |
+
</script>
|
| 23 |
+
|
| 24 |
+
<Image
|
| 25 |
+
bind:upload_promise
|
| 26 |
+
slider_color="var(--border-color-primary)"
|
| 27 |
+
position={0.5}
|
| 28 |
+
bind:value
|
| 29 |
+
bind:dragging
|
| 30 |
+
{root}
|
| 31 |
+
on:edit
|
| 32 |
+
on:clear
|
| 33 |
+
on:stream
|
| 34 |
+
on:drag={({ detail }) => (dragging = detail)}
|
| 35 |
+
on:upload
|
| 36 |
+
on:select
|
| 37 |
+
on:share
|
| 38 |
+
{label}
|
| 39 |
+
{show_label}
|
| 40 |
+
{upload_count}
|
| 41 |
+
{stream_handler}
|
| 42 |
+
{upload}
|
| 43 |
+
{max_file_size}
|
| 44 |
+
{max_height}
|
| 45 |
+
{i18n}
|
| 46 |
+
>
|
| 47 |
+
<slot />
|
| 48 |
+
</Image>
|
6.0.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.0.0/imageslider/types.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import type { SelectData, ValueData, ShareData } from "@gradio/utils";
|
| 2 |
+
import type { LoadingStatus } from "@gradio/statustracker";
|
| 3 |
+
import type { FileData } from "@gradio/client";
|
| 4 |
+
|
| 5 |
+
export interface ImageSliderEvents {
|
| 6 |
+
input: never;
|
| 7 |
+
change: never;
|
| 8 |
+
error: string;
|
| 9 |
+
edit: never;
|
| 10 |
+
stream: ValueData;
|
| 11 |
+
drag: never;
|
| 12 |
+
upload: never;
|
| 13 |
+
clear: never;
|
| 14 |
+
select: SelectData;
|
| 15 |
+
share: ShareData;
|
| 16 |
+
clear_status: LoadingStatus;
|
| 17 |
+
close_stream: string;
|
| 18 |
+
}
|
| 19 |
+
|
| 20 |
+
export interface ImageSliderProps {
|
| 21 |
+
value: [FileData | null, FileData | null];
|
| 22 |
+
height: number | undefined;
|
| 23 |
+
width: number | undefined;
|
| 24 |
+
placeholder: string | undefined;
|
| 25 |
+
buttons: string[];
|
| 26 |
+
input_ready: boolean;
|
| 27 |
+
slider_position: number;
|
| 28 |
+
upload_count: number;
|
| 29 |
+
slider_color: string;
|
| 30 |
+
max_height: number;
|
| 31 |
+
}
|