Debian Codegraph @

extensions/VisualEditor/modules/ve-mw/ui/ve.ui.MWWikitextDataTransferHandlerFactory.js

raw symbols SCIP
1 /*!
2 * VisualEditor MWWikitextDataTransferHandlerFactory class.
3 *
4 * @copyright See AUTHORS.txt
5 */
6
7 /**
8 * Drop handler Factory.
9 *
10 * @class
11 * @extends ve.ui.DataTransferHandlerFactory
12 * @constructor
13 */
14 ve.ui.MWWikitextDataTransferHandlerFactory = function VeUiMwWikitextDataTransferHandlerFactory() {
15 // Parent constructor
16 ve.ui.MWWikitextDataTransferHandlerFactory.super.apply( this, arguments );
17
18 for ( const name in ve.ui.dataTransferHandlerFactory.registry ) {
19 this.register( ve.ui.dataTransferHandlerFactory.registry[ name ] );
20 }
21
22 ve.ui.dataTransferHandlerFactory.on( 'register', ( n, data ) => {
23 this.register( data );
24 } );
25 };
26
27 /* Inheritance */
28
29 OO.inheritClass( ve.ui.MWWikitextDataTransferHandlerFactory, ve.ui.DataTransferHandlerFactory );
30
31 /* Methods */
32
33 /**
34 * Create an object based on a name.
35 *
36 * Name is used to look up the constructor to use, while all additional arguments are passed to the
37 * constructor directly, so leaving one out will pass an undefined to the constructor.
38 *
39 * See https://doc.wikimedia.org/oojs/master/OO.Factory.html
40 *
41 * @param {string} name Object name
42 * @param {...any} [args] Arguments to pass to the constructor
43 * @return {Object} The new object
44 * @throws {Error} Unknown object name
45 */
46 ve.ui.MWWikitextDataTransferHandlerFactory.prototype.create = function () {
47 // Parent method
48 const handler = ve.ui.MWWikitextDataTransferHandlerFactory.super.prototype.create.apply( this, arguments ),
49 resolve = handler.resolve.bind( handler );
50
51 function isPlain( data ) {
52 return typeof data === 'string' || ve.dm.LinearData.static.getType( data ) === 'paragraph';
53 }
54
55 handler.resolve = function ( dataOrDoc ) {
56 if ( typeof dataOrDoc === 'string' || ( Array.isArray( dataOrDoc ) && dataOrDoc.every( isPlain ) ) ) {
57 resolve( dataOrDoc );
58 } else {
59 const doc = dataOrDoc instanceof ve.dm.Document ?
60 dataOrDoc :
61 // The handler may have also written items to the store
62 new ve.dm.Document( new ve.dm.ElementLinearData( handler.surface.getModel().getDocument().getStore(), dataOrDoc ) );
63
64 // Optimization: we can skip a server hit if this is a plain link,
65 // with no title, whose href is equal to the contained text. This
66 // avoids a stutter in the common case of pasting a link into the
67 // document.
68 const annotations = doc.data.getAnnotationsFromRange( new ve.Range( 0, doc.data.getLength() ) );
69 if ( annotations.getLength() === 1 ) {
70 const text = doc.data.getText();
71 if ( annotations.get( 0 ).getAttribute( 'href' ) === text ) {
72 return resolve( text );
73 }
74 }
75
76 ve.init.target.getWikitextFragment( doc, false )
77 .done( resolve )
78 .fail( () => {
79 handler.abort();
80 } );
81 }
82 };
83
84 return handler;
85 };
86
87 /* Initialization */
88
89 ve.ui.wikitextDataTransferHandlerFactory = new ve.ui.MWWikitextDataTransferHandlerFactory();
90
91 ve.ui.wikitextDataTransferHandlerFactory.unregister( ve.ui.MWWikitextStringTransferHandler );