浏览代码

Other: getOptimalPosition utility should accept the target option
defined as a function. Closes #157.

Aleksander Nowodzinski 8 年之前
父节点
当前提交
7a8e4ffc79
共有 2 个文件被更改,包括 21 次插入1 次删除
  1. 7 1
      packages/ckeditor5-utils/src/dom/position.js
  2. 14 0
      packages/ckeditor5-utils/tests/dom/position.js

+ 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

@@ -18,6 +18,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 );