Spaces:
Running
Running
File size: 5,688 Bytes
2c85448 de19334 2c85448 de19334 2c85448 de19334 2c85448 | 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 | // ===== util =====
const toastEl = document.getElementById("toast");
function toast(msg){
toastEl.hidden = false;
toastEl.textContent = msg;
clearTimeout(toast._t);
toast._t = setTimeout(()=>toastEl.hidden=true, 1400);
}
// ===== éléments =====
const promptInput = document.getElementById("promptInput");
const generateBtn = document.getElementById("generateBtn");
const micBtn = document.getElementById("micBtn");
const codeOutput = document.getElementById("codeOutput");
const copyBtn = document.getElementById("copyBtn");
const clearBtn = document.getElementById("clearBtn");
const colRight = document.getElementById("colRight");
const appTitle = document.getElementById("appTitle");
const appSubtitle = document.getElementById("appSubtitle");
const stage = document.getElementById("stage");
// ===== générateur de code (temporaire) =====
function fakeGenerate(prompt){
const p = (prompt || "").trim();
if(!p) return "// Écris une demande puis clique sur Générer ✨";
return `// ✅ Demande reçue : ${p}
function main() {
console.log("Hello Espace Codage!");
}
main();`;
}
// ===== Rosalinda helpers =====
async function rosalindaGenerateImage(prompt) {
const r = await fetch("http://localhost:3001/api/image", {
method: "POST",
headers: {"Content-Type":"application/json"},
body: JSON.stringify({ prompt, steps: 24 })
});
const data = await r.json();
if (!data.ok) throw new Error(data.error || "Erreur Rosalinda");
stage.innerHTML = `
<div style="font-weight:800;margin-bottom:10px;">Rosalinda — Image</div>
<img src="${data.url}" style="width:100%;border-radius:14px;border:1px solid #1f2a44;" />
`;
}
async function rosalindaGenerateVideo(prompt) {
const r = await fetch("http://localhost:3001/api/video", {
method: "POST",
headers: {"Content-Type":"application/json"},
body: JSON.stringify({ prompt })
});
const data = await r.json();
if (!data.ok) throw new Error(data.error || "Erreur Rosalinda");
stage.innerHTML = `
<div style="font-weight:800;margin-bottom:10px;">Rosalinda — Vidéo</div>
<video src="${data.url}" controls style="width:100%;border-radius:14px;border:1px solid #1f2a44;"></video>
`;
}
// ===== moteur de consignes UI (simple et efficace) =====
function isUiCommand(text){
const t = (text||"").toLowerCase();
return ["colonne","cache","affiche","vide","titre","sous-titre","espace codage"].some(k=>t.includes(k));
}
function applyUiCommand(text){
const raw = text || "";
const t = raw.toLowerCase();
if(t.includes("cache") && (t.includes("colonne 3") || t.includes("colonne trois") || t.includes("droite"))){
colRight.style.display = "none";
toast("✅ Colonne 3 masquée");
return true;
}
if((t.includes("affiche") || t.includes("montre")) && (t.includes("colonne 3") || t.includes("droite"))){
colRight.style.display = "";
toast("✅ Colonne 3 affichée");
return true;
}
if(t.includes("colonne 3") && t.includes("vide")){
colRight.innerHTML = "";
colRight.style.display = "";
toast("✅ Colonne 3 vidée");
return true;
}
// Titre: ...
const mTitle = raw.match(/(?:titre|nom)\s*[:=]\s*(.+)$/i);
if(mTitle){
appTitle.textContent = mTitle[1].trim();
toast("✅ Titre mis à jour");
return true;
}
// Sous-titre: ...
const mSub = raw.match(/(?:sous[-\s]?titre|description)\s*[:=]\s*(.+)$/i);
if(mSub){
appSubtitle.textContent = mSub[1].trim();
toast("✅ Sous-titre mis à jour");
return true;
}
toast("ℹ️ Consigne non reconnue");
return false;
}
// ===== actions =====
generateBtn.addEventListener("click", ()=>{
const text = promptInput.value;
if(isUiCommand(text)){
if(applyUiCommand(text)) return;
}
// Example: si tu écris "image: ..." -> image
if (text.toLowerCase().startsWith("image:")) {
rosalindaGenerateImage(text.slice(6).trim()).catch(err => {
console.error(err);
toast("❌ " + (err.message || "Erreur image"));
});
return;
}
// Example: si tu écris "video: ..." -> video
if (text.toLowerCase().startsWith("video:")) {
rosalindaGenerateVideo(text.slice(6).trim()).catch(err => {
console.error(err);
toast("❌ " + (err.message || "Erreur vidéo"));
});
return;
}
codeOutput.textContent = fakeGenerate(text);
});
document.querySelectorAll(".chip").forEach(btn=>{
btn.addEventListener("click", ()=>{
const p = btn.dataset.prompt || "";
promptInput.value = p;
codeOutput.textContent = fakeGenerate(p);
});
});
copyBtn.addEventListener("click", async ()=>{
try{
await navigator.clipboard.writeText(codeOutput.textContent);
toast("✅ Copié");
}catch{
alert("Copie impossible automatiquement.");
}
});
clearBtn.addEventListener("click", ()=>{
promptInput.value = "";
codeOutput.textContent = "// Le code généré apparaîtra ici…";
toast("✅ Effacé");
});
// Micro (si dispo)
let recognition = null;
if ("webkitSpeechRecognition" in window || "SpeechRecognition" in window) {
const SR = window.SpeechRecognition || window.webkitSpeechRecognition;
recognition = new SR();
recognition.lang = "fr-FR";
recognition.interimResults = true;
recognition.onresult = (event) => {
let transcript = "";
for (let i = event.resultIndex; i < event.results.length; i++) {
transcript += event.results[i][0].transcript;
}
promptInput.value = transcript.trim();
};
}
micBtn.addEventListener("click", ()=>{
if(!recognition){ alert("Micro non disponible sur ce navigateur."); return; }
try{ recognition.start(); }catch{}
});
// ✅ preuve que le JS charge
toast("✅ Espace Codage prêt"); |