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.input_label": "Answer requested by the program",
|
||||
"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.failed": "Not yet. Here is what is wrong:",
|
||||
"cell.line": "Line {line}",
|
||||
|
||||
+1
-1
@@ -18,7 +18,7 @@
|
||||
"cell.output_empty": "(aucun affichage)",
|
||||
"cell.input_label": "Réponse demandée par le programme",
|
||||
"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.failed": "Pas encore. Voici ce qui ne va pas :",
|
||||
"cell.line": "Ligne {line}",
|
||||
|
||||
+80
-1
@@ -5,11 +5,14 @@ import { el } from './dom.js';
|
||||
import { tokenizePython } from './highlightPython.js';
|
||||
|
||||
const MIN_ROWS = 3;
|
||||
const INDENT = ' '; // 4 espaces, convention du projet
|
||||
|
||||
export class CodeEditor {
|
||||
#root;
|
||||
#textarea;
|
||||
#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
|
||||
@@ -29,7 +32,11 @@ export class CodeEditor {
|
||||
...attrs,
|
||||
oninput: () => this.#render(),
|
||||
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.value = value;
|
||||
@@ -52,6 +59,78 @@ export class CodeEditor {
|
||||
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() {
|
||||
const code = this.#textarea.value;
|
||||
this.#textarea.rows = Math.max(MIN_ROWS, code.split('\n').length + 1);
|
||||
|
||||
Reference in New Issue
Block a user