2015-08-25 05:48:16 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor UserInterface MWMagicLinkNodeInspector class.
|
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright See AUTHORS.txt
|
2015-08-25 05:48:16 +00:00
|
|
|
* @license The MIT License (MIT); see LICENSE.txt
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Inspector for editing MediaWiki magic links (RFC/ISBN/PMID).
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.ui.NodeInspector
|
|
|
|
*
|
|
|
|
* @constructor
|
|
|
|
* @param {Object} [config] Configuration options
|
|
|
|
*/
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.ui.MWMagicLinkNodeInspector = function VeUiMWMagicLinkNodeInspector() {
|
2015-08-25 05:48:16 +00:00
|
|
|
// Parent constructor
|
2016-08-22 21:44:59 +00:00
|
|
|
ve.ui.MWMagicLinkNodeInspector.super.apply( this, arguments );
|
2015-08-25 05:48:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.MWMagicLinkNodeInspector, ve.ui.NodeInspector );
|
|
|
|
|
|
|
|
/* Static properties */
|
|
|
|
|
|
|
|
ve.ui.MWMagicLinkNodeInspector.static.name = 'linkMagicNode';
|
|
|
|
|
|
|
|
ve.ui.MWMagicLinkNodeInspector.static.title = null; // see #getSetupProcess
|
|
|
|
|
|
|
|
ve.ui.MWMagicLinkNodeInspector.static.modelClasses = [ ve.dm.MWMagicLinkNode ];
|
|
|
|
|
2024-05-29 16:03:23 +00:00
|
|
|
ve.ui.MWMagicLinkNodeInspector.static.actions = [
|
|
|
|
...ve.ui.MWMagicLinkNodeInspector.super.static.actions,
|
2015-08-25 05:48:16 +00:00
|
|
|
{
|
|
|
|
action: 'convert',
|
|
|
|
label: OO.ui.deferMsg( 'visualeditor-magiclinknodeinspector-convert-link' ),
|
|
|
|
modes: [ 'edit' ]
|
|
|
|
}
|
2024-05-29 16:03:23 +00:00
|
|
|
];
|
2015-08-25 05:48:16 +00:00
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWMagicLinkNodeInspector.prototype.initialize = function () {
|
|
|
|
// Parent method
|
|
|
|
ve.ui.MWMagicLinkNodeInspector.super.prototype.initialize.call( this );
|
|
|
|
|
|
|
|
// Properties
|
|
|
|
this.targetInput = new OO.ui.TextInputWidget( {
|
|
|
|
validate: this.validate.bind( this )
|
|
|
|
} );
|
|
|
|
this.targetInput.on( 'change', this.onChange.bind( this ) );
|
|
|
|
|
|
|
|
// Initialization
|
|
|
|
this.form.$element.append( this.targetInput.$element );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if the given string is a valid magic link of the
|
|
|
|
* appropriate type.
|
|
|
|
*
|
|
|
|
* @private
|
2016-10-28 19:02:36 +00:00
|
|
|
* @param {string} str String to validate
|
|
|
|
* @return {boolean} String is valid
|
2015-08-25 05:48:16 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWMagicLinkNodeInspector.prototype.validate = function ( str ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const node = this.getFragment().getSelectedNode();
|
2015-08-25 05:48:16 +00:00
|
|
|
return node.constructor.static.validateContent( str, node.getMagicType() );
|
|
|
|
};
|
|
|
|
|
|
|
|
ve.ui.MWMagicLinkNodeInspector.prototype.onChange = function ( value ) {
|
|
|
|
// Disable the unsafe action buttons if the input isn't valid
|
2024-05-21 14:22:56 +00:00
|
|
|
const isValid = this.validate( value );
|
2024-04-30 16:44:25 +00:00
|
|
|
this.actions.forEach( null, ( action ) => {
|
2015-08-25 05:48:16 +00:00
|
|
|
if ( !action.hasFlag( 'safe' ) ) {
|
|
|
|
action.setDisabled( !isValid );
|
|
|
|
}
|
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWMagicLinkNodeInspector.prototype.getActionProcess = function ( action ) {
|
|
|
|
if ( ( action === 'done' || action === 'convert' ) &&
|
|
|
|
!this.validate( this.targetInput.getValue() ) ) {
|
|
|
|
// Don't close dialog: input isn't valid.
|
|
|
|
return new OO.ui.Process( 0 );
|
|
|
|
}
|
|
|
|
if ( action === 'convert' ) {
|
2024-04-30 21:25:39 +00:00
|
|
|
return new OO.ui.Process( () => {
|
2015-08-25 05:48:16 +00:00
|
|
|
this.close( { action: action } );
|
2024-04-30 21:25:39 +00:00
|
|
|
} );
|
2015-08-25 05:48:16 +00:00
|
|
|
}
|
|
|
|
return ve.ui.MWMagicLinkNodeInspector.super.prototype.getActionProcess.call( this, action );
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWMagicLinkNodeInspector.prototype.getSetupProcess = function ( data ) {
|
|
|
|
// Set the title based on the node type
|
2024-05-21 14:22:56 +00:00
|
|
|
const fragment = data.fragment,
|
2015-08-25 05:48:16 +00:00
|
|
|
node = fragment instanceof ve.dm.SurfaceFragment ?
|
|
|
|
fragment.getSelectedNode() : null,
|
|
|
|
type = node instanceof ve.dm.MWMagicLinkNode ?
|
|
|
|
node.getMagicType() : null,
|
|
|
|
msg = type ?
|
|
|
|
'visualeditor-magiclinknodeinspector-title-' + type.toLowerCase() :
|
|
|
|
null;
|
|
|
|
|
2021-11-15 17:25:51 +00:00
|
|
|
data = ve.extendObject( {
|
2019-11-01 16:20:22 +00:00
|
|
|
// The following messages are used here
|
|
|
|
// * visualeditor-magiclinknodeinspector-title-isbn
|
|
|
|
// * visualeditor-magiclinknodeinspector-title-pmid
|
|
|
|
// * visualeditor-magiclinknodeinspector-title-rfc
|
2015-08-25 05:48:16 +00:00
|
|
|
title: msg ? OO.ui.deferMsg( msg ) : null
|
|
|
|
}, data );
|
|
|
|
return ve.ui.MWMagicLinkNodeInspector.super.prototype.getSetupProcess.call( this, data )
|
2024-04-30 21:25:39 +00:00
|
|
|
.next( () => {
|
2015-08-25 05:48:16 +00:00
|
|
|
// Initialization
|
|
|
|
this.targetInput.setValue(
|
|
|
|
this.selectedNode ? this.selectedNode.getAttribute( 'content' ) : ''
|
2019-02-24 12:38:03 +00:00
|
|
|
).setReadOnly( this.isReadOnly() );
|
2024-04-30 21:25:39 +00:00
|
|
|
} );
|
2015-08-25 05:48:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWMagicLinkNodeInspector.prototype.getReadyProcess = function ( data ) {
|
|
|
|
return ve.ui.MWMagicLinkNodeInspector.super.prototype.getReadyProcess.call( this, data )
|
2024-04-30 21:25:39 +00:00
|
|
|
.next( () => {
|
2015-08-25 05:48:16 +00:00
|
|
|
this.targetInput.focus().select();
|
2024-04-30 21:25:39 +00:00
|
|
|
} );
|
2015-08-25 05:48:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @inheritdoc
|
|
|
|
*/
|
|
|
|
ve.ui.MWMagicLinkNodeInspector.prototype.getTeardownProcess = function ( data ) {
|
|
|
|
data = data || {};
|
|
|
|
return ve.ui.MWMagicLinkNodeInspector.super.prototype.getTeardownProcess.call( this, data )
|
2024-04-30 21:25:39 +00:00
|
|
|
.first( () => {
|
2024-05-21 14:22:56 +00:00
|
|
|
const surfaceView = this.manager.getSurface().getView(),
|
2015-08-25 05:48:16 +00:00
|
|
|
surfaceModel = this.getFragment().getSurface(),
|
|
|
|
doc = surfaceModel.getDocument(),
|
|
|
|
nodeRange = this.selectedNode.getOuterRange(),
|
|
|
|
value = this.targetInput.getValue(),
|
|
|
|
done = data.action === 'done',
|
|
|
|
convert = data.action === 'convert',
|
|
|
|
remove = data.action === 'remove' || ( done && !value );
|
|
|
|
|
|
|
|
if ( remove ) {
|
|
|
|
surfaceModel.change(
|
2016-11-30 22:03:03 +00:00
|
|
|
ve.dm.TransactionBuilder.static.newFromRemoval( doc, nodeRange )
|
2015-08-25 05:48:16 +00:00
|
|
|
);
|
|
|
|
} else if ( convert ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const annotation = ve.dm.MWMagicLinkNode.static.annotationFromContent(
|
2015-08-25 05:48:16 +00:00
|
|
|
value
|
|
|
|
);
|
|
|
|
if ( annotation ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const annotations = doc.data.getAnnotationsFromOffset( nodeRange.start ).clone();
|
2015-08-25 05:48:16 +00:00
|
|
|
annotations.push( annotation );
|
2024-05-21 14:22:56 +00:00
|
|
|
const content = value.split( '' );
|
2015-08-25 05:48:16 +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 )
|
2015-08-25 05:48:16 +00:00
|
|
|
);
|
2024-04-30 16:44:25 +00:00
|
|
|
setTimeout( () => {
|
|
|
|
surfaceView.selectAnnotation( ( view ) => view.model instanceof ve.dm.LinkAnnotation );
|
2019-04-16 15:07:37 +00:00
|
|
|
} );
|
2015-08-25 05:48:16 +00:00
|
|
|
}
|
|
|
|
} else if ( done && this.validate( value ) ) {
|
|
|
|
surfaceModel.change(
|
2016-11-30 22:03:03 +00:00
|
|
|
ve.dm.TransactionBuilder.static.newFromAttributeChanges(
|
2015-08-25 05:48:16 +00:00
|
|
|
doc, nodeRange.start, { content: value }
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
2024-04-30 21:25:39 +00:00
|
|
|
} );
|
2015-08-25 05:48:16 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
/* Registration */
|
|
|
|
|
|
|
|
ve.ui.windowFactory.register( ve.ui.MWMagicLinkNodeInspector );
|