From a51f750f83b828164ac2abda9d9011baa9046888 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bartosz=20Dziewo=C5=84ski?= Date: Tue, 11 Jun 2019 17:21:16 +0200 Subject: [PATCH] Handle VisualEditor when it skips the "tempWikitextEditor" stage Depending on the user's internet connection, the code that sets up mw.libs.ve.tempWikitextEditor may be completely skipped if the normal editor code loads fast enough. Refactor the code to handle all three cases using the same function. Bug: T185279 Change-Id: I003b27f838d1986ed8d238089efd657b96e8ec91 --- modules/ext.linter.edit.js | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/modules/ext.linter.edit.js b/modules/ext.linter.edit.js index c79c8936..d3493cb7 100644 --- a/modules/ext.linter.edit.js +++ b/modules/ext.linter.edit.js @@ -1,18 +1,23 @@ $( function () { - var location = mw.config.get( 'wgLinterErrorLocation' ), - // eslint-disable-next-line no-jquery/no-global-selector - $textbox = $( '#wpTextbox1' ); + var location = mw.config.get( 'wgLinterErrorLocation' ); + + function highlightPosition( $textbox ) { + $textbox.trigger( 'focus' ).textSelection( 'setSelection', { start: location[ 0 ], end: location[ 1 ] } ); + $textbox.textSelection( 'scrollToCaretPosition' ); + } if ( location ) { - if ( $textbox.length ) { - $textbox.trigger( 'focus' ).textSelection( 'setSelection', { start: location[ 0 ], end: location[ 1 ] } ); - $textbox.textSelection( 'scrollToCaretPosition' ); - } + // eslint-disable-next-line no-jquery/no-global-selector + highlightPosition( $( '#wpTextbox1' ) ); + mw.hook( 've.wikitextInteractive' ).add( function () { - mw.libs.ve.tempWikitextEditor.$element[ 0 ].setSelectionRange( - location[ 0 ], location[ 1 ] - ); - mw.libs.ve.tempWikitextEditor.focus(); + if ( mw.libs.ve.tempWikitextEditor ) { + highlightPosition( mw.libs.ve.tempWikitextEditor.$element ); + } else { + // VE dummy textbox + // eslint-disable-next-line no-jquery/no-global-selector + highlightPosition( $( '#wpTextbox1' ) ); + } } ); } } );