227 lines
7.3 KiB
Vue
227 lines
7.3 KiB
Vue
<script setup>
|
||
// The small card a note shows when the pointer rests on a `[[…]]` link to a character or an item.
|
||
// It only reads what the sidebar lists already answer with; pressing the card's button opens the
|
||
// full sheet window, the same one clicking the sidebar row opens.
|
||
import { computed } from 'vue';
|
||
import { useI18n } from 'vue-i18n';
|
||
import { getBaseUrl } from '~/services/Server';
|
||
|
||
const props = defineProps(['preview']);
|
||
const emit = defineEmits(['enter', 'leave', 'close', 'open']);
|
||
const { t } = useI18n();
|
||
|
||
const target = computed(() => props.preview?.target ?? {});
|
||
const entry = computed(() => props.preview?.entry ?? {});
|
||
const isActor = computed(() => target.value.type === 'actor');
|
||
const isItem = computed(() => target.value.type === 'item');
|
||
|
||
const position = computed(() => ({
|
||
left: `${props.preview?.x ?? 0}px`,
|
||
top: `${props.preview?.y ?? 0}px`
|
||
}));
|
||
|
||
// Uploaded images are served by `/api/public`; a synced world hands over full URLs.
|
||
function resolveImage(image) {
|
||
if (!image) return '';
|
||
return /^(https?:|data:|\/\/)/i.test(image)
|
||
? image
|
||
: `${getBaseUrl()}/public/${image}`;
|
||
}
|
||
|
||
const imageUrl = computed(() => resolveImage(entry.value.image));
|
||
const initial = computed(() => String(entry.value.name ?? '?').slice(0, 1).toUpperCase());
|
||
|
||
const actorLine = computed(() => {
|
||
const parts = [];
|
||
if (entry.value.className) parts.push(entry.value.className);
|
||
if (entry.value.species) parts.push(entry.value.species);
|
||
if (entry.value.level) parts.push(`${t('campaigns.character-sheet.level')} ${entry.value.level}`);
|
||
return parts.join(' · ');
|
||
});
|
||
|
||
const itemLine = computed(() => {
|
||
const parts = [];
|
||
if (entry.value.category) parts.push(t(`campaigns.item.categories.${entry.value.category}`));
|
||
if (entry.value.rarity) parts.push(t(`campaigns.item.rarities.${entry.value.rarity}`));
|
||
return parts.join(' · ');
|
||
});
|
||
|
||
const hitPoints = computed(() => {
|
||
const hp = entry.value.hitPoints;
|
||
if (!hp) return '';
|
||
return `${Number(hp.current ?? 0)} / ${Number(hp.max ?? 0)}`;
|
||
});
|
||
|
||
const description = computed(() => String(entry.value.description ?? '').trim());
|
||
|
||
const openLabel = computed(() => t(isActor.value
|
||
? 'campaigns.link-preview.open-character'
|
||
: 'campaigns.link-preview.open-item'));
|
||
</script>
|
||
|
||
<template>
|
||
<Teleport to="body">
|
||
<div
|
||
class="link-preview"
|
||
:style="position"
|
||
role="dialog"
|
||
:aria-label="entry.name"
|
||
@mouseenter="emit('enter')"
|
||
@mouseleave="emit('leave')"
|
||
>
|
||
<div class="preview-head">
|
||
<div class="preview-mark" :class="{ empty: !imageUrl }">
|
||
<img v-if="imageUrl" :src="imageUrl" :alt="''">
|
||
<span v-else aria-hidden="true">{{ initial }}</span>
|
||
</div>
|
||
<div class="preview-identity">
|
||
<strong class="preview-name">{{ entry.name }}</strong>
|
||
<span class="preview-line">{{ isActor ? actorLine : itemLine }}</span>
|
||
</div>
|
||
<button class="preview-close" type="button" :aria-label="$t('campaigns.link-preview.close')" @click="emit('close')">×</button>
|
||
</div>
|
||
|
||
<div v-if="isActor" class="preview-stats">
|
||
<div class="preview-stat">
|
||
<span>{{ $t('campaigns.character-sheet.armor-class') }}</span>
|
||
<strong>{{ entry.armorClass ?? '—' }}</strong>
|
||
</div>
|
||
<div class="preview-stat">
|
||
<span>{{ $t('campaigns.character-sheet.hit-points') }}</span>
|
||
<strong>{{ hitPoints || '—' }}</strong>
|
||
</div>
|
||
<div class="preview-stat">
|
||
<span>{{ $t('campaigns.character-sheet.speed') }}</span>
|
||
<strong>{{ entry.speed ?? '—' }}</strong>
|
||
</div>
|
||
</div>
|
||
|
||
<template v-else-if="isItem">
|
||
<p class="preview-copy">{{ description || $t('campaigns.link-preview.no-description') }}</p>
|
||
<p v-if="entry.weight !== undefined && entry.weight !== ''" class="preview-weight">
|
||
{{ $t('campaigns.item.weight') }}: {{ Number(entry.weight).toFixed(1) }}
|
||
</p>
|
||
</template>
|
||
|
||
<button class="preview-open" type="button" @click="emit('open')">{{ openLabel }}</button>
|
||
</div>
|
||
</Teleport>
|
||
</template>
|
||
|
||
<style scoped>
|
||
/* The card floats above the notes, which scroll under it, so it is fixed to the viewport and
|
||
positioned from the link's rectangle by Note.vue. */
|
||
.link-preview {
|
||
position: fixed;
|
||
z-index: 4000;
|
||
width: 320px;
|
||
max-height: min(70vh, 420px);
|
||
overflow: auto;
|
||
padding: 13px 14px 12px;
|
||
box-sizing: border-box;
|
||
border: 1px solid var(--color-border);
|
||
border-radius: 10px;
|
||
background: var(--color-background-light);
|
||
box-shadow: 0 10px 30px var(--color-window-shadow);
|
||
color: var(--color-text);
|
||
font-size: 13px;
|
||
}
|
||
|
||
.preview-head {
|
||
display: flex;
|
||
align-items: center;
|
||
gap: 10px;
|
||
padding-bottom: 10px;
|
||
margin-bottom: 10px;
|
||
border-bottom: 1px solid var(--color-border);
|
||
}
|
||
|
||
.preview-mark {
|
||
width: 44px;
|
||
height: 44px;
|
||
flex: 0 0 44px;
|
||
display: grid;
|
||
place-items: center;
|
||
overflow: hidden;
|
||
border: 1px solid var(--color-border);
|
||
border-radius: 9px;
|
||
background: var(--color-background-soft);
|
||
color: var(--color-link);
|
||
font: 21px MrEavesRemake;
|
||
}
|
||
|
||
.preview-mark.empty { border-style: dashed; }
|
||
.preview-mark img { width: 100%; height: 100%; object-fit: contain; }
|
||
|
||
.preview-identity { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 3px; }
|
||
|
||
.preview-name {
|
||
font: 18px MrEavesRemake;
|
||
letter-spacing: .02em;
|
||
overflow-wrap: anywhere;
|
||
}
|
||
|
||
.preview-line { font-size: 11px; opacity: .72; overflow-wrap: anywhere; }
|
||
|
||
.preview-close {
|
||
flex: 0 0 auto;
|
||
width: 22px;
|
||
height: 22px;
|
||
padding: 0;
|
||
border: 0;
|
||
border-radius: 5px;
|
||
background: transparent;
|
||
color: var(--color-text);
|
||
font-size: 18px;
|
||
line-height: 1;
|
||
cursor: pointer;
|
||
}
|
||
|
||
.preview-close:hover { background: var(--color-button-hover); }
|
||
|
||
.preview-stats { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 7px; }
|
||
|
||
.preview-stat {
|
||
display: flex;
|
||
flex-direction: column;
|
||
align-items: center;
|
||
gap: 3px;
|
||
padding: 7px 4px;
|
||
border: 1px solid var(--color-border);
|
||
border-radius: 7px;
|
||
background: var(--color-background-soft);
|
||
text-align: center;
|
||
}
|
||
|
||
.preview-stat span { font-size: 9px; text-transform: uppercase; letter-spacing: .05em; opacity: .68; }
|
||
.preview-stat strong { font: 15px MrEavesRemake; white-space: nowrap; }
|
||
|
||
.preview-copy {
|
||
margin: 0;
|
||
display: -webkit-box;
|
||
-webkit-line-clamp: 5;
|
||
-webkit-box-orient: vertical;
|
||
overflow: hidden;
|
||
white-space: pre-wrap;
|
||
overflow-wrap: anywhere;
|
||
line-height: 1.45;
|
||
opacity: .85;
|
||
}
|
||
|
||
.preview-weight { margin: 7px 0 0; font-size: 11px; opacity: .68; }
|
||
|
||
.preview-open {
|
||
width: 100%;
|
||
margin-top: 11px;
|
||
padding: 7px 10px;
|
||
border: 1px solid var(--color-link);
|
||
border-radius: 7px;
|
||
background: transparent;
|
||
color: var(--color-link);
|
||
font: inherit;
|
||
font-size: 12px;
|
||
cursor: pointer;
|
||
}
|
||
|
||
.preview-open:hover { background: var(--color-button-hover); }
|
||
</style> |