File size: 7,987 Bytes
6f9c9e5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
<script lang="ts">
	import { Image } from "@gradio/image/shared";
	import { MarkdownCode as Markdown } from "@gradio/markdown-code";
	import { File, Music, Video } from "@gradio/icons";
	import type { ExampleMessage } from "../types";
	import { createEventDispatcher } from "svelte";
	import type { SelectData } from "@gradio/utils";
	import type { FileData } from "@gradio/client";

	export let examples: ExampleMessage[] | null = null;
	export let placeholder: string | null = null;
	export let latex_delimiters: {
		left: string;
		right: string;
		display: boolean;
	}[];

	const dispatch = createEventDispatcher<{
		example_select: SelectData;
	}>();

	function handle_example_select(
		i: number,
		example: ExampleMessage | string
	): void {
		const example_obj =
			typeof example === "string" ? { text: example } : example;
		dispatch("example_select", {
			index: i,
			value: { text: example_obj.text, files: example_obj.files }
		});
	}
</script>

<div class="placeholder-content" role="complementary">
	{#if placeholder !== null}
		<div class="placeholder">
			<Markdown message={placeholder} {latex_delimiters} />
		</div>
	{/if}
	{#if examples !== null}
		<div class="examples" role="list">
			{#each examples as example, i}
				<button
					class="example"
					on:click={() =>
						handle_example_select(
							i,
							typeof example === "string" ? { text: example } : example
						)}
					aria-label={`Select example ${i + 1}: ${example.display_text || example.text}`}
				>
					<div class="example-content">
						{#if example?.icon?.url}
							<div class="example-image-container">
								<Image
									class="example-image"
									src={example.icon.url}
									alt="Example icon"
								/>
							</div>
						{:else if example?.icon?.mime_type === "text"}
							<div class="example-icon" aria-hidden="true">
								<span class="text-icon-aa">Aa</span>
							</div>
						{:else if example.files !== undefined && example.files.length > 0}
							{#if example.files.length > 1}
								<div
									class="example-icons-grid"
									role="group"
									aria-label="Example attachments"
								>
									{#each example.files.slice(0, 4) as file, i}
										{#if file.mime_type?.includes("image")}
											<div class="example-image-container">
												<Image
													class="example-image"
													src={file.url}
													alt={file.orig_name || `Example image ${i + 1}`}
												/>
												{#if i === 3 && example.files.length > 4}
													<div
														class="image-overlay"
														role="status"
														aria-label={`${example.files.length - 4} more files`}
													>
														+{example.files.length - 4}
													</div>
												{/if}
											</div>
										{:else if file.mime_type?.includes("video")}
											<div class="example-image-container">
												<video
													class="example-image"
													src={file.url}
													aria-hidden="true"
												/>
												{#if i === 3 && example.files.length > 4}
													<div
														class="image-overlay"
														role="status"
														aria-label={`${example.files.length - 4} more files`}
													>
														+{example.files.length - 4}
													</div>
												{/if}
											</div>
										{:else}
											<div
												class="example-icon"
												aria-label={`File: ${file.orig_name}`}
											>
												{#if file.mime_type?.includes("audio")}
													<Music />
												{:else}
													<File />
												{/if}
											</div>
										{/if}
									{/each}
									{#if example.files.length > 4}
										<div class="example-icon">
											<div
												class="file-overlay"
												role="status"
												aria-label={`${example.files.length - 4} more files`}
											>
												+{example.files.length - 4}
											</div>
										</div>
									{/if}
								</div>
							{:else if example.files[0].mime_type?.includes("image")}
								<div class="example-image-container">
									<Image
										class="example-image"
										src={example.files[0].url}
										alt={example.files[0].orig_name || "Example image"}
									/>
								</div>
							{:else if example.files[0].mime_type?.includes("video")}
								<div class="example-image-container">
									<video
										class="example-image"
										src={example.files[0].url}
										aria-hidden="true"
									/>
								</div>
							{:else if example.files[0].mime_type?.includes("audio")}
								<div
									class="example-icon"
									aria-label={`File: ${example.files[0].orig_name}`}
								>
									<Music />
								</div>
							{:else}
								<div
									class="example-icon"
									aria-label={`File: ${example.files[0].orig_name}`}
								>
									<File />
								</div>
							{/if}
						{/if}

						<div class="example-text-content">
							<span class="example-text"
								>{example.display_text || example.text}</span
							>
						</div>
					</div>
				</button>
			{/each}
		</div>
	{/if}
</div>

<style>
	.placeholder-content {
		display: flex;
		flex-direction: column;
		height: 100%;
	}

	.placeholder {
		align-items: center;
		display: flex;
		justify-content: center;
		height: 100%;
		flex-grow: 1;
	}

	.examples :global(img) {
		pointer-events: none;
	}

	.examples {
		margin: auto;
		padding: var(--spacing-xxl);
		display: grid;
		grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
		gap: var(--spacing-xl);
		max-width: calc(min(4 * 240px + 5 * var(--spacing-xxl), 100%));
	}

	.example {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		padding: var(--spacing-xxl);
		border: none;
		border-radius: var(--radius-lg);
		background-color: var(--block-background-fill);
		cursor: pointer;
		transition: all 150ms ease-in-out;
		width: 100%;
		gap: var(--spacing-sm);
		border: var(--block-border-width) solid var(--block-border-color);
		transform: translateY(0px);
	}

	.example:hover {
		transform: translateY(-2px);
		background-color: var(--color-accent-soft);
	}

	.example-content {
		display: flex;
		flex-direction: column;
		align-items: flex-start;
		width: 100%;
		height: 100%;
	}

	.example-text-content {
		margin-top: var(--spacing-sm);
		text-align: left;
	}

	.example-text {
		font-size: var(--text-md);
		text-align: left;
		overflow: hidden;
		text-overflow: ellipsis;
	}

	.example-icons-grid {
		display: flex;
		gap: var(--spacing-sm);
		width: 100%;
	}

	.example-icon {
		flex-shrink: 0;
		width: var(--size-8);
		height: var(--size-8);
		display: flex;
		align-items: center;
		justify-content: center;
		border-radius: var(--radius-lg);
		border: var(--block-border-width) solid var(--block-border-color);
		background-color: var(--block-background-fill);
		position: relative;
	}

	.example-icon :global(svg) {
		width: var(--size-4);
		height: var(--size-4);
		color: var(--color-text-secondary);
	}

	.text-icon-aa {
		font-size: var(--text-sm);
		font-weight: var(--weight-semibold);
		color: var(--color-text-secondary);
		line-height: 1;
	}

	.example-image-container {
		width: var(--size-8);
		height: var(--size-8);
		border-radius: var(--radius-lg);
		overflow: hidden;
		position: relative;
	}

	.example-image-container :global(img) {
		width: 100%;
		height: 100%;
		object-fit: cover;
	}

	.image-overlay {
		position: absolute;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background: rgba(0, 0, 0, 0.6);
		color: white;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: var(--text-lg);
		font-weight: var(--weight-semibold);
		border-radius: var(--radius-lg);
	}

	.file-overlay {
		position: absolute;
		inset: 0;
		background: rgba(0, 0, 0, 0.6);
		color: white;
		display: flex;
		align-items: center;
		justify-content: center;
		font-size: var(--text-sm);
		font-weight: var(--weight-semibold);
		border-radius: var(--radius-lg);
	}
</style>