|
| 1 | + |
| 2 | +<template> |
| 3 | + <!-- Main modal --> |
| 4 | + <div tabindex="-1" class="overflow-y-auto overflow-x-hidden fixed top-0 right-0 left-0 bottom-0 z-50 flex justify-center items-center w-full md:inset-0 h-[calc(100%-1rem)] max-h-full bg-white bg-opacity-50 dark:bg-gray-900 dark:bg-opacity-50"> |
| 5 | + <div class="relative p-4 w-full max-w-2xl max-h-full "> |
| 6 | + <!-- Modal content --> |
| 7 | + <div class="relative bg-white rounded-lg shadow-xl dark:bg-gray-700"> |
| 8 | + <!-- Modal header --> |
| 9 | + <div class="flex items-center justify-between p-3 md:p-4 border-b rounded-t dark:border-gray-600"> |
| 10 | + <h3 class="text-xl font-semibold text-gray-900 dark:text-white"> |
| 11 | + {{ $t('Generate image with AI') }} |
| 12 | + </h3> |
| 13 | + <button type="button" |
| 14 | + @click="emit('close')" |
| 15 | + class="text-gray-400 bg-transparent hover:bg-gray-200 hover:text-gray-900 rounded-lg text-sm w-8 h-8 ms-auto inline-flex justify-center items-center dark:hover:bg-gray-600 dark:hover:text-white" > |
| 16 | + <svg class="w-3 h-3" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 14 14"> |
| 17 | + <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 1 6 6m0 0 6 6M7 7l6-6M7 7l-6 6"/> |
| 18 | + </svg> |
| 19 | + <span class="sr-only">{{ $t('Close modal') }}</span> |
| 20 | + </button> |
| 21 | + </div> |
| 22 | + <!-- Modal body --> |
| 23 | + <div class="p-4 md:p-5 space-y-4"> |
| 24 | + <textarea id="message" rows="3" class="block p-2.5 w-full text-sm text-gray-900 bg-gray-50 rounded-lg border border-gray-300 focus:ring-blue-500 focus:border-blue-500 dark:bg-gray-700 dark:border-gray-600 dark:placeholder-gray-400 dark:text-white dark:focus:ring-blue-500 dark:focus:border-blue-500" |
| 25 | + :placeholder="$t('Prompt which will be passed to AI network')" v-model="prompt" |
| 26 | + :title="$t('Prompt which will be passed to AI network')" |
| 27 | + ></textarea> |
| 28 | + |
| 29 | + <div class="flex items-center justify-center w-full relative"> |
| 30 | + <div |
| 31 | + v-if="loading" |
| 32 | + class=" absolute flex items-center justify-center w-full h-full z-50 bg-white/80 dark:bg-gray-900/80 rounded-lg" |
| 33 | + > |
| 34 | + <div role="status" class="absolute -translate-x-1/2 -translate-y-1/2 top-2/4 left-1/2"> |
| 35 | + <svg aria-hidden="true" class="w-8 h-8 text-gray-200 animate-spin dark:text-gray-600 fill-blue-600" viewBox="0 0 100 101" fill="none" xmlns="http://www.w3.org/2000/svg"><path d="M100 50.5908C100 78.2051 77.6142 100.591 50 100.591C22.3858 100.591 0 78.2051 0 50.5908C0 22.9766 22.3858 0.59082 50 0.59082C77.6142 0.59082 100 22.9766 100 50.5908ZM9.08144 50.5908C9.08144 73.1895 27.4013 91.5094 50 91.5094C72.5987 91.5094 90.9186 73.1895 90.9186 50.5908C90.9186 27.9921 72.5987 9.67226 50 9.67226C27.4013 9.67226 9.08144 27.9921 9.08144 50.5908Z" fill="currentColor"/><path d="M93.9676 39.0409C96.393 38.4038 97.8624 35.9116 97.0079 33.5539C95.2932 28.8227 92.871 24.3692 89.8167 20.348C85.8452 15.1192 80.8826 10.7238 75.2124 7.41289C69.5422 4.10194 63.2754 1.94025 56.7698 1.05124C51.7666 0.367541 46.6976 0.446843 41.7345 1.27873C39.2613 1.69328 37.813 4.19778 38.4501 6.62326C39.0873 9.04874 41.5694 10.4717 44.0505 10.1071C47.8511 9.54855 51.7191 9.52689 55.5402 10.0491C60.8642 10.7766 65.9928 12.5457 70.6331 15.2552C75.2735 17.9648 79.3347 21.5619 82.5849 25.841C84.9175 28.9121 86.7997 32.2913 88.1811 35.8758C89.083 38.2158 91.5421 39.6781 93.9676 39.0409Z" fill="currentFill"/></svg> |
| 36 | + <span class="sr-only">{{ $t('Loading...') }}</span> |
| 37 | + </div> |
| 38 | + </div> |
| 39 | + |
| 40 | + <div id="gallery" class="relative w-full" data-carousel="static"> |
| 41 | + <!-- Carousel wrapper --> |
| 42 | + <div class="relative h-56 overflow-hidden rounded-lg md:h-72"> |
| 43 | + <!-- Item 1 --> |
| 44 | + <div v-for="(img, index) in images" :key="index" class="hidden duration-700 ease-in-out" data-carousel-item> |
| 45 | + <img :src="img" class="absolute block max-w-full h-auto -translate-x-1/2 -translate-y-1/2 top-1/2 left-1/2" alt=""> |
| 46 | + </div> |
| 47 | + |
| 48 | + <div v-if="images.length === 0" class="flex items-center justify-center w-full h-full"> |
| 49 | + |
| 50 | + <button @click="generateImages" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 |
| 51 | + focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center |
| 52 | + dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800 ms-2">{{ $t('Generate images') }}</button> |
| 53 | + |
| 54 | + </div> |
| 55 | + |
| 56 | + </div> |
| 57 | + <!-- Slider controls --> |
| 58 | + <button type="button" class="absolute top-0 start-0 z-30 flex items-center justify-center h-full px-4 cursor-pointer group focus:outline-none" |
| 59 | + @click="slide(-1)" |
| 60 | + > |
| 61 | + <span class="inline-flex items-center justify-center w-10 h-10 rounded-full bg-white/30 dark:bg-gray-800/30 group-hover:bg-white/50 dark:group-hover:bg-gray-800/60 group-focus:ring-4 group-focus:ring-white dark:group-focus:ring-gray-800/70 group-focus:outline-none"> |
| 62 | + <svg class="w-4 h-4 text-white dark:text-gray-800 rtl:rotate-180" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 6 10"> |
| 63 | + <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 1 1 5l4 4"/> |
| 64 | + </svg> |
| 65 | + <span class="sr-only">{{ $t('Previous') }}</span> |
| 66 | + </span> |
| 67 | + </button> |
| 68 | + <button type="button" class="absolute top-0 end-0 z-30 flex items-center justify-center h-full px-4 cursor-pointer group focus:outline-none" |
| 69 | + @click="slide(1)" |
| 70 | + > |
| 71 | + <span class="inline-flex items-center justify-center w-10 h-10 rounded-full bg-white/30 dark:bg-gray-800/30 group-hover:bg-white/50 dark:group-hover:bg-gray-800/60 group-focus:ring-4 group-focus:ring-white dark:group-focus:ring-gray-800/70 group-focus:outline-none"> |
| 72 | + <svg class="w-4 h-4 text-white dark:text-gray-800 rtl:rotate-180" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 6 10"> |
| 73 | + <path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="m1 9 4-4-4-4"/> |
| 74 | + </svg> |
| 75 | + <span class="sr-only">{{ $t('Next') }}</span> |
| 76 | + </span> |
| 77 | + </button> |
| 78 | + |
| 79 | + |
| 80 | + </div> |
| 81 | + </div> |
| 82 | + </div> |
| 83 | + <!-- Modal footer --> |
| 84 | + <div class="flex items-center p-4 md:p-5 border-t border-gray-200 rounded-b dark:border-gray-600"> |
| 85 | + <button type="button" @click="confirmImage" |
| 86 | + :disabled="loading" |
| 87 | + class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:outline-none focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center |
| 88 | + dark:bg-blue-600 dark:hover:bg-blue-700 dark:focus:ring-blue-800 |
| 89 | + disabled:opacity-50 disabled:cursor-not-allowed" |
| 90 | + >{{ $t('Use image') }}</button> |
| 91 | + <button type="button" class="py-2.5 px-5 ms-3 text-sm font-medium text-gray-900 focus:outline-none bg-white rounded-lg border border-gray-200 hover:bg-gray-100 hover:text-blue-700 focus:z-10 focus:ring-4 focus:ring-gray-100 dark:focus:ring-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700" |
| 92 | + @click="emit('close')" |
| 93 | + >{{ $t('Cancel') }}</button> |
| 94 | + </div> |
| 95 | + </div> |
| 96 | + </div> |
| 97 | + </div> |
| 98 | + |
| 99 | + |
| 100 | + |
| 101 | + |
| 102 | +</template> |
| 103 | + |
| 104 | +<script setup lang="ts"> |
| 105 | +
|
| 106 | +import { ref, onMounted, nextTick } from 'vue' |
| 107 | +import { Carousel } from 'flowbite'; |
| 108 | +import { callAdminForthApi } from '@/utils'; |
| 109 | +import { useI18n } from 'vue-i18n'; |
| 110 | +import adminforth from '@/adminforth'; |
| 111 | +
|
| 112 | +const { t: $t } = useI18n(); |
| 113 | +
|
| 114 | +const prompt = ref(''); |
| 115 | +const emit = defineEmits(['close', 'uploadImage']); |
| 116 | +const props = defineProps(['meta', 'record']); |
| 117 | +const images = ref([]); |
| 118 | +const loading = ref(false); |
| 119 | +
|
| 120 | +function minifyField(field: string): string { |
| 121 | + if (field.length > 100) { |
| 122 | + return field.slice(0, 100) + '...'; |
| 123 | + } |
| 124 | + return field; |
| 125 | +} |
| 126 | +
|
| 127 | +const caurosel = ref(null); |
| 128 | +onMounted(() => { |
| 129 | + // Initialize carousel |
| 130 | + let additionalContext = null; |
| 131 | + if (props.meta.fieldsForContext) { |
| 132 | + additionalContext = props.meta.fieldsForContext.filter((field: string) => props.record[field]).map((field: string) => { |
| 133 | + return `${field}: ${minifyField(props.record[field])}`; |
| 134 | + }).join('\n'); |
| 135 | + } |
| 136 | +
|
| 137 | + prompt.value = $t('Generate image for field "{field}" in {resource}. No text should be on image.', { |
| 138 | + field: props.meta.pathColumnLabel, |
| 139 | + resource: props.meta.resourceLabel, |
| 140 | + }); |
| 141 | + if (additionalContext) { |
| 142 | + prompt.value += ` ${additionalContext}`; |
| 143 | + } |
| 144 | +
|
| 145 | +}) |
| 146 | +
|
| 147 | +async function slide(direction: number) { |
| 148 | + if (!caurosel.value) return; |
| 149 | + const curPos = caurosel.value.getActiveItem().position; |
| 150 | + if (curPos === 0 && direction === -1) return; |
| 151 | + if (curPos === images.value.length - 1 && direction === 1) { |
| 152 | + await generateImages(); |
| 153 | + } |
| 154 | + if (direction === 1) { |
| 155 | + caurosel.value.next(); |
| 156 | + } else { |
| 157 | + caurosel.value.prev(); |
| 158 | + } |
| 159 | +} |
| 160 | +
|
| 161 | +async function confirmImage() { |
| 162 | + loading.value = true; |
| 163 | +
|
| 164 | + const currentIndex = caurosel.value?.getActiveItem()?.position || 0; |
| 165 | + const img = images.value[currentIndex]; |
| 166 | + // read url to base64 and send it to the parent component |
| 167 | + const imgBlob = await fetch( |
| 168 | + `${import.meta.env.VITE_ADMINFORTH_PUBLIC_PATH || ''}/adminapi/v1/plugin/${props.meta.pluginInstanceId}/cors-proxy?url=${encodeURIComponent(img)}` |
| 169 | + ).then(res => { return res.blob() }); |
| 170 | +
|
| 171 | + emit('uploadImage', imgBlob); |
| 172 | + emit('close'); |
| 173 | +
|
| 174 | + loading.value = false; |
| 175 | +} |
| 176 | +
|
| 177 | +async function generateImages() { |
| 178 | + loading.value = true; |
| 179 | + const currentIndex = caurosel.value?.getActiveItem()?.position || 0; |
| 180 | + const resp = await callAdminForthApi({ |
| 181 | + path: `/plugin/${props.meta.pluginInstanceId}/generate_images`, |
| 182 | + method: 'POST', |
| 183 | + body: { |
| 184 | + prompt: prompt.value, |
| 185 | + }, |
| 186 | + }); |
| 187 | +
|
| 188 | + if (resp.error) { |
| 189 | + adminforth.alert({ |
| 190 | + message: $t('Error: {error}', { error: JSON.stringify(resp.error) }), |
| 191 | + variant: 'danger', |
| 192 | + timeout: 15, |
| 193 | + }); |
| 194 | + loading.value = false; |
| 195 | + return; |
| 196 | + } |
| 197 | +
|
| 198 | + images.value = [ |
| 199 | + ...images.value, |
| 200 | + ...resp.images.map(im => im.data[0].url), |
| 201 | + ]; |
| 202 | +
|
| 203 | + // images.value = [ |
| 204 | + // 'https://via.placeholder.com/600x400?text=Image+1', |
| 205 | + // 'https://via.placeholder.com/600x400?text=Image+2', |
| 206 | + // ]; |
| 207 | + await nextTick(); |
| 208 | +
|
| 209 | + caurosel.value = new Carousel( |
| 210 | + document.getElementById('gallery'), |
| 211 | + images.value.map((img, index) => ({ |
| 212 | + el: document.getElementById('gallery').querySelector(`[data-carousel-item]:nth-child(${index + 1})`), |
| 213 | + position: index, |
| 214 | + })), |
| 215 | + { |
| 216 | + internal: 0, |
| 217 | + defaultPosition: currentIndex, |
| 218 | + }, |
| 219 | + { |
| 220 | + override: true, |
| 221 | + } |
| 222 | + ); |
| 223 | + await nextTick(); |
| 224 | + loading.value = false; |
| 225 | +} |
| 226 | +
|
| 227 | +
|
| 228 | +
|
| 229 | +</script> |
0 commit comments