|
|
Línea 1: |
Línea 1: |
| function makeUnlinkYearsTool() { | | mw.loader.using( [ 'ext.visualEditor.mediawiki' ] ).then( function() { |
| // Function to modify wikitext
| | function addGroup( target ) { |
| function unlinkYears( wikitext ) { | | target.static.toolbarGroups.push( { |
| return wikitext.replace( /\[\[(\d+)\]\]/g, '$1' ) | | name: 'mytoolgroup', |
| .replace( /\[\[\d+\|(.*?)\]\]/g, '$1' ); | | label: 'My Group', |
| | type: 'list', |
| | indicator: 'down', |
| | include: [ { group: 'mytoolgroup' } ], |
| | } ); |
| } | | } |
| | | for ( var n in ve.init.mw.targetFactory.registry ) { |
| // Create and register command
| | addGroup( ve.init.mw.targetFactory.lookup( n ) ); |
| function UnlinkYearsCommand() { | |
| UnlinkYearsCommand.parent.call( this, 'unlinkYears' );
| |
| }
| |
| OO.inheritClass( UnlinkYearsCommand, ve.ui.Command );
| |
| | |
| UnlinkYearsCommand.prototype.execute = function ( surface ) {
| |
| var surfaceModel, fragment, wikitext, data = [], onCompleteText; | |
| // Get fragment to work on
| |
| surfaceModel = surface.getModel();
| |
| fragment = surfaceModel.getFragment();
| |
| if ( fragment.getSelection().isCollapsed() ) {
| |
| surfaceModel.setLinearSelection(
| |
| new ve.Range( 0, surfaceModel.getDocument().data.getLength() )
| |
| );
| |
| fragment = surfaceModel.getFragment();
| |
| onCompleteText = true;
| |
| }
| |
| // Visual mode
| |
| if ( ve.init.target.getSurface().getMode() !== 'source' ) {
| |
| fragment.annotateContent(
| |
| 'clear',
| |
| fragment.getAnnotations( true ).filter( function ( annotation ) {
| |
| return annotation.getType() === 'link/mwInternal' &&
| |
| /^\d+$/.test( annotation.getAttribute( 'normalizedTitle' ) );
| |
| } )
| |
| );
| |
| return true;
| |
| }
| |
| // Source mode
| |
| wikitext = fragment.getText( true ).replace( /^\n/, '' ).replace( /\n\n/g, '\n' );
| |
| wikitext = unlinkYears( wikitext );
| |
| wikitext.split( '' ).forEach( function ( c ) {
| |
| if ( c === '\n' ) {
| |
| data.push( { type: '/paragraph' } );
| |
| data.push( { type: 'paragraph' } );
| |
| } else {
| |
| data.push( c );
| |
| }
| |
| } );
| |
| if ( onCompleteText ) {
| |
| fragment.insertContent( wikitext );
| |
| } else {
| |
| fragment.insertContent( data );
| |
| }
| |
| if ( onCompleteText ) {
| |
| fragment.collapseToStart().select();
| |
| }
| |
| return true;
| |
| };
| |
| | |
| ve.ui.commandRegistry.register( new UnlinkYearsCommand() );
| |
| | |
| // Create, register and insert tool
| |
| function UnlinkYearsTool() {
| |
| UnlinkYearsTool.parent.apply( this, arguments );
| |
| } | | } |
| OO.inheritClass( UnlinkYearsTool, ve.ui.Tool );
| | ve.init.mw.targetFactory.on( 'register', function ( name, target ) { |
| | | addGroup( target ); |
| UnlinkYearsTool.static.name = 'unlinkYears';
| |
| UnlinkYearsTool.static.group = 'utility';
| |
| UnlinkYearsTool.static.title = 'Unlink years';
| |
| UnlinkYearsTool.static.icon = 'noWikiText';
| |
| UnlinkYearsTool.static.commandName = 'unlinkYears';
| |
| UnlinkYearsTool.static.autoAddToCatchall = false;
| |
| UnlinkYearsTool.static.deactivateOnSelect = false;
| |
| | |
| UnlinkYearsTool.prototype.onUpdateState = function () {
| |
| UnlinkYearsTool.parent.prototype.onUpdateState.apply( this, arguments );
| |
| this.setActive( false );
| |
| };
| |
| | |
| ve.ui.toolFactory.register( UnlinkYearsTool );
| |
| | |
| ve.init.mw.DesktopArticleTarget.static.actionGroups[ 1 ].include.push( 'unlinkYears' ); | |
| }
| |
| | |
| // Initialize
| |
| mw.loader.using( 'ext.visualEditor.desktopArticleTarget.init' ).then( function () {
| |
| mw.libs.ve.addPlugin( function () {
| |
| return mw.loader.using( [ 'ext.visualEditor.core' ] ) | |
| .then( function () {
| |
| makeUnlinkYearsTool();
| |
| } );
| |
| } ); | | } ); |
| } ); | | } ); |