From 3970288c8c907ca32866dcdfdb5b342f5a6ae483 Mon Sep 17 00:00:00 2001 From: Ed Sanders Date: Wed, 6 Jul 2016 22:11:30 +0100 Subject: [PATCH] Make ArticleTarget#goToHeading work at all times Was previously written to work on initialise when the surface isn't focused. If the surface is focused, run immediately. Change-Id: Ie7accd63a4ca3e18448914d34609fe0418f15b4e --- modules/ve-mw/init/ve.init.mw.ArticleTarget.js | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/modules/ve-mw/init/ve.init.mw.ArticleTarget.js b/modules/ve-mw/init/ve.init.mw.ArticleTarget.js index 92cbca3b42..88fdd32771 100644 --- a/modules/ve-mw/init/ve.init.mw.ArticleTarget.js +++ b/modules/ve-mw/init/ve.init.mw.ArticleTarget.js @@ -1637,15 +1637,22 @@ ve.init.mw.ArticleTarget.prototype.goToHeading = function ( headingNode ) { offset = surfaceModel.getDocument().data.getNearestContentOffset( offsetNode.getModel().getOffset(), 1 ); - // onDocumentFocus is debounced, so wait for that to happen before setting - // the model selection, otherwise it will get reset - surfaceView.once( 'focus', function () { + + function scrollAndSetSelection() { surfaceModel.setLinearSelection( new ve.Range( offset ) ); // Focussing the document triggers showSelection which calls scrollIntoView // which uses a jQuery animation, so make sure this is aborted. $( OO.ui.Element.static.getClosestScrollableContainer( surfaceView.$element[ 0 ] ) ).stop( true ); target.scrollToHeading( headingNode ); - } ); + } + + if ( surfaceView.isFocused() ) { + scrollAndSetSelection(); + } else { + // onDocumentFocus is debounced, so wait for that to happen before setting + // the model selection, otherwise it will get reset + surfaceView.once( 'focus', scrollAndSetSelection ); + } }; /**