Skip to content

Commit 341844f

Browse files
Click to preview images in chatbot (#8377)
* click to preview image * add changeset * color of svg * notebook * fix * fix * fix preview bug * thumbnail spacing * add changeset * add changeset --------- Co-authored-by: gradio-pr-bot <[email protected]>
1 parent 83bdf5c commit 341844f

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed

.changeset/dirty-wings-double.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"@gradio/chatbot": patch
3+
"@gradio/multimodaltextbox": patch
4+
"gradio": patch
5+
---
6+
7+
feat:Click to preview images in chatbot

js/chatbot/shared/ChatBot.svelte

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import { Audio } from "@gradio/audio/shared";
99
import { Image } from "@gradio/image/shared";
1010
import { Video } from "@gradio/video/shared";
11+
import { Clear } from "@gradio/icons";
1112
import type { SelectData, LikeData } from "@gradio/utils";
1213
import { MarkdownCode as Markdown } from "@gradio/markdown";
1314
import { type FileData } from "@gradio/client";
@@ -95,6 +96,12 @@
9596
div.scrollTo(0, div.scrollHeight);
9697
}
9798
};
99+
100+
let image_preview_source: string;
101+
let image_preview_source_alt: string;
102+
let is_image_preview_open = false;
103+
let image_preview_close_button: HTMLButtonElement;
104+
98105
afterUpdate(() => {
99106
if (autoscroll) {
100107
scroll();
@@ -104,6 +111,16 @@
104111
});
105112
});
106113
}
114+
div.querySelectorAll("img").forEach((n) => {
115+
n.addEventListener("click", (e) => {
116+
const target = e.target as HTMLImageElement;
117+
if (target) {
118+
image_preview_source = target.src;
119+
image_preview_source_alt = target.alt;
120+
is_image_preview_open = true;
121+
}
122+
});
123+
});
107124
});
108125
109126
$: {
@@ -163,6 +180,21 @@
163180
{#each value as message_pair, i}
164181
{#each message_pair as message, j}
165182
{#if message !== null}
183+
{#if is_image_preview_open}
184+
<div class="image-preview">
185+
<img
186+
src={image_preview_source}
187+
alt={image_preview_source_alt}
188+
/>
189+
<button
190+
bind:this={image_preview_close_button}
191+
class="image-preview-close-button"
192+
on:click={() => {
193+
is_image_preview_open = false;
194+
}}><Clear /></button
195+
>
196+
</div>
197+
{/if}
166198
<div class="message-row {layout} {j == 0 ? 'user-row' : 'bot-row'}">
167199
{#if avatar_images[j] !== null}
168200
<div class="avatar-container">
@@ -554,4 +586,45 @@
554586
height: 100%;
555587
color: var(--body-text-color);
556588
}
589+
590+
/* Image preview */
591+
.message :global(.preview) {
592+
object-fit: contain;
593+
width: 95%;
594+
max-height: 93%;
595+
}
596+
.image-preview {
597+
position: absolute;
598+
z-index: 999;
599+
left: 0;
600+
top: 0;
601+
width: 100%;
602+
height: 100%;
603+
overflow: auto;
604+
background-color: rgba(0, 0, 0, 0.9);
605+
}
606+
.image-preview :global(img) {
607+
width: 100%;
608+
height: 100%;
609+
object-fit: contain;
610+
}
611+
.image-preview :global(svg) {
612+
stroke: white;
613+
}
614+
.image-preview-close-button {
615+
position: absolute;
616+
top: 10px;
617+
right: 10px;
618+
background: none;
619+
border: none;
620+
font-size: 1.5em;
621+
cursor: pointer;
622+
height: 30px;
623+
width: 30px;
624+
padding: 3px;
625+
background: var(--bg-color);
626+
box-shadow: var(--shadow-drop);
627+
border: 1px solid var(--button-secondary-border-color);
628+
border-radius: var(--radius-lg);
629+
}
557630
</style>

js/multimodaltextbox/shared/MultimodalTextbox.svelte

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,7 @@
353353
border-radius: 50%;
354354
width: 30px;
355355
height: 30px;
356+
bottom: 15px;
356357
}
357358
358359
.upload-button:hover,

0 commit comments

Comments
 (0)