211 lines
9.9 KiB
Vue
211 lines
9.9 KiB
Vue
<script setup>
|
||
import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
|
||
import { useI18n } from 'vue-i18n';
|
||
import { SetupHandle, SetSize, ResetPosition, ClearWindow, Top } from '@/services/Windows';
|
||
import WindowHandle from './partials/WindowHandle.vue';
|
||
import Server, { getBaseUrl } from '~/services/Server';
|
||
import { emitter } from '~/services/Emitter';
|
||
|
||
const props = defineProps(['data']);
|
||
const id = props.data.id;
|
||
const itemId = props.data.item?._id;
|
||
const handle = ref(null);
|
||
const wrapper = ref(null);
|
||
const error = ref('');
|
||
const uploadingIcon = ref(false);
|
||
const applyingFoundryIcon = ref(false);
|
||
const photoInput = ref(null);
|
||
const { t } = useI18n();
|
||
|
||
function normalizeItem(item) {
|
||
return {
|
||
name: item?.name ?? '',
|
||
category: item?.category ?? 'adventuring-gear',
|
||
rarity: item?.rarity ?? 'common',
|
||
weight: item?.weight ?? '',
|
||
properties: item?.properties ?? '',
|
||
description: item?.description ?? '',
|
||
image: item?.image ?? '',
|
||
foundryItemId: item?.foundryItemId ?? '',
|
||
foundryImage: item?.foundryImage ?? ''
|
||
};
|
||
}
|
||
|
||
const form = ref(normalizeItem(props.data.item));
|
||
|
||
onMounted(() => {
|
||
Top(wrapper);
|
||
SetupHandle(id, handle);
|
||
SetSize(id, { width: 460, height: 560 });
|
||
ResetPosition(id, 'center');
|
||
});
|
||
|
||
onBeforeUnmount(() => ClearWindow({ type: 'item_sheet' }));
|
||
|
||
// The sidebar keeps this entry fresh (Foundry sync lands in the background);
|
||
// follow along unless the window is mid-icon-upload.
|
||
function handleItemSync({ entityType, entry }) {
|
||
if (entityType !== 'item' || entry?._id !== itemId || uploadingIcon.value) return;
|
||
form.value = normalizeItem(entry);
|
||
}
|
||
|
||
onMounted(() => emitter.on('campaign-entry-updated', handleItemSync));
|
||
onBeforeUnmount(() => emitter.off('campaign-entry-updated', handleItemSync));
|
||
|
||
const imageUrl = computed(() => {
|
||
if (!form.value.image) return '';
|
||
return /^(https?:|data:|\/\/)/i.test(form.value.image)
|
||
? form.value.image
|
||
: `${getBaseUrl()}/public/${form.value.image}`;
|
||
});
|
||
|
||
const canUseFoundryIcon = computed(() => Boolean(form.value.foundryImage) && form.value.foundryImage !== form.value.image);
|
||
|
||
async function updateItem(payload) {
|
||
const response = await Server().post('/item/update', { id: itemId, ...payload });
|
||
if (response.data.status !== 'ok') return false;
|
||
form.value = normalizeItem(response.data.item);
|
||
emitter.emit('campaign-entry-updated', { entityType: 'item', entry: response.data.item });
|
||
return true;
|
||
}
|
||
|
||
function pickIcon() {
|
||
photoInput.value?.click();
|
||
}
|
||
|
||
async function onIconSelected(event) {
|
||
const file = event.target.files?.[0];
|
||
event.target.value = '';
|
||
if (!file || uploadingIcon.value) return;
|
||
uploadingIcon.value = true;
|
||
error.value = '';
|
||
try {
|
||
const formData = new FormData();
|
||
formData.append('image', file);
|
||
const response = await Server().post(`/item/upload-image?item=${itemId}`, formData, {
|
||
headers: { "Content-Type": "multipart/form-data" }
|
||
});
|
||
if (response.data.status !== 'ok') {
|
||
error.value = t('campaigns.item.sheet.upload-error');
|
||
return;
|
||
}
|
||
form.value = normalizeItem(response.data.item);
|
||
emitter.emit('campaign-entry-updated', { entityType: 'item', entry: response.data.item });
|
||
} catch (requestError) {
|
||
error.value = t('campaigns.item.sheet.upload-error');
|
||
} finally {
|
||
uploadingIcon.value = false;
|
||
}
|
||
}
|
||
|
||
// "Fetch from Foundry's icons": the module keeps the world's own icon on the
|
||
// item, so this just switches back to it.
|
||
async function useFoundryIcon() {
|
||
if (!canUseFoundryIcon.value || applyingFoundryIcon.value) return;
|
||
applyingFoundryIcon.value = true;
|
||
error.value = '';
|
||
try {
|
||
const done = await updateItem({ image: form.value.foundryImage });
|
||
if (!done) error.value = t('campaigns.item.sheet.save-error');
|
||
} catch (requestError) {
|
||
error.value = t('campaigns.item.sheet.save-error');
|
||
} finally {
|
||
applyingFoundryIcon.value = false;
|
||
}
|
||
}
|
||
|
||
async function removeIcon() {
|
||
if (!form.value.image) return;
|
||
error.value = '';
|
||
try {
|
||
const done = await updateItem({ image: '' });
|
||
if (!done) error.value = t('campaigns.item.sheet.save-error');
|
||
} catch (requestError) {
|
||
error.value = t('campaigns.item.sheet.save-error');
|
||
}
|
||
}
|
||
</script>
|
||
|
||
<template>
|
||
<div class="window-wrapper item-window" :id="'window-wrapper-' + id" ref="wrapper">
|
||
<WindowHandle :window="id" ref="handle" />
|
||
<div class="item-body">
|
||
<header class="item-hero">
|
||
<div class="item-icon" :class="{ empty: !form.image }">
|
||
<img v-if="form.image" :src="imageUrl" :alt="$t('campaigns.item.sheet.icon')">
|
||
<span v-else aria-hidden="true">{{ form.name.slice(0, 1).toUpperCase() }}</span>
|
||
</div>
|
||
<div class="item-identity">
|
||
<h1>{{ form.name }}</h1>
|
||
<div class="item-tags">
|
||
<span class="item-tag">{{ $t(`campaigns.item.categories.${form.category}`) }}</span>
|
||
<span class="item-tag">{{ $t(`campaigns.item.rarities.${form.rarity}`) }}</span>
|
||
<span v-if="form.foundryItemId" class="item-tag synced">{{ $t('campaigns.synced') }}</span>
|
||
</div>
|
||
</div>
|
||
</header>
|
||
|
||
<div v-if="error" class="item-error">{{ error }}</div>
|
||
|
||
<div class="icon-controls">
|
||
<button class="sheet-button" type="button" :disabled="uploadingIcon" @click="pickIcon">
|
||
{{ uploadingIcon
|
||
? $t('campaigns.item.sheet.uploading')
|
||
: (form.image ? $t('campaigns.item.sheet.change-icon') : $t('campaigns.item.sheet.choose-icon')) }}
|
||
</button>
|
||
<button v-if="canUseFoundryIcon" class="sheet-button secondary" type="button" :disabled="applyingFoundryIcon" @click="useFoundryIcon">
|
||
{{ $t('campaigns.item.sheet.foundry-icon') }}
|
||
</button>
|
||
<button v-if="form.image" class="sheet-button secondary" type="button" :disabled="uploadingIcon" :aria-label="$t('campaigns.item.sheet.remove-icon')" @click="removeIcon">×</button>
|
||
<input ref="photoInput" class="photo-input" type="file" accept="image/*" @change="onIconSelected">
|
||
</div>
|
||
|
||
<!-- Quantity is the actor inventory's business (Syndria98/dragonroll#63); the item
|
||
sheet only states what the item itself is. -->
|
||
<dl class="item-facts">
|
||
<div v-if="form.weight !== '' && form.weight !== undefined" class="item-fact">
|
||
<dt>{{ $t('campaigns.item.weight') }}</dt>
|
||
<dd>{{ Number(form.weight).toFixed(1) }} lb</dd>
|
||
</div>
|
||
</dl>
|
||
|
||
<section v-if="form.properties" class="item-section">
|
||
<h2>{{ $t('campaigns.item.properties') }}</h2>
|
||
<p class="item-copy">{{ form.properties }}</p>
|
||
</section>
|
||
|
||
<section class="item-section">
|
||
<h2>{{ $t('campaigns.fields.description') }}</h2>
|
||
<p class="item-copy">{{ form.description || $t('campaigns.item.sheet.no-description') }}</p>
|
||
</section>
|
||
</div>
|
||
</div>
|
||
</template>
|
||
|
||
<style scoped>
|
||
.item-window { min-width: 0; min-height: 0; }
|
||
.item-body { width: 100%; min-height: 0; flex: 1; overflow: auto; padding: 14px 16px 16px; box-sizing: border-box; }
|
||
.item-hero { display: flex; align-items: center; gap: 13px; padding: 2px 2px 13px; margin-bottom: 11px; border-bottom: 1px solid var(--color-border); }
|
||
.item-icon { width: 64px; height: 64px; flex: 0 0 64px; display: grid; place-items: center; overflow: hidden; border-radius: 10px; border: 1px solid var(--color-border); background: var(--color-background-soft); color: var(--color-link); font: 30px MrEavesRemake; }
|
||
.item-icon.empty { border-style: dashed; }
|
||
.item-icon img { width: 100%; height: 100%; object-fit: contain; }
|
||
.item-identity { min-width: 0; }
|
||
.item-identity h1 { margin: 0 0 6px; font: 24px MrEavesRemake; overflow-wrap: anywhere; }
|
||
.item-tags { display: flex; align-items: center; flex-wrap: wrap; gap: 6px; }
|
||
.item-tag { padding: 3px 9px; border: 1px solid var(--color-border); border-radius: 999px; background: var(--color-background-soft); font-size: 12px; opacity: .9; }
|
||
.item-tag.synced { font-size: 10px; letter-spacing: .04em; text-transform: uppercase; opacity: .6; }
|
||
.item-error { padding: 7px 10px; margin-bottom: 10px; border-radius: 6px; color: var(--color-red); background: var(--color-background-soft); font-size: 12px; }
|
||
.icon-controls { display: flex; align-items: center; flex-wrap: wrap; gap: 7px; margin-bottom: 13px; }
|
||
.sheet-button { border: 1px solid var(--color-border); border-radius: 7px; padding: 7px 12px; background: var(--color-background-soft); color: var(--color-text); cursor: pointer; white-space: nowrap; font-size: 13px; }
|
||
.sheet-button:hover { background: var(--color-button-hover); }
|
||
.sheet-button:disabled { opacity: .55; cursor: wait; }
|
||
.photo-input { display: none; }
|
||
.item-facts { display: flex; flex-wrap: wrap; gap: 8px; margin: 0 0 13px; }
|
||
.item-fact { flex: 1 1 120px; min-width: 0; padding: 9px 11px; border: 1px solid var(--color-border); border-radius: 8px; background: var(--color-background-soft); }
|
||
.item-fact dt { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; opacity: .72; }
|
||
.item-fact dd { margin: 3px 0 0; font: 17px MrEavesRemake; }
|
||
.item-section { padding: 11px 13px; margin: 0 0 10px; border: 1px solid var(--color-border); border-radius: 10px; background: var(--color-background-light); }
|
||
.item-section h2 { margin: 0 0 8px; padding-bottom: 6px; border-bottom: 1px solid var(--color-border); font: 15px MrEavesRemake; letter-spacing: .03em; }
|
||
.item-copy { margin: 0; white-space: pre-wrap; overflow-wrap: anywhere; font-size: 13px; line-height: 1.45; opacity: .85; }
|
||
</style>
|