Files
dragonroll/frontend/app/components/viewer/content/EmbeddedEntry.vue
Aran Roig 1ec29121e1
Some checks failed
Test / test (push) Failing after 2s
Build and Deploy Nuxt / build (push) Successful in 1m51s
Deploy deepseek 2
2026-10-19 19:04:28 +02:00

302 lines
9.9 KiB
Vue

<script setup>
// `![[Name]]` in a note mounts this card where the token was written. It resolves the label against
// the campaign's lists and draws the character's sheet or the item's description inline; the button
// opens the full sheet window, the same one the sidebar uses. Mounted like a widget: its only prop
// is the label the note wrote, and it talks to the services on its own.
import { computed, nextTick, onMounted, ref } from 'vue';
import { getBaseUrl } from '~/services/Server';
import { FindLinkTarget } from '~/services/LinkAutocomplete';
import { LoadLinkEntry, LoadLinkTargets } from '~/services/LinkTargets';
import { useCampaignService } from '~/services/Campaign';
import { ClearWindow, CreateWindow, GetFirstWindowId } from '~/services/Windows';
// `t` comes from the note as a prop: a card mounted with its own `createApp` is not inside the
// app's plugin tree, so it cannot call `useI18n()` the way the note can.
const props = defineProps(['href', 't']);
const { Campaign } = useCampaignService();
const translate = (key) => (props.t ? props.t(key) : key);
const target = ref(null);
const entry = ref(null);
const loading = ref(true);
const campaignId = computed(() => Campaign.value?._id ?? Campaign.value?.id ?? null);
const isActor = computed(() => target.value?.type === 'actor');
const isItem = computed(() => target.value?.type === 'item');
onMounted(async () => {
const targets = await LoadLinkTargets(campaignId.value);
const found = FindLinkTarget(targets, props.href);
if (!found) {
loading.value = false;
return;
}
target.value = found;
entry.value = await LoadLinkEntry(campaignId.value, found.type, found.id);
loading.value = false;
});
// --- The character sheet, in the small ---
const abilityStats = [
{ key: 'str', label: 'STR' }, { key: 'dex', label: 'DEX' }, { key: 'con', label: 'CON' },
{ key: 'int', label: 'INT' }, { key: 'wis', label: 'WIS' }, { key: 'cha', label: 'CHA' }
];
function abilityModifier(score) {
const modifier = Math.floor((Number(score ?? 10) - 10) / 2);
return modifier >= 0 ? `+${modifier}` : String(modifier);
}
function barPercent(current, max) {
const top = Number(max) || 0;
return top > 0 ? Math.min(100, Math.max(0, ((Number(current) || 0) / top) * 100)) : 0;
}
const hitPointsPercent = computed(() => barPercent(entry.value?.hitPoints?.current, entry.value?.hitPoints?.max));
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(`${translate('campaigns.character-sheet.level')} ${entry.value.level}`);
return parts.join(' · ');
});
// 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());
// --- The item's sheet, in the small ---
const itemTags = computed(() => {
const parts = [];
if (entry.value?.category) parts.push(translate(`campaigns.item.categories.${entry.value.category}`));
if (entry.value?.rarity) parts.push(translate(`campaigns.item.rarities.${entry.value.rarity}`));
return parts;
});
const weight = computed(() => {
if (entry.value?.weight === undefined || entry.value?.weight === '') return '';
return Number(entry.value.weight).toFixed(1);
});
async function openSheet() {
const type = target.value?.type;
if ((type !== 'actor' && type !== 'item') || !entry.value) return;
const windowType = type === 'actor' ? 'character_sheet' : 'item_sheet';
const existingWindowId = GetFirstWindowId(windowType);
if (existingWindowId !== undefined) {
ClearWindow({ id: existingWindowId });
await nextTick();
}
CreateWindow(windowType, type === 'actor' ? { actor: entry.value } : { item: entry.value });
}
</script>
<template>
<span v-if="loading" class="embedded-entry embedded-loading">{{ href }}</span>
<span v-else-if="entry && isActor" class="embedded-entry embedded-actor">
<span class="embed-head">
<span class="embed-mark" :class="{ empty: !imageUrl }">
<img v-if="imageUrl" :src="imageUrl" alt="">
<span v-else aria-hidden="true">{{ initial }}</span>
</span>
<span class="embed-identity">
<strong class="embed-name">{{ entry.name }}</strong>
<span class="embed-line">{{ actorLine }}</span>
</span>
<button class="embed-open" type="button" @click="openSheet">
{{ translate('campaigns.link-preview.open-character') }}
</button>
</span>
</span>
<span v-else-if="entry && isItem" class="embedded-entry embedded-item">
<span class="embed-head">
<span class="embed-mark" :class="{ empty: !imageUrl }">
<img v-if="imageUrl" :src="imageUrl" alt="">
<span v-else aria-hidden="true">{{ initial }}</span>
</span>
<span class="embed-identity">
<strong class="embed-name">{{ entry.name }}</strong>
<span class="embed-tags">
<span v-for="tag in itemTags" :key="tag" class="embed-tag">{{ tag }}</span>
</span>
</span>
<button class="embed-open" type="button" @click="openSheet">
{{ translate('campaigns.link-preview.open-item') }}
</button>
</span>
</span>
<span v-else class="embedded-entry embedded-missing" :title="translate('campaigns.link-preview.not-found')">{{ href }}</span>
</template>
<style scoped>
/* A card written between paragraphs: it takes the note's width but is still inline content, so the
markdown paragraph around it does not turn into an invalid block. */
.embedded-entry {
display: inline-block;
width: 100%;
margin: 8px 0;
padding: 13px 14px;
box-sizing: border-box;
border: 1px solid var(--color-border);
border-radius: 10px;
background: var(--color-background-light);
box-shadow: 0 3px 12px var(--color-window-shadow);
color: var(--color-text);
font-size: 13px;
vertical-align: top;
}
.embedded-loading,
.embedded-missing {
color: var(--color-gray);
font-size: 12px;
opacity: .75;
}
.embed-head {
display: flex;
align-items: center;
gap: 11px;
border-bottom: 1px solid var(--color-border);
}
.embed-mark {
width: 52px;
height: 52px;
flex: 0 0 52px;
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: 24px MrEavesRemake;
}
.embed-mark.empty { border-style: dashed; }
.embed-mark img { width: 100%; height: 100%; object-fit: contain; }
.embed-identity { flex: 1; min-width: 0; display: flex; flex-direction: column; gap: 5px; }
.embed-name {
font: 21px MrEavesRemake;
letter-spacing: .02em;
overflow-wrap: anywhere;
}
.embed-line { font-size: 11px; opacity: .72; overflow-wrap: anywhere; }
.embed-weight { display: block; margin: 0 0 10px; }
.embed-tags { display: flex; flex-wrap: wrap; gap: 5px; }
.embed-tag {
padding: 2px 8px;
border: 1px solid var(--color-border);
border-radius: 999px;
background: var(--color-background-soft);
font-size: 11px;
opacity: .9;
}
.embed-open {
flex: 0 0 auto;
padding: 6px 10px;
border: 1px solid var(--color-link);
border-radius: 7px;
background: transparent;
color: var(--color-link);
font: inherit;
font-size: 11px;
cursor: pointer;
white-space: nowrap;
}
.embed-open:hover { background: var(--color-button-hover); }
.embed-vitals { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 6px; }
.embed-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;
}
.embed-stat span { font-size: 9px; text-transform: uppercase; letter-spacing: .05em; opacity: .68; }
.embed-stat strong { font: 16px MrEavesRemake; white-space: nowrap; }
.embed-hp-track {
display: block;
height: 6px;
margin: 8px 0 10px;
overflow: hidden;
border-radius: 999px;
background: var(--color-background-soft);
}
.embed-hp-fill { display: block; height: 100%; border-radius: inherit; background: var(--color-red); }
.embed-abilities { display: grid; grid-template-columns: repeat(6, minmax(0, 1fr)); gap: 5px; }
.embed-ability {
display: flex;
flex-direction: column;
align-items: center;
gap: 1px;
padding: 6px 3px;
border: 1px solid var(--color-border);
border-radius: 7px;
background: var(--color-background-soft);
text-align: center;
}
.embed-ability > span:first-child { font-size: 9px; font-weight: 700; letter-spacing: .06em; opacity: .68; }
.embed-ability strong { font: 17px MrEavesRemake; }
.embed-ability em { font-style: normal; font-size: 10px; opacity: .72; }
.embed-section { display: block; margin-top: 10px; }
.embed-section-title {
display: block;
padding-bottom: 5px;
margin-bottom: 5px;
border-bottom: 1px solid var(--color-border);
font: 14px MrEavesRemake;
letter-spacing: .03em;
}
.embed-copy {
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;
}
@media (max-width: 520px) {
.embed-head { align-items: flex-start; flex-wrap: wrap; }
.embed-open { margin-left: auto; }
.embed-abilities { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}
</style>