2016-05-26 12:08:26 +00:00
|
|
|
/*!
|
|
|
|
* VisualEditor MWWikitextDataTransferHandlerFactory class.
|
|
|
|
*
|
2023-12-01 16:06:11 +00:00
|
|
|
* @copyright See AUTHORS.txt
|
2016-05-26 12:08:26 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Drop handler Factory.
|
|
|
|
*
|
|
|
|
* @class
|
|
|
|
* @extends ve.ui.DataTransferHandlerFactory
|
|
|
|
* @constructor
|
|
|
|
*/
|
|
|
|
ve.ui.MWWikitextDataTransferHandlerFactory = function VeUiMwWikitextDataTransferHandlerFactory() {
|
|
|
|
// Parent constructor
|
|
|
|
ve.ui.MWWikitextDataTransferHandlerFactory.super.apply( this, arguments );
|
|
|
|
|
2024-05-21 14:22:56 +00:00
|
|
|
for ( const name in ve.ui.dataTransferHandlerFactory.registry ) {
|
2016-05-26 12:08:26 +00:00
|
|
|
this.register( ve.ui.dataTransferHandlerFactory.registry[ name ] );
|
|
|
|
}
|
|
|
|
|
2024-04-30 16:44:25 +00:00
|
|
|
ve.ui.dataTransferHandlerFactory.on( 'register', ( n, data ) => {
|
2024-05-01 12:32:49 +00:00
|
|
|
this.register( data );
|
2016-05-26 12:08:26 +00:00
|
|
|
} );
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Inheritance */
|
|
|
|
|
|
|
|
OO.inheritClass( ve.ui.MWWikitextDataTransferHandlerFactory, ve.ui.DataTransferHandlerFactory );
|
|
|
|
|
|
|
|
/* Methods */
|
|
|
|
|
|
|
|
/**
|
2019-09-05 00:49:48 +00:00
|
|
|
* Create an object based on a name.
|
|
|
|
*
|
|
|
|
* Name is used to look up the constructor to use, while all additional arguments are passed to the
|
|
|
|
* constructor directly, so leaving one out will pass an undefined to the constructor.
|
|
|
|
*
|
|
|
|
* See https://doc.wikimedia.org/oojs/master/OO.Factory.html
|
|
|
|
*
|
|
|
|
* @param {string} name Object name
|
2024-05-02 09:36:01 +00:00
|
|
|
* @param {...any} [args] Arguments to pass to the constructor
|
2019-09-05 00:49:48 +00:00
|
|
|
* @return {Object} The new object
|
|
|
|
* @throws {Error} Unknown object name
|
2016-05-26 12:08:26 +00:00
|
|
|
*/
|
|
|
|
ve.ui.MWWikitextDataTransferHandlerFactory.prototype.create = function () {
|
|
|
|
// Parent method
|
2024-05-21 14:22:56 +00:00
|
|
|
const handler = ve.ui.MWWikitextDataTransferHandlerFactory.super.prototype.create.apply( this, arguments ),
|
2016-05-26 12:08:26 +00:00
|
|
|
resolve = handler.resolve.bind( handler );
|
|
|
|
|
|
|
|
function isPlain( data ) {
|
|
|
|
return typeof data === 'string' || ve.dm.LinearData.static.getType( data ) === 'paragraph';
|
|
|
|
}
|
|
|
|
|
|
|
|
handler.resolve = function ( dataOrDoc ) {
|
|
|
|
if ( typeof dataOrDoc === 'string' || ( Array.isArray( dataOrDoc ) && dataOrDoc.every( isPlain ) ) ) {
|
|
|
|
resolve( dataOrDoc );
|
|
|
|
} else {
|
2024-05-21 14:22:56 +00:00
|
|
|
const doc = dataOrDoc instanceof ve.dm.Document ?
|
2016-12-14 22:54:07 +00:00
|
|
|
dataOrDoc :
|
|
|
|
// The handler may have also written items to the store
|
|
|
|
new ve.dm.Document( new ve.dm.ElementLinearData( handler.surface.getModel().getDocument().getStore(), dataOrDoc ) );
|
|
|
|
|
2017-05-09 14:03:10 +00:00
|
|
|
// Optimization: we can skip a server hit if this is a plain link,
|
|
|
|
// with no title, whose href is equal to the contained text. This
|
|
|
|
// avoids a stutter in the common case of pasting a link into the
|
|
|
|
// document.
|
2024-05-21 14:22:56 +00:00
|
|
|
const annotations = doc.data.getAnnotationsFromRange( new ve.Range( 0, doc.data.getLength() ) );
|
2017-05-09 14:03:10 +00:00
|
|
|
if ( annotations.getLength() === 1 ) {
|
2024-05-21 14:22:56 +00:00
|
|
|
const text = doc.data.getText();
|
2017-05-09 14:03:10 +00:00
|
|
|
if ( annotations.get( 0 ).getAttribute( 'href' ) === text ) {
|
|
|
|
return resolve( text );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-19 12:02:51 +00:00
|
|
|
ve.init.target.getWikitextFragment( doc, false )
|
2016-05-26 12:08:26 +00:00
|
|
|
.done( resolve )
|
2024-04-30 16:44:25 +00:00
|
|
|
.fail( () => {
|
2016-05-26 12:08:26 +00:00
|
|
|
handler.abort();
|
|
|
|
} );
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return handler;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Initialization */
|
|
|
|
|
|
|
|
ve.ui.wikitextDataTransferHandlerFactory = new ve.ui.MWWikitextDataTransferHandlerFactory();
|
2016-09-21 20:08:10 +00:00
|
|
|
|
|
|
|
ve.ui.wikitextDataTransferHandlerFactory.unregister( ve.ui.MWWikitextStringTransferHandler );
|