201 lines
9.8 KiB
Vue
201 lines
9.8 KiB
Vue
<script setup>
|
||
import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
|
||
import { useI18n } from 'vue-i18n';
|
||
import { SetupHandle, SetSize, ResetPosition, ClearWindow, Top, CreateChildWindow } from '@/services/Windows';
|
||
import WindowHandle from './partials/WindowHandle.vue';
|
||
import Server, { getBaseUrl } from '~/services/Server';
|
||
import { useCampaignService } from '~/services/Campaign.js';
|
||
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('');
|
||
// Set while this window writes the item's icon, so a Foundry sync landing behind
|
||
// the window does not rewrite the form under the change being made.
|
||
const applyingIcon = ref(false);
|
||
const { Campaign } = useCampaignService();
|
||
const { t } = useI18n();
|
||
|
||
function normalizeItem(item) {
|
||
return {
|
||
name: item?.name ?? '',
|
||
category: item?.category ?? 'adventuring-gear',
|
||
rarity: item?.rarity ?? 'common',
|
||
quantity: Number(item?.quantity ?? 1),
|
||
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 this window is mid-icon-change. The icon windows leave
|
||
// their result here, on the same event.
|
||
function handleItemSync({ entityType, entry }) {
|
||
if (entityType !== 'item' || entry?._id !== itemId || applyingIcon.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;
|
||
}
|
||
|
||
// Choosing an icon is a window of its own: upload one from this device, or browse
|
||
// the icon library of the linked Foundry world. Both leave their result on the
|
||
// `campaign-entry-updated` event, which this window follows.
|
||
function chooseIcon() {
|
||
CreateChildWindow(id, 'item_icon_source', {
|
||
item: { _id: itemId, name: form.value.name, image: form.value.image },
|
||
campaign: Campaign.value?._id || ''
|
||
});
|
||
}
|
||
|
||
// The world's own icon for this item: the module keeps it on the item, so this
|
||
// just switches back to it. Picking one of the world's other icons is what the
|
||
// icon browser is for.
|
||
async function useFoundryIcon() {
|
||
if (!canUseFoundryIcon.value || applyingIcon.value) return;
|
||
applyingIcon.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 {
|
||
applyingIcon.value = false;
|
||
}
|
||
}
|
||
|
||
async function removeIcon() {
|
||
if (!form.value.image || applyingIcon.value) return;
|
||
applyingIcon.value = true;
|
||
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');
|
||
} finally {
|
||
applyingIcon.value = false;
|
||
}
|
||
}
|
||
</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">
|
||
<button class="item-icon" :class="{ empty: !form.image }" type="button"
|
||
:aria-label="$t(form.image ? 'campaigns.item.sheet.change-icon' : 'campaigns.item.sheet.choose-icon')"
|
||
@click="chooseIcon">
|
||
<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>
|
||
</button>
|
||
<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" @click="chooseIcon">
|
||
{{ 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="applyingIcon" @click="useFoundryIcon">
|
||
{{ $t('campaigns.item.sheet.foundry-icon') }}
|
||
</button>
|
||
<button v-if="form.image" class="sheet-button secondary" type="button" :disabled="applyingIcon" :aria-label="$t('campaigns.item.sheet.remove-icon')" @click="removeIcon">×</button>
|
||
</div>
|
||
|
||
<dl class="item-facts">
|
||
<div class="item-fact">
|
||
<dt>{{ $t('campaigns.item.quantity') }}</dt>
|
||
<dd>{{ form.quantity }}</dd>
|
||
</div>
|
||
<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; padding: 0; cursor: pointer; }
|
||
.item-icon:hover { border-color: var(--color-link); }
|
||
.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; }
|
||
.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>
|