2014-05-31 04:47:08 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWLinkNodeInspector class.
|
|
|
|
*
|
2020-01-08 17:13:04 +00:00
|
|
|
* @copyright 2011-2020 VisualEditor Team and others; see AUTHORS.txt
|
2014-05-31 04:47:08 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2014-07-14 21:32:49 +00:00
|
|
|
* Inspector for editing unlabeled MediaWiki external links.
|
2014-05-31 04:47:08 +00:00
|
|
|
*
|
|
|
|
* @class
|
2014-07-14 21:32:49 +00:00
|
|
|
* @extends ve.ui.NodeInspector
|
2014-05-31 04:47:08 +00:00
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.ui.MWLinkNodeInspector = function VeUiMWLinkNodeInspector() {
|
2014-05-31 04:47:08 +00:00
|
|
|
// Parent constructor
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.ui.MWLinkNodeInspector.super.apply( this, arguments );
|
2014-05-31 04:47:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
2014-07-14 21:32:49 +00:00
|
|
|
OO.inheritClass( ve.ui.MWLinkNodeInspector, ve.ui.NodeInspector );
|
2014-05-31 04:47:08 +00:00
|
|
|
|
|
|
|
/* Static properties */
|
|
|
|
|
|
|
|
ve.ui.MWLinkNodeInspector.static.name = 'linkNode';
|
|
|
|
|
|
|
|
ve.ui.MWLinkNodeInspector.static.title = OO.ui.deferMsg( 'visualeditor-linknodeinspector-title' );
|
|
|
|
|
|
|
|
ve.ui.MWLinkNodeInspector.static.modelClasses = [ ve.dm.MWNumberedExternalLinkNode ];
|
|
|
|
|
2014-07-14 21:32:49 +00:00
|
|
|
ve.ui.MWLinkNodeInspector.static.actions = ve.ui.MWLinkNodeInspector.super.static.actions.concat( [
|
|
|
|
{
|
2014-08-22 20:50:48 +00:00
|
|
|
action: 'convert',
|
2015-03-12 18:31:01 +00:00
|
|
|
label: OO.ui.deferMsg( 'visualeditor-linknodeinspector-add-label' ),
|
|
|
|
modes: [ 'edit' ]
|
2014-07-14 21:32:49 +00:00
|
|
|
}
|
|
|
|
] );
|
2014-05-31 04:47:08 +00:00
|
|
|
|
2014-07-14 21:32:49 +00:00
|
|
|
/* Methods */
|
2014-05-31 04:47:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWLinkNodeInspector.prototype.initialize = function () {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.MWLinkNodeInspector.super.prototype.initialize.call( this );
|
|
|
|
|
|
|
|
// Properties
|
2014-10-22 00:35:28 +00:00
|
|
|
this.targetInput = new OO.ui.TextInputWidget( {
|
|
|
|
validate: ve.init.platform.getExternalLinkUrlProtocolsRegExp()
|
|
|
|
} );
|
|
|
|
|
2014-05-31 04:47:08 +00:00
|
|
|
// Initialization
|
2014-07-14 21:32:49 +00:00
|
|
|
this.form.$element.append( this.targetInput.$element );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWLinkNodeInspector.prototype.getActionProcess = function ( action ) {
|
|
|
|
if ( action === 'convert' ) {
|
|
|
|
return new OO.ui.Process( function () {
|
2014-08-22 20:50:48 +00:00
|
|
|
this.close( { action: action } );
|
2014-07-14 21:32:49 +00:00
|
|
|
}, this );
|
|
|
|
}
|
|
|
|
return ve.ui.MWLinkNodeInspector.super.prototype.getActionProcess.call( this, action );
|
2014-05-31 04:47:08 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWLinkNodeInspector.prototype.getSetupProcess = function ( data ) {
|
|
|
|
return ve.ui.MWLinkNodeInspector.super.prototype.getSetupProcess.call( this, data )
|
|
|
|
.next( function () {
|
|
|
|
// Initialization
|
2014-07-14 21:32:49 +00:00
|
|
|
this.targetInput.setValue(
|
|
|
|
this.selectedNode ? this.selectedNode.getAttribute( 'href' ) : ''
|
|
|
|
);
|
2019-02-08 19:18:22 +00:00
|
|
|
this.targetInput.setReadOnly( this.isReadOnly() );
|
2014-05-31 04:47:08 +00:00
|
|
|
}, this );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWLinkNodeInspector.prototype.getReadyProcess = function ( data ) {
|
|
|
|
return ve.ui.MWLinkNodeInspector.super.prototype.getReadyProcess.call( this, data )
|
|
|
|
.next( function () {
|
|
|
|
this.targetInput.focus().select();
|
|
|
|
}, this );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWLinkNodeInspector.prototype.getTeardownProcess = function ( data ) {
|
2014-07-15 22:49:51 +00:00
|
|
|
data = data || {};
|
2014-05-31 04:47:08 +00:00
|
|
|
return ve.ui.MWLinkNodeInspector.super.prototype.getTeardownProcess.call( this, data )
|
|
|
|
.first( function () {
|
2022-02-21 18:07:07 +00:00
|
|
|
var surfaceView = this.manager.getSurface().getView(),
|
2014-05-31 04:47:08 +00:00
|
|
|
surfaceModel = this.getFragment().getSurface(),
|
|
|
|
doc = surfaceModel.getDocument(),
|
2014-07-14 21:32:49 +00:00
|
|
|
nodeRange = this.selectedNode.getOuterRange(),
|
2014-05-31 04:47:08 +00:00
|
|
|
value = this.targetInput.getValue(),
|
|
|
|
convert = data.action === 'convert',
|
2014-07-01 19:46:23 +00:00
|
|
|
remove = data.action === 'remove' || !value;
|
|
|
|
|
|
|
|
// Default to http:// if the external link doesn't already begin with a supported
|
|
|
|
// protocol - this prevents the link from being converted into literal text upon
|
|
|
|
// save and also fixes a common mistake users may make
|
|
|
|
if ( !ve.init.platform.getExternalLinkUrlProtocolsRegExp().test( value ) ) {
|
|
|
|
value = 'http://' + value;
|
|
|
|
}
|
2014-05-31 04:47:08 +00:00
|
|
|
|
|
|
|
if ( remove ) {
|
|
|
|
surfaceModel.change(
|
2016-11-30 22:03:03 +00:00
|
|
|
ve.dm.TransactionBuilder.static.newFromRemoval( doc, nodeRange )
|
2014-05-31 04:47:08 +00:00
|
|
|
);
|
2014-07-01 19:46:23 +00:00
|
|
|
} else if ( convert ) {
|
2022-02-21 18:07:07 +00:00
|
|
|
var annotation = new ve.dm.MWExternalLinkAnnotation( {
|
2014-08-22 20:50:48 +00:00
|
|
|
type: 'link/mwExternal',
|
|
|
|
attributes: {
|
|
|
|
href: value
|
2014-05-31 04:47:08 +00:00
|
|
|
}
|
|
|
|
} );
|
2022-02-21 18:07:07 +00:00
|
|
|
var annotations = doc.data.getAnnotationsFromOffset( nodeRange.start ).clone();
|
2014-05-31 04:47:08 +00:00
|
|
|
annotations.push( annotation );
|
2022-02-21 18:07:07 +00:00
|
|
|
var content = value.split( '' );
|
2014-05-31 04:47:08 +00:00
|
|
|
ve.dm.Document.static.addAnnotationsToData( content, annotations );
|
|
|
|
surfaceModel.change(
|
2016-11-30 22:03:03 +00:00
|
|
|
ve.dm.TransactionBuilder.static.newFromReplacement( doc, nodeRange, content )
|
2014-07-01 19:46:23 +00:00
|
|
|
);
|
2015-11-21 00:33:46 +00:00
|
|
|
setTimeout( function () {
|
2019-04-13 16:04:43 +00:00
|
|
|
surfaceView.selectAnnotation( function ( view ) {
|
|
|
|
return view.model instanceof ve.dm.MWExternalLinkAnnotation;
|
|
|
|
} );
|
2015-11-21 00:33:46 +00:00
|
|
|
} );
|
2014-07-01 19:46:23 +00:00
|
|
|
} else {
|
|
|
|
surfaceModel.change(
|
2016-11-30 22:03:03 +00:00
|
|
|
ve.dm.TransactionBuilder.static.newFromAttributeChanges(
|
2014-08-22 20:50:48 +00:00
|
|
|
doc, nodeRange.start, { href: value }
|
2014-07-01 19:46:23 +00:00
|
|
|
)
|
2014-05-31 04:47:08 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}, this );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ui.windowFactory.register( ve.ui.MWLinkNodeInspector );
|