Spaces:
Sleeping
Sleeping
Update main.js
Browse files
main.js
CHANGED
|
@@ -1,131 +1,183 @@
|
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
/**
|
| 2 |
+
* main.js — YOOtheme Alchemy Controller
|
| 3 |
+
* Architecture: ES6 Modules | Async/Await | Event Delegation
|
| 4 |
+
* * "The Hand that wields the logic."
|
| 5 |
+
*/
|
| 6 |
+
|
| 7 |
+
// === CONFIGURATION ===
|
| 8 |
+
const CONFIG = {
|
| 9 |
+
API_ENDPOINT: "/gradio_api/call/predict", // Points to local Gradio instance
|
| 10 |
+
ANIMATION_DURATION: 300,
|
| 11 |
+
DEBOUNCE_DELAY: 150
|
| 12 |
};
|
| 13 |
+
|
| 14 |
+
// === UTILITIES ===
|
| 15 |
+
const Utils = {
|
| 16 |
+
debounce(func, wait) {
|
| 17 |
+
let timeout;
|
| 18 |
+
return function(...args) {
|
| 19 |
+
clearTimeout(timeout);
|
| 20 |
+
timeout = setTimeout(() => func.apply(this, args), wait);
|
| 21 |
+
};
|
| 22 |
+
},
|
| 23 |
+
|
| 24 |
+
sanitize(str) {
|
| 25 |
+
const div = document.createElement('div');
|
| 26 |
+
div.textContent = str;
|
| 27 |
+
return div.innerHTML;
|
| 28 |
+
},
|
| 29 |
+
|
| 30 |
+
copyToClipboard(text) {
|
| 31 |
+
navigator.clipboard.writeText(text).then(() => {
|
| 32 |
+
UI.showToast("✨ Code copied to clipboard", "success");
|
| 33 |
+
}).catch(err => {
|
| 34 |
+
UI.showToast("❌ Failed to copy", "error");
|
| 35 |
+
console.error(err);
|
| 36 |
+
});
|
| 37 |
+
}
|
| 38 |
};
|
| 39 |
+
|
| 40 |
+
// === API CLIENT ===
|
| 41 |
+
class AlchemyClient {
|
| 42 |
+
constructor(endpoint) {
|
| 43 |
+
this.endpoint = endpoint;
|
| 44 |
+
}
|
| 45 |
+
|
| 46 |
+
async generate(prompt, componentType) {
|
| 47 |
+
try {
|
| 48 |
+
// Simulating connection to the Gradio Backend (app.py)
|
| 49 |
+
// Note: Actual implementation depends on specific Gradio API exposure
|
| 50 |
+
const response = await fetch(this.endpoint, {
|
| 51 |
+
method: "POST",
|
| 52 |
+
headers: { "Content-Type": "application/json" },
|
| 53 |
+
body: JSON.stringify({
|
| 54 |
+
data: [prompt, [], componentType] // Standard Gradio payload structure
|
| 55 |
+
})
|
| 56 |
+
});
|
| 57 |
+
|
| 58 |
+
if (!response.ok) throw new Error(`HTTP Error: ${response.status}`);
|
| 59 |
+
|
| 60 |
+
const result = await response.json();
|
| 61 |
+
return result.data ? result.data[0] : null;
|
| 62 |
+
|
| 63 |
+
} catch (error) {
|
| 64 |
+
console.error("Alchemy API Error:", error);
|
| 65 |
+
throw error;
|
| 66 |
+
}
|
| 67 |
+
}
|
| 68 |
+
}
|
| 69 |
+
|
| 70 |
+
// === UI CONTROLLER ===
|
| 71 |
+
class UIController {
|
| 72 |
+
constructor() {
|
| 73 |
+
this.elements = {
|
| 74 |
+
input: document.getElementById('user-prompt'),
|
| 75 |
+
generateBtn: document.getElementById('generate-btn'),
|
| 76 |
+
outputContainer: document.getElementById('code-output'),
|
| 77 |
+
outputPre: document.getElementById('output-pre'),
|
| 78 |
+
componentSelect: document.getElementById('component-select'),
|
| 79 |
+
loader: document.getElementById('alchemy-loader'),
|
| 80 |
+
toast: document.getElementById('toast-container')
|
| 81 |
+
};
|
| 82 |
+
|
| 83 |
+
this.client = new AlchemyClient(CONFIG.API_ENDPOINT);
|
| 84 |
+
this.initEventListeners();
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
initEventListeners() {
|
| 88 |
+
// Generate Button
|
| 89 |
+
this.elements.generateBtn.addEventListener('click', () => this.handleGeneration());
|
| 90 |
+
|
| 91 |
+
// Ctrl+Enter Shortcut
|
| 92 |
+
this.elements.input.addEventListener('keydown', (e) => {
|
| 93 |
+
if ((e.ctrlKey || e.metaKey) && e.key === 'Enter') {
|
| 94 |
+
this.handleGeneration();
|
| 95 |
+
}
|
| 96 |
+
});
|
| 97 |
+
|
| 98 |
+
// Copy Button (Event Delegation)
|
| 99 |
+
document.addEventListener('click', (e) => {
|
| 100 |
+
if (e.target.closest('.copy-btn')) {
|
| 101 |
+
const code = this.elements.outputPre.textContent;
|
| 102 |
+
Utils.copyToClipboard(code);
|
| 103 |
+
}
|
| 104 |
+
});
|
| 105 |
+
}
|
| 106 |
+
|
| 107 |
+
setLoading(isLoading) {
|
| 108 |
+
if (isLoading) {
|
| 109 |
+
this.elements.generateBtn.disabled = true;
|
| 110 |
+
this.elements.generateBtn.innerHTML = `<span>Transmuting...</span> <div class="spinner"></div>`;
|
| 111 |
+
this.elements.outputContainer.classList.add('opacity-50', 'blur-sm');
|
| 112 |
+
} else {
|
| 113 |
+
this.elements.generateBtn.disabled = false;
|
| 114 |
+
this.elements.generateBtn.innerHTML = `<span>Generate Code</span> <span class="icon">✨</span>`;
|
| 115 |
+
this.elements.outputContainer.classList.remove('opacity-50', 'blur-sm');
|
| 116 |
+
}
|
| 117 |
+
}
|
| 118 |
+
|
| 119 |
+
async handleGeneration() {
|
| 120 |
+
const prompt = this.elements.input.value.trim();
|
| 121 |
+
const type = this.elements.componentSelect.value;
|
| 122 |
+
|
| 123 |
+
if (!prompt) {
|
| 124 |
+
this.showToast("⚠️ Please enter a prompt first.", "warning");
|
| 125 |
+
return;
|
| 126 |
+
}
|
| 127 |
+
|
| 128 |
+
this.setLoading(true);
|
| 129 |
+
|
| 130 |
+
try {
|
| 131 |
+
// For demo purposes, if API is not connected, we mock a response
|
| 132 |
+
// to show the UI behavior (remove this in production)
|
| 133 |
+
const result = await this.client.generate(prompt, type).catch(() => {
|
| 134 |
+
// Fallback mock for UI demonstration if backend isn't reachable
|
| 135 |
+
return new Promise(resolve => setTimeout(() => resolve(`\n<div class="uk-section uk-section-default">\n <div class="uk-container">\n <h1>${Utils.sanitize(prompt)}</h1>\n \n </div>\n</div>`), 1500));
|
| 136 |
+
});
|
| 137 |
+
|
| 138 |
+
this.renderOutput(result);
|
| 139 |
+
this.showToast("✅ Generation Complete", "success");
|
| 140 |
+
} catch (error) {
|
| 141 |
+
this.showToast("❌ System Error: Could not reach Alchemy Core.", "error");
|
| 142 |
+
} finally {
|
| 143 |
+
this.setLoading(false);
|
| 144 |
+
}
|
| 145 |
+
}
|
| 146 |
+
|
| 147 |
+
renderOutput(code) {
|
| 148 |
+
// Simple syntax highlighting simulation
|
| 149 |
+
const highlighted = Utils.sanitize(code)
|
| 150 |
+
.replace(/</g, '<').replace(/>/g, '>') // Revert for display
|
| 151 |
+
.replace(/(".+?")/g, '<span class="token-string">$1</span>')
|
| 152 |
+
.replace(/(<\/?.+?>)/g, '<span class="token-tag">$1</span>');
|
| 153 |
+
|
| 154 |
+
this.elements.outputPre.innerHTML = highlighted;
|
| 155 |
+
|
| 156 |
+
// Animate in
|
| 157 |
+
this.elements.outputContainer.classList.remove('hidden');
|
| 158 |
+
this.elements.outputContainer.animate([
|
| 159 |
+
{ opacity: 0, transform: 'translateY(20px)' },
|
| 160 |
+
{ opacity: 1, transform: 'translateY(0)' }
|
| 161 |
+
], { duration: 400, easing: 'cubic-bezier(0.2, 0.8, 0.2, 1)' });
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
showToast(message, type = 'info') {
|
| 165 |
+
const toast = document.createElement('div');
|
| 166 |
+
toast.className = `toast toast-${type}`;
|
| 167 |
+
toast.textContent = message;
|
| 168 |
+
|
| 169 |
+
this.elements.toast.appendChild(toast);
|
| 170 |
+
|
| 171 |
+
// Remove after 3s
|
| 172 |
+
setTimeout(() => {
|
| 173 |
+
toast.style.opacity = '0';
|
| 174 |
+
setTimeout(() => toast.remove(), 300);
|
| 175 |
+
}, 3000);
|
| 176 |
+
}
|
| 177 |
+
}
|
| 178 |
+
|
| 179 |
+
// === BOOTSTRAP ===
|
| 180 |
+
document.addEventListener('DOMContentLoaded', () => {
|
| 181 |
+
window.UI = new UIController();
|
| 182 |
+
console.log("⚡ Alchemy Client Initialized");
|
| 183 |
+
});
|