mirror of
https://github.com/deunix-educ/PyQuest.git
synced 2026-11-25 08:19:44 +01:00
Editor: Tab indents, Shift+Tab dedents, Esc then Tab leaves the editor
Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
+1
-1
@@ -18,7 +18,7 @@
|
|||||||
"cell.output_empty": "(nothing printed)",
|
"cell.output_empty": "(nothing printed)",
|
||||||
"cell.input_label": "Answer requested by the program",
|
"cell.input_label": "Answer requested by the program",
|
||||||
"cell.input_send": "Send",
|
"cell.input_send": "Send",
|
||||||
"cell.shortcut": "Ctrl/Cmd + Enter to run",
|
"cell.shortcut": "Ctrl/Cmd + Enter to run · Tab indents, Esc then Tab leaves the editor",
|
||||||
"cell.passed": "Well done, that's correct!",
|
"cell.passed": "Well done, that's correct!",
|
||||||
"cell.failed": "Not yet. Here is what is wrong:",
|
"cell.failed": "Not yet. Here is what is wrong:",
|
||||||
"cell.line": "Line {line}",
|
"cell.line": "Line {line}",
|
||||||
|
|||||||
+1
-1
@@ -18,7 +18,7 @@
|
|||||||
"cell.output_empty": "(aucun affichage)",
|
"cell.output_empty": "(aucun affichage)",
|
||||||
"cell.input_label": "Réponse demandée par le programme",
|
"cell.input_label": "Réponse demandée par le programme",
|
||||||
"cell.input_send": "Envoyer",
|
"cell.input_send": "Envoyer",
|
||||||
"cell.shortcut": "Ctrl/Cmd + Entrée pour exécuter",
|
"cell.shortcut": "Ctrl/Cmd + Entrée pour exécuter · Tab indente, Échap puis Tab pour quitter l'éditeur",
|
||||||
"cell.passed": "Bravo, c'est correct !",
|
"cell.passed": "Bravo, c'est correct !",
|
||||||
"cell.failed": "Pas encore. Voici ce qui ne va pas :",
|
"cell.failed": "Pas encore. Voici ce qui ne va pas :",
|
||||||
"cell.line": "Ligne {line}",
|
"cell.line": "Ligne {line}",
|
||||||
|
|||||||
+80
-1
@@ -5,11 +5,14 @@ import { el } from './dom.js';
|
|||||||
import { tokenizePython } from './highlightPython.js';
|
import { tokenizePython } from './highlightPython.js';
|
||||||
|
|
||||||
const MIN_ROWS = 3;
|
const MIN_ROWS = 3;
|
||||||
|
const INDENT = ' '; // 4 espaces, convention du projet
|
||||||
|
|
||||||
export class CodeEditor {
|
export class CodeEditor {
|
||||||
#root;
|
#root;
|
||||||
#textarea;
|
#textarea;
|
||||||
#highlight;
|
#highlight;
|
||||||
|
// Échap suspend la capture de Tab : le clavier ne reste jamais piégé dans l'éditeur (WCAG 2.1.2).
|
||||||
|
#tabReleased = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param {object} options
|
* @param {object} options
|
||||||
@@ -29,7 +32,11 @@ export class CodeEditor {
|
|||||||
...attrs,
|
...attrs,
|
||||||
oninput: () => this.#render(),
|
oninput: () => this.#render(),
|
||||||
onscroll: () => this.#syncScroll(),
|
onscroll: () => this.#syncScroll(),
|
||||||
onkeydown: onKeyDown,
|
onkeydown: (event) => {
|
||||||
|
this.#onKeyDown(event);
|
||||||
|
onKeyDown(event);
|
||||||
|
},
|
||||||
|
onblur: () => (this.#tabReleased = false),
|
||||||
});
|
});
|
||||||
this.#root = el('div', { class: 'code-editor-wrap' }, this.#highlight, this.#textarea);
|
this.#root = el('div', { class: 'code-editor-wrap' }, this.#highlight, this.#textarea);
|
||||||
this.value = value;
|
this.value = value;
|
||||||
@@ -52,6 +59,78 @@ export class CodeEditor {
|
|||||||
this.#textarea.focus();
|
this.#textarea.focus();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** Tab indente (Maj+Tab désindente) ; Échap puis Tab quitte l'éditeur. */
|
||||||
|
#onKeyDown(event) {
|
||||||
|
if (event.key === 'Escape') {
|
||||||
|
this.#tabReleased = true;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (event.key !== 'Tab' || event.ctrlKey || event.metaKey || event.altKey) {
|
||||||
|
if (!['Shift', 'Control', 'Alt', 'Meta'].includes(event.key)) {
|
||||||
|
this.#tabReleased = false;
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (this.#tabReleased) {
|
||||||
|
return; // Tab garde son rôle de navigation
|
||||||
|
}
|
||||||
|
event.preventDefault();
|
||||||
|
const { selectionStart: start, selectionEnd: end, value } = this.#textarea;
|
||||||
|
const multiline = value.slice(start, end).includes('\n');
|
||||||
|
if (event.shiftKey || multiline) {
|
||||||
|
this.#shiftLines(start, end, event.shiftKey ? -1 : 1);
|
||||||
|
} else {
|
||||||
|
// Complète jusqu'à la prochaine colonne multiple de 4.
|
||||||
|
const column = start - (value.lastIndexOf('\n', start - 1) + 1);
|
||||||
|
this.#replace(start, end, INDENT.slice(column % INDENT.length));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Indente (+1) ou désindente (-1) toutes les lignes touchées par la sélection. */
|
||||||
|
#shiftLines(start, end, direction) {
|
||||||
|
const { value } = this.#textarea;
|
||||||
|
const from = value.lastIndexOf('\n', start - 1) + 1;
|
||||||
|
// Une sélection qui se termine en début de ligne ne touche pas cette dernière ligne.
|
||||||
|
const to = end > start && value[end - 1] === '\n' ? end - 1 : end;
|
||||||
|
const lines = value.slice(from, to).split('\n');
|
||||||
|
let firstDelta = 0;
|
||||||
|
let total = 0;
|
||||||
|
const changed = lines.map((line, index) => {
|
||||||
|
let next = line;
|
||||||
|
if (direction > 0) {
|
||||||
|
next = line === '' ? line : INDENT + line;
|
||||||
|
} else {
|
||||||
|
const remove = /^ {1,4}|^\t/.exec(line)?.[0].length ?? 0;
|
||||||
|
next = line.slice(remove);
|
||||||
|
}
|
||||||
|
const delta = next.length - line.length;
|
||||||
|
if (index === 0) {
|
||||||
|
firstDelta = delta;
|
||||||
|
}
|
||||||
|
total += delta;
|
||||||
|
return next;
|
||||||
|
});
|
||||||
|
this.#replace(from, to, changed.join('\n'), Math.max(from, start + firstDelta), Math.max(from, end + total));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Remplace [start, end) par `text` en conservant l'historique d'annulation quand le navigateur le permet. */
|
||||||
|
#replace(start, end, text, selectionStart = start + text.length, selectionEnd = selectionStart) {
|
||||||
|
const area = this.#textarea;
|
||||||
|
area.setSelectionRange(start, end);
|
||||||
|
// execCommand est obsolète mais reste le seul moyen d'insérer en gardant Ctrl+Z ; repli : setRangeText.
|
||||||
|
let done = false;
|
||||||
|
try {
|
||||||
|
done = document.execCommand('insertText', false, text);
|
||||||
|
} catch {
|
||||||
|
done = false;
|
||||||
|
}
|
||||||
|
if (!done) {
|
||||||
|
area.setRangeText(text, start, end, 'end');
|
||||||
|
this.#render();
|
||||||
|
}
|
||||||
|
area.setSelectionRange(selectionStart, selectionEnd);
|
||||||
|
}
|
||||||
|
|
||||||
#render() {
|
#render() {
|
||||||
const code = this.#textarea.value;
|
const code = this.#textarea.value;
|
||||||
this.#textarea.rows = Math.max(MIN_ROWS, code.split('\n').length + 1);
|
this.#textarea.rows = Math.max(MIN_ROWS, code.split('\n').length + 1);
|
||||||
|
|||||||
@@ -386,6 +386,56 @@ test('éditeur : coloration syntaxique, textarea réel, polices de la charte', {
|
|||||||
assert.deepEqual(errors, []);
|
assert.deepEqual(errors, []);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('éditeur : Tab indente, Maj+Tab désindente, Échap puis Tab quitte l\'éditeur', { skip, timeout: 120000 }, async () => {
|
||||||
|
const page = await (await browser.newContext()).newPage();
|
||||||
|
await page.goto(`${base}#/levels/07-while`);
|
||||||
|
await page.waitForSelector('.explain', { timeout: 60000 });
|
||||||
|
await page.locator('.notebook-action button').last().click();
|
||||||
|
const area = page.locator('.cell').last().locator('textarea');
|
||||||
|
const value = () => area.inputValue();
|
||||||
|
await area.fill('while True:\nx = 1');
|
||||||
|
|
||||||
|
// Tab au début de la ligne 2 : 4 espaces (et non un changement de focus).
|
||||||
|
await area.evaluate((node) => node.setSelectionRange(12, 12));
|
||||||
|
await area.focus();
|
||||||
|
await page.keyboard.press('Tab');
|
||||||
|
assert.equal(await value(), 'while True:\n x = 1');
|
||||||
|
assert.equal(await page.evaluate(() => document.activeElement.tagName), 'TEXTAREA');
|
||||||
|
|
||||||
|
// Tab en milieu de ligne : complète jusqu'à la colonne multiple de 4.
|
||||||
|
await page.keyboard.press('End');
|
||||||
|
await page.keyboard.press('Tab');
|
||||||
|
assert.equal(await value(), 'while True:\n x = 1 ');
|
||||||
|
|
||||||
|
// Maj+Tab désindente la ligne.
|
||||||
|
await page.keyboard.press('Shift+Tab');
|
||||||
|
assert.equal(await value(), 'while True:\nx = 1 ');
|
||||||
|
|
||||||
|
// Sélection de plusieurs lignes : toutes sont indentées, puis désindentées.
|
||||||
|
await page.keyboard.press('Control+a');
|
||||||
|
await page.keyboard.press('Tab');
|
||||||
|
assert.equal(await value(), ' while True:\n x = 1 ');
|
||||||
|
await page.keyboard.press('Shift+Tab');
|
||||||
|
assert.equal(await value(), 'while True:\nx = 1 ');
|
||||||
|
|
||||||
|
// Ctrl+Z annule l'indentation (historique conservé).
|
||||||
|
await page.keyboard.press('Control+End');
|
||||||
|
await page.keyboard.press('Tab');
|
||||||
|
await page.keyboard.press('Control+z');
|
||||||
|
assert.equal(await value(), 'while True:\nx = 1 ');
|
||||||
|
|
||||||
|
// Échap puis Tab : le focus quitte l'éditeur (pas de piège clavier).
|
||||||
|
await page.keyboard.press('Escape');
|
||||||
|
await page.keyboard.press('Tab');
|
||||||
|
assert.notEqual(await page.evaluate(() => document.activeElement.tagName), 'TEXTAREA');
|
||||||
|
// Revenu dans l'éditeur, Tab indente de nouveau.
|
||||||
|
await page.keyboard.press('Shift+Tab');
|
||||||
|
assert.equal(await page.evaluate(() => document.activeElement.tagName), 'TEXTAREA');
|
||||||
|
await page.keyboard.press('Control+Home');
|
||||||
|
await page.keyboard.press('Tab');
|
||||||
|
assert.equal((await value()).startsWith(' while'), true);
|
||||||
|
});
|
||||||
|
|
||||||
test('personnage-guide : messages variés selon la situation, réglage pour le masquer', { skip, timeout: 120000 }, async () => {
|
test('personnage-guide : messages variés selon la situation, réglage pour le masquer', { skip, timeout: 120000 }, async () => {
|
||||||
const guideFr = JSON.parse(readFileSync(`${root}app/js/i18n/guide.fr.json`, 'utf8'));
|
const guideFr = JSON.parse(readFileSync(`${root}app/js/i18n/guide.fr.json`, 'utf8'));
|
||||||
const page = await (await browser.newContext()).newPage();
|
const page = await (await browser.newContext()).newPage();
|
||||||
|
|||||||
Reference in New Issue
Block a user