Explorar o código

Improved ContextualBalloon#updatePosition().

Oskar Wróbel %!s(int64=8) %!d(string=hai) anos
pai
achega
62d91de789

+ 11 - 4
packages/ckeditor5-ui/src/contextualballoon.js

@@ -10,6 +10,7 @@
 import Plugin from '@ckeditor/ckeditor5-core/src/plugin';
 import BalloonPanelView from './panel/balloon/balloonpanelview';
 import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
+import nth from '@ckeditor/ckeditor5-utils/src/nth';
 
 /**
  * Provides the common contextual balloon panel for the editor.
@@ -151,10 +152,16 @@ export default class ContextualBalloon extends Plugin {
 	}
 
 	/**
-	 * Updates the position of the balloon panel according to position data
-	 * of the first view in the stack.
+	 * Updates the position of the balloon panel according to the given position data
+	 * or position data of the first view in the stack.
+	 *
+	 * @param {module:utils/dom/position~Options} [position] position options.
 	 */
-	updatePosition() {
+	updatePosition( position ) {
+		if ( position ) {
+			nth( 0, this._stack )[ 1 ].position = position;
+		}
+
 		this.view.attachTo( this._getBalloonPosition() );
 	}
 
@@ -178,7 +185,7 @@ export default class ContextualBalloon extends Plugin {
 	 * @returns {module:utils/dom/position~Options}
 	 */
 	_getBalloonPosition() {
-		return Array.from( this._stack.values() )[ 0 ].position;
+		return nth( 0, this._stack )[ 1 ].position;
 	}
 
 	/**

+ 19 - 0
packages/ckeditor5-ui/tests/contextualballoon.js

@@ -251,6 +251,25 @@ describe( 'ContextualBalloon', () => {
 			expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( { target: 'fake' } );
 		} );
 
+		it( 'should attach balloon to the target using new position options', () => {
+			balloon.add( {
+				view: viewA,
+				position: { target: 'fake' }
+			} );
+
+			balloon.view.attachTo.reset();
+
+			balloon.updatePosition( { target: 'new' } );
+
+			expect( balloon.view.attachTo.calledOnce );
+			expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( { target: 'new' } );
+
+			balloon.updatePosition();
+
+			expect( balloon.view.attachTo.calledTwice );
+			expect( balloon.view.attachTo.firstCall.args[ 0 ] ).to.deep.equal( { target: 'new' } );
+		} );
+
 		it( 'should attach balloon to the target using the same position options as currently set when there is more than one view', () => {
 			balloon.add( {
 				view: viewA,