diff --git a/src/server/public/assets/js/dashboard_items.js b/src/server/public/assets/js/dashboard_items.js index 3fac785..74a6dc9 100644 --- a/src/server/public/assets/js/dashboard_items.js +++ b/src/server/public/assets/js/dashboard_items.js @@ -48,10 +48,10 @@ async function onDelete(e){ const id = e.currentTarget && e.currentTarget.dataset ? e.currentTarget.dataset.id : null; if(!id) return; if(!confirm('Eliminar item?')) return; try{ const res = await fetch('/api/dashboard/' + encodeURIComponent(guildId) + '/items/' + encodeURIComponent(id), { method:'DELETE' }); if(!res.ok) throw new Error('delete-failed'); await fetchItems(); alert('Item eliminado'); }catch(err){ alert('Error al eliminar'); } } - function onEdit(e){ const id = e.currentTarget && e.currentTarget.dataset ? e.currentTarget.dataset.id : null; if(!id) return; window.location.href = '/dashboard/' + encodeURIComponent(guildId) + '/items/lab?edit=' + encodeURIComponent(id); } + function onEdit(e){ const id = e.currentTarget && e.currentTarget.dataset ? e.currentTarget.dataset.id : null; if(!id) return; window.location.href = '/items/lab?guild=' + encodeURIComponent(guildId) + '&edit=' + encodeURIComponent(id); } // wire create button (if present) to navigate to lab - const createBtn = $('createItemBtn'); if(createBtn) createBtn.addEventListener('click', ()=>{ window.location.href = '/dashboard/' + encodeURIComponent(guildId) + '/items/lab'; }); + const createBtn = $('createItemBtn'); if(createBtn) createBtn.addEventListener('click', (ev)=>{ ev.preventDefault(); window.location.href = '/items/lab?guild=' + encodeURIComponent(guildId); }); // initial load fetchItems(); diff --git a/src/server/public/assets/js/item_lab.js b/src/server/public/assets/js/item_lab.js index be100df..ff2c71d 100644 --- a/src/server/public/assets/js/item_lab.js +++ b/src/server/public/assets/js/item_lab.js @@ -1,6 +1,14 @@ (function(){ const $ = id => document.getElementById(id); - const guildId = (() => { try{ return (document.getElementById('itemsRoot') && document.getElementById('itemsRoot').dataset && document.getElementById('itemsRoot').dataset.guildId) || ''; }catch(e){ return ''; } })(); + // Detect guildId from itemsRoot dataset when embedded, otherwise read ?guild= from query string when opened as standalone page + const guildId = (() => { + try{ + const root = document.getElementById('itemsRoot'); + if(root && root.dataset && root.dataset.guildId) return root.dataset.guildId; + const p = new URLSearchParams(location.search || ''); + return p.get('guild') || ''; + }catch(e){ return ''; } + })(); // form elements const form = $('itemLabForm'); @@ -54,6 +62,34 @@ labReset.addEventListener('click', resetForm); } + // Prefill when editing: detect ?edit= + (async function tryPrefill(){ + try{ + const params = new URLSearchParams(location.search || ''); + const editId = params.get('edit'); + if(!editId) return; + // show temporary loading state + const prev = labPreview.textContent; + labPreview.textContent = 'Cargando item...'; + const res = await fetch('/api/dashboard/' + encodeURIComponent(guildId) + '/items/' + encodeURIComponent(editId)); + if(!res.ok){ labPreview.textContent = 'Error cargando item: HTTP ' + res.status; return; } + const j = await res.json(); + if(!j || !j.ok || !j.item){ labPreview.textContent = 'Item no encontrado'; return; } + const item = j.item; + // populate fields safely + if(labKey) labKey.value = item.key || ''; + if(labName) labName.value = item.name || ''; + if(labCategory) labCategory.value = item.category || ''; + if(labIcon) labIcon.value = item.icon || ''; + if(labDescription) labDescription.value = item.description || ''; + if(labTags) labTags.value = Array.isArray(item.tags) ? item.tags.join(',') : (item.tags||''); + try{ labProps.value = item.props && typeof item.props === 'object' ? JSON.stringify(item.props, null, 2) : (item.props||'{}'); }catch(e){ labProps.value = '{}'; } + renderPreview(); + // update browser history to remove query param (optional) + try{ const u = new URL(location.href); u.searchParams.delete('edit'); window.history.replaceState({}, '', u.toString()); }catch(e){} + }catch(e){ console.warn('prefill failed', e); } + })(); + // minimal three.js preview function init3D(){ try{ diff --git a/src/server/public/views/item_lab.ejs b/src/server/public/views/item_lab.ejs index 954acd8..27537a4 100644 --- a/src/server/public/views/item_lab.ejs +++ b/src/server/public/views/item_lab.ejs @@ -1,5 +1,14 @@ -<%- include('partials/head') %> -
+ + + + + + Item Lab + + + + +

Item Lab

@@ -58,4 +67,5 @@
-<%- include('partials/foot') %> + + diff --git a/src/server/server.ts b/src/server/server.ts index 6313aa4..ec11e21 100644 --- a/src/server/server.ts +++ b/src/server/server.ts @@ -1867,6 +1867,41 @@ export const server = createServer( } } + // Standalone Item Lab (opens as a separate page, not inside dashboard) + if (url.pathname === "/items/lab" || url.pathname === "/item-lab") { + try { + const publicViews = path.join(__dirname, "public", "views"); + const locals = { + appName: pkg.name ?? "Amayo Bot", + user, + selectedGuildId: url.searchParams.get("guild") || null, + }; + const pageFile = path.join(publicViews, "item_lab.ejs"); + const pageBody = await ejs.renderFile(pageFile, locals, { + async: true, + views: [publicViews, viewsDir], + }); + res.writeHead( + 200, + applySecurityHeaders({ + "Content-Type": "text/html; charset=utf-8", + }) + ); + res.end(pageBody); + return; + } catch (err) { + console.error("render item lab failed", err); + res.writeHead( + 500, + applySecurityHeaders({ + "Content-Type": "text/plain; charset=utf-8", + }) + ); + res.end("Item Lab render error"); + return; + } + } + // /dashboard -> main dashboard if (url.pathname === "/dashboard" || url.pathname === "/dashboard/") { // determine whether bot is in each guild (if we have a bot token) diff --git a/src/server/views/partials/dashboard/dashboard_items.ejs b/src/server/views/partials/dashboard/dashboard_items.ejs index 7c216e6..3eba325 100644 --- a/src/server/views/partials/dashboard/dashboard_items.ejs +++ b/src/server/views/partials/dashboard/dashboard_items.ejs @@ -2,7 +2,7 @@ diff --git a/src/server/views/partials/dashboard/dashboard_sidebar.ejs b/src/server/views/partials/dashboard/dashboard_sidebar.ejs index 2040f43..0247469 100644 --- a/src/server/views/partials/dashboard/dashboard_sidebar.ejs +++ b/src/server/views/partials/dashboard/dashboard_sidebar.ejs @@ -47,7 +47,7 @@
  • ⚙️ General Settings
  • 🧭 Items
  • - + 🔬 Lab