@adminforth/upload 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.woodpecker/buildRelease.sh +13 -0
- package/.woodpecker/buildSlackNotify.sh +44 -0
- package/.woodpecker/release.yml +42 -0
- package/CHANGELOG.md +5 -0
- package/LICENSE +21 -0
- package/build.log +15 -0
- package/custom/imageGenerator.vue +229 -0
- package/custom/package-lock.json +310 -0
- package/custom/package.json +16 -0
- package/custom/preview.vue +111 -0
- package/custom/tsconfig.json +19 -0
- package/custom/uploader.vue +304 -0
- package/dist/custom/imageGenerator.vue +229 -0
- package/dist/custom/package-lock.json +310 -0
- package/dist/custom/package.json +16 -0
- package/dist/custom/preview.vue +111 -0
- package/dist/custom/tsconfig.json +19 -0
- package/dist/custom/uploader.vue +304 -0
- package/dist/index.js +482 -0
- package/dist/types.js +1 -0
- package/index.ts +546 -0
- package/package.json +52 -0
- package/tsconfig.json +112 -0
- package/types.ts +162 -0
|
@@ -0,0 +1,304 @@
|
|
|
1
|
+
<template>
|
|
2
|
+
<div class="relative w-full">
|
|
3
|
+
<ImageGenerator v-if="showImageGen" @close="showImageGen = false" :record="record" :meta="meta"
|
|
4
|
+
@uploadImage="uploadGeneratedImage"
|
|
5
|
+
></ImageGenerator>
|
|
6
|
+
|
|
7
|
+
<button v-if="meta.generateImages"
|
|
8
|
+
type="button" @click="showImageGen = true"
|
|
9
|
+
class="text-white bg-gradient-to-r from-purple-500 via-purple-600 to-purple-700 hover:bg-gradient-to-br focus:ring-4 focus:outline-none focus:ring-purple-300 dark:focus:ring-purple-800
|
|
10
|
+
font-medium rounded-lg text-sm px-2.5 py-2.5 text-center me-2 mb-2 absolute right-0 top-2">
|
|
11
|
+
<IconMagic class="w-5 h-5"/>
|
|
12
|
+
</button>
|
|
13
|
+
|
|
14
|
+
<label :for="inputId"
|
|
15
|
+
class="flex flex-col px-3 items-center justify-center w-full h-64 border-2 border-dashed rounded-lg cursor-pointer dark:hover:bg-gray-800 hover:bg-gray-100 dark:hover:border-gray-500 dark:hover:bg-gray-600"
|
|
16
|
+
@dragover.prevent="() => dragging = true"
|
|
17
|
+
@dragleave.prevent="() => dragging = false"
|
|
18
|
+
@drop.prevent="onFileChange"
|
|
19
|
+
:class="{
|
|
20
|
+
'border-blue-600 dark:border-blue-400': dragging,
|
|
21
|
+
'border-gray-300 dark:border-gray-600': !dragging,
|
|
22
|
+
'bg-blue-50 dark:bg-blue-800': dragging,
|
|
23
|
+
'bg-gray-50 dark:bg-gray-800': !dragging,
|
|
24
|
+
}"
|
|
25
|
+
>
|
|
26
|
+
<div class="flex flex-col items-center justify-center pt-5 pb-6">
|
|
27
|
+
<img v-if="imgPreview" :src="imgPreview" class="w-100 mt-4 rounded-lg h-40 object-contain" />
|
|
28
|
+
|
|
29
|
+
<svg v-else class="w-8 h-8 mb-4 text-gray-500 dark:text-gray-400" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 20 16">
|
|
30
|
+
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M13 13h3a3 3 0 0 0 0-6h-.025A5.56 5.56 0 0 0 16 6.5 5.5 5.5 0 0 0 5.207 5.021C5.137 5.017 5.071 5 5 5a4 4 0 0 0 0 8h2.167M10 15V6m0 0L8 8m2-2 2 2"/>
|
|
31
|
+
</svg>
|
|
32
|
+
|
|
33
|
+
<template v-if="!uploaded">
|
|
34
|
+
<p class="mb-2 text-sm text-gray-500 dark:text-gray-400"><span class="font-semibold">{{ $t('Click to upload') }}</span> {{ $t('or drag and drop') }}</p>
|
|
35
|
+
<p class="text-xs text-gray-500 dark:text-gray-400">
|
|
36
|
+
{{ allowedExtensionsLabel }} {{ meta.maxFileSize ? $t(`(up to {size})`, { size: humanifySize(meta.maxFileSize) }) : '' }}
|
|
37
|
+
</p>
|
|
38
|
+
</template>
|
|
39
|
+
|
|
40
|
+
<div class="w-full bg-gray-200 rounded-full dark:bg-gray-700 mt-1 mb-2" v-if="progress > 0 && !uploaded">
|
|
41
|
+
<!-- progress bar with smooth progress animation -->
|
|
42
|
+
<div class="bg-blue-600 text-xs font-medium text-blue-100 text-center p-0.5 leading-none rounded-full
|
|
43
|
+
transition-all duration-200 ease-in-out"
|
|
44
|
+
:style="{width: `${progress}%`}">{{ progress }}%
|
|
45
|
+
</div>
|
|
46
|
+
</div>
|
|
47
|
+
|
|
48
|
+
<div v-else-if="uploaded" class="flex items-center justify-center w-full mt-1">
|
|
49
|
+
<svg class="w-4 h-4 text-green-600 dark:text-green-400" aria-hidden="true" xmlns="http://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24">
|
|
50
|
+
<path stroke="currentColor" stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M5 13l4 4L19 7"/>
|
|
51
|
+
</svg>
|
|
52
|
+
<p class="ml-2 text-sm text-green-600 dark:text-green-400 flex items-center">
|
|
53
|
+
{{ $t('File uploaded') }}
|
|
54
|
+
<span class="text-xs text-gray-500 dark:text-gray-400">{{ humanifySize(uploadedSize) }}</span>
|
|
55
|
+
</p>
|
|
56
|
+
|
|
57
|
+
<button @click.stop.prevent="clear" class="ml-2 text-xs text-gray-500 dark:text-gray-400 hover:text-gray-600 dark:hover:text-gray-500
|
|
58
|
+
hover:underline dark:hover:underline focus:outline-none">{{ $t('Clear') }}</button>
|
|
59
|
+
</div>
|
|
60
|
+
|
|
61
|
+
</div>
|
|
62
|
+
<input :id="inputId" type="file" :accept="allowedExtensionsAttribute" class="hidden" @change="onFileChange" ref="uploadInputRef" />
|
|
63
|
+
</label>
|
|
64
|
+
</div>
|
|
65
|
+
|
|
66
|
+
</template>
|
|
67
|
+
|
|
68
|
+
<script setup lang="ts">
|
|
69
|
+
import { computed, ref, onMounted, watch } from 'vue'
|
|
70
|
+
import { callAdminForthApi } from '@/utils'
|
|
71
|
+
import { IconMagic } from '@iconify-prerendered/vue-mdi';
|
|
72
|
+
import { useI18n } from 'vue-i18n';
|
|
73
|
+
import { useRoute } from 'vue-router';
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
const route = useRoute();
|
|
77
|
+
const { t } = useI18n();
|
|
78
|
+
|
|
79
|
+
const inputId = computed(() => `dropzone-file-${props.meta.pluginInstanceId}`);
|
|
80
|
+
|
|
81
|
+
import ImageGenerator from '@@/plugins/UploadPlugin/imageGenerator.vue';
|
|
82
|
+
import adminforth from '@/adminforth';
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
const props = defineProps({
|
|
86
|
+
meta: Object,
|
|
87
|
+
record: Object,
|
|
88
|
+
})
|
|
89
|
+
|
|
90
|
+
const emit = defineEmits([
|
|
91
|
+
'update:value',
|
|
92
|
+
'update:inValidity',
|
|
93
|
+
'update:emptiness',
|
|
94
|
+
]);
|
|
95
|
+
|
|
96
|
+
const uploadInputRef = ref(null);
|
|
97
|
+
|
|
98
|
+
const showImageGen = ref(false);
|
|
99
|
+
const dragging = ref(false);
|
|
100
|
+
|
|
101
|
+
const imgPreview = ref(null);
|
|
102
|
+
const progress = ref(0);
|
|
103
|
+
|
|
104
|
+
const uploaded = ref(false);
|
|
105
|
+
const uploadedSize = ref(0);
|
|
106
|
+
|
|
107
|
+
watch(() => uploaded, (value) => {
|
|
108
|
+
emit('update:emptiness', !value);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
function uploadGeneratedImage(imgBlob) {
|
|
112
|
+
// update the value
|
|
113
|
+
|
|
114
|
+
const file = new File([imgBlob], 'generated.png', { type: imgBlob.type });
|
|
115
|
+
onFileChange({
|
|
116
|
+
target: {
|
|
117
|
+
files: [file],
|
|
118
|
+
},
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
onMounted(() => {
|
|
123
|
+
const previewColumnName = `previewUrl_${props.meta.pluginInstanceId}`;
|
|
124
|
+
if (props.record[previewColumnName]) {
|
|
125
|
+
imgPreview.value = props.record[previewColumnName];
|
|
126
|
+
uploaded.value = true;
|
|
127
|
+
emit('update:emptiness', false);
|
|
128
|
+
}
|
|
129
|
+
});
|
|
130
|
+
|
|
131
|
+
const allowedExtensionsLabel = computed(() => {
|
|
132
|
+
const allowedExtensions = props.meta.allowedExtensions || []
|
|
133
|
+
if (allowedExtensions.length === 0) {
|
|
134
|
+
return 'Any file type'
|
|
135
|
+
}
|
|
136
|
+
// make upper case and write in format EXT1, EXT2 or EXT3
|
|
137
|
+
let label = allowedExtensions.map(ext => ext.toUpperCase()).join(', ');
|
|
138
|
+
// last comma to 'or'
|
|
139
|
+
label = label.replace(/,([^,]*)$/, ` ${t('or')}$1`)
|
|
140
|
+
return label
|
|
141
|
+
});
|
|
142
|
+
|
|
143
|
+
const allowedExtensionsAttribute = computed(() => {
|
|
144
|
+
const allowedExtensions = props.meta.allowedExtensions || [];
|
|
145
|
+
return allowedExtensions.length > 0
|
|
146
|
+
? allowedExtensions.map(ext => `.${ext}`).join(', ')
|
|
147
|
+
: '';
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
function clear() {
|
|
151
|
+
imgPreview.value = null;
|
|
152
|
+
progress.value = 0;
|
|
153
|
+
uploaded.value = false;
|
|
154
|
+
uploadedSize.value = 0;
|
|
155
|
+
uploadInputRef.value.value = null;
|
|
156
|
+
emit('update:value', null);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function humanifySize(size) {
|
|
160
|
+
if (!size) {
|
|
161
|
+
return '';
|
|
162
|
+
}
|
|
163
|
+
const units = ['B', 'KB', 'MB', 'GB', 'TB']
|
|
164
|
+
let i = 0
|
|
165
|
+
while (size >= 1024 && i < units.length - 1) {
|
|
166
|
+
size /= 1024
|
|
167
|
+
i++
|
|
168
|
+
}
|
|
169
|
+
return `${size.toFixed(1)} ${units[i]}`
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
const onFileChange = async (e) => {
|
|
174
|
+
// if empty then return
|
|
175
|
+
const files = e.target?.files || e.dataTransfer.files
|
|
176
|
+
|
|
177
|
+
if (!files || files.length === 0) {
|
|
178
|
+
return;
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
imgPreview.value = null;
|
|
182
|
+
progress.value = 0;
|
|
183
|
+
uploaded.value = false;
|
|
184
|
+
|
|
185
|
+
const file = files[0];
|
|
186
|
+
|
|
187
|
+
// get filename, extension, size, mimeType
|
|
188
|
+
const { name, size, type } = file;
|
|
189
|
+
|
|
190
|
+
uploadedSize.value = size;
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
const extension = name.split('.').pop();
|
|
194
|
+
const nameNoExtension = name.replace(`.${extension}`, '');
|
|
195
|
+
console.log('File details:', { name, extension, size, type });
|
|
196
|
+
// validate file extension
|
|
197
|
+
const allowedExtensions = props.meta.allowedExtensions || []
|
|
198
|
+
if (allowedExtensions.length > 0 && !allowedExtensions.includes(extension)) {
|
|
199
|
+
adminforth.alert({
|
|
200
|
+
message: t('Sorry but the file type {extension} is not allowed. Please upload a file with one of the following extensions: {allowedExtensionsLabel}', {
|
|
201
|
+
extension,
|
|
202
|
+
allowedExtensionsLabel: allowedExtensionsLabel.value,
|
|
203
|
+
}),
|
|
204
|
+
variant: 'danger'
|
|
205
|
+
});
|
|
206
|
+
return;
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// validate file size
|
|
210
|
+
if (props.meta.maxFileSize && size > props.meta.maxFileSize) {
|
|
211
|
+
adminforth.alert({
|
|
212
|
+
message: t('Sorry but the file size {size} is too large. Please upload a file with a maximum size of {maxFileSize}', {
|
|
213
|
+
size: humanifySize(size),
|
|
214
|
+
maxFileSize: humanifySize(props.meta.maxFileSize),
|
|
215
|
+
}),
|
|
216
|
+
variant: 'danger'
|
|
217
|
+
});
|
|
218
|
+
return;
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
emit('update:inValidity', t('Upload in progress...'));
|
|
222
|
+
try {
|
|
223
|
+
// supports preview
|
|
224
|
+
if (type.startsWith('image/')) {
|
|
225
|
+
const reader = new FileReader();
|
|
226
|
+
reader.onload = (e) => {
|
|
227
|
+
imgPreview.value = e.target.result;
|
|
228
|
+
}
|
|
229
|
+
reader.readAsDataURL(file);
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
const { uploadUrl, tagline, s3Path, error } = await callAdminForthApi({
|
|
233
|
+
path: `/plugin/${props.meta.pluginInstanceId}/get_s3_upload_url`,
|
|
234
|
+
method: 'POST',
|
|
235
|
+
body: {
|
|
236
|
+
originalFilename: nameNoExtension,
|
|
237
|
+
contentType: type,
|
|
238
|
+
size,
|
|
239
|
+
originalExtension: extension,
|
|
240
|
+
recordPk: route?.params?.primaryKey,
|
|
241
|
+
},
|
|
242
|
+
});
|
|
243
|
+
|
|
244
|
+
if (error) {
|
|
245
|
+
adminforth.alert({
|
|
246
|
+
message: t('File was not uploaded because of error: {error}', { error }),
|
|
247
|
+
variant: 'danger'
|
|
248
|
+
});
|
|
249
|
+
imgPreview.value = null;
|
|
250
|
+
uploaded.value = false;
|
|
251
|
+
progress.value = 0;
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
const xhr = new XMLHttpRequest();
|
|
256
|
+
const success = await new Promise((resolve) => {
|
|
257
|
+
xhr.upload.onprogress = (e) => {
|
|
258
|
+
if (e.lengthComputable) {
|
|
259
|
+
progress.value = Math.round((e.loaded / e.total) * 100);
|
|
260
|
+
}
|
|
261
|
+
};
|
|
262
|
+
xhr.addEventListener('loadend', () => {
|
|
263
|
+
const success = xhr.readyState === 4 && xhr.status === 200;
|
|
264
|
+
// try to read response
|
|
265
|
+
resolve(success);
|
|
266
|
+
});
|
|
267
|
+
xhr.open('PUT', uploadUrl, true);
|
|
268
|
+
xhr.setRequestHeader('Content-Type', type);
|
|
269
|
+
xhr.setRequestHeader('x-amz-tagging', tagline);
|
|
270
|
+
xhr.send(file);
|
|
271
|
+
});
|
|
272
|
+
if (!success) {
|
|
273
|
+
adminforth.alert({
|
|
274
|
+
messageHtml: `<div>${t('Sorry but the file was not uploaded because of S3 Request Error:')}</div>
|
|
275
|
+
<pre style="white-space: pre-wrap; word-wrap: break-word; overflow-wrap: break-word; max-width: 100%;">${
|
|
276
|
+
xhr.responseText.replace(/</g, '<').replace(/>/g, '>')
|
|
277
|
+
}</pre>`,
|
|
278
|
+
variant: 'danger',
|
|
279
|
+
timeout: 30,
|
|
280
|
+
});
|
|
281
|
+
imgPreview.value = null;
|
|
282
|
+
uploaded.value = false;
|
|
283
|
+
progress.value = 0;
|
|
284
|
+
return;
|
|
285
|
+
}
|
|
286
|
+
uploaded.value = true;
|
|
287
|
+
emit('update:value', s3Path);
|
|
288
|
+
} catch (error) {
|
|
289
|
+
console.error('Error uploading file:', error);
|
|
290
|
+
adminforth.alert({
|
|
291
|
+
message: t('Sorry but the file was not be uploaded. Please try again: {error}', { error: error.message }),
|
|
292
|
+
variant: 'danger'
|
|
293
|
+
});
|
|
294
|
+
imgPreview.value = null;
|
|
295
|
+
uploaded.value = false;
|
|
296
|
+
progress.value = 0;
|
|
297
|
+
} finally {
|
|
298
|
+
emit('update:inValidity', false);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
|
|
304
|
+
</script>
|
|
@@ -0,0 +1,229 @@
|
|
|
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>
|