/* * MCP App for displaying Cloudinary assets in an interactive * gallery. Attached to list-images, list-videos, list-files, and * search-assets tools. * * Shares CLDS tokens, MCPApp client, helpers, and detail renderers * with the details app via app-shared.ts. */ import { SHARED_CSS_TOKENS, SHARED_CSS_COMPONENTS, SHARED_JS_ICONS, SHARED_JS_MCP_CLIENT, SHARED_JS_HELPERS, SHARED_JS_TOOLTIPS, SHARED_JS_MODAL, SHARED_JS_DETAIL_RENDERERS, SHARED_JS_HOST_CONTEXT, } from "./app-shared.js"; import { injectToolName } from "./uri.js"; export function getAssetGalleryHtml(toolName?: string): string { return injectToolName(ASSET_GALLERY_HTML, toolName); } const GALLERY_CSS = /* css */ ` /* ── Header ── */ .header { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--cld-sp-sm); } .header-left { display: flex; align-items: center; gap: 10px; } .header h1 { font-size: 13px; font-weight: 600; color: var(--cld-text); } .count-badge { font-size: var(--cld-font-xxs); color: var(--cld-text3); background: var(--cld-bg3); padding: 2px 8px; border-radius: 20px; font-weight: 500; } /* action-btn svg sizing */ .action-btn svg { width: 12px; height: 12px; fill: none; stroke: currentColor; stroke-width: 2.5; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; } /* select bar svg */ .bar-btn svg { width: 13px; height: 13px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; } /* ── Grid ── */ .grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: var(--cld-sp-sm); } /* ── Card ── */ .card { position: relative; background: var(--cld-bg2); border: 2px solid transparent; border-radius: var(--cld-radius); overflow: hidden; transition: box-shadow 0.18s ease, transform 0.18s ease, border-color 0.18s ease; cursor: default; outline: none; } .card:hover { box-shadow: 0 8px 24px rgba(0,0,0,0.18); transform: translateY(-2px); } .card.selected { border-color: var(--cld-accent); box-shadow: 0 0 0 1px var(--cld-accent), 0 6px 20px rgba(52,72,197,0.2); } /* Thumbnail */ .thumb { position: relative; width: 100%; aspect-ratio: 4/3; background: var(--cld-bg3); overflow: hidden; display: flex; align-items: center; justify-content: center; } .thumb img { width: 100%; height: 100%; object-fit: cover; transition: transform 0.3s ease; } .card:hover .thumb img { transform: scale(1.04); } .thumb .badge { position: absolute; bottom: 6px; left: 6px; background: rgba(0,0,0,0.65); color: #fff; font-size: 10px; font-weight: 700; padding: 2px 6px; border-radius: 4px; text-transform: uppercase; letter-spacing: 0.5px; backdrop-filter: blur(4px); z-index: 2; } .thumb .placeholder { color: var(--cld-text3); font-size: 28px; } .thumb .audio-placeholder { color: var(--cld-text3); font-size: 32px; font-weight: 700; } .thumb.link:hover { opacity: 0.9; } /* Selection checkbox */ .card-check { position: absolute; top: 8px; left: 8px; width: 20px; height: 20px; border-radius: 50%; background: rgba(255,255,255,0.9); border: 2px solid rgba(255,255,255,0.9); display: flex; align-items: center; justify-content: center; opacity: 0; transition: opacity 0.18s, background 0.18s; z-index: 5; box-shadow: 0 1px 4px rgba(0,0,0,0.25); pointer-events: none; } .card:hover .card-check, .card.selected .card-check { opacity: 1; pointer-events: auto; cursor: pointer; } .card.selected .card-check { background: var(--cld-accent); border-color: var(--cld-accent); } .card-check svg { width: 11px; height: 11px; opacity: 0; transition: opacity 0.18s; } .card.selected .card-check svg { opacity: 1; } /* Tags overlay on thumbnail */ .tags-overlay { position: absolute; top: 8px; right: 8px; display: flex; flex-wrap: wrap; gap: 4px; justify-content: flex-end; opacity: 0; transition: opacity 0.18s; z-index: 4; pointer-events: none; } .card:hover .tags-overlay { opacity: 1; } .tag-overlay { font-size: 10px; color: white; background: rgba(10, 12, 18, 0.55); padding: 2px 7px; border-radius: 20px; backdrop-filter: blur(6px); font-weight: 600; letter-spacing: 0.02em; } /* Floating action buttons */ .card-actions { position: absolute; bottom: 10px; left: 0; right: 0; display: flex; align-items: center; justify-content: center; gap: 5px; opacity: 0; transform: translateY(6px); transition: opacity 0.18s, transform 0.18s; z-index: 5; } .card:hover .card-actions { opacity: 1; transform: translateY(0); } .action-btn { height: 28px; padding: 0 11px; border-radius: 20px; border: none; font-size: 11px; font-weight: 600; cursor: pointer; display: flex; align-items: center; gap: 4px; transition: transform 0.12s ease, box-shadow 0.12s ease; white-space: nowrap; box-shadow: 0 2px 8px rgba(0,0,0,0.25); font-family: inherit; } .action-btn:hover { transform: translateY(-1px); box-shadow: 0 4px 12px rgba(0,0,0,0.3); } .action-btn:active { transform: translateY(0); } .action-btn.act-original { background: rgba(255,255,255,0.92); color: #111318; backdrop-filter: blur(8px); } [data-theme="dark"] .action-btn.act-original { background: rgba(40,44,56,0.92); color: #e0e4ec; } .action-btn.act-optimized { background: var(--cld-accent); color: white; } .action-btn.act-download { background: rgba(10,12,18,0.7); color: white; backdrop-filter: blur(8px); padding: 0 9px; font-size: 13px; } [data-theme="dark"] .action-btn.act-download { background: rgba(255,255,255,0.2); } /* ── Info section ── */ .info { padding: var(--cld-sp-xs) var(--cld-sp-sm) var(--cld-sp-sm); } .info .name { font-size: 13px; font-weight: 600; color: var(--cld-text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 6px; } .info .name .link { color: inherit; text-decoration: none; } .info .name .link:hover { color: var(--cld-accent); text-decoration: underline; } .pills { display: flex; flex-wrap: wrap; gap: 4px; } .pill { font-size: 10px; color: var(--cld-text2); background: var(--cld-bg); padding: 2px 7px; border-radius: 4px; border: 1px solid var(--cld-border); white-space: nowrap; } .tags { margin-top: 6px; display: flex; flex-wrap: wrap; gap: 4px; } .tag { font-size: var(--cld-font-xxs); background: var(--cld-chip-tag-bg); color: var(--cld-chip-tag-fg); padding: 2px 7px; border-radius: var(--cld-radius-lg); font-weight: 500; } .tag-more { background: var(--cld-bg3); color: var(--cld-text3); } .card-footer { display: flex; align-items: center; justify-content: space-between; margin-top: 8px; } .date { font-size: 10px; color: var(--cld-text3); } .details-link { font-size: 11px; color: var(--cld-accent); cursor: pointer; font-weight: 500; margin-left: auto; } .details-link:hover { text-decoration: underline; } /* ── Skeleton loading ── */ .skeleton { background: linear-gradient(90deg, var(--cld-bg3) 25%, var(--cld-bg4) 50%, var(--cld-bg3) 75%); background-size: 200% 100%; animation: shimmer 1.4s infinite; border-radius: var(--cld-radius); aspect-ratio: 4/3; } @keyframes shimmer { 0% { background-position: 200% 0; } 100% { background-position: -200% 0; } } /* ── Multi-select bar ── */ /* Wrapper takes layout space ONLY when a selection is active. When idle * it's display:none so it contributes 0 to scrollHeight (no phantom * scrollbar gutter / no extra iframe height). */ .select-bar-wrap { display: none; position: sticky; bottom: 0; left: 0; right: 0; justify-content: center; pointer-events: none; z-index: 100; height: 64px; } .select-bar-wrap.visible { display: flex; } .select-bar { position: absolute; bottom: 8px; transform: translateY(80px); background: #1a1d24; color: white; border-radius: 40px; padding: 0 6px 0 16px; height: 48px; display: flex; align-items: center; gap: 4px; box-shadow: 0 8px 32px rgba(0,0,0,0.4); transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.2s; opacity: 0; pointer-events: none; white-space: nowrap; } .select-bar-wrap.visible .select-bar { transform: translateY(0); opacity: 1; pointer-events: all; } .select-count { font-size: 13px; font-weight: 600; margin-right: 8px; } .bar-btn { height: 36px; padding: 0 14px; border: none; border-radius: 30px; font-size: 12px; font-weight: 600; cursor: pointer; transition: background 0.18s; display: flex; align-items: center; gap: 6px; font-family: inherit; } .bar-btn.bar-primary { background: var(--cld-accent); color: white; } .bar-btn.bar-primary:hover { opacity: 0.85; } .bar-btn.bar-secondary { background: rgba(255,255,255,0.12); color: white; } .bar-btn.bar-secondary:hover { background: rgba(255,255,255,0.2); } .bar-btn.bar-ghost { background: none; color: rgba(255,255,255,0.6); padding: 0 10px; } .bar-btn.bar-ghost:hover { color: white; } .bar-divider { width: 1px; height: 20px; background: rgba(255,255,255,0.15); margin: 0 4px; } /* ── Toast ── */ .gallery-toast { position: fixed; bottom: 86px; right: 20px; transform: translateY(20px); opacity: 0; background: #1a1d24; color: white; padding: 9px 18px; border-radius: 24px; font-size: 12px; font-weight: 600; box-shadow: 0 4px 16px rgba(0,0,0,0.25); transition: transform 0.25s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.2s; z-index: 999; pointer-events: none; } .gallery-toast.show { transform: translateY(0); opacity: 1; } `; const GALLERY_JS = /* js */ ` var LOG_PREFIX = "[gallery]"; var MIN_HEIGHT = 120; var allResources = []; var lastCursor = null; var pendingCall = { name: (typeof window.__MCP_TOOL_NAME__ === "string" && window.__MCP_TOOL_NAME__) || null, args: null, }; var selected = new Set(); var app = new MCPApp({ name: "Cloudinary Asset Gallery", version: "1.0.0" }); setupHostContext(app); function optimizedUrl(url, resource) { if (!url) return ""; if (resource && resource.resource_type === "raw") return url; return insertTransformation(url, "f_auto,q_auto", resource) || url; } function downloadUrl(url, resource) { if (!url) return ""; return insertTransformation(url, "fl_attachment", resource) || url; } var _toastTimer; function showToast(msg) { var t = document.getElementById("gallery-toast"); if (!t) return; t.textContent = msg; t.classList.add("show"); clearTimeout(_toastTimer); _toastTimer = setTimeout(function() { t.classList.remove("show"); }, 2000); } function updateSelectBar() { var wrap = document.getElementById("select-bar-wrap"); var countEl = document.getElementById("select-count"); if (!wrap || !countEl) return; var n = selected.size; countEl.textContent = n + " selected"; wrap.classList.toggle("visible", n > 0); var btn = document.getElementById("select-all-btn"); if (btn) { var visible = getVisibleIndices(); var allSelected = visible.length > 0 && visible.every(function(i) { return selected.has(i); }); btn.textContent = allSelected ? "Deselect all" : "Select all"; } var optBtn = document.getElementById("bar-copy-optimized"); if (optBtn) { var hasOptimizable = false; selected.forEach(function(i) { var r = allResources[i]; if (r && r.resource_type !== "raw") hasOptimizable = true; }); optBtn.style.display = hasOptimizable ? "" : "none"; } } function getVisibleIndices() { var visible = []; for (var i = 0; i < allResources.length; i++) { var card = document.getElementById("card-" + i); if (card && card.style.display !== "none") visible.push(i); } return visible; } function toggleSelectAll() { var visible = getVisibleIndices(); var allSelected = visible.length > 0 && visible.every(function(i) { return selected.has(i); }); if (allSelected) { clearSelection(); } else { for (var vi = 0; vi < visible.length; vi++) { selected.add(visible[vi]); var card = document.getElementById("card-" + visible[vi]); if (card) card.classList.add("selected"); } updateSelectBar(); } } function clearSelection() { selected.forEach(function(i) { var card = document.getElementById("card-" + i); if (card) card.classList.remove("selected"); }); selected.clear(); updateSelectBar(); } function toggleCard(idx) { var card = document.getElementById("card-" + idx); if (!card) return; if (selected.has(idx)) { selected.delete(idx); card.classList.remove("selected"); } else { selected.add(idx); card.classList.add("selected"); } updateSelectBar(); } function copyAssetUrl(type, idx) { var r = allResources[idx]; if (!r) return; var url = r.secure_url || r.url || ""; var copyUrl = type === "optimized" ? optimizedUrl(url, r) : url; if (!copyUrl) return; copyText(copyUrl).then(function() { showToast(type === "optimized" ? "\\u2728 Optimized URL copied" : "URL copied"); }).catch(function(e) { showError("Copy Failed", e && e.message ? e.message : String(e)); }); } function downloadOne(idx) { var r = allResources[idx]; if (!r) return; var url = r.secure_url || r.url || ""; var dl = downloadUrl(url, r); if (!dl) return; app._rpc("ui/open-link", { url: dl }); showToast("Downloading " + (r.display_name || r.filename || r.public_id || "asset")); } function copySelectedUrls(type) { var urls = []; selected.forEach(function(i) { var r = allResources[i]; if (!r) return; var url = r.secure_url || r.url || ""; urls.push(type === "optimized" ? optimizedUrl(url, r) : url); }); if (!urls.length) return; copyText(urls.join("\\n")).then(function() { showToast(urls.length + " " + (type === "optimized" ? "optimized " : "") + "URLs copied"); }).catch(function(e) { showError("Copy Failed", e && e.message ? e.message : String(e)); }); } async function downloadSelected() { if (selected.size === 0) return; var picks = []; selected.forEach(function(i) { var r = allResources[i]; if (r && r.public_id) picks.push(r); }); if (!picks.length) return; var requestBody = { mode: "create", target_format: "zip", keep_derived: true, target_public_id: "mcp-gallery-archive-" + Date.now(), fully_qualified_public_ids: picks.map(function(r) { return (r.resource_type || "image") + "/" + (r.type || "upload") + "/" + r.public_id; }), }; var btn = document.getElementById("bar-download"); var origLabel = btn ? btn.innerHTML : ""; if (btn) { btn.innerHTML = "Creating archive\\u2026"; btn.disabled = true; } try { var res = await app.callServerTool({ name: "generate-archive", arguments: { resource_type: "all", RequestBody: requestBody, }, }); var data = ingestResult(res); if (data && (data._error || data._parseError)) { showError("Archive Failed", unwrapApiError(data._message)); return; } var archiveUrl = data && (data.secure_url || data.url); if (!archiveUrl) { showError("Archive Failed", "No delivery URL returned."); return; } try { await copyText(archiveUrl); } catch (e) { /* ignore */ } app._rpc("ui/open-link", { url: archiveUrl }); showToast("Archive saved as raw in Cloudinary \\u2014 opening URL (" + picks.length + " asset" + (picks.length > 1 ? "s" : "") + ")"); } catch (e) { showError("Archive Failed", unwrapApiError(e && e.message ? e.message : String(e))); } finally { if (btn) { btn.innerHTML = origLabel; btn.disabled = false; } } } function unwrapApiError(raw) { if (!raw) return "Unknown error."; var msg = String(raw); try { if (msg.charAt(0) === "{") { var parsed = JSON.parse(msg); msg = (parsed && parsed.error && parsed.error.message) || msg; } } catch (e) { /* keep raw */ } return msg; } function render() { var root = document.getElementById("app"); if (allResources.length === 0) { selected.clear(); root.innerHTML = '