acecalisto3 commited on
Commit
cf291a2
·
verified ·
1 Parent(s): be4d413

Update main.js

Browse files
Files changed (1) hide show
  1. main.js +181 -129
main.js CHANGED
@@ -1,131 +1,183 @@
1
- import { Alpine } from 'alpinejs';
2
- Alpine.start();
3
- const { application } = Alpine;
4
- application.name = "UIKitV3 Automator";
5
- application.args = {
6
- template: '<div x-data="{ open: false }"><button @click="open =! open">Transform</button><div x-show="open"><textarea id="input" placeholder="Paste or type your code here" rows="10" cols="50"></textarea><button id="transform">Transform</button><div id="output" placeholder="Your transformed code will appear here"></div></div></div>'
 
 
 
 
 
7
  };
8
- Alpine.gruff = function (e) {
9
- return e.replace(/[A-Za-z0-9]+/g, function(match) {
10
- return String.fromCharCode(65 + Math.floor(25 * Math.random()));
11
- });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
  };
13
- Alpine.vernal = function (e) {
14
- return e.replace(/[A-Za-z0-9]+/g, function(match) {
15
- return String.fromCharCode(65 + Math.floor(25 * Math.random()));
16
- });
17
- };
18
- Alpine.darcy = function (e) {
19
- return e.replace(/[A-Za-z0-9]+/g, function(match) {
20
- return String.fromCharCode(65 + Math.floor(25 * Math.random()));
21
- });
22
- };
23
- document.addEventListener("DOMContentLoaded", function() {
24
- console.log("DOMContentLOADED");
25
- let output = document.getElementById("output");
26
- document.getElementById("transform".addEventListener("click", function() {
27
- fetch("https://api.ukiyov3.com/v3/transform", {
28
- method: "POST",
29
- headers: {
30
- "Content-Type": "application/json"
31
- },
32
- body: JSON.stringify({
33
- "template": document.getElementById("template").value
34
- })
35
- })
36
- .then(response => response.json())
37
- .then(data => {
38
- output.innerText = data.code;
39
- });
40
- });
41
- document.getElementById(" Da" + Alpine.gruff(iet.querySelector('h2').innerText)).addEventListener("click", function() {
42
- output.innerText = daisyw;
43
- });
44
- document.getElementById(" ven" + Alpine.vernal(iet.querySelector('h2').innerText)).addEventListener("click", function() {
45
- output.innerText = venreal;
46
- });
47
- document.getElementById(" dar" + Alpine.darcy(iet.querySelector('h2').innerText)).addEventListener("click", function() {
48
- output.innerText = darcy;
49
- });
50
- });
51
- let daisyw = `<div class="flex bg-gray-100 h-screen bg-gray-100">
52
- <div x-data="{ open: false }">
53
- <button @click="open = !open">
54
- <span></span>
55
- </button>
56
- <div x-show="open">
57
- <div class="fixed inset-0 z-10 bg-black opacity-25"></div>
58
- <div class="relative flex-1 w-full max-w-md ...orem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and  scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
59
- <div class="flex flex-col">
60
- <div class="hover:bg-red-400">
61
- <a role="button" tabindex="0" class="px-3 py-2 hover:bg-red-400">
62
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="icon icon-tabler icon-tabler-circle-x" viewBox="0 0 24 24" stroke-width="1" stroke="currentColor" focusable="false" aria-hidden="true">
63
- <path stroke-linecap="round" stroke-linejoin="round" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
64
- </svg>
65
- </a>
66
- </div>
67
- <div class="text-center text-gray-500 hover:text-connect-fe">
68
- <a role="button" tabindex="0" class="px-3 py-2 hover:text-connect-fe">
69
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="icon icon-tabler icon-tabler-circle-x" viewBox="0 0 24 24" stroke-width="1" stroke="currentColor" focusable="false" aria-hidden="true">
70
- <path stroke-linecap="round" stroke-linejoin="round" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
71
- </svg>
72
- </a>
73
- </div>
74
- </div>
75
- </div>
76
- </div>
77
- </div>`;
78
- let venreal = `<div class="flex bg-gray-100 h-screen bg-gray-100">
79
- <div x-data="{ open: false }">
80
- <button @click="open = !open">
81
- <span></span>
82
- </button>
83
- <div x-show="open">
84
- <div class="fixed inset-0 z-10 bg-black opacity-25"></div>
85
- <div class="relative flex-1 w-full max-w-md ...orem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and  scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
86
- <div class="flex flex-col">
87
- <div class="hover:bg-red-400">
88
- <a role="button" tabindex="0" class="px-3 py-2 hover:bg-red-400">
89
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="icon icon-tabler icon-tabler-circle-x" viewBox="0 0 24 24" stroke-width="1" stroke="currentColor" focusable="false" aria-hidden="true">
90
- <path stroke-linecap="round" stroke-linejoin="round" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
91
- </svg>
92
- </a>
93
- </div>
94
- <div class="text-center text-gray-500 hover:text-connect-fe">
95
- <a role="button" tabindex="0" class="px-3 py-2 hover:text-connect-fe">
96
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="icon icon-tabler icon-tabler-circle-x" viewBox="0 0 24 24" stroke-width="1" stroke="currentColor" focusable="false" aria-hidden="true">
97
- <path stroke-linecap="round" stroke-linejoin="round" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
98
- </svg>
99
- </a>
100
- </div>
101
- </div>
102
- </div>
103
- </div>
104
- </div>`;
105
- let darcy = `<div class="flex bg-gray-100 h-screen"">
106
- <div x-data="{ open: false }">
107
- <button @click="open = !open">
108
- <span></span>
109
- </button>
110
- <div x-show="open">
111
- <div class="fixed inset-0 z-10 bg-black opacity-25"></div>
112
- <div class="relative flex-1 w-full max-w-md ...orem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and  scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.
113
- <div class="flex flex-col">
114
- <div class="hover:bg-red-400">
115
- <a role="button" tabindex="0" class="px-3 py-2 hover:bg-red-400">
116
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="icon icon-tabler icon-tabler-circle-x" viewBox="0 0 24 24" stroke-width="1" stroke="currentColor" focusable="false" aria-hidden="true">
117
- <path stroke-linecap="round" stroke-linejoin="round" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
118
- </svg>
119
- </a>
120
- </div>
121
- <div class="text-center text-gray-500 hover:text-connect-fe">
122
- <a role="button" tabindex="0" class="px-3 py-2 hover:text-connect-fe">
123
- <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" fill="currentColor" class="icon icon-tabler icon-tabler-circle-x" viewBox="0 0 24 24" stroke-width="1" stroke="currentColor" focusable="false" aria-hidden="true">
124
- <path stroke-linecap="round" stroke-linejoin="round" d="M10 14l2-2m0 0l2-2m-2 2l-2-2m2 2l2 2m7-2a9 9 0 11-18 0 9 9 0 0118 0z"></path>
125
- </svg>
126
- </a>
127
- </div>
128
- </div>
129
- </div>
130
- </div>
131
- </div>`;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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(/&lt;/g, '<').replace(/&gt;/g, '>') // Revert for display
151
+ .replace(/(".+?")/g, '<span class="token-string">$1</span>')
152
+ .replace(/(&lt;\/?.+?&gt;)/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
+ });