« MediaWiki:Gadget-outils-prepa.js » : différence entre les versions
Page de l’interface de MediaWiki
Autres actions
Mise en place du wiki de notes |
Mise en place du wiki de notes |
||
| Ligne 1 : | Ligne 1 : | ||
// <nowiki> (keeps MediaWiki from scanning this script as wikitext) | |||
/* | /* | ||
* Outils de prise de notes pour l'éditeur de code (WikiEditor + CodeMirror). | * Outils de prise de notes pour l'éditeur de code (WikiEditor + CodeMirror). | ||
| Ligne 159 : | Ligne 160 : | ||
} ); | } ); | ||
}() ); | }() ); | ||
// </nowiki> | |||
Dernière version du 24 septembre 2026 à 19:53
// <nowiki> (keeps MediaWiki from scanning this script as wikitext)
/*
* Outils de prise de notes pour l'éditeur de code (WikiEditor + CodeMirror).
*
* Ctrl+M <math>…</math> autour de la sélection
* Ctrl+Shift+M <math display="block">…</math>
* Ctrl+Shift+K <chem>…</chem>
*
* Ajoute aussi un onglet « Prépa » à la barre d'outils : boîtes de cours,
* formules et blocs de code OCaml / C / Python / SQL.
*/
( function () {
'use strict';
function insert( $textarea, pre, peri, post ) {
$textarea.textSelection( 'encapsulateSelection', {
pre: pre,
peri: peri,
post: post
} );
}
var shortcuts = {
m: [ '<math>', 'x', '</math>' ],
M: [ '<math display="block">', 'x', '</math>' ],
K: [ '<chem>', 'H2O', '</chem>' ]
};
$( document ).on( 'keydown', function ( e ) {
if ( !( e.ctrlKey || e.metaKey ) || e.altKey ) {
return;
}
if ( !$( e.target ).closest( '.wikiEditor-ui, #wpTextbox1' ).length ) {
return;
}
var key = e.key.length === 1 ? ( e.shiftKey ? e.key.toUpperCase() : e.key.toLowerCase() ) : '';
var s = shortcuts[ key ];
if ( !s ) {
return;
}
e.preventDefault();
insert( $( '#wpTextbox1' ), s[ 0 ], s[ 1 ], s[ 2 ] );
} );
function box( name, extra ) {
return {
label: name,
action: {
type: 'encapsulate',
options: {
pre: '{{' + name + '\n' + ( extra || '|titre=' ) + '\n|contenu=\n',
peri: '',
post: '\n}}\n',
ownline: true
}
}
};
}
function code( lang, label, sample ) {
return {
label: label,
action: {
type: 'encapsulate',
options: {
pre: '<syntaxhighlight lang="' + lang + '" line>\n',
peri: sample,
post: '\n</syntaxhighlight>\n',
ownline: true
}
}
};
}
function button( label, pre, peri, post ) {
return {
label: label,
type: 'button',
action: { type: 'encapsulate', options: { pre: pre, peri: peri, post: post } }
};
}
mw.hook( 'wikiEditor.toolbarReady' ).add( function ( $textarea ) {
$textarea.wikiEditor( 'addToToolbar', {
sections: {
prepa: {
type: 'toolbar',
label: 'Prépa',
groups: {
maths: {
label: 'Formules',
tools: {
math: button( 'Formule (Ctrl+M)', '<math>', 'x', '</math>' ),
mathBlock: button( 'Formule centrée (Ctrl+Maj+M)', '<math display="block">', 'x', '</math>' ),
chem: button( 'Chimie (Ctrl+Maj+K)', '<chem>', 'H2O', '</chem>' )
}
},
boites: {
label: 'Boîtes',
tools: {
boites: {
label: 'Insérer une boîte',
type: 'select',
list: {
definition: box( 'Définition' ),
theoreme: box( 'Théorème', '|type=Théorème\n|titre=' ),
demo: box( 'Démonstration', '|titre=' ),
methode: box( 'Méthode' ),
formule: box( 'Formule' ),
exemple: box( 'Exemple' ),
remarque: box( 'Remarque' ),
attention: box( 'Attention' ),
exercice: {
label: 'Exercice',
action: {
type: 'encapsulate',
options: {
pre: '{{Exercice\n|titre=\n|source=\n|énoncé=\n',
peri: '',
post: '\n|solution=\n\n}}\n',
ownline: true
}
}
},
revoir: {
label: 'À revoir',
action: { type: 'encapsulate', options: { pre: '{{À revoir|', peri: '', post: '}}\n', ownline: true } }
}
}
}
}
},
code: {
label: 'Code',
tools: {
code: {
label: 'Bloc de code',
type: 'select',
list: {
ocaml: code( 'ocaml', 'OCaml', 'let rec f x =\n x' ),
c: code( 'c', 'C', '#include <stdio.h>\n\nint main(void) {\n return 0;\n}' ),
python: code( 'python', 'Python', 'def f(x):\n return x' ),
sql: code( 'sql', 'SQL', 'SELECT * FROM table;' ),
inlineOcaml: {
label: 'Code en ligne (OCaml)',
action: { type: 'encapsulate', options: { pre: '<syntaxhighlight lang="ocaml" inline>', peri: 'f x', post: '</syntaxhighlight>' } }
},
inlineC: {
label: 'Code en ligne (C)',
action: { type: 'encapsulate', options: { pre: '<syntaxhighlight lang="c" inline>', peri: 'f(x)', post: '</syntaxhighlight>' } }
}
}
}
}
}
}
}
}
} );
} );
}() );
// </nowiki>