From 41ca5287052f49f679690f701c0ec730dbd1c689 Mon Sep 17 00:00:00 2001 From: David Lynch Date: Tue, 9 May 2017 09:03:10 -0500 Subject: [PATCH] MWWikitextDataTransferHandlerFactory: skip getWikitextFragment if possible This lets us avoid a fraction of a second lag when someone pastes a plain URL into source mode. Bug: T163237 Change-Id: I0e673856dec5f273fb92731b27494762299d2d6c --- .../ve.ui.MWWikitextDataTransferHandlerFactory.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/modules/ve-mw/ui/ve.ui.MWWikitextDataTransferHandlerFactory.js b/modules/ve-mw/ui/ve.ui.MWWikitextDataTransferHandlerFactory.js index 507cf2f38a..240ddeb415 100644 --- a/modules/ve-mw/ui/ve.ui.MWWikitextDataTransferHandlerFactory.js +++ b/modules/ve-mw/ui/ve.ui.MWWikitextDataTransferHandlerFactory.js @@ -47,6 +47,7 @@ ve.ui.MWWikitextDataTransferHandlerFactory.prototype.create = function () { } handler.resolve = function ( dataOrDoc ) { + var annotations, text; if ( typeof dataOrDoc === 'string' || ( Array.isArray( dataOrDoc ) && dataOrDoc.every( isPlain ) ) ) { resolve( dataOrDoc ); } else { @@ -55,6 +56,18 @@ ve.ui.MWWikitextDataTransferHandlerFactory.prototype.create = function () { // The handler may have also written items to the store new ve.dm.Document( new ve.dm.ElementLinearData( handler.surface.getModel().getDocument().getStore(), dataOrDoc ) ); + // 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. + annotations = doc.data.getAnnotationsFromRange( new ve.Range( 0, doc.data.getLength() ) ); + if ( annotations.getLength() === 1 ) { + text = doc.getData().reduce( function ( acc, val ) { return ( Array.isArray( acc ) ? acc[ 0 ] : acc ) + val[ 0 ]; } ); + if ( annotations.get( 0 ).getAttribute( 'href' ) === text ) { + return resolve( text ); + } + } + ve.init.target.getWikitextFragment( doc, false ) .done( resolve ) .fail( function () {