Fix issue #44: Sync item folders
Some checks failed
Test / test (push) Failing after 2s

This commit is contained in:
Gitea OpenCode Agent
2026-10-14 23:39:26 +00:00
parent c7d20dd1f7
commit 1f9ab330b9
14 changed files with 981 additions and 23 deletions

View File

@@ -5,6 +5,7 @@ import { useI18n } from 'vue-i18n';
import { useCampaignService } from '~/services/Campaign.js';
import { AnchorMap, AnchoredMap, ClearAnchoredMap, SetActivePane } from '~/services/Content.js';
import { emitter } from '~/services/Emitter';
import { BuildItemRows } from '~/services/ItemFolders';
import Server from '~/services/Server';
import { ClearWindow, CreateWindow, GetFirstWindowId } from '~/services/Windows';
import { HideContextMenu, ShowContextMenu } from '~/services/ContextMenu';
@@ -17,6 +18,10 @@ const notes = ref([]);
const folders = ref([]);
const actors = ref([]);
const items = ref([]);
// The folders items stand in, and which of them the list has collapsed. Folders are
// open by default: a folder synced from Foundry should show its items, not hide them.
const itemFolders = ref([]);
const collapsedItemFolderIds = ref([]);
const maps = ref([]);
const entryLists = { actor: actors, item: items, map: maps };
const loadingNotes = ref(false);
@@ -202,6 +207,9 @@ async function fetchCampaignEntries(entityType = activeSection.value, silent = f
const previousById = new Map(list.value.map((entry) => [entry._id, entry]));
list.value = response.data[entityType] ?? [];
// Items come with the folder tree their world synced over, so the list can
// group them the way the Foundry item directory does.
if (kind === 'item') itemFolders.value = response.data.folders ?? [];
// Tell open windows about Foundry changes landing behind them.
for (const entry of list.value) {
const previous = previousById.get(entry._id);
@@ -345,10 +353,27 @@ function toggleSidebar() {
const rootNotes = computed(() => notes.value.filter(n => !n._folder));
// The items of the campaign, each under the folder it stands in (see
// services/ItemFolders.js). Without any folder this is the plain item list.
const itemRows = computed(() => BuildItemRows(items.value, itemFolders.value, collapsedItemFolderIds.value));
// Rows one step deeper per folder level; the sidebar list's own padding is the base.
function rowIndent(depth) {
return { paddingLeft: `${10 + depth * 14}px` };
}
function toggleItemFolder(folderId) {
collapsedItemFolderIds.value = collapsedItemFolderIds.value.includes(folderId)
? collapsedItemFolderIds.value.filter((id) => id !== folderId)
: [...collapsedItemFolderIds.value, folderId];
}
watch(campaignId, (newVal) => {
if (newVal) {
actors.value = [];
items.value = [];
itemFolders.value = [];
collapsedItemFolderIds.value = [];
maps.value = [];
ClearAnchoredMap();
fetchCampaignNotes();
@@ -358,6 +383,8 @@ watch(campaignId, (newVal) => {
folders.value = [];
actors.value = [];
items.value = [];
itemFolders.value = [];
collapsedItemFolderIds.value = [];
maps.value = [];
notesError.value = '';
entriesError.value = '';
@@ -512,13 +539,33 @@ onBeforeUnmount(() => {
<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>
<div v-for="item in items" :key="item._id" class="entity-entry" role="button" tabindex="0" @click="openItemSheet(item)" @keydown.enter="openItemSheet(item)" @keydown.space.prevent="openItemSheet(item)" @contextmenu.prevent="showEntryMenu(item, 'item')">
<span class="entity-entry-name-row">
<span class="entity-entry-name">{{ item.name }}</span>
<span v-if="item.foundryItemId" class="entity-entry-sync">{{ t('campaigns.synced') }}</span>
</span>
<span class="entity-entry-meta">{{ itemSummary(item) }}</span>
</div>
<template v-for="row in itemRows" :key="row.key">
<button
v-if="row.kind === 'folder'"
class="item-folder-entry"
type="button"
:style="rowIndent(row.depth)"
:aria-expanded="(!collapsedItemFolderIds.includes(row.folder._id)).toString()"
:aria-label="t('campaigns.item.folder-toggle', { name: row.folder.name })"
@click="toggleItemFolder(row.folder._id)"
>
<img
class="item-folder-arrow"
:src="collapsedItemFolderIds.includes(row.folder._id) ? '/icons/iconoir/regular/nav-arrow-right.svg' : '/icons/iconoir/regular/nav-arrow-down.svg'"
alt=""
aria-hidden="true"
>
<img class="item-folder-icon" src="/icons/iconoir/regular/folder.svg" alt="" aria-hidden="true">
<span class="item-folder-name">{{ row.folder.name }}</span>
</button>
<div v-else class="entity-entry" :style="rowIndent(row.depth)" role="button" tabindex="0" @click="openItemSheet(row.item)" @keydown.enter="openItemSheet(row.item)" @keydown.space.prevent="openItemSheet(row.item)" @contextmenu.prevent="showEntryMenu(row.item, 'item')">
<span class="entity-entry-name-row">
<span class="entity-entry-name">{{ row.item.name }}</span>
<span v-if="row.item.foundryItemId" class="entity-entry-sync">{{ t('campaigns.synced') }}</span>
</span>
<span class="entity-entry-meta">{{ itemSummary(row.item) }}</span>
</div>
</template>
</template>
<template v-else>
@@ -684,6 +731,18 @@ onBeforeUnmount(() => {
}
.entity-entry:hover { border-color: var(--color-border); background: var(--color-background-soft); }
.entity-entry:focus-visible { outline: 1px solid var(--color-link); }
/* Item folders, synced from the linked world's item directory: the same row box as
an entry, since they share the sidebar list and its width rules. */
.item-folder-entry {
width: 100%; min-width: 0; box-sizing: border-box; margin: 0; padding: 8px 10px; border: 1px solid transparent;
border-radius: 8px; background: transparent; color: var(--color-text); text-align: left; display: flex;
align-items: center; gap: 6px; cursor: pointer; font: inherit;
}
.item-folder-entry:hover { background: var(--color-background-soft); }
.item-folder-entry:focus-visible { outline: 1px solid var(--color-link); }
.item-folder-arrow { width: 12px; height: 12px; flex: 0 0 12px; filter: invert(var(--color-icon-invert)); opacity: 0.6; }
.item-folder-icon { width: 15px; height: 15px; flex: 0 0 15px; filter: invert(var(--color-icon-invert)); }
.item-folder-name { min-width: 0; font-weight: bold; word-break: break-word; }
/* The map currently anchored on the note container. */
.entity-entry.anchored { border-color: var(--color-link); background: var(--color-background-soft); }
.entity-entry-name-row { display: flex; align-items: baseline; gap: 6px; min-width: 0; }

View File

@@ -0,0 +1,68 @@
// The rules behind grouping the campaign's items under the folders they stand in.
// Item folders arrive from the linked Foundry world (routes/foundry-link.js mirrors
// its item directory), so the campaign only reads this tree: the helper holds no
// state, talks to no API and builds rows from what `/item/list` already returns.
function ByName(a, b) {
return String(a?.name ?? '').localeCompare(String(b?.name ?? ''));
}
// The folder a folder hangs under. A parent the campaign does not know puts it at
// the root, and so does a parent chain that leads back to the folder itself: a
// broken tree must not hide everything below it.
function ParentKey(folder, knownFolders) {
let parentKey = String(folder.parentFolder ?? '');
if (!parentKey || !knownFolders.has(parentKey)) return '';
const seen = new Set([String(folder._id)]);
while (parentKey) {
if (seen.has(parentKey)) return '';
seen.add(parentKey);
const parent = knownFolders.get(parentKey);
parentKey = knownFolders.has(String(parent?.parentFolder ?? '')) ? String(parent.parentFolder) : '';
}
return String(folder.parentFolder);
}
// The rows of the item list in the order they are shown: every folder followed by
// what stands in it, deeper and deeper, with the items standing at that level last.
// `collapsedFolderIds` hides a folder's whole subtree; folders start open so a
// synced item is never hidden behind a click. Rows are `{ kind: 'folder', key,
// depth, folder }` or `{ kind: 'item', key, depth, item }`. An item whose folder the
// campaign does not know reads as a root item instead of vanishing from the list.
function BuildItemRows(items, folders, collapsedFolderIds = []) {
const knownFolders = new Map((folders ?? []).map((folder) => [String(folder._id), folder]));
const collapsed = new Set((collapsedFolderIds ?? []).map(String));
const childFolders = new Map([['', []]]);
const itemsByFolder = new Map([['', []]]);
for (const folder of knownFolders.values()) {
const parentKey = ParentKey(folder, knownFolders);
if (!childFolders.has(parentKey)) childFolders.set(parentKey, []);
childFolders.get(parentKey).push(folder);
}
for (const item of items ?? []) {
const folderKey = item?.folder && knownFolders.has(String(item.folder)) ? String(item.folder) : '';
if (!itemsByFolder.has(folderKey)) itemsByFolder.set(folderKey, []);
itemsByFolder.get(folderKey).push(item);
}
const rows = [];
const visitedFolders = new Set();
function walk(folderKey, depth) {
for (const folder of (childFolders.get(folderKey) ?? []).slice().sort(ByName)) {
if (visitedFolders.has(String(folder._id))) continue;
visitedFolders.add(String(folder._id));
rows.push({ kind: 'folder', key: `folder-${folder._id}`, depth, folder });
if (!collapsed.has(String(folder._id))) walk(String(folder._id), depth + 1);
}
for (const item of (itemsByFolder.get(folderKey) ?? []).slice().sort(ByName)) {
rows.push({ kind: 'item', key: `item-${item._id}`, depth, item });
}
}
walk('', 0);
return rows;
}
export { BuildItemRows };