Fix issue #65: Item selecting icon
Some checks failed
Test / test (push) Failing after 2s

This commit is contained in:
Gitea OpenCode Agent
2026-10-15 16:01:36 +00:00
parent cdafd1f53a
commit b47bbb4cab
20 changed files with 823 additions and 59 deletions

View File

@@ -1,9 +1,10 @@
<script setup>
import { computed, onBeforeUnmount, onMounted, ref } from 'vue';
import { useI18n } from 'vue-i18n';
import { SetupHandle, SetSize, ResetPosition, ClearWindow, Top } from '@/services/Windows';
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']);
@@ -12,9 +13,10 @@ 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);
// 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) {
@@ -44,9 +46,10 @@ onMounted(() => {
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.
// 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 || uploadingIcon.value) return;
if (entityType !== 'item' || entry?._id !== itemId || applyingIcon.value) return;
form.value = normalizeItem(entry);
}
@@ -70,40 +73,22 @@ async function updateItem(payload) {
return true;
}
function pickIcon() {
photoInput.value?.click();
// 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 || ''
});
}
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.
// 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 || applyingFoundryIcon.value) return;
applyingFoundryIcon.value = true;
if (!canUseFoundryIcon.value || applyingIcon.value) return;
applyingIcon.value = true;
error.value = '';
try {
const done = await updateItem({ image: form.value.foundryImage });
@@ -111,18 +96,21 @@ async function useFoundryIcon() {
} catch (requestError) {
error.value = t('campaigns.item.sheet.save-error');
} finally {
applyingFoundryIcon.value = false;
applyingIcon.value = false;
}
}
async function removeIcon() {
if (!form.value.image) return;
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>
@@ -132,10 +120,12 @@ async function removeIcon() {
<WindowHandle :window="id" ref="handle" />
<div class="item-body">
<header class="item-hero">
<div class="item-icon" :class="{ empty: !form.image }">
<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>
</div>
</button>
<div class="item-identity">
<h1>{{ form.name }}</h1>
<div class="item-tags">
@@ -149,16 +139,13 @@ async function removeIcon() {
<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 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="applyingFoundryIcon" @click="useFoundryIcon">
<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="uploadingIcon" :aria-label="$t('campaigns.item.sheet.remove-icon')" @click="removeIcon">×</button>
<input ref="photoInput" class="photo-input" type="file" accept="image/*" @change="onIconSelected">
<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">
@@ -189,7 +176,8 @@ async function removeIcon() {
.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 { 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; }
@@ -202,7 +190,6 @@ async function removeIcon() {
.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; }