8
0
Quellcode durchsuchen

Merge pull request #158 from ckeditor/t/157

Other: `getOptimalPosition` utility should accept the target option defined as a function. Closes #157.
Oskar Wróbel vor 8 Jahren
Ursprung
Commit
9f1054844a

+ 7 - 1
packages/ckeditor5-utils/src/dom/position.js

@@ -76,6 +76,12 @@ import getPositionedAncestor from './getpositionedancestor';
  * @returns {module:utils/dom/position~Position}
  */
 export function getOptimalPosition( { element, target, positions, limiter, fitInViewport } ) {
+	// If the {@link module:utils/dom/position~Options#target} is a function, use what it returns.
+	// https://github.com/ckeditor/ckeditor5-utils/issues/157
+	if ( typeof target == 'function' ) {
+		target = target();
+	}
+
 	const positionedElementAncestor = getPositionedAncestor( element.parentElement );
 	const elementRect = new Rect( element );
 	const targetRect = new Rect( target );
@@ -255,7 +261,7 @@ function getAbsoluteRectCoordinates( { left, top } ) {
 /**
  * Target with respect to which the `element` is to be positioned.
  *
- * @member {HTMLElement|Range|ClientRect} #target
+ * @member {HTMLElement|Range|ClientRect|Function} #target
  */
 
 /**

+ 14 - 0
packages/ckeditor5-utils/tests/dom/position.js

@@ -58,6 +58,20 @@ describe( 'getOptimalPosition()', () => {
 		} );
 	} );
 
+	it( 'should work when the target is a Function', () => {
+		setElementTargetPlayground();
+
+		assertPosition( {
+			element,
+			target: () => target,
+			positions: [ attachLeft ]
+		}, {
+			top: 100,
+			left: 80,
+			name: 'left'
+		} );
+	} );
+
 	describe( 'for single position', () => {
 		beforeEach( setElementTargetPlayground );