366 lines
10 KiB
Vue
366 lines
10 KiB
Vue
<script setup lang="ts">
|
||
import { ref, onMounted, onBeforeUnmount, nextTick, computed } from 'vue'
|
||
import { getCachedImageUrl } from '@/common/imageCache'
|
||
|
||
const props = defineProps<{
|
||
modelValue?: string | null // selected image id or local-upload
|
||
imageType?: number // 1 or 2, default 1
|
||
}>()
|
||
const emit = defineEmits(['update:modelValue', 'add-image'])
|
||
|
||
const fileInput = ref<HTMLInputElement | null>(null)
|
||
const localImageUrl = ref<string | null>(null)
|
||
const showCamera = ref(false)
|
||
const cameraStream = ref<MediaStream | null>(null)
|
||
const cameraVideo = ref<HTMLVideoElement | null>(null)
|
||
const cameraError = ref<string | null>(null)
|
||
const capturedImageUrl = ref<string | null>(null)
|
||
const cameraFile = ref<File | null>(null)
|
||
|
||
const availableImages = ref<{ id: string; url: string }[]>([])
|
||
const loadingImages = ref(false)
|
||
|
||
const typeParam = computed(() => props.imageType ?? 1)
|
||
|
||
const selectImage = (id: string | undefined) => {
|
||
if (!id) {
|
||
console.warn('selectImage called with null id')
|
||
return
|
||
}
|
||
emit('update:modelValue', id)
|
||
}
|
||
|
||
const addFromLocal = () => {
|
||
fileInput.value?.click()
|
||
}
|
||
|
||
const onFileChange = async (event: Event) => {
|
||
const files = (event.target as HTMLInputElement).files
|
||
if (files && files.length > 0) {
|
||
const file = files[0]
|
||
if (localImageUrl.value) URL.revokeObjectURL(localImageUrl.value)
|
||
const { blob, url } = await resizeImageFile(file, 512)
|
||
localImageUrl.value = url
|
||
updateLocalImage(url, new File([blob], file.name, { type: 'image/png' }))
|
||
}
|
||
}
|
||
|
||
onBeforeUnmount(() => {
|
||
if (localImageUrl.value) URL.revokeObjectURL(localImageUrl.value)
|
||
})
|
||
|
||
const addFromCamera = async () => {
|
||
cameraError.value = null
|
||
capturedImageUrl.value = null
|
||
showCamera.value = true
|
||
await nextTick()
|
||
try {
|
||
const stream = await navigator.mediaDevices.getUserMedia({ video: true })
|
||
cameraStream.value = stream
|
||
if (cameraVideo.value) {
|
||
cameraVideo.value.srcObject = stream
|
||
await cameraVideo.value.play()
|
||
}
|
||
} catch (err) {
|
||
cameraError.value = 'Unable to access camera'
|
||
cameraStream.value = null
|
||
}
|
||
}
|
||
|
||
const takePhoto = async () => {
|
||
if (!cameraVideo.value) return
|
||
const canvas = document.createElement('canvas')
|
||
canvas.width = cameraVideo.value.videoWidth
|
||
canvas.height = cameraVideo.value.videoHeight
|
||
const ctx = canvas.getContext('2d')
|
||
if (ctx) {
|
||
ctx.drawImage(cameraVideo.value, 0, 0, canvas.width, canvas.height)
|
||
const dataUrl = canvas.toDataURL('image/png')
|
||
capturedImageUrl.value = dataUrl
|
||
}
|
||
}
|
||
|
||
const confirmPhoto = async () => {
|
||
if (capturedImageUrl.value) {
|
||
if (localImageUrl.value) URL.revokeObjectURL(localImageUrl.value)
|
||
// Convert dataURL to Blob
|
||
const res = await fetch(capturedImageUrl.value)
|
||
const originalBlob = await res.blob()
|
||
const { blob, url } = await resizeImageFile(originalBlob, 512)
|
||
localImageUrl.value = url
|
||
cameraFile.value = new File([blob], 'camera.png', { type: 'image/png' })
|
||
updateLocalImage(url, cameraFile.value)
|
||
}
|
||
closeCamera()
|
||
}
|
||
|
||
const retakePhoto = async () => {
|
||
capturedImageUrl.value = null
|
||
cameraFile.value = null
|
||
await resumeCameraStream()
|
||
}
|
||
|
||
const closeCamera = () => {
|
||
showCamera.value = false
|
||
capturedImageUrl.value = null
|
||
if (cameraStream.value) {
|
||
cameraStream.value.getTracks().forEach((track) => track.stop())
|
||
cameraStream.value = null
|
||
}
|
||
if (cameraVideo.value) {
|
||
cameraVideo.value.srcObject = null
|
||
}
|
||
}
|
||
|
||
const resumeCameraStream = async () => {
|
||
await nextTick()
|
||
if (cameraVideo.value && cameraStream.value) {
|
||
cameraVideo.value.srcObject = cameraStream.value
|
||
try {
|
||
await cameraVideo.value.play()
|
||
} catch (e) {}
|
||
}
|
||
}
|
||
|
||
// Fetch images on mount
|
||
onMounted(async () => {
|
||
loadingImages.value = true
|
||
try {
|
||
const resp = await fetch(`/api/image/list?type=${typeParam.value}`)
|
||
if (resp.ok) {
|
||
const data = await resp.json()
|
||
const ids = data.ids || []
|
||
// Fetch URLs for each image id using the cache
|
||
const urls = await Promise.all(
|
||
ids.map(async (id: string) => {
|
||
try {
|
||
const url = await getCachedImageUrl(id)
|
||
return { id, url }
|
||
} catch {
|
||
return null
|
||
}
|
||
}),
|
||
)
|
||
const images = urls.filter(Boolean) as { id: string; url: string }[]
|
||
// Move the selected image to the front if it exists
|
||
if (props.modelValue) {
|
||
const idx = images.findIndex((img) => img.id === props.modelValue)
|
||
if (idx > 0) {
|
||
const [selected] = images.splice(idx, 1)
|
||
images.unshift(selected)
|
||
}
|
||
}
|
||
availableImages.value = images
|
||
}
|
||
} catch (err) {
|
||
// Optionally handle error
|
||
} finally {
|
||
loadingImages.value = false
|
||
}
|
||
})
|
||
|
||
async function resizeImageFile(
|
||
file: File | Blob,
|
||
maxDim = 512,
|
||
): Promise<{ blob: Blob; url: string }> {
|
||
const img = new window.Image()
|
||
const url = URL.createObjectURL(file)
|
||
img.src = url
|
||
await new Promise((resolve) => {
|
||
img.onload = resolve
|
||
})
|
||
|
||
let { width, height } = img
|
||
if (width > maxDim || height > maxDim) {
|
||
if (width > height) {
|
||
height = Math.round((height * maxDim) / width)
|
||
width = maxDim
|
||
} else {
|
||
width = Math.round((width * maxDim) / height)
|
||
height = maxDim
|
||
}
|
||
}
|
||
const canvas = document.createElement('canvas')
|
||
canvas.width = width
|
||
canvas.height = height
|
||
const ctx = canvas.getContext('2d')
|
||
ctx?.drawImage(img, 0, 0, width, height)
|
||
const blob: Blob = await new Promise((resolve) => canvas.toBlob((b) => resolve(b!), 'image/png'))
|
||
URL.revokeObjectURL(url)
|
||
return { blob, url: URL.createObjectURL(blob) }
|
||
}
|
||
|
||
function updateLocalImage(url: string, file: File) {
|
||
const idx = availableImages.value.findIndex((img) => img.id === 'local-upload')
|
||
if (idx === -1) {
|
||
availableImages.value.unshift({ id: 'local-upload', url })
|
||
} else {
|
||
availableImages.value[idx].url = url
|
||
}
|
||
emit('add-image', { id: 'local-upload', url, file })
|
||
emit('update:modelValue', 'local-upload')
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div>
|
||
<div class="image-scroll">
|
||
<div v-if="loadingImages" class="loading-images">Loading images...</div>
|
||
<div v-else class="image-list">
|
||
<img
|
||
v-for="img in availableImages"
|
||
:key="img.id"
|
||
:src="img.url"
|
||
class="selectable-image"
|
||
:class="{ selected: modelValue === img.id }"
|
||
:alt="`Image ${img.id}`"
|
||
@click="selectImage(img.id)"
|
||
/>
|
||
</div>
|
||
</div>
|
||
<input
|
||
ref="fileInput"
|
||
type="file"
|
||
accept=".png,.jpg,.jpeg,.gif,image/png,image/jpeg,image/gif"
|
||
capture="environment"
|
||
style="display: none"
|
||
@change="onFileChange"
|
||
/>
|
||
<div class="image-actions">
|
||
<button type="button" class="icon-btn" @click="addFromLocal" aria-label="Add from device">
|
||
<span class="icon">+</span>
|
||
</button>
|
||
<button type="button" class="icon-btn" @click="addFromCamera" aria-label="Add from camera">
|
||
<span class="icon">
|
||
<svg width="20" height="20" viewBox="0 0 20 20" fill="none">
|
||
<rect x="3" y="6" width="14" height="10" rx="2" stroke="#667eea" stroke-width="1.5" />
|
||
<circle cx="10" cy="11" r="3" stroke="#667eea" stroke-width="1.5" />
|
||
<rect x="7" y="3" width="6" height="3" rx="1" stroke="#667eea" stroke-width="1.5" />
|
||
</svg>
|
||
</span>
|
||
</button>
|
||
</div>
|
||
|
||
<!-- Camera modal -->
|
||
<div v-if="showCamera" class="modal-backdrop">
|
||
<div class="modal camera-modal">
|
||
<h3>Take a photo</h3>
|
||
<div v-if="cameraError" class="error">{{ cameraError }}</div>
|
||
<div v-else>
|
||
<div v-if="!capturedImageUrl">
|
||
<video ref="cameraVideo" autoplay playsinline class="camera-display"></video>
|
||
<div class="actions">
|
||
<button type="button" class="btn btn-primary" @click="takePhoto">Capture</button>
|
||
<button type="button" class="btn btn-secondary" @click="closeCamera">Cancel</button>
|
||
</div>
|
||
</div>
|
||
<div v-else>
|
||
<img :src="capturedImageUrl" class="camera-display" alt="Preview" />
|
||
<div class="actions">
|
||
<button type="button" class="btn btn-primary" @click="confirmPhoto">Choose</button>
|
||
<button type="button" class="btn btn-secondary" @click="retakePhoto">Retake</button>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.image-scroll {
|
||
width: 100%;
|
||
margin: 0.7rem 0 0.2rem 0;
|
||
overflow-x: auto;
|
||
overflow-y: hidden;
|
||
padding-bottom: 0.2rem;
|
||
}
|
||
.image-list {
|
||
display: flex;
|
||
gap: 0.7rem;
|
||
min-width: min-content;
|
||
align-items: center;
|
||
}
|
||
.selectable-image {
|
||
width: 64px;
|
||
height: 64px;
|
||
object-fit: cover;
|
||
border-radius: 8px;
|
||
border: 2px solid var(--selectable-image-border);
|
||
background: var(--selectable-image-bg);
|
||
cursor: pointer;
|
||
transition: border 0.18s;
|
||
}
|
||
.selectable-image:hover,
|
||
.selectable-image.selected {
|
||
border-color: var(--selectable-image-selected);
|
||
box-shadow: 0 0 0 2px #667eea55;
|
||
}
|
||
.loading-images {
|
||
color: var(--loading-text);
|
||
font-size: 0.98rem;
|
||
padding: 0.5rem 0;
|
||
text-align: center;
|
||
}
|
||
|
||
.image-actions {
|
||
display: flex;
|
||
gap: 4rem;
|
||
justify-content: center;
|
||
margin-top: 1.2rem; /* Increased space below images */
|
||
}
|
||
|
||
.icon-btn {
|
||
background: var(--icon-btn-bg);
|
||
border: none;
|
||
border-radius: 50%;
|
||
width: 56px; /* Increased size */
|
||
height: 56px; /* Increased size */
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
cursor: pointer;
|
||
transition: background 0.18s;
|
||
font-size: 2.2rem; /* Bigger + icon */
|
||
color: var(--icon-btn-color);
|
||
box-shadow: var(--icon-btn-shadow);
|
||
}
|
||
.icon-btn svg {
|
||
width: 32px; /* Bigger camera icon */
|
||
height: 32px;
|
||
}
|
||
|
||
.icon {
|
||
display: flex;
|
||
align-items: center;
|
||
justify-content: center;
|
||
}
|
||
|
||
/* Camera modal styles */
|
||
.camera-modal {
|
||
position: fixed;
|
||
top: 50%;
|
||
left: 50%;
|
||
transform: translate(-50%, -50%);
|
||
background: var(--modal-bg);
|
||
border-radius: 12px;
|
||
box-shadow: var(--modal-shadow);
|
||
z-index: 1300;
|
||
width: 380px;
|
||
max-width: calc(100vw - 32px);
|
||
padding-bottom: 1.5rem;
|
||
text-align: center;
|
||
}
|
||
|
||
.camera-display {
|
||
width: auto;
|
||
max-width: 100%;
|
||
max-height: 240px;
|
||
border-radius: 12px;
|
||
display: block;
|
||
margin-left: auto;
|
||
margin-right: auto;
|
||
object-fit: contain;
|
||
}
|
||
</style>
|