From 500570b4ca1c3f54633e01e3be4e8bbc1a9888c5 Mon Sep 17 00:00:00 2001 From: Roan Kattouw Date: Tue, 5 Nov 2013 22:13:12 -0800 Subject: [PATCH] Restore selection when editor is refocused When the editor is focused, the selection goes back to the start of the document. This was remedied in the .focus() method, but not in response to native focus events, so when external code blurred then refocused the editor, the selection would move to the top. This broke section editing on wikis where ULS is installed: the selection would be initialized at the start of the section, but then ULS would load and blur the documentNode (by focusing the pasteTarget) and then focus it again, so the selection would move to the top. Instead of restoring the selection only in .focus(), restore it in response to focus events on the documentNode. When this is done, saving and restoring the scrollTop is no longer needed. Bug: 56651 Change-Id: I14700174ee092f9b208215d31a7d1871078a89bf --- modules/ve/ce/ve.ce.Surface.js | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/modules/ve/ce/ve.ce.Surface.js b/modules/ve/ce/ve.ce.Surface.js index cbf8d6422a..125b7d9ae5 100644 --- a/modules/ve/ce/ve.ce.Surface.js +++ b/modules/ve/ce/ve.ce.Surface.js @@ -297,20 +297,16 @@ ve.ce.Surface.prototype.destroy = function () { }; /** - * Give focus to the surface, preserving the previous selection. + * Give focus to the surface, reapplying the model selection. * * This is used when switching between surfaces, e.g. when closing a dialog window. + * + * If the surface is already focused, this does nothing. In particular, the selection won't be + * reapplied. */ ve.ce.Surface.prototype.focus = function () { - var $document = this.documentView.getDocumentNode().$element, - $window = this.$( OO.ui.Element.getWindow( $document ) ), - scrollTop = $window.scrollTop(); - - $document[0].focus(); - // Calling focus sets the cursor to zero offset, so we need to restore scrollTop - $window.scrollTop( scrollTop ); - this.focusedNode = null; - this.onModelSelect( this.surface.getModel().selection ); + this.documentView.getDocumentNode().$element[0].focus(); + // documentOnFocus takes care of the rest }; /*! Native Browser Events */ @@ -321,7 +317,12 @@ ve.ce.Surface.prototype.focus = function () { * @method * @param {Event} e Focus event (native event, NOT a jQuery event!) */ -ve.ce.Surface.prototype.documentOnFocus = function () { +ve.ce.Surface.prototype.documentOnFocus = function ( e ) { + if ( e.target === this.documentView.getDocumentNode().$element[0] ) { + // The document node was focused (as opposed to the paste target) + // Restore the selection + this.onModelSelect( this.surface.getModel().getSelection() ); + } this.eventSequencer.attach( this.$element ); this.surfaceObserver.startTimerLoop(); };