File size: 8,284 Bytes
cfb0fa4 | 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 | <script lang="ts">
import { toast } from 'svelte-sonner';
import { onMount, getContext } from 'svelte';
import { goto } from '$app/navigation';
import { user } from '$lib/stores';
import { imageGenerations, imageEdits } from '$lib/apis/images';
import Spinner from '$lib/components/common/Spinner.svelte';
const i18n = getContext('i18n');
let loaded = false;
let loading = false;
let prompt = '';
let sourceImages: string[] = [];
let generatedImages: { url: string }[] = [];
let promptTextareaElement: HTMLTextAreaElement;
let fileInputElement: HTMLInputElement;
const resizePromptTextarea = () => {
if (promptTextareaElement) {
promptTextareaElement.style.height = '';
promptTextareaElement.style.height = Math.min(promptTextareaElement.scrollHeight, 150) + 'px';
}
};
const handleFileUpload = (event: Event) => {
const input = event.target as HTMLInputElement;
if (input.files) {
Array.from(input.files).forEach((file) => {
const reader = new FileReader();
reader.onload = (e) => {
if (e.target?.result) {
sourceImages = [...sourceImages, e.target.result as string];
}
};
reader.readAsDataURL(file);
});
}
};
const handleDrop = (event: DragEvent) => {
event.preventDefault();
const files = event.dataTransfer?.files;
if (files) {
Array.from(files).forEach((file) => {
if (file.type.startsWith('image/')) {
const reader = new FileReader();
reader.onload = (e) => {
if (e.target?.result) {
sourceImages = [...sourceImages, e.target.result as string];
}
};
reader.readAsDataURL(file);
}
});
}
};
const removeImage = (index: number) => {
sourceImages = sourceImages.filter((_, i) => i !== index);
};
const submitHandler = async () => {
if (!prompt.trim()) {
toast.error($i18n.t('Please enter a prompt'));
return;
}
loading = true;
try {
let result;
if (sourceImages.length > 0) {
console.log('Calling imageEdits with', sourceImages.length, 'images');
result = await imageEdits(
localStorage.token,
sourceImages.length === 1 ? sourceImages[0] : sourceImages,
prompt
);
} else {
console.log('Calling imageGenerations');
result = await imageGenerations(localStorage.token, prompt);
}
console.log('Result:', result);
if (result) {
generatedImages = [...result, ...generatedImages];
}
} catch (error) {
console.error('Image generation/edit error:', error);
toast.error(`${error}`);
} finally {
loading = false;
}
};
const downloadImage = async (url: string, index: number) => {
try {
const response = await fetch(url);
const blob = await response.blob();
const blobUrl = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = blobUrl;
a.download = `image-${Date.now()}-${index}.png`;
a.click();
URL.revokeObjectURL(blobUrl);
} catch (error) {
toast.error($i18n.t('Failed to download image'));
}
};
onMount(async () => {
if ($user?.role !== 'admin') {
await goto('/');
return;
}
loaded = true;
});
</script>
<div class=" flex flex-col justify-between w-full overflow-y-auto h-full">
<div class="mx-auto w-full md:px-0 h-full">
<div class=" flex flex-col h-full px-4">
<!-- Results Area -->
<div
class=" pt-0.5 pb-2.5 flex flex-col justify-between w-full flex-auto overflow-auto h-0"
id="images-container"
>
<div class=" h-full w-full flex flex-col">
<div class="flex-1 p-1">
{#if generatedImages.length > 0}
<div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-4 gap-3">
{#each generatedImages as image, index}
<button
class="relative group cursor-pointer"
on:click={() => downloadImage(image.url, index)}
>
<img
src={image.url}
alt=""
class="w-full aspect-square object-cover rounded-lg border border-gray-100/30 dark:border-gray-850/30"
/>
<div
class="absolute inset-0 bg-black/40 opacity-0 group-hover:opacity-100 transition rounded-lg flex items-center justify-center"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="w-6 h-6 text-white"
viewBox="0 0 24 24"
fill="none"
stroke="currentColor"
stroke-width="2"
>
<path d="M21 15v4a2 2 0 0 1-2 2H5a2 2 0 0 1-2-2v-4" />
<polyline points="7,10 12,15 17,10" />
<line x1="12" y1="15" x2="12" y2="3" />
</svg>
</div>
</button>
{/each}
</div>
{:else}
<div
class="h-full flex items-center justify-center text-gray-400 dark:text-gray-600 text-sm"
>
{$i18n.t('Generated images will appear here')}
</div>
{/if}
</div>
</div>
</div>
<!-- Input Area -->
<div class="pb-3">
<div
class="border border-gray-100/30 dark:border-gray-850/30 w-full px-3 py-2.5 rounded-xl"
>
<!-- Source Images -->
{#if sourceImages.length > 0}
<div class="flex flex-wrap gap-2 mb-2">
{#each sourceImages as image, index}
<div class=" relative group">
<div class="relative flex items-center">
<img src={image} alt="" class="size-10 rounded-xl object-cover" />
</div>
<div class=" absolute -top-1 -right-1">
<button
class=" bg-white text-black border border-white rounded-full group-hover:visible invisible transition"
type="button"
on:click={() => removeImage(index)}
>
<svg
xmlns="http://www.w3.org/2000/svg"
viewBox="0 0 20 20"
fill="currentColor"
aria-hidden="true"
class="size-4"
>
<path
d="M6.28 5.22a.75.75 0 00-1.06 1.06L8.94 10l-3.72 3.72a.75.75 0 101.06 1.06L10 11.06l3.72 3.72a.75.75 0 101.06-1.06L11.06 10l3.72-3.72a.75.75 0 00-1.06-1.06L10 8.94 6.28 5.22z"
/>
</svg>
</button>
</div>
</div>
{/each}
</div>
{/if}
<!-- Prompt Textarea -->
<div class="py-0.5">
<textarea
bind:this={promptTextareaElement}
bind:value={prompt}
class=" w-full h-full bg-transparent resize-none outline-hidden text-sm"
placeholder={sourceImages.length > 0
? $i18n.t('Describe the edit...')
: $i18n.t('Describe the image...')}
on:input={resizePromptTextarea}
on:focus={resizePromptTextarea}
on:keydown={(e) => {
if (e.key === 'Enter' && (e.metaKey || e.ctrlKey) && !loading) {
e.preventDefault();
submitHandler();
}
}}
rows="2"
/>
</div>
<!-- Actions -->
<div class="flex justify-between items-center gap-2 mt-2">
<div class="shrink-0">
<input
type="file"
accept="image/*"
multiple
class="hidden"
bind:this={fileInputElement}
on:change={handleFileUpload}
/>
<button
type="button"
class="px-3.5 py-1.5 text-sm font-medium bg-gray-50 hover:bg-gray-100 text-gray-900 dark:bg-gray-850 dark:hover:bg-gray-800 dark:text-gray-200 transition rounded-lg"
on:click={() => fileInputElement?.click()}
on:dragover|preventDefault
on:drop={handleDrop}
>
{$i18n.t('Add Image')}
</button>
</div>
<div class="flex gap-2 shrink-0">
{#if !loading}
<button
disabled={prompt.trim() === ''}
class="px-3.5 py-1.5 text-sm font-medium bg-black hover:bg-gray-900 text-white dark:bg-white dark:text-black dark:hover:bg-gray-100 transition rounded-lg disabled:opacity-50 disabled:cursor-not-allowed"
on:click={submitHandler}
>
{$i18n.t('Run')}
</button>
{:else}
<button
class="px-3.5 py-1.5 text-sm font-medium bg-gray-300 text-black transition rounded-lg flex items-center gap-2"
disabled
>
<Spinner className="size-4" />
{$i18n.t('Running...')}
</button>
{/if}
</div>
</div>
</div>
</div>
</div>
</div>
</div>
|