2022-04-14 22:12:30 +08:00
|
|
|
<script lang="ts">
|
|
|
|
import { createEventDispatcher } from "svelte";
|
2023-04-11 05:47:33 +08:00
|
|
|
import type { SelectData } from "@gradio/utils";
|
2023-02-24 04:56:37 +08:00
|
|
|
import { BlockLabel, Empty, IconButton } from "@gradio/atoms";
|
|
|
|
import { Download } from "@gradio/icons";
|
2023-04-11 05:47:33 +08:00
|
|
|
import { get_coordinates_of_clicked_image } from "./utils";
|
2022-04-14 22:12:30 +08:00
|
|
|
|
2022-05-10 08:26:09 +08:00
|
|
|
import { Image } from "@gradio/icons";
|
2022-04-14 22:12:30 +08:00
|
|
|
|
|
|
|
export let value: null | string;
|
|
|
|
export let label: string | undefined = undefined;
|
2022-04-27 18:47:15 +08:00
|
|
|
export let show_label: boolean;
|
2023-04-11 05:47:33 +08:00
|
|
|
export let selectable: boolean = false;
|
2022-04-14 22:12:30 +08:00
|
|
|
|
|
|
|
const dispatch = createEventDispatcher<{
|
|
|
|
change: string;
|
2023-04-11 05:47:33 +08:00
|
|
|
select: SelectData;
|
2022-04-14 22:12:30 +08:00
|
|
|
}>();
|
|
|
|
|
|
|
|
$: value && dispatch("change", value);
|
2023-04-11 05:47:33 +08:00
|
|
|
|
|
|
|
const handle_click = (evt: MouseEvent) => {
|
|
|
|
let coordinates = get_coordinates_of_clicked_image(evt);
|
|
|
|
if (coordinates) {
|
|
|
|
dispatch("select", { index: coordinates, value: null });
|
|
|
|
}
|
|
|
|
};
|
2022-04-14 22:12:30 +08:00
|
|
|
</script>
|
|
|
|
|
2022-05-10 08:26:09 +08:00
|
|
|
<BlockLabel {show_label} Icon={Image} label={label || "Image"} />
|
2022-04-26 22:48:39 +08:00
|
|
|
{#if value === null}
|
2023-06-08 20:24:13 +08:00
|
|
|
<Empty unpadded_box={true} size="large"><Image /></Empty>
|
2022-04-26 22:48:39 +08:00
|
|
|
{:else}
|
2023-02-24 04:56:37 +08:00
|
|
|
<div class="download">
|
|
|
|
<a
|
|
|
|
href={value}
|
|
|
|
target={window.__is_colab__ ? "_blank" : null}
|
|
|
|
download={"image"}
|
|
|
|
>
|
|
|
|
<IconButton Icon={Download} label="Download" />
|
|
|
|
</a>
|
|
|
|
</div>
|
2023-04-11 05:47:33 +08:00
|
|
|
<!-- svelte-ignore a11y-click-events-have-key-events -->
|
|
|
|
<img src={value} alt="" class:selectable on:click={handle_click} />
|
2022-04-26 22:48:39 +08:00
|
|
|
{/if}
|
2023-01-18 04:47:40 +08:00
|
|
|
|
|
|
|
<style>
|
|
|
|
img {
|
|
|
|
width: var(--size-full);
|
|
|
|
height: var(--size-full);
|
|
|
|
object-fit: contain;
|
|
|
|
}
|
2023-02-24 04:56:37 +08:00
|
|
|
|
2023-04-11 05:47:33 +08:00
|
|
|
.selectable {
|
|
|
|
cursor: crosshair;
|
|
|
|
}
|
|
|
|
|
2023-02-24 04:56:37 +08:00
|
|
|
.download {
|
|
|
|
position: absolute;
|
|
|
|
top: 6px;
|
|
|
|
right: 6px;
|
|
|
|
}
|
2023-01-18 04:47:40 +08:00
|
|
|
</style>
|