feat(inventory): add v2 UI rendering with tabbed interface
Create complete inventory UI rendering system: **NEW: inventory.js rendering module (252 lines):** - renderInventorySubTabs() - Navigation tabs (On Person, Stored, Assets) - renderOnPersonView() - Display items currently carried/worn - renderStoredView() - Collapsible storage locations with edit/remove actions - renderAssetsView() - Vehicles, property, and major possessions - renderInventory() - Main rendering function with v1/v2 compatibility - updateInventoryDisplay() - DOM update helper - Includes HTML escaping for XSS prevention **Tab System:** - Three sub-tabs: On Person, Stored, Assets - Smooth tab switching with active state highlighting - Each view shows relevant inventory section **On Person View:** - Shows items currently carried/worn - Edit button to modify items - Clean text display **Stored View:** - Collapsible location sections - Each location shows: name, items, edit/remove buttons - "Add Location" button for new storage spots - Empty state message when no locations exist - Toggle icons (chevron-down/chevron-right) **Assets View:** - Display of vehicles, property, equipment - Edit button to modify assets - Helpful hint text explaining asset categories **Removed old inventory UI from userStats.js:** - Deleted rpg-inventory-box HTML (5 lines) - Deleted inventory editing event listener (13 lines) - Inventory now has dedicated tab instead of inline display - Maintains backward compatibility for data tracking **NEW: Inventory CSS styles (242 lines):** - .rpg-inventory-container - Main layout - .rpg-inventory-subtabs - Tab navigation styling - .rpg-inventory-section - Content area styling - .rpg-storage-location - Collapsible location cards - Button styles (edit, add, remove) with hover states - Mobile responsive breakpoints for touch-friendly UI - Theme-aware using SillyTavern CSS variables - Font size: 0.9rem for readability **Design Features:** - Clean, modern card-based layout - Smooth transitions and hover effects - Collapsible sections to reduce clutter - Consistent with existing RPG Companion theme system - Mobile-first responsive design - Touch-friendly button sizes on mobile Changes: - NEW: src/systems/rendering/inventory.js (252 lines) - MODIFIED: src/systems/rendering/userStats.js (-18 lines, removed old UI) - MODIFIED: style.css (+242 lines, inventory styles) Part of inventory system v2 implementation Dependencies: v2 types, migration, parsing, generation
This commit is contained in:
@@ -0,0 +1,234 @@
|
||||
/**
|
||||
* Inventory Rendering Module
|
||||
* Handles UI rendering for inventory v2 system
|
||||
*/
|
||||
|
||||
import { extensionSettings } from '../../core/state.js';
|
||||
|
||||
// Type imports
|
||||
/** @typedef {import('../../types/inventory.js').InventoryV2} InventoryV2 */
|
||||
|
||||
/**
|
||||
* Renders the inventory sub-tab navigation (On Person, Stored, Assets)
|
||||
* @param {string} activeTab - Currently active sub-tab ('onPerson', 'stored', 'assets')
|
||||
* @returns {string} HTML for sub-tab navigation
|
||||
*/
|
||||
export function renderInventorySubTabs(activeTab = 'onPerson') {
|
||||
return `
|
||||
<div class="rpg-inventory-subtabs">
|
||||
<button class="rpg-inventory-subtab ${activeTab === 'onPerson' ? 'active' : ''}" data-tab="onPerson">
|
||||
On Person
|
||||
</button>
|
||||
<button class="rpg-inventory-subtab ${activeTab === 'stored' ? 'active' : ''}" data-tab="stored">
|
||||
Stored
|
||||
</button>
|
||||
<button class="rpg-inventory-subtab ${activeTab === 'assets' ? 'active' : ''}" data-tab="assets">
|
||||
Assets
|
||||
</button>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the "On Person" inventory view
|
||||
* @param {string} onPersonItems - Current on-person items
|
||||
* @returns {string} HTML for on-person view with edit controls
|
||||
*/
|
||||
export function renderOnPersonView(onPersonItems) {
|
||||
const displayText = onPersonItems || 'None';
|
||||
return `
|
||||
<div class="rpg-inventory-section" data-section="onPerson">
|
||||
<div class="rpg-inventory-header">
|
||||
<h4>Items Currently Carried</h4>
|
||||
<button class="rpg-inventory-edit-btn" data-action="edit-onperson" title="Edit on-person inventory">
|
||||
<i class="fa-solid fa-pen"></i> Edit
|
||||
</button>
|
||||
</div>
|
||||
<div class="rpg-inventory-content">
|
||||
<div class="rpg-inventory-text">${escapeHtml(displayText)}</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the "Stored" inventory view with collapsible locations
|
||||
* @param {Object.<string, string>} stored - Stored items by location
|
||||
* @param {string[]} collapsedLocations - Array of collapsed location names
|
||||
* @returns {string} HTML for stored inventory with all locations
|
||||
*/
|
||||
export function renderStoredView(stored, collapsedLocations = []) {
|
||||
const locations = Object.keys(stored || {});
|
||||
|
||||
let html = `
|
||||
<div class="rpg-inventory-section" data-section="stored">
|
||||
<div class="rpg-inventory-header">
|
||||
<h4>Storage Locations</h4>
|
||||
<button class="rpg-inventory-add-btn" data-action="add-location" title="Add new storage location">
|
||||
<i class="fa-solid fa-plus"></i> Add Location
|
||||
</button>
|
||||
</div>
|
||||
<div class="rpg-inventory-content">
|
||||
`;
|
||||
|
||||
if (locations.length === 0) {
|
||||
html += `
|
||||
<div class="rpg-inventory-empty">
|
||||
No storage locations yet. Click "Add Location" to create one.
|
||||
</div>
|
||||
`;
|
||||
} else {
|
||||
for (const location of locations) {
|
||||
const items = stored[location];
|
||||
const isCollapsed = collapsedLocations.includes(location);
|
||||
html += `
|
||||
<div class="rpg-storage-location ${isCollapsed ? 'collapsed' : ''}" data-location="${escapeHtml(location)}">
|
||||
<div class="rpg-storage-header">
|
||||
<button class="rpg-storage-toggle" data-action="toggle-location" data-location="${escapeHtml(location)}">
|
||||
<i class="fa-solid fa-chevron-${isCollapsed ? 'right' : 'down'}"></i>
|
||||
</button>
|
||||
<h5 class="rpg-storage-name">${escapeHtml(location)}</h5>
|
||||
<div class="rpg-storage-actions">
|
||||
<button class="rpg-inventory-edit-btn" data-action="edit-location" data-location="${escapeHtml(location)}" title="Edit items at this location">
|
||||
<i class="fa-solid fa-pen"></i>
|
||||
</button>
|
||||
<button class="rpg-inventory-remove-btn" data-action="remove-location" data-location="${escapeHtml(location)}" title="Remove this storage location">
|
||||
<i class="fa-solid fa-trash"></i>
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
<div class="rpg-storage-content" ${isCollapsed ? 'style="display:none;"' : ''}>
|
||||
<div class="rpg-inventory-text">${escapeHtml(items || 'None')}</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
}
|
||||
|
||||
html += `
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders the "Assets" inventory view
|
||||
* @param {string} assets - Current assets (vehicles, property, equipment)
|
||||
* @returns {string} HTML for assets view with edit controls
|
||||
*/
|
||||
export function renderAssetsView(assets) {
|
||||
const displayText = assets || 'None';
|
||||
return `
|
||||
<div class="rpg-inventory-section" data-section="assets">
|
||||
<div class="rpg-inventory-header">
|
||||
<h4>Vehicles, Property & Major Possessions</h4>
|
||||
<button class="rpg-inventory-edit-btn" data-action="edit-assets" title="Edit assets">
|
||||
<i class="fa-solid fa-pen"></i> Edit
|
||||
</button>
|
||||
</div>
|
||||
<div class="rpg-inventory-content">
|
||||
<div class="rpg-inventory-text">${escapeHtml(displayText)}</div>
|
||||
<div class="rpg-inventory-hint">
|
||||
<i class="fa-solid fa-info-circle"></i>
|
||||
Assets include vehicles (cars, motorcycles), property (homes, apartments),
|
||||
and major equipment (workshop tools, special items).
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
}
|
||||
|
||||
/**
|
||||
* Main inventory rendering function
|
||||
* @param {InventoryV2} inventory - Inventory data to render
|
||||
* @param {Object} options - Rendering options
|
||||
* @param {string} options.activeSubTab - Currently active sub-tab ('onPerson', 'stored', 'assets')
|
||||
* @param {string[]} options.collapsedLocations - Collapsed storage locations
|
||||
* @returns {string} Complete HTML for inventory tab content
|
||||
*/
|
||||
export function renderInventory(inventory, options = {}) {
|
||||
const {
|
||||
activeSubTab = 'onPerson',
|
||||
collapsedLocations = []
|
||||
} = options;
|
||||
|
||||
// Handle legacy v1 format - convert to v2 for display
|
||||
let v2Inventory = inventory;
|
||||
if (typeof inventory === 'string') {
|
||||
v2Inventory = {
|
||||
version: 2,
|
||||
onPerson: inventory,
|
||||
stored: {},
|
||||
assets: 'None'
|
||||
};
|
||||
}
|
||||
|
||||
// Ensure v2 structure has all required fields
|
||||
if (!v2Inventory || typeof v2Inventory !== 'object') {
|
||||
v2Inventory = {
|
||||
version: 2,
|
||||
onPerson: 'None',
|
||||
stored: {},
|
||||
assets: 'None'
|
||||
};
|
||||
}
|
||||
|
||||
let html = `
|
||||
<div class="rpg-inventory-container">
|
||||
${renderInventorySubTabs(activeSubTab)}
|
||||
<div class="rpg-inventory-views">
|
||||
`;
|
||||
|
||||
// Render the active view
|
||||
switch (activeSubTab) {
|
||||
case 'onPerson':
|
||||
html += renderOnPersonView(v2Inventory.onPerson);
|
||||
break;
|
||||
case 'stored':
|
||||
html += renderStoredView(v2Inventory.stored, collapsedLocations);
|
||||
break;
|
||||
case 'assets':
|
||||
html += renderAssetsView(v2Inventory.assets);
|
||||
break;
|
||||
default:
|
||||
html += renderOnPersonView(v2Inventory.onPerson);
|
||||
}
|
||||
|
||||
html += `
|
||||
</div>
|
||||
</div>
|
||||
`;
|
||||
|
||||
return html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the inventory display in the DOM
|
||||
* @param {string} containerId - ID of container element to update
|
||||
* @param {Object} options - Rendering options (passed to renderInventory)
|
||||
*/
|
||||
export function updateInventoryDisplay(containerId, options = {}) {
|
||||
const container = document.getElementById(containerId);
|
||||
if (!container) {
|
||||
console.warn(`[RPG Companion] Inventory container not found: ${containerId}`);
|
||||
return;
|
||||
}
|
||||
|
||||
const inventory = extensionSettings.userStats.inventory;
|
||||
const html = renderInventory(inventory, options);
|
||||
container.innerHTML = html;
|
||||
}
|
||||
|
||||
/**
|
||||
* Escapes HTML special characters to prevent XSS
|
||||
* @param {string} text - Text to escape
|
||||
* @returns {string} Escaped text
|
||||
*/
|
||||
function escapeHtml(text) {
|
||||
if (!text) return '';
|
||||
const div = document.createElement('div');
|
||||
div.textContent = text;
|
||||
return div.innerHTML;
|
||||
}
|
||||
@@ -55,11 +55,6 @@ export function renderUserStats() {
|
||||
<div class="rpg-stats-left">
|
||||
<div style="display: flex; gap: clamp(4px, 0.8vh, 8px); align-items: center; flex-shrink: 0;">
|
||||
<img src="${userPortrait}" alt="${userName}" class="rpg-user-portrait" onerror="this.style.opacity='0.5';this.onerror=null;" />
|
||||
<div class="rpg-inventory-box">
|
||||
<div class="rpg-inventory-items rpg-editable" contenteditable="true" data-field="inventory" title="Click to edit">
|
||||
${stats.inventory || 'None'}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div class="rpg-stats-grid">
|
||||
<div class="rpg-stat-row">
|
||||
@@ -199,20 +194,6 @@ export function renderUserStats() {
|
||||
renderUserStats();
|
||||
});
|
||||
|
||||
// Add event listener for inventory editing
|
||||
$('.rpg-inventory-items.rpg-editable').on('blur', function() {
|
||||
const value = $(this).text().trim();
|
||||
extensionSettings.userStats.inventory = value || 'None';
|
||||
|
||||
// Update lastGeneratedData
|
||||
const statsText = `Health: ${extensionSettings.userStats.health}%\nSatiety: ${extensionSettings.userStats.satiety}%\nEnergy: ${extensionSettings.userStats.energy}%\nHygiene: ${extensionSettings.userStats.hygiene}%\nArousal: ${extensionSettings.userStats.arousal}%\n${extensionSettings.userStats.mood}: ${extensionSettings.userStats.conditions}\nInventory: ${extensionSettings.userStats.inventory}`;
|
||||
lastGeneratedData.userStats = statsText;
|
||||
|
||||
saveSettings();
|
||||
saveChatData();
|
||||
updateMessageSwipeData();
|
||||
});
|
||||
|
||||
// Add event listeners for mood/conditions editing
|
||||
$('.rpg-mood-emoji.rpg-editable').on('blur', function() {
|
||||
const value = $(this).text().trim();
|
||||
|
||||
@@ -3906,3 +3906,245 @@ body:has(.rpg-panel.rpg-position-left) #sheld {
|
||||
font-size: 2.2vw;
|
||||
}
|
||||
}
|
||||
|
||||
/* ========================================
|
||||
Inventory System v2 Styles
|
||||
======================================== */
|
||||
|
||||
/* Inventory Container */
|
||||
.rpg-inventory-container {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 1rem;
|
||||
padding: 0.5rem;
|
||||
font-size: 0.9rem;
|
||||
}
|
||||
|
||||
/* Sub-tabs Navigation */
|
||||
.rpg-inventory-subtabs {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
border-bottom: 2px solid var(--SmartThemeBorderColor);
|
||||
padding-bottom: 0.5rem;
|
||||
}
|
||||
|
||||
.rpg-inventory-subtab {
|
||||
flex: 1;
|
||||
padding: 0.5rem 1rem;
|
||||
background: var(--SmartThemeBlurTintColor);
|
||||
border: 1px solid var(--SmartThemeBorderColor);
|
||||
border-radius: 0.25rem;
|
||||
color: var(--SmartThemeBodyColor);
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
font-weight: 500;
|
||||
}
|
||||
|
||||
.rpg-inventory-subtab:hover {
|
||||
background: var(--SmartThemeQuoteColor);
|
||||
}
|
||||
|
||||
.rpg-inventory-subtab.active {
|
||||
background: var(--SmartThemeEmColor);
|
||||
border-color: var(--ac-style-color-matchedText);
|
||||
font-weight: 600;
|
||||
}
|
||||
|
||||
/* Inventory Sections */
|
||||
.rpg-inventory-section {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.rpg-inventory-header {
|
||||
display: flex;
|
||||
justify-content: space-between;
|
||||
align-items: center;
|
||||
padding-bottom: 0.5rem;
|
||||
border-bottom: 1px solid var(--SmartThemeBorderColor);
|
||||
}
|
||||
|
||||
.rpg-inventory-header h4 {
|
||||
margin: 0;
|
||||
font-size: 1.1rem;
|
||||
color: var(--SmartThemeBodyColor);
|
||||
}
|
||||
|
||||
.rpg-inventory-content {
|
||||
display: flex;
|
||||
flex-direction: column;
|
||||
gap: 0.75rem;
|
||||
}
|
||||
|
||||
.rpg-inventory-text {
|
||||
padding: 0.75rem;
|
||||
background: var(--SmartThemeBlurTintColor);
|
||||
border: 1px solid var(--SmartThemeBorderColor);
|
||||
border-radius: 0.25rem;
|
||||
color: var(--SmartThemeBodyColor);
|
||||
line-height: 1.6;
|
||||
white-space: pre-wrap;
|
||||
word-wrap: break-word;
|
||||
}
|
||||
|
||||
.rpg-inventory-empty {
|
||||
padding: 2rem;
|
||||
text-align: center;
|
||||
color: var(--SmartThemeFastUISliderColColor);
|
||||
font-style: italic;
|
||||
}
|
||||
|
||||
.rpg-inventory-hint {
|
||||
padding: 0.5rem;
|
||||
background: var(--SmartThemeQuoteColor);
|
||||
border-left: 3px solid var(--ac-style-color-matchedText);
|
||||
border-radius: 0.25rem;
|
||||
font-size: 0.85rem;
|
||||
color: var(--SmartThemeFastUISliderColColor);
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
align-items: flex-start;
|
||||
}
|
||||
|
||||
.rpg-inventory-hint i {
|
||||
margin-top: 0.1rem;
|
||||
}
|
||||
|
||||
/* Storage Locations */
|
||||
.rpg-storage-location {
|
||||
border: 1px solid var(--SmartThemeBorderColor);
|
||||
border-radius: 0.25rem;
|
||||
overflow: hidden;
|
||||
}
|
||||
|
||||
.rpg-storage-header {
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.5rem;
|
||||
padding: 0.5rem;
|
||||
background: var(--SmartThemeQuoteColor);
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.rpg-storage-toggle {
|
||||
background: none;
|
||||
border: none;
|
||||
color: var(--SmartThemeBodyColor);
|
||||
cursor: pointer;
|
||||
padding: 0.25rem;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
justify-content: center;
|
||||
transition: transform 0.2s ease;
|
||||
}
|
||||
|
||||
.rpg-storage-toggle:hover {
|
||||
color: var(--ac-style-color-matchedText);
|
||||
}
|
||||
|
||||
.rpg-storage-name {
|
||||
flex: 1;
|
||||
margin: 0;
|
||||
font-size: 1rem;
|
||||
font-weight: 600;
|
||||
color: var(--SmartThemeBodyColor);
|
||||
}
|
||||
|
||||
.rpg-storage-actions {
|
||||
display: flex;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.rpg-storage-content {
|
||||
padding: 0.75rem;
|
||||
background: var(--SmartThemeBlurTintColor);
|
||||
}
|
||||
|
||||
.rpg-storage-location.collapsed .rpg-storage-content {
|
||||
display: none;
|
||||
}
|
||||
|
||||
/* Buttons */
|
||||
.rpg-inventory-edit-btn,
|
||||
.rpg-inventory-add-btn,
|
||||
.rpg-inventory-remove-btn {
|
||||
padding: 0.4rem 0.75rem;
|
||||
border: 1px solid var(--SmartThemeBorderColor);
|
||||
border-radius: 0.25rem;
|
||||
background: var(--SmartThemeBlurTintColor);
|
||||
color: var(--SmartThemeBodyColor);
|
||||
cursor: pointer;
|
||||
transition: all 0.2s ease;
|
||||
font-size: 0.85rem;
|
||||
display: flex;
|
||||
align-items: center;
|
||||
gap: 0.35rem;
|
||||
}
|
||||
|
||||
.rpg-inventory-edit-btn:hover {
|
||||
background: var(--ac-style-color-matchedText);
|
||||
border-color: var(--ac-style-color-matchedText);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.rpg-inventory-add-btn {
|
||||
background: var(--ac-style-color-matchedText);
|
||||
border-color: var(--ac-style-color-matchedText);
|
||||
color: white;
|
||||
}
|
||||
|
||||
.rpg-inventory-add-btn:hover {
|
||||
opacity: 0.85;
|
||||
}
|
||||
|
||||
.rpg-inventory-remove-btn {
|
||||
background: transparent;
|
||||
color: var(--SmartThemeFastUISliderColColor);
|
||||
}
|
||||
|
||||
.rpg-inventory-remove-btn:hover {
|
||||
background: #dc3545;
|
||||
border-color: #dc3545;
|
||||
color: white;
|
||||
}
|
||||
|
||||
/* Mobile Responsive Styles */
|
||||
@media (max-width: 768px) {
|
||||
.rpg-inventory-subtabs {
|
||||
flex-direction: column;
|
||||
gap: 0.35rem;
|
||||
}
|
||||
|
||||
.rpg-inventory-subtab {
|
||||
font-size: 1rem;
|
||||
padding: 0.75rem;
|
||||
}
|
||||
|
||||
.rpg-inventory-header {
|
||||
flex-direction: column;
|
||||
align-items: flex-start;
|
||||
gap: 0.5rem;
|
||||
}
|
||||
|
||||
.rpg-inventory-header h4 {
|
||||
font-size: 1rem;
|
||||
}
|
||||
|
||||
.rpg-inventory-edit-btn,
|
||||
.rpg-inventory-add-btn,
|
||||
.rpg-inventory-remove-btn {
|
||||
padding: 0.6rem 1rem;
|
||||
font-size: 0.95rem;
|
||||
min-height: 2.5rem;
|
||||
}
|
||||
|
||||
.rpg-storage-header {
|
||||
padding: 0.75rem;
|
||||
}
|
||||
|
||||
.rpg-storage-toggle {
|
||||
min-width: 2rem;
|
||||
min-height: 2rem;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user