File size: 7,520 Bytes
227ab66 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 |
<script lang="ts">
import { createEventDispatcher, tick } from "svelte";
import { BlockLabel, IconButtonWrapper, IconButton } from "@gradio/atoms";
import { Clear, Image as ImageIcon } from "@gradio/icons";
import { FullscreenButton } from "@gradio/atoms";
import {
type SelectData,
type I18nFormatter,
type ValueData
} from "@gradio/utils";
import { get_coordinates_of_clicked_image } from "./utils";
import Webcam from "./Webcam.svelte";
import { Upload, UploadProgress } from "@gradio/upload";
import { FileData, type Client } from "@gradio/client";
import { SelectSource } from "@gradio/atoms";
import Image from "./Image.svelte";
import type { Base64File, WebcamOptions } from "./types";
export let value: null | FileData | Base64File = null;
export let label: string | undefined = undefined;
export let show_label: boolean;
type source_type = "upload" | "webcam" | "clipboard" | "microphone" | null;
export let sources: source_type[] = ["upload", "clipboard", "webcam"];
export let streaming = false;
export let pending = false;
export let webcam_options: WebcamOptions;
export let selectable = false;
export let root: string;
export let i18n: I18nFormatter;
export let max_file_size: number | null = null;
export let upload: Client["upload"];
export let stream_handler: Client["stream"];
export let stream_every: number;
export let time_limit: number;
export let show_fullscreen_button = true;
export let stream_state: "open" | "waiting" | "closed" = "closed";
export let upload_promise: Promise<any> | null = null;
let upload_input: Upload;
export let uploading = false;
export let active_source: source_type = null;
export let fullscreen = false;
let files: FileData[] = [];
let upload_id: string;
async function handle_upload({
detail
}: CustomEvent<FileData>): Promise<void> {
if (!streaming) {
if (detail.path?.toLowerCase().endsWith(".svg") && detail.url) {
const response = await fetch(detail.url);
const svgContent = await response.text();
value = {
...detail,
url: `data:image/svg+xml,${encodeURIComponent(svgContent)}`
};
} else {
value = detail;
}
await tick();
dispatch("upload");
}
}
function handle_clear(): void {
value = null;
dispatch("clear");
dispatch("change", null);
}
function handle_remove_image_click(event: MouseEvent): void {
handle_clear();
event.stopPropagation();
}
async function handle_save(
img_blob: Blob | any,
event: "change" | "stream" | "upload"
): Promise<void> {
if (event === "stream") {
dispatch("stream", {
value: { url: img_blob } as Base64File,
is_value_data: true
});
return;
}
upload_id = Math.random().toString(36).substring(2, 15);
const f_ = new File([img_blob], `image.${streaming ? "jpeg" : "png"}`);
files = [
new FileData({
path: f_.name,
orig_name: f_.name,
blob: f_,
size: f_.size,
mime_type: f_.type,
is_stream: false
})
];
pending = true;
const f = await upload_input.load_files([f_], upload_id);
if (event === "change" || event === "upload") {
value = f?.[0] || null;
await tick();
dispatch("change");
}
pending = false;
}
$: active_streaming = streaming && active_source === "webcam";
$: if (uploading && !active_streaming) value = null;
const dispatch = createEventDispatcher<{
change?: never;
stream: ValueData;
clear?: never;
drag: boolean;
upload?: never;
select: SelectData;
end_stream: never;
}>();
export let dragging = false;
$: dispatch("drag", dragging);
function handle_click(evt: MouseEvent): void {
let coordinates = get_coordinates_of_clicked_image(evt);
if (coordinates) {
dispatch("select", { index: coordinates, value: null });
}
}
$: if (!active_source && sources) {
active_source = sources[0];
}
async function handle_select_source(
source: (typeof sources)[number]
): Promise<void> {
switch (source) {
case "clipboard":
upload_input.paste_clipboard();
break;
default:
break;
}
}
let image_container: HTMLElement;
function on_drag_over(evt: DragEvent): void {
evt.preventDefault();
evt.stopPropagation();
if (evt.dataTransfer) {
evt.dataTransfer.dropEffect = "copy";
}
dragging = true;
}
async function on_drop(evt: DragEvent): Promise<void> {
evt.preventDefault();
evt.stopPropagation();
dragging = false;
if (value) {
handle_clear();
await tick();
}
active_source = "upload";
await tick();
upload_input.load_files_from_drop(evt);
}
</script>
<BlockLabel {show_label} Icon={ImageIcon} label={label || "Image"} />
<div data-testid="image" class="image-container" bind:this={image_container}>
<IconButtonWrapper>
{#if value?.url && !active_streaming}
{#if show_fullscreen_button}
<FullscreenButton {fullscreen} on:fullscreen />
{/if}
<IconButton
Icon={Clear}
label="Remove Image"
on:click={handle_remove_image_click}
/>
{/if}
</IconButtonWrapper>
<!-- svelte-ignore a11y-no-static-element-interactions -->
<div
class="upload-container"
class:reduced-height={sources.length > 1}
style:width={value ? "auto" : "100%"}
on:dragover={on_drag_over}
on:drop={on_drop}
>
<Upload
bind:upload_promise
hidden={value !== null || active_source === "webcam"}
bind:this={upload_input}
bind:uploading
bind:dragging
filetype={active_source === "clipboard" ? "clipboard" : "image/*"}
on:load={handle_upload}
on:error
{root}
{max_file_size}
disable_click={!sources.includes("upload") || value !== null}
{upload}
{stream_handler}
aria_label={i18n("image.drop_to_upload")}
>
{#if value === null}
<slot />
{/if}
</Upload>
{#if active_source === "webcam" && !streaming && pending}
<UploadProgress {root} {upload_id} {stream_handler} {files} />
{:else if active_source === "webcam" && (streaming || (!streaming && !value))}
<Webcam
{root}
{value}
on:capture={(e) => handle_save(e.detail, "change")}
on:stream={(e) => handle_save(e.detail, "stream")}
on:error
on:drag
on:upload={(e) => handle_save(e.detail, "upload")}
on:close_stream
{stream_state}
mirror_webcam={webcam_options.mirror}
{stream_every}
{streaming}
mode="image"
include_audio={false}
{i18n}
{upload}
{time_limit}
webcam_constraints={webcam_options.constraints}
/>
{:else if value !== null && !streaming}
<!-- svelte-ignore a11y-click-events-have-key-events-->
<!-- svelte-ignore a11y-no-static-element-interactions-->
<div class:selectable class="image-frame" on:click={handle_click}>
<Image src={value.url} restProps={{ alt: value.alt_text }} />
</div>
{/if}
</div>
{#if sources.length > 1 || sources.includes("clipboard")}
<SelectSource
{sources}
bind:active_source
{handle_clear}
handle_select={handle_select_source}
/>
{/if}
</div>
<style>
.image-frame :global(img) {
width: var(--size-full);
height: var(--size-full);
object-fit: scale-down;
}
.upload-container {
display: flex;
align-items: center;
justify-content: center;
height: 100%;
flex-shrink: 1;
max-height: 100%;
}
.reduced-height {
height: calc(100% - var(--size-10));
}
.image-container {
display: flex;
height: 100%;
flex-direction: column;
justify-content: center;
align-items: center;
max-height: 100%;
}
.selectable {
cursor: crosshair;
}
.image-frame {
object-fit: cover;
width: 100%;
height: 100%;
}
</style>
|