Utilisateur:DreZhsh/VisualEditor.js
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.
Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;
Firefox (sur GNU/Linux) / Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5./* globals ve */
/* jshint esversion: 6 */
( () => {
function alignTool() {
function makeAlignTool( type ) {
const emptyTextToInsert = [
{
type: 'mwTransclusionInline',
attributes: {
mw: {
parts: [
{
template: {
target: {
href: `Modèle:${type}`,
wt: type
},
params: {
1: { wt: '' }
}
}
}
]
}
}
}, {
type: '/mwTransclusionInline'
}
];
function removeTextStyleTemplate( wikitext ) {
if ( ( /^({{([Cc]entrer|[Cc]enter|[Gg]auche|[Ll]eft|[Dd]roite|[Rr]ight))/ )
.test( wikitext ) ) {
wikitext = wikitext.replace( /^({{([Cc]entrer|[Cc]enter|[Gg]auche|[Ll]eft|[Dd]roite|[Rr]ight)\|)/, '' );
wikitext = wikitext.replace( /}}$/, '' );
}
return wikitext;
}
function CenterTextCommand() {
CenterTextCommand.parent.call( this, 'centerText' );
}
function CenterTextTool() {
CenterTextTool.parent.apply( this, arguments );
}
function RightTextCommand() {
RightTextCommand.parent.call( this, 'rightText' );
}
function RightTextTool() {
RightTextTool.parent.apply( this, arguments );
}
function LeftTextCommand() {
LeftTextCommand.parent.call( this, 'leftText' );
}
function LeftTextTool() {
LeftTextTool.parent.apply( this, arguments );
}
ve.ui.triggerRegistry.register(
'centerText', {
mac: new ve.ui.Trigger( 'cmd+alt+c' ),
pc: new ve.ui.Trigger( 'ctrl+alt+c' )
}
);
ve.ui.triggerRegistry.register(
'rightText', {
mac: new ve.ui.Trigger( 'cmd+alt+d' ),
pc: new ve.ui.Trigger( 'ctrl+alt+d' )
}
);
ve.ui.triggerRegistry.register(
'leftText', {
mac: new ve.ui.Trigger( 'cmd+alt+g' ),
pc: new ve.ui.Trigger( 'ctrl+alt+g' )
}
);
if ( type === 'Centrer' ) {
OO.inheritClass( CenterTextCommand, ve.ui.Command );
CenterTextCommand.prototype.execute = function () {
let textToInsert;
const surface = ve.init.target.getSurface(), surfaceSelection = surface
.getModel().getFragment()
.getSelection().isCollapsed(),
surfaceModel = surface.getModel(),
doc = surfaceModel.getDocument(),
range = surfaceModel.getSelection().getRange(),
docRange = doc.shallowCloneFromRange( range );
if ( surfaceSelection ) {
textToInsert = emptyTextToInsert;
} else if ( surfaceSelection === false ) {
ve.init.target
.getWikitextFragment( docRange, false ).done( function ( wikitext ) {
textToInsert = [
{
type: 'mwTransclusionInline',
attributes: {
mw: {
parts: [
{
template: {
target: {
href: `Modèle:${type}`,
wt: type
},
params: {
1: {
wt:
removeTextStyleTemplate( wikitext )
}
}
}
}
]
}
}
}, {
type: '/mwTransclusionInline'
}
];
surfaceModel.getFragment().insertContent( textToInsert );
} );
}
};
ve.ui.commandRegistry.register( new CenterTextCommand() );
OO.inheritClass( CenterTextTool, ve.ui.Tool );
CenterTextTool.static.name = 'centerText';
CenterTextTool.static.group = 'textStyle';
CenterTextTool.static.title = 'Centre';
CenterTextTool.static.icon = 'alignCenter';
CenterTextTool.static.commandName = 'centerText';
CenterTextTool.static.autoAddToCatchall = false;
CenterTextTool.static.deactivateOnSelect = false;
CenterTextTool.prototype.onUpdateState = function () {
CenterTextTool.parent.prototype.onUpdateState.apply( this, arguments );
this.setActive( false );
};
ve.ui.toolFactory.register( CenterTextTool );
} else if ( type === 'Droite' ) {
OO.inheritClass( RightTextCommand, ve.ui.Command );
RightTextCommand.prototype.execute = function () {
let textToInsert;
const surface = ve.init.target.getSurface(), surfaceSelection = surface
.getModel().getFragment()
.getSelection().isCollapsed(),
surfaceModel = surface.getModel(),
doc = surfaceModel.getDocument(),
range = surfaceModel.getSelection().getRange(),
docRange = doc.shallowCloneFromRange( range );
if ( surfaceSelection ) {
textToInsert = emptyTextToInsert;
} else if ( surfaceSelection === false ) {
ve.init.target
.getWikitextFragment( docRange, false ).done( function ( wikitext ) {
textToInsert = [
{
type: 'mwTransclusionInline',
attributes: {
mw: {
parts: [
{
template: {
target: {
href: `Modèle:${type}`,
wt: type
},
params: {
1: {
wt:
removeTextStyleTemplate( wikitext )
}
}
}
}
]
}
}
}, {
type: '/mwTransclusionInline'
}
];
surfaceModel.getFragment().insertContent( textToInsert );
} );
}
};
ve.ui.commandRegistry.register( new RightTextCommand() );
OO.inheritClass( RightTextTool, ve.ui.Tool );
RightTextTool.static.name = 'rightText';
RightTextTool.static.group = 'textStyle';
RightTextTool.static.title = 'Droite';
RightTextTool.static.icon = 'alignRight';
RightTextTool.static.commandName = 'rightText';
RightTextTool.static.autoAddToCatchall = false;
RightTextTool.static.deactivateOnSelect = false;
RightTextTool.prototype.onUpdateState = function () {
RightTextTool.parent.prototype.onUpdateState.apply( this, arguments );
this.setActive( false );
};
ve.ui.toolFactory.register( RightTextTool );
} else if ( type === 'Gauche' ) {
OO.inheritClass( LeftTextCommand, ve.ui.Command );
LeftTextCommand.prototype.execute = function () {
let textToInsert;
const surface = ve.init.target.getSurface(), surfaceSelection = surface
.getModel().getFragment()
.getSelection().isCollapsed(),
surfaceModel = surface.getModel(),
doc = surfaceModel.getDocument(),
range = surfaceModel.getSelection().getRange(),
docRange = doc.shallowCloneFromRange( range );
if ( surfaceSelection ) {
textToInsert = emptyTextToInsert;
} else if ( surfaceSelection === false ) {
ve.init.target
.getWikitextFragment( docRange, false ).done( function ( wikitext ) {
textToInsert = [
{
type: 'mwTransclusionInline',
attributes: {
mw: {
parts: [
{
template: {
target: {
href: `Modèle:${type}`,
wt: type
},
params: {
1: {
wt:
removeTextStyleTemplate( wikitext )
}
}
}
}
]
}
}
}, {
type: '/mwTransclusionInline'
}
];
surfaceModel.getFragment().insertContent( textToInsert );
} );
}
};
ve.ui.commandRegistry.register( new LeftTextCommand() );
OO.inheritClass( LeftTextTool, ve.ui.Tool );
LeftTextTool.static.name = 'leftText';
LeftTextTool.static.group = 'textStyle';
LeftTextTool.static.title = 'Gauche';
LeftTextTool.static.icon = 'alignLeft';
LeftTextTool.static.commandName = 'leftText';
LeftTextTool.static.autoAddToCatchall = false;
LeftTextTool.static.deactivateOnSelect = false;
LeftTextTool.prototype.onUpdateState = function () {
LeftTextTool.parent.prototype.onUpdateState.apply( this, arguments );
this.setActive( false );
};
ve.ui.toolFactory.register( LeftTextTool );
}
}
makeAlignTool( 'Centrer' );
makeAlignTool( 'Droite' );
makeAlignTool( 'Gauche' );
}
mw.hook( 've.loadModules' ).add( function ( addPlugin ) {
addPlugin( alignTool );
} );
} )();