Spaces:
Running
Running
File size: 13,718 Bytes
e855e5e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 |
/**
* Image Manager Web Component
* Centralized management of all image prompts and configurations
* Provides a single source of truth for all image URLs in the application
*/
class ImageManager extends HTMLElement {
constructor() {
super();
this.attachShadow({ mode: 'open' });
this.config = null;
this.imageCache = new Map();
}
connectedCallback() {
this.loadConfig();
this.render();
}
loadConfig() {
// Try to load from JSON config element
const configElement = document.getElementById('imagePromptsConfig');
if (configElement) {
try {
this.config = JSON.parse(configElement.textContent);
} catch (e) {
console.error('Failed to parse image prompts config:', e);
this.config = this.getDefaultConfig();
}
} else {
this.config = this.getDefaultConfig();
}
// Store in global window object for easy access
window.imagePrompts = this.config;
}
getDefaultConfig() {
return {
hero: "https://static.photos/medical/800x600/1",
productImages: [
"https://static.photos/medical/400x300/101",
"https://static.photos/medical/400x300/102",
"https://static.photos/medical/400x300/103",
"https://static.photos/medical/400x300/104"
],
about: "https://static.photos/workspace/800x600/201",
testimonials: [
"https://static.photos/people/48x48/301",
"https://static.photos/people/48x48/302",
"https://static.photos/people/48x48/303"
],
categories: {
medical: "medical",
workspace: "workspace",
people: "people",
abstract: "abstract"
},
placeholder: "https://static.photos/abstract/100x100",
fallback: "https://static.photos/abstract/400x300"
};
}
render() {
this.shadowRoot.innerHTML = `
<style>
:host {
display: none;
}
.image-manager-panel {
position: fixed;
top: 100px;
right: 20px;
width: 300px;
background: white;
border-radius: 8px;
box-shadow: 0 4px 20px rgba(0,0,0,0.15);
padding: 20px;
z-index: 9999;
display: none;
font-family: -apple-system, BlinkMacSystemFont, sans-serif;
}
.image-manager-panel.active {
display: block;
}
.image-manager-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 15px;
padding-bottom: 10px;
border-bottom: 1px solid #e5e5e5;
}
.image-manager-title {
font-weight: bold;
font-size: 16px;
color: #333;
}
.image-manager-close {
background: none;
border: none;
font-size: 20px;
cursor: pointer;
color: #666;
}
.image-manager-section {
margin-bottom: 15px;
}
.image-manager-section-title {
font-size: 14px;
font-weight: 600;
color: #555;
margin-bottom: 8px;
}
.image-manager-list {
display: flex;
flex-direction: column;
gap: 5px;
}
.image-manager-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 8px;
background: #f8f9fa;
border-radius: 4px;
font-size: 12px;
}
.image-manager-item:hover {
background: #e9ecef;
}
.image-manager-url {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
max-width: 180px;
}
.image-manager-copy {
background: #5d9245;
color: white;
border: none;
border-radius: 3px;
padding: 3px 8px;
font-size: 11px;
cursor: pointer;
}
.image-manager-copy:hover {
background: #3f7344;
}
.image-manager-toggle {
position: fixed;
top: 150px;
right: 20px;
background: #5d9245;
color: white;
border: none;
border-radius: 50%;
width: 40px;
height: 40px;
display: flex;
align-items: center;
justify-content: center;
cursor: pointer;
z-index: 9998;
box-shadow: 0 2px 10px rgba(0,0,0,0.2);
}
</style>
<button class="image-manager-toggle">
<i class="fas fa-images"></i>
</button>
<div class="image-manager-panel">
<div class="image-manager-header">
<div class="image-manager-title">Image Prompts Manager</div>
<button class="image-manager-close">×</button>
</div>
<div class="image-manager-section">
<div class="image-manager-section-title">Hero Image</div>
<div class="image-manager-list">
<div class="image-manager-item">
<div class="image-manager-url" title="${this.config?.hero || ''}">
${this.config?.hero ? this.config.hero.substring(0, 30) + '...' : ''}
</div>
<button class="image-manager-copy" data-url="${this.config?.hero || ''}">Copy</button>
</div>
</div>
</div>
<div class="image-manager-section">
<div class="image-manager-section-title">Product Images (${this.config?.productImages?.length || 0})</div>
<div class="image-manager-list">
${this.config?.productImages?.map((url, index) => `
<div class="image-manager-item">
<div class="image-manager-url" title="${url}">
Product ${index + 1}: ${url.substring(0, 25)}...
</div>
<button class="image-manager-copy" data-url="${url}">Copy</button>
</div>
`).join('') || '<div style="padding: 8px; color: #666; font-size: 12px;">No product images configured</div>'}
</div>
</div>
<div class="image-manager-section">
<div class="image-manager-section-title">Testimonial Images (${this.config?.testimonials?.length || 0})</div>
<div class="image-manager-list">
${this.config?.testimonials?.map((url, index) => `
<div class="image-manager-item">
<div class="image-manager-url" title="${url}">
Testimonial ${index + 1}: ${url.substring(0, 25)}...
</div>
<button class="image-manager-copy" data-url="${url}">Copy</button>
</div>
`).join('') || '<div style="padding: 8px; color: #666; font-size: 12px;">No testimonial images configured</div>'}
</div>
</div>
<div class="image-manager-section">
<div class="image-manager-section-title">Configuration</div>
<div class="image-manager-list">
<div class="image-manager-item">
<div>Categories: ${this.config?.categories ? Object.keys(this.config.categories).join(', ') : ''}</div>
<button class="image-manager-copy" data-url="${JSON.stringify(this.config)}">Copy Config</button>
</div>
</div>
</div>
</div>
`;
this.addEventListeners();
}
addEventListeners() {
const toggleBtn = this.shadowRoot.querySelector('.image-manager-toggle');
const closeBtn = this.shadowRoot.querySelector('.image-manager-close');
const panel = this.shadowRoot.querySelector('.image-manager-panel');
const copyButtons = this.shadowRoot.querySelectorAll('.image-manager-copy');
if (toggleBtn && panel) {
toggleBtn.addEventListener('click', () => {
panel.classList.toggle('active');
});
}
if (closeBtn && panel) {
closeBtn.addEventListener('click', () => {
panel.classList.remove('active');
});
}
if (copyButtons) {
copyButtons.forEach(btn => {
btn.addEventListener('click', (e) => {
const url = e.currentTarget.getAttribute('data-url');
this.copyToClipboard(url);
// Show feedback
const originalText = e.currentTarget.textContent;
e.currentTarget.textContent = 'Copied!';
e.currentTarget.style.background = '#28a745';
setTimeout(() => {
e.currentTarget.textContent = originalText;
e.currentTarget.style.background = '#5d9245';
}, 1500);
});
});
}
}
copyToClipboard(text) {
navigator.clipboard.writeText(text).then(() => {
console.log('Image URL copied to clipboard:', text);
}).catch(err => {
console.error('Failed to copy:', err);
// Fallback method
const textArea = document.createElement('textarea');
textArea.value = text;
document.body.appendChild(textArea);
textArea.select();
document.execCommand('copy');
document.body.removeChild(textArea);
});
}
// Public API methods
getImage(category, index = 0) {
if (!this.config) return this.config?.fallback || '';
switch(category) {
case 'hero':
return this.config.hero;
case 'product':
return this.config.productImages[index] || this.config.fallback;
case 'testimonial':
return this.config.testimonials[index] || this.config.fallback;
case 'about':
return this.config.about;
case 'placeholder':
return this.config.placeholder;
default:
return this.config.fallback;
}
}
updateImage(category, value, index = 0) {
if (!this.config) return false;
switch(category) {
case 'hero':
this.config.hero = value;
break;
case 'product':
if (this.config.productImages && index < this.config.productImages.length) {
this.config.productImages[index] = value;
}
break;
case 'testimonial':
if (this.config.testimonials && index < this.config.testimonials.length) {
this.config.testimonials[index] = value;
}
break;
case 'about':
this.config.about = value;
break;
default:
return false;
}
// Update the JSON config element
const configElement = document.getElementById('imagePromptsConfig');
if (configElement) {
configElement.textContent = JSON.stringify(this.config, null, 2);
}
return true;
}
static get observedAttributes() {
return ['config'];
}
attributeChangedCallback(name, oldValue, newValue) {
if (name === 'config' && oldValue !== newValue) {
this.loadConfig();
this.render();
}
}
}
customElements.define('image-manager', ImageManager); |