Add UI components, i18n, settings store and base styles

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
denis
2026-10-14 22:11:52 +02:00
parent 07f3b22e58
commit 81f8402260
14 changed files with 1029 additions and 1 deletions
+151
View File
@@ -0,0 +1,151 @@
// Orchestre un niveau : affiche les étapes, exécute les cellules, valide, attribue la progression.
// Émet 'levelcomplete' (detail: résultat de ProgressStore.completeLevel).
import { ValidationEngine } from '../core/ValidationEngine.js';
import { CellView } from './CellView.js';
export class CellManager extends EventTarget {
#deps;
#session = null;
/**
* @param {object} deps
* @param {import('../i18n/i18n.js').I18n} deps.i18n
* @param {import('./NotebookView.js').NotebookView} deps.notebook
* @param {import('../core/PyodideWorkerClient.js').PyodideWorkerClient} deps.client
* @param {import('../core/ValidationEngine.js').ValidationEngine} deps.validator
* @param {import('../core/ErrorTranslator.js').ErrorTranslator} deps.translator
* @param {import('../core/AudioEngine.js').AudioEngine} deps.audio
* @param {import('../core/ProgressStore.js').ProgressStore} deps.progress
* @param {import('./BadgeToast.js').BadgeToast} deps.toast
*/
constructor(deps) {
super();
this.#deps = deps;
}
/** Démarre (ou redémarre) un niveau à partir d'une LevelSession. */
async start(session) {
const { notebook, client, i18n } = this.#deps;
this.#session = session;
notebook.clear();
notebook.setStatus(i18n.t('engine.loading'));
try {
await client.start();
await client.reset();
} catch {
notebook.setStatus(i18n.t('engine.failed'));
return;
}
notebook.setStatus('');
this.#showCurrent();
}
#showCurrent() {
const session = this.#session;
if (session.onMission) {
this.#showMission();
} else if (session.step.type === 'explain') {
this.#showExplain(session.step);
} else {
this.#showCode(session.step);
}
}
#announceProgress() {
const { notebook, i18n } = this.#deps;
const { index, stepCount } = this.#session;
notebook.setStatus(i18n.t('step.progress', { current: Math.min(index + 1, stepCount), total: stepCount }));
}
#advance() {
this.#session.next();
this.#showCurrent();
}
#showExplain(step) {
const { notebook, i18n } = this.#deps;
this.#announceProgress();
notebook.addExplain(step);
notebook.addAction(i18n.t('step.continue'), () => this.#advance());
}
#showCode(step) {
const { notebook, i18n } = this.#deps;
this.#announceProgress();
const cell = new CellView(i18n, step);
notebook.add(cell.element);
cell.focus();
cell.addEventListener('stop', () => this.#deps.client.interrupt());
let continueShown = false;
cell.addEventListener('run', async (event) => {
const passed = await this.#execute(cell, event.detail.code, step.tests);
if (passed && !continueShown) {
continueShown = true;
notebook.addAction(i18n.t('step.continue'), () => this.#advance());
}
});
}
#showMission() {
const { notebook, i18n } = this.#deps;
const mission = this.#session.mission;
notebook.setStatus(`${i18n.t('mission.label')} : ${mission.title}`);
if (mission.tests.length === 0) {
// Mission sans code (ex. niveau 00) : un simple bouton de validation.
notebook.addExplain({ text: mission.goal, voice: false, image: null });
notebook.addAction(i18n.t('mission.understand'), () => this.#completeLevel(mission));
return;
}
const cell = new CellView(i18n, { starter: mission.starter, instruction: mission.goal });
notebook.add(cell.element);
cell.focus();
cell.addEventListener('stop', () => this.#deps.client.interrupt());
cell.addEventListener('run', async (event) => {
if (await this.#execute(cell, event.detail.code, mission.tests)) {
await this.#completeLevel(mission);
}
});
}
/** Exécute le code d'une cellule et valide. Renvoie vrai si tous les tests passent. */
async #execute(cell, code, tests) {
const { client, validator, translator, audio, i18n } = this.#deps;
await audio.unlock(); // le clic sur « Exécuter » est le geste utilisateur requis
cell.clearOutput();
cell.setBusy(true);
let stdout = '';
const onOutput = (text) => {
stdout += text;
cell.appendOutput(text);
};
const result = await client.run(code, {
collect: ValidationEngine.variablesToCollect(tests),
onStdout: onOutput,
onStderr: onOutput,
});
cell.setBusy(false);
if (result.error) {
cell.showError(translator.translate(result.error, i18n.lang));
audio.play('error');
return false;
}
const validation = validator.validate(tests, { stdout, result });
cell.showValidation(validation);
audio.play(validation.passed ? 'success_small' : 'error');
return validation.passed;
}
async #completeLevel(mission) {
const { progress, toast, audio, i18n, notebook } = this.#deps;
const outcome = await progress.completeLevel(this.#session.id, this.#session.xp);
audio.play(mission.rewardSound ?? 'success_small');
toast.show(i18n.t(outcome.alreadyCompleted ? 'mission.already' : 'mission.done', { xp: outcome.xpGained }));
for (const badge of outcome.newBadges) {
toast.showBadge(badge);
}
notebook.setStatus(i18n.t('mission.done', { xp: outcome.xpGained }));
this.dispatchEvent(new CustomEvent('levelcomplete', { detail: outcome }));
}
}