200 lines
4.7 KiB
Vue
200 lines
4.7 KiB
Vue
<script setup lang="ts">
|
|
import type { PlaymatSelection } from '~/utils/playmat'
|
|
import { resizeImage } from '~/utils/image'
|
|
|
|
interface CardSummary {
|
|
oracle_id: string
|
|
name: string
|
|
}
|
|
|
|
const props = defineProps<{
|
|
current?: { kind: 'card'; oracleId: string } | { kind: 'upload'; url: string } | null
|
|
busy?: boolean
|
|
}>()
|
|
|
|
const emit = defineEmits<{ select: [selection: PlaymatSelection] }>()
|
|
const { public: { apiBase } } = useRuntimeConfig()
|
|
|
|
const query = ref('')
|
|
const results = ref<CardSummary[]>([])
|
|
const searching = ref(false)
|
|
const localError = ref('')
|
|
let searchTimer: ReturnType<typeof setTimeout> | undefined
|
|
|
|
const previewUrl = computed(() => {
|
|
const current = props.current
|
|
if (!current) return undefined
|
|
return current.kind === 'card'
|
|
? `${apiBase}/cards/${encodeURIComponent(current.oracleId)}/art`
|
|
: current.url
|
|
})
|
|
|
|
async function search() {
|
|
const term = query.value.trim()
|
|
if (term.length < 2) {
|
|
results.value = []
|
|
searching.value = false
|
|
return
|
|
}
|
|
searching.value = true
|
|
try {
|
|
const data = await $fetch<{ cards: CardSummary[] }>(`${apiBase}/cards`, {
|
|
query: { q: term, limit: 24 },
|
|
})
|
|
if (query.value.trim() === term) results.value = data.cards
|
|
} catch {
|
|
if (query.value.trim() === term) results.value = []
|
|
} finally {
|
|
if (query.value.trim() === term) searching.value = false
|
|
}
|
|
}
|
|
|
|
watch(query, () => {
|
|
clearTimeout(searchTimer)
|
|
searchTimer = setTimeout(search, 300)
|
|
})
|
|
|
|
onUnmounted(() => clearTimeout(searchTimer))
|
|
|
|
function choose(card: CardSummary) {
|
|
results.value = []
|
|
query.value = ''
|
|
emit('select', { kind: 'card', oracleId: card.oracle_id })
|
|
}
|
|
|
|
/** The API caps uploaded JPEG playmats at 3 MB, so shrink large images first. */
|
|
async function upload(event: Event) {
|
|
const input = event.target as HTMLInputElement
|
|
const file = input.files?.[0]
|
|
input.value = ''
|
|
if (!file) return
|
|
localError.value = ''
|
|
if (!file.type.startsWith('image/')) {
|
|
localError.value = 'Choose an image file.'
|
|
return
|
|
}
|
|
try {
|
|
const data = await resizeImage(file, 1920)
|
|
if (data.length > 4_000_000) {
|
|
localError.value = 'That image is too large. Try a smaller one.'
|
|
return
|
|
}
|
|
emit('select', { kind: 'upload', data })
|
|
} catch {
|
|
localError.value = 'Could not read that image.'
|
|
}
|
|
}
|
|
</script>
|
|
|
|
<template>
|
|
<div class="playmat-picker">
|
|
<div
|
|
class="preview"
|
|
role="img"
|
|
:aria-label="previewUrl ? 'Current playmat' : 'No playmat selected'"
|
|
:style="previewUrl ? { backgroundImage: `url('${previewUrl}')` } : undefined"
|
|
>
|
|
<span v-if="!previewUrl" class="muted">No playmat</span>
|
|
</div>
|
|
<input
|
|
v-model="query"
|
|
type="search"
|
|
placeholder="Search Magic art…"
|
|
aria-label="Search Magic art"
|
|
>
|
|
<p v-if="busy || searching" class="muted">Working…</p>
|
|
<p v-else-if="query.trim().length >= 2 && !results.length" class="muted">No cards found.</p>
|
|
<div v-if="results.length" class="results">
|
|
<button
|
|
v-for="card in results"
|
|
:key="card.oracle_id"
|
|
type="button"
|
|
:disabled="busy"
|
|
:title="`Use ${card.name} as your playmat`"
|
|
@click="choose(card)"
|
|
>
|
|
<img :src="`${apiBase}/cards/${card.oracle_id}/art`" :alt="card.name" loading="lazy">
|
|
<span>{{ card.name }}</span>
|
|
</button>
|
|
</div>
|
|
<label class="field">
|
|
<span class="muted">Upload an image</span>
|
|
<input type="file" accept="image/*" :disabled="busy" @change="upload">
|
|
</label>
|
|
<p v-if="localError" class="error">{{ localError }}</p>
|
|
<button
|
|
type="button"
|
|
class="remove"
|
|
:disabled="busy || !current"
|
|
@click="emit('select', { kind: 'none' })"
|
|
>Remove playmat</button>
|
|
</div>
|
|
</template>
|
|
|
|
<style scoped>
|
|
.playmat-picker {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 0.6rem;
|
|
}
|
|
|
|
.preview {
|
|
display: flex;
|
|
align-items: center;
|
|
justify-content: center;
|
|
aspect-ratio: 16 / 7;
|
|
max-height: 14rem;
|
|
border: 1px dashed var(--line);
|
|
border-radius: 6px;
|
|
background: var(--panel-2) center / cover no-repeat;
|
|
}
|
|
|
|
.field {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 0.3rem;
|
|
}
|
|
|
|
.field input[type="file"] {
|
|
padding: 0.3rem;
|
|
font-size: 0.8rem;
|
|
}
|
|
|
|
.results {
|
|
display: grid;
|
|
grid-template-columns: repeat(2, minmax(0, 1fr));
|
|
gap: 0.4rem;
|
|
max-height: 18rem;
|
|
overflow-y: auto;
|
|
}
|
|
|
|
.results button {
|
|
display: flex;
|
|
flex-direction: column;
|
|
gap: 0.2rem;
|
|
padding: 0.25rem;
|
|
text-align: left;
|
|
}
|
|
|
|
.results img {
|
|
display: block;
|
|
width: 100%;
|
|
aspect-ratio: 626 / 457;
|
|
border-radius: 4px;
|
|
object-fit: cover;
|
|
}
|
|
|
|
.results span {
|
|
overflow: hidden;
|
|
font-size: 0.72rem;
|
|
text-overflow: ellipsis;
|
|
white-space: nowrap;
|
|
}
|
|
|
|
.remove {
|
|
align-self: flex-start;
|
|
padding: 0.2rem 0.45rem;
|
|
font-size: 0.75rem;
|
|
}
|
|
</style>
|