This commit is contained in:
128
frontend/app/components/windows/ItemIconSourceWindow.vue
Normal file
128
frontend/app/components/windows/ItemIconSourceWindow.vue
Normal file
@@ -0,0 +1,128 @@
|
||||
<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 { emitter } from '~/services/Emitter';
|
||||
|
||||
const props = defineProps(['data']);
|
||||
const id = props.data.id;
|
||||
const item = props.data.item || {};
|
||||
const itemId = item._id;
|
||||
const campaign = props.data.campaign || '';
|
||||
const handle = ref(null);
|
||||
const wrapper = ref(null);
|
||||
const error = ref('');
|
||||
const uploading = ref(false);
|
||||
const photoInput = ref(null);
|
||||
const image = ref(item.image || '');
|
||||
const { t } = useI18n();
|
||||
|
||||
onMounted(() => {
|
||||
Top(wrapper);
|
||||
SetupHandle(id, handle);
|
||||
SetSize(id, { width: 430, height: 320 });
|
||||
ResetPosition(id, 'center');
|
||||
});
|
||||
|
||||
onBeforeUnmount(() => ClearWindow({ type: 'item_icon_source' }));
|
||||
|
||||
// An uploaded icon is a file of the backend's uploads folder and a Foundry one an
|
||||
// absolute url of the world; both are drawn the way the item window draws them.
|
||||
const imageUrl = computed(() => {
|
||||
if (!image.value) return '';
|
||||
return /^(https?:|data:|\/\/)/i.test(image.value)
|
||||
? image.value
|
||||
: `${getBaseUrl()}/public/${image.value}`;
|
||||
});
|
||||
|
||||
function pickLocalIcon() {
|
||||
photoInput.value?.click();
|
||||
}
|
||||
|
||||
// The icon browser opens on top of this window, as its child: closing the chooser
|
||||
// takes the browser with it, like every other child window here.
|
||||
function openFoundryIcons() {
|
||||
CreateChildWindow(id, 'foundry_icons', { item: { _id: itemId, name: item.name }, campaign });
|
||||
}
|
||||
|
||||
async function onIconSelected(event) {
|
||||
const file = event.target.files?.[0];
|
||||
event.target.value = '';
|
||||
if (!file || uploading.value) return;
|
||||
uploading.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;
|
||||
}
|
||||
// The item window follows this event and shows the new icon.
|
||||
emitter.emit('campaign-entry-updated', { entityType: 'item', entry: response.data.item });
|
||||
ClearWindow({ id });
|
||||
} catch (requestError) {
|
||||
error.value = t('campaigns.item.sheet.upload-error');
|
||||
} finally {
|
||||
uploading.value = false;
|
||||
}
|
||||
}
|
||||
</script>
|
||||
|
||||
<template>
|
||||
<div class="window-wrapper icon-source-window" :id="'window-wrapper-' + id" ref="wrapper">
|
||||
<WindowHandle :window="id" ref="handle" />
|
||||
<div class="source-body">
|
||||
<header class="source-hero">
|
||||
<div class="source-icon" :class="{ empty: !image }">
|
||||
<img v-if="imageUrl" :src="imageUrl" :alt="$t('campaigns.item.sheet.icon')">
|
||||
<span v-else aria-hidden="true">{{ (item.name || '?').slice(0, 1).toUpperCase() }}</span>
|
||||
</div>
|
||||
<p class="source-intro">{{ $t('campaigns.item.icon-picker.intro') }}</p>
|
||||
</header>
|
||||
|
||||
<div v-if="error" class="source-error">{{ error }}</div>
|
||||
|
||||
<div class="source-options">
|
||||
<button class="source-option" type="button" :disabled="uploading" @click="pickLocalIcon">
|
||||
<img class="icon" src="/icons/iconoir/regular/cloud-upload.svg" alt="" aria-hidden="true">
|
||||
<span class="source-option-title">{{ uploading
|
||||
? $t('campaigns.item.sheet.uploading')
|
||||
: $t('campaigns.item.icon-picker.upload') }}</span>
|
||||
<span class="source-option-hint">{{ $t('campaigns.item.icon-picker.upload-hint') }}</span>
|
||||
</button>
|
||||
<button class="source-option" type="button" @click="openFoundryIcons">
|
||||
<img class="icon" src="/icons/iconoir/regular/media-image.svg" alt="" aria-hidden="true">
|
||||
<span class="source-option-title">{{ $t('campaigns.item.icon-picker.foundry') }}</span>
|
||||
<span class="source-option-hint">{{ $t('campaigns.item.icon-picker.foundry-hint') }}</span>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<input ref="photoInput" class="photo-input" type="file" accept="image/*" @change="onIconSelected">
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
<style scoped>
|
||||
.icon-source-window { min-width: 0; min-height: 0; }
|
||||
.source-body { width: 100%; min-height: 0; flex: 1; overflow: auto; padding: 14px 16px 16px; box-sizing: border-box; display: flex; flex-direction: column; gap: 12px; }
|
||||
.source-hero { display: flex; align-items: center; gap: 13px; padding-bottom: 12px; border-bottom: 1px solid var(--color-border); }
|
||||
.source-icon { width: 56px; height: 56px; flex: 0 0 56px; 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: 26px MrEavesRemake; }
|
||||
.source-icon.empty { border-style: dashed; }
|
||||
.source-icon img { width: 100%; height: 100%; object-fit: contain; }
|
||||
.source-intro { margin: 0; font-size: 13px; opacity: .85; }
|
||||
.source-error { padding: 7px 10px; border-radius: 6px; color: var(--color-red); background: var(--color-background-soft); font-size: 12px; }
|
||||
.source-options { display: flex; flex-direction: column; gap: 10px; }
|
||||
.source-option { display: grid; grid-template-columns: 26px 1fr; align-items: center; gap: 4px 10px; padding: 12px 14px; text-align: left; border: 1px solid var(--color-border); border-radius: 10px; background: var(--color-background-soft); color: var(--color-text); cursor: pointer; }
|
||||
.source-option:hover { background: var(--color-button-hover); }
|
||||
.source-option:disabled { opacity: .55; cursor: wait; }
|
||||
.source-option .icon { width: 20px; height: 20px; grid-row: span 2; }
|
||||
.source-option-title { font: 15px MrEavesRemake; }
|
||||
.source-option-hint { font-size: 12px; opacity: .7; }
|
||||
.photo-input { display: none; }
|
||||
</style>
|
||||
Reference in New Issue
Block a user