Fix issue #10: Add map support

This commit is contained in:
Gitea OpenCode Agent
2026-10-14 10:25:49 +00:00
parent af76cf1050
commit bcd173d133
14 changed files with 683 additions and 31 deletions

View File

@@ -15,6 +15,8 @@ const notes = ref([]);
const folders = ref([]);
const actors = ref([]);
const items = ref([]);
const maps = ref([]);
const entryLists = { actor: actors, item: items, map: maps };
const loadingNotes = ref(false);
const loadingEntries = ref(false);
const notesError = ref('');
@@ -105,14 +107,16 @@ function handleFolderDeleted(folderId) {
function handleCampaignEntryCreated({ entityType, entry }) {
if (!entry || (entry.campaign && String(entry.campaign) !== String(campaignId.value))) return;
const list = entityType === 'actor' ? actors : items;
const list = entryLists[entityType];
if (!list) return;
list.value = [...list.value.filter((current) => current._id !== entry._id), entry]
.sort((a, b) => a.name.localeCompare(b.name));
}
function handleCampaignEntryUpdated({ entityType, entry }) {
if (!entry) return;
const list = entityType === 'actor' ? actors : items;
const list = entryLists[entityType];
if (!list) return;
list.value = list.value.map((current) => current._id === entry._id ? entry : current)
.sort((a, b) => a.name.localeCompare(b.name));
}
@@ -158,24 +162,34 @@ async function fetchCampaignNotes() {
}
}
function entryEntityType(section) {
if (section === 'actors') return 'actor';
if (section === 'items') return 'item';
if (section === 'maps') return 'map';
return null;
}
function isEntrySection(section) {
return entryEntityType(section) !== null;
}
async function fetchCampaignEntries(entityType = activeSection.value, silent = false) {
if (!campaignId.value || !['actors', 'items'].includes(entityType)) return;
const kind = entryEntityType(entityType);
const list = entryLists[kind];
if (!campaignId.value || !list) return;
if (!silent) {
loadingEntries.value = true;
entriesError.value = '';
}
try {
const collection = entityType === 'actors' ? 'actor' : 'item';
const response = await Server().get(`/${collection}/list`, { params: { campaign: campaignId.value } });
const response = await Server().get(`/${kind}/list`, { params: { campaign: campaignId.value } });
if (response.data.status !== 'ok') {
if (!silent) entriesError.value = response.data.msg ?? t('campaigns.errors.load');
return;
}
const kind = entityType === 'actors' ? 'actor' : 'item';
const list = entityType === 'actors' ? actors : items;
const previousById = new Map(list.value.map((entry) => [entry._id, entry]));
list.value = response.data[entityType];
list.value = response.data[entityType] ?? [];
// Tell open windows about Foundry changes landing behind them.
for (const entry of list.value) {
const previous = previousById.get(entry._id);
@@ -192,11 +206,15 @@ async function fetchCampaignEntries(entityType = activeSection.value, silent = f
function selectSection(section) {
activeSection.value = section;
if (section === 'actors' || section === 'items') fetchCampaignEntries(section);
if (isEntrySection(section)) fetchCampaignEntries(section);
}
function createEntry(entityType) {
if (!campaignId.value) return;
if (entityType === 'map') {
CreateWindow('create_map', { campaign: campaignId.value });
return;
}
CreateWindow('create_campaign_entry', { entityType, campaign: campaignId.value });
}
@@ -213,22 +231,37 @@ function itemSummary(item) {
return `${t(`campaigns.item.categories.${item.category}`)} · ${t(`campaigns.item.rarities.${item.rarity}`)} · ${t('campaigns.item.quantity-short')} ${item.quantity}`;
}
function mapSummary(map) {
return t('campaigns.map.summary', { size: map.gridSize, scale: map.scale });
}
function editItem(entry) {
if (!campaignId.value) return;
CreateWindow('create_campaign_entry', { entityType: 'item', campaign: campaignId.value, entry });
}
function editMap(map) {
if (!campaignId.value) return;
CreateWindow('create_map', { campaign: campaignId.value, entry: map });
}
function showEntryMenu(entry, entityType) {
const collection = entityType === 'actor' ? 'actor' : 'item';
const list = entryLists[entityType];
const menuItems = [];
// Characters are edited in their sheet; items get the entry window in edit mode.
if (entityType !== 'actor') {
// Characters are edited in their sheet; items and maps get their window in edit mode.
if (entityType === 'item') {
menuItems.push({
name: t('campaigns.actions.edit'),
icon: '/icons/iconoir/regular/edit.svg',
action: () => { HideContextMenu(); editItem(entry); }
});
} else if (entityType === 'map') {
menuItems.push({
name: t('campaigns.actions.edit'),
icon: '/icons/iconoir/regular/edit.svg',
action: () => { HideContextMenu(); editMap(entry); }
});
}
menuItems.push({
@@ -236,10 +269,9 @@ function showEntryMenu(entry, entityType) {
icon: '/icons/iconoir/regular/trash.svg',
action: async () => {
try {
const response = await Server().post(`/${collection}/delete`, { id: entry._id });
if (response.data.status === 'ok') {
if (entityType === 'actor') actors.value = actors.value.filter((actor) => actor._id !== entry._id);
else items.value = items.value.filter((item) => item._id !== entry._id);
const response = await Server().post(`/${entityType}/delete`, { id: entry._id });
if (response.data.status === 'ok' && list) {
list.value = list.value.filter((current) => current._id !== entry._id);
}
} catch (error) {} finally { HideContextMenu(); }
}
@@ -262,6 +294,15 @@ async function openCharacterSheet(actor) {
CreateWindow('character_sheet', { actor });
}
async function openMapViewer(map) {
const existingWindowId = GetFirstWindowId('map_viewer');
if (existingWindowId !== undefined) {
ClearWindow({ id: existingWindowId });
await nextTick();
}
CreateWindow('map_viewer', { map, campaign: campaignId.value });
}
function createNote() {
if (!Campaign.value) return;
@@ -282,13 +323,15 @@ watch(campaignId, (newVal) => {
if (newVal) {
actors.value = [];
items.value = [];
maps.value = [];
fetchCampaignNotes();
if (activeSection.value === 'actors' || activeSection.value === 'items') fetchCampaignEntries(activeSection.value);
if (isEntrySection(activeSection.value)) fetchCampaignEntries(activeSection.value);
} else {
notes.value = [];
folders.value = [];
actors.value = [];
items.value = [];
maps.value = [];
notesError.value = '';
entriesError.value = '';
}
@@ -375,6 +418,16 @@ onBeforeUnmount(() => {
>
<img class="sidebar-action-icon" src="/icons/iconoir/regular/box.svg" alt="" aria-hidden="true">
</button>
<button
class="sidebar-action"
:class="{ selected: activeSection === 'maps' }"
type="button" @click="selectSection('maps')"
:title="t('campaigns.sections.maps')" :aria-label="t('campaigns.sections.maps')"
:aria-pressed="(activeSection === 'maps').toString()"
>
<img class="sidebar-action-icon" src="/icons/iconoir/regular/map.svg" alt="" aria-hidden="true">
</button>
</nav>
<aside class="notes-sidebar" :class="{ collapsed: sidebarCollapsed }">
@@ -396,7 +449,7 @@ onBeforeUnmount(() => {
</button>
</template>
<template v-else>
<button class="sidebar-action" type="button" @click="createEntry(activeSection === 'actors' ? 'actor' : 'item')" :disabled="!Campaign" :title="t('campaigns.actions.create', { section: t(`campaigns.sections.${activeSection}`).toLowerCase() })" :aria-label="t('campaigns.actions.create', { section: t(`campaigns.sections.${activeSection}`).toLowerCase() })">
<button class="sidebar-action" type="button" @click="createEntry(entryEntityType(activeSection))" :disabled="!Campaign" :title="t('campaigns.actions.create', { section: t(`campaigns.sections.${activeSection}`).toLowerCase() })" :aria-label="t('campaigns.actions.create', { section: t(`campaigns.sections.${activeSection}`).toLowerCase() })">
<img class="sidebar-action-icon" src="/icons/iconoir/regular/plus.svg" alt="" aria-hidden="true">
</button>
<button class="sidebar-action" type="button" @click="fetchCampaignEntries(activeSection)" :disabled="!Campaign || loadingEntries" :title="t('campaigns.actions.refresh')" :aria-label="t('campaigns.actions.refresh')">
@@ -427,7 +480,7 @@ onBeforeUnmount(() => {
</div>
</template>
<template v-else>
<template v-else-if="activeSection === 'items'">
<div v-if="loadingEntries" class="sidebar-state">{{ t('campaigns.states.loading') }}</div>
<div v-else-if="entriesError" class="sidebar-state error">{{ entriesError }}</div>
<div v-else-if="items.length === 0" class="sidebar-state">{{ t('campaigns.states.empty-items') }}</div>
@@ -444,6 +497,17 @@ onBeforeUnmount(() => {
</template>
</div>
</template>
<template v-else>
<div v-if="loadingEntries" class="sidebar-state">{{ t('campaigns.states.loading') }}</div>
<div v-else-if="entriesError" class="sidebar-state error">{{ entriesError }}</div>
<div v-else-if="maps.length === 0" class="sidebar-state">{{ t('campaigns.states.empty-maps') }}</div>
<div v-for="map in maps" :key="map._id" class="entity-entry" role="button" tabindex="0" @click="openMapViewer(map)" @keydown.enter="openMapViewer(map)" @keydown.space.prevent="openMapViewer(map)" @contextmenu.prevent="showEntryMenu(map, 'map')">
<span class="entity-entry-name">{{ map.name }}</span>
<span class="entity-entry-meta">{{ mapSummary(map) }}</span>
<span v-if="!map.image" class="entity-entry-description">{{ t('campaigns.map.no-image') }}</span>
</div>
</template>
</div>
</aside>
</div>