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-07-06 08:50:17 +08:00
|
|
|
import { uploadToHuggingFace } from "@gradio/utils";
|
|
|
|
import { BlockLabel, Empty, IconButton, ShareButton } from "@gradio/atoms";
|
2023-07-20 02:00:02 +08:00
|
|
|
import { Download } from "@gradio/icons";
|
2023-08-04 06:01:18 +08:00
|
|
|
import { get_coordinates_of_clicked_image } from "../shared/utils";
|
2023-08-18 19:36:52 +08:00
|
|
|
import { _ } from "svelte-i18n";
|
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-07-20 02:00:02 +08:00
|
|
|
export let show_download_button = true;
|
|
|
|
export let selectable = false;
|
|
|
|
export let show_share_button = 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
|
|
|
|
2023-07-20 02:00:02 +08:00
|
|
|
const handle_click = (evt: MouseEvent): void => {
|
2023-04-11 05:47:33 +08:00
|
|
|
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>
|
|
|
|
|
2023-08-18 19:36:52 +08:00
|
|
|
<BlockLabel {show_label} Icon={Image} label={label || $_("image.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-07-06 08:50:17 +08:00
|
|
|
<div class="icon-buttons">
|
2023-07-20 02:00:02 +08:00
|
|
|
{#if show_download_button}
|
2023-08-04 06:01:18 +08:00
|
|
|
<a
|
|
|
|
href={value}
|
|
|
|
target={window.__is_colab__ ? "_blank" : null}
|
|
|
|
download={"image"}
|
|
|
|
>
|
2023-08-18 19:36:52 +08:00
|
|
|
<IconButton Icon={Download} label={$_("common.download")} />
|
2023-08-04 06:01:18 +08:00
|
|
|
</a>
|
2023-07-20 02:00:02 +08:00
|
|
|
{/if}
|
2023-07-06 08:50:17 +08:00
|
|
|
{#if show_share_button}
|
|
|
|
<ShareButton
|
|
|
|
on:share
|
|
|
|
on:error
|
|
|
|
formatter={async (value) => {
|
|
|
|
if (!value) return "";
|
|
|
|
let url = await uploadToHuggingFace(value, "base64");
|
|
|
|
return `<img src="${url}" />`;
|
|
|
|
}}
|
|
|
|
{value}
|
|
|
|
/>
|
|
|
|
{/if}
|
2023-02-24 04:56:37 +08:00
|
|
|
</div>
|
2023-09-22 20:12:26 +08:00
|
|
|
<button on:click={handle_click}>
|
|
|
|
<img src={value} alt="" class:selectable loading="lazy" />
|
|
|
|
</button>
|
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-07-06 08:50:17 +08:00
|
|
|
.icon-buttons {
|
|
|
|
display: flex;
|
2023-02-24 04:56:37 +08:00
|
|
|
position: absolute;
|
|
|
|
top: 6px;
|
|
|
|
right: 6px;
|
2023-07-06 08:50:17 +08:00
|
|
|
gap: var(--size-1);
|
2023-02-24 04:56:37 +08:00
|
|
|
}
|
2023-01-18 04:47:40 +08:00
|
|
|
</style>
|