Esanders has uploaded a new change for review. https://gerrit.wikimedia.org/r/270266
Change subject: [BREAKING CHANGE] Move scrollIntoView down into the surface ...................................................................... [BREAKING CHANGE] Move scrollIntoView down into the surface We already tell the surface about toolbar height, we just need to tell it the $scrollContainer and it can do the calculations itself, instead of having the target reach in the surface. Change-Id: I1416385253f6b5521cee9219a356a81d6238f234 --- M src/init/ve.init.Target.js M src/ui/ve.ui.Surface.js 2 files changed, 58 insertions(+), 64 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/VisualEditor/VisualEditor refs/changes/66/270266/1 diff --git a/src/init/ve.init.Target.js b/src/init/ve.init.Target.js index 09b54f7..9c6ab8b 100644 --- a/src/init/ve.init.Target.js +++ b/src/init/ve.init.Target.js @@ -296,6 +296,7 @@ */ ve.init.Target.prototype.getSurfaceConfig = function ( config ) { return ve.extendObject( { + $scrollContainer: this.$scrollContainer, includeCommands: this.constructor.static.includeCommands, excludeCommands: OO.simpleArrayUnion( this.constructor.static.excludeCommands, @@ -317,8 +318,7 @@ var surface = this.createSurface( dmDoc, config ); this.surfaces.push( surface ); surface.getView().connect( this, { - focus: this.onSurfaceViewFocus.bind( this, surface ), - keyup: this.onSurfaceViewKeyUp.bind( this, surface ) + focus: this.onSurfaceViewFocus.bind( this, surface ) } ); return surface; }; @@ -339,68 +339,6 @@ */ ve.init.Target.prototype.onSurfaceViewFocus = function ( surface ) { this.setSurface( surface ); -}; - -/** - * Handle key up events from a surface's view - * - * @param {ve.ui.Surface} surface Surface firing the event - */ -ve.init.Target.prototype.onSurfaceViewKeyUp = function ( surface ) { - this.scrollCursorIntoView( surface ); -}; - -/** - * Check if the toolbar is overlapping the surface - * - * @return {boolean} Toolbar is overlapping the surface - */ -ve.init.Target.prototype.isToolbarOverSurface = function () { - return this.getToolbar().isFloating(); -}; - -/** - * Scroll the cursor into view. - * - * @param {ve.ui.Surface} surface Surface to scroll - */ -ve.init.Target.prototype.scrollCursorIntoView = function ( surface ) { - var nativeRange, clientRect, cursorTop, scrollTo, toolbarBottom; - - if ( !this.isToolbarOverSurface() ) { - return; - } - - nativeRange = surface.getView().getNativeRange(); - if ( !nativeRange ) { - return; - } - - if ( OO.ui.contains( surface.getView().$pasteTarget[ 0 ], nativeRange.startContainer, true ) ) { - return; - } - - clientRect = RangeFix.getBoundingClientRect( nativeRange ); - if ( !clientRect ) { - return; - } - - cursorTop = clientRect.top - 5; - toolbarBottom = this.getSurface().toolbarHeight; - - if ( cursorTop < toolbarBottom ) { - scrollTo = this.$scrollContainer.scrollTop() + cursorTop - toolbarBottom; - this.scrollTo( scrollTo ); - } -}; - -/** - * Scroll the scroll container to a specific offset - * - * @param {number} offset Scroll offset - */ -ve.init.Target.prototype.scrollTo = function ( offset ) { - this.$scrollContainer.scrollTop( offset ); }; /** diff --git a/src/ui/ve.ui.Surface.js b/src/ui/ve.ui.Surface.js index 2c576ad..b92ab98 100644 --- a/src/ui/ve.ui.Surface.js +++ b/src/ui/ve.ui.Surface.js @@ -14,6 +14,7 @@ * @constructor * @param {HTMLDocument|Array|ve.dm.LinearData|ve.dm.Document} dataOrDoc Document data to edit * @param {Object} [config] Configuration options + * @cfg {jQuery} [$scrollContainer] The scroll container of the surface * @cfg {string[]|null} [includeCommands] List of commands to include, null for all registered commands * @cfg {string[]} [excludeCommands] List of commands to exclude * @cfg {Object} [importRules] Import rules @@ -29,6 +30,7 @@ ve.ui.Surface.super.call( this, config ); // Properties + this.$scrollContainer = config.$scrollContainer || $( this.getElementWindow() ); this.inDialog = config.inDialog || ''; this.globalOverlay = new ve.ui.Overlay( { classes: [ 've-ui-overlay-global' ] } ); this.localOverlay = new ve.ui.Overlay( { classes: [ 've-ui-overlay-local' ] } ); @@ -69,6 +71,7 @@ } ); // Events + this.getView().connect( this, { keyup: 'scrollCursorIntoView' } ); this.getModel().getDocument().connect( this, { transact: 'onDocumentTransact' } ); // Initialization @@ -91,6 +94,12 @@ * When a surface is destroyed. * * @event destroy + */ + +/** + * The surface was scrolled programmatically + * + * @event scroll */ /* Static Properties */ @@ -405,6 +414,53 @@ }; /** + * Scroll the cursor into view. + * + * This is required when the cursor disappears under the floating toolbar. + */ +ve.ui.Surface.prototype.scrollCursorIntoView = function () { + var view, nativeRange, clientRect, cursorTop, scrollTo, toolbarBottom; + + if ( !this.toolbarHeight ) { + return; + } + + view = this.getView(); + nativeRange = view.getNativeRange(); + if ( !nativeRange ) { + return; + } + + if ( OO.ui.contains( view.$pasteTarget[ 0 ], nativeRange.startContainer, true ) ) { + return; + } + + clientRect = RangeFix.getBoundingClientRect( nativeRange ); + if ( !clientRect ) { + return; + } + + cursorTop = clientRect.top - 5; + toolbarBottom = this.toolbarHeight; + + if ( cursorTop < toolbarBottom ) { + scrollTo = this.$scrollContainer.scrollTop() + cursorTop - toolbarBottom; + this.scrollTo( scrollTo ); + } +}; + +/** + * Scroll the scroll container to a specific offset + * + * @param {number} offset Scroll offset + * @fires scroll + */ +ve.ui.Surface.prototype.scrollTo = function ( offset ) { + this.$scrollContainer.scrollTop( offset ); + this.emit( 'scroll' ); +}; + +/** * Set placeholder text * * @param {string} [placeholder] Placeholder text, clears placeholder if not set -- To view, visit https://gerrit.wikimedia.org/r/270266 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I1416385253f6b5521cee9219a356a81d6238f234 Gerrit-PatchSet: 1 Gerrit-Project: VisualEditor/VisualEditor Gerrit-Branch: master Gerrit-Owner: Esanders <esand...@wikimedia.org> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits