@ckeditor/ckeditor5-utils 40.0.0 → 40.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -26
- package/LICENSE.md +3 -3
- package/package.json +1 -1
- package/src/abortabledebounce.d.ts +17 -0
- package/src/abortabledebounce.js +22 -0
- package/src/areconnectedthroughproperties.d.ts +11 -11
- package/src/areconnectedthroughproperties.js +73 -73
- package/src/ckeditorerror.d.ts +123 -123
- package/src/ckeditorerror.js +176 -176
- package/src/collection.d.ts +433 -427
- package/src/collection.js +583 -575
- package/src/comparearrays.d.ts +30 -30
- package/src/comparearrays.js +47 -47
- package/src/config.d.ts +163 -163
- package/src/config.js +163 -162
- package/src/count.d.ts +18 -18
- package/src/count.js +24 -24
- package/src/delay.d.ts +19 -19
- package/src/delay.js +26 -26
- package/src/diff.d.ts +31 -31
- package/src/diff.js +115 -115
- package/src/difftochanges.d.ts +59 -59
- package/src/difftochanges.js +79 -79
- package/src/dom/createelement.d.ts +57 -57
- package/src/dom/createelement.js +40 -40
- package/src/dom/emittermixin.d.ts +142 -142
- package/src/dom/emittermixin.js +239 -239
- package/src/dom/findclosestscrollableancestor.d.ts +11 -11
- package/src/dom/findclosestscrollableancestor.js +31 -31
- package/src/dom/getancestors.d.ts +17 -17
- package/src/dom/getancestors.js +27 -27
- package/src/dom/getborderwidths.d.ts +24 -24
- package/src/dom/getborderwidths.js +24 -24
- package/src/dom/getcommonancestor.d.ts +12 -12
- package/src/dom/getcommonancestor.js +25 -25
- package/src/dom/getdatafromelement.d.ts +14 -14
- package/src/dom/getdatafromelement.js +20 -20
- package/src/dom/getpositionedancestor.d.ts +10 -10
- package/src/dom/getpositionedancestor.js +22 -22
- package/src/dom/global.d.ts +32 -32
- package/src/dom/global.js +35 -35
- package/src/dom/indexof.d.ts +14 -14
- package/src/dom/indexof.js +21 -21
- package/src/dom/insertat.d.ts +15 -15
- package/src/dom/insertat.js +17 -17
- package/src/dom/iscomment.d.ts +11 -11
- package/src/dom/iscomment.js +14 -14
- package/src/dom/isnode.d.ts +11 -11
- package/src/dom/isnode.js +21 -21
- package/src/dom/isrange.d.ts +11 -11
- package/src/dom/isrange.js +13 -13
- package/src/dom/istext.d.ts +11 -11
- package/src/dom/istext.js +13 -13
- package/src/dom/isvalidattributename.d.ts +10 -10
- package/src/dom/isvalidattributename.js +22 -22
- package/src/dom/isvisible.d.ts +18 -18
- package/src/dom/isvisible.js +20 -20
- package/src/dom/iswindow.d.ts +11 -11
- package/src/dom/iswindow.js +22 -22
- package/src/dom/position.d.ts +211 -211
- package/src/dom/position.js +313 -313
- package/src/dom/rect.d.ts +195 -195
- package/src/dom/rect.js +474 -474
- package/src/dom/remove.d.ts +13 -13
- package/src/dom/remove.js +18 -18
- package/src/dom/resizeobserver.d.ts +74 -74
- package/src/dom/resizeobserver.js +126 -126
- package/src/dom/scroll.d.ts +73 -73
- package/src/dom/scroll.js +383 -383
- package/src/dom/setdatainelement.d.ts +14 -14
- package/src/dom/setdatainelement.js +20 -20
- package/src/dom/tounit.d.ts +22 -22
- package/src/dom/tounit.js +16 -16
- package/src/elementreplacer.d.ts +31 -31
- package/src/elementreplacer.js +43 -43
- package/src/emittermixin.d.ts +312 -312
- package/src/emittermixin.js +453 -453
- package/src/env.d.ts +117 -117
- package/src/env.js +122 -122
- package/src/eventinfo.d.ts +58 -58
- package/src/eventinfo.js +26 -26
- package/src/fastdiff.d.ts +112 -112
- package/src/fastdiff.js +248 -248
- package/src/first.d.ts +11 -11
- package/src/first.js +17 -17
- package/src/focustracker.d.ts +75 -75
- package/src/focustracker.js +95 -95
- package/src/index.d.ts +64 -61
- package/src/index.js +63 -60
- package/src/inserttopriorityarray.d.ts +30 -30
- package/src/inserttopriorityarray.js +21 -21
- package/src/isiterable.d.ts +14 -14
- package/src/isiterable.js +16 -16
- package/src/keyboard.d.ts +126 -126
- package/src/keyboard.js +221 -221
- package/src/keystrokehandler.d.ts +87 -87
- package/src/keystrokehandler.js +122 -122
- package/src/language.d.ts +17 -17
- package/src/language.js +19 -19
- package/src/locale.d.ts +120 -120
- package/src/locale.js +76 -76
- package/src/mapsequal.d.ts +15 -15
- package/src/mapsequal.js +27 -27
- package/src/mix.d.ts +85 -85
- package/src/mix.js +50 -50
- package/src/nth.d.ts +16 -16
- package/src/nth.js +24 -24
- package/src/objecttomap.d.ts +23 -23
- package/src/objecttomap.js +27 -27
- package/src/observablemixin.d.ts +560 -560
- package/src/observablemixin.js +580 -580
- package/src/priorities.d.ts +33 -33
- package/src/priorities.js +23 -23
- package/src/retry.d.ts +33 -0
- package/src/retry.js +47 -0
- package/src/splicearray.d.ts +26 -26
- package/src/splicearray.js +40 -40
- package/src/spy.d.ts +21 -21
- package/src/spy.js +22 -22
- package/src/toarray.d.ts +25 -25
- package/src/toarray.js +7 -7
- package/src/tomap.d.ts +19 -19
- package/src/tomap.js +29 -29
- package/src/translation-service.d.ts +168 -168
- package/src/translation-service.js +198 -198
- package/src/uid.d.ts +15 -15
- package/src/uid.js +57 -57
- package/src/unicode.d.ts +54 -54
- package/src/unicode.js +85 -85
- package/src/verifylicense.d.ts +15 -15
- package/src/verifylicense.js +87 -87
- package/src/version.d.ts +10 -10
- package/src/version.js +153 -153
- package/src/wait.d.ts +16 -0
- package/src/wait.js +29 -0
package/src/dom/position.js
CHANGED
|
@@ -1,313 +1,313 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
-
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* @module utils/dom/position
|
|
7
|
-
*/
|
|
8
|
-
import global from './global';
|
|
9
|
-
import Rect from './rect';
|
|
10
|
-
import getPositionedAncestor from './getpositionedancestor';
|
|
11
|
-
import { isFunction } from 'lodash-es';
|
|
12
|
-
// @if CK_DEBUG_POSITION // const {
|
|
13
|
-
// @if CK_DEBUG_POSITION // default: RectDrawer,
|
|
14
|
-
// @if CK_DEBUG_POSITION // diagonalStylesBlack,
|
|
15
|
-
// @if CK_DEBUG_POSITION // diagonalStylesGreen,
|
|
16
|
-
// @if CK_DEBUG_POSITION // diagonalStylesRed
|
|
17
|
-
// @if CK_DEBUG_POSITION // } = require( '@ckeditor/ckeditor5-utils/tests/_utils/rectdrawer' );
|
|
18
|
-
// @if CK_DEBUG_POSITION // const TARGET_RECT_STYLE = {
|
|
19
|
-
// @if CK_DEBUG_POSITION // outlineWidth: '2px', outlineStyle: 'dashed', outlineColor: 'blue', outlineOffset: '2px'
|
|
20
|
-
// @if CK_DEBUG_POSITION // };
|
|
21
|
-
// @if CK_DEBUG_POSITION // const VISIBLE_TARGET_RECT_STYLE = {
|
|
22
|
-
// @if CK_DEBUG_POSITION // ...diagonalStylesBlack,
|
|
23
|
-
// @if CK_DEBUG_POSITION // opacity: '1',
|
|
24
|
-
// @if CK_DEBUG_POSITION // backgroundColor: '#00000033',
|
|
25
|
-
// @if CK_DEBUG_POSITION // outlineWidth: '2px'
|
|
26
|
-
// @if CK_DEBUG_POSITION // };
|
|
27
|
-
// @if CK_DEBUG_POSITION // const VIEWPORT_RECT_STYLE = {
|
|
28
|
-
// @if CK_DEBUG_POSITION // outlineWidth: '2px',
|
|
29
|
-
// @if CK_DEBUG_POSITION // outlineOffset: '-2px',
|
|
30
|
-
// @if CK_DEBUG_POSITION // outlineStyle: 'solid',
|
|
31
|
-
// @if CK_DEBUG_POSITION // outlineColor: 'red'
|
|
32
|
-
// @if CK_DEBUG_POSITION // };
|
|
33
|
-
// @if CK_DEBUG_POSITION // const VISIBLE_LIMITER_RECT_STYLE = {
|
|
34
|
-
// @if CK_DEBUG_POSITION // ...diagonalStylesGreen,
|
|
35
|
-
// @if CK_DEBUG_POSITION // outlineWidth: '2px',
|
|
36
|
-
// @if CK_DEBUG_POSITION // outlineOffset: '-2px'
|
|
37
|
-
// @if CK_DEBUG_POSITION // };
|
|
38
|
-
// @if CK_DEBUG_POSITION // const ELEMENT_RECT_STYLE = {
|
|
39
|
-
// @if CK_DEBUG_POSITION // outlineWidth: '2px', outlineColor: 'orange', outlineOffset: '-2px'
|
|
40
|
-
// @if CK_DEBUG_POSITION // };
|
|
41
|
-
// @if CK_DEBUG_POSITION // const CHOSEN_POSITION_RECT_STYLE = {
|
|
42
|
-
// @if CK_DEBUG_POSITION // opacity: .5, outlineColor: 'magenta', backgroundColor: 'magenta'
|
|
43
|
-
// @if CK_DEBUG_POSITION // };
|
|
44
|
-
/**
|
|
45
|
-
* Calculates the `position: absolute` coordinates of a given element so it can be positioned with respect to the
|
|
46
|
-
* target in the visually most efficient way, taking various restrictions like viewport or limiter geometry
|
|
47
|
-
* into consideration.
|
|
48
|
-
*
|
|
49
|
-
* **Note**: If there are no position coordinates found that meet the requirements (arguments of this helper),
|
|
50
|
-
* `null` is returned.
|
|
51
|
-
*
|
|
52
|
-
* ```ts
|
|
53
|
-
* // The element which is to be positioned.
|
|
54
|
-
* const element = document.body.querySelector( '#toolbar' );
|
|
55
|
-
*
|
|
56
|
-
* // A target to which the element is positioned relatively.
|
|
57
|
-
* const target = document.body.querySelector( '#container' );
|
|
58
|
-
*
|
|
59
|
-
* // Finding the optimal coordinates for the positioning.
|
|
60
|
-
* const { left, top, name } = getOptimalPosition( {
|
|
61
|
-
* element: element,
|
|
62
|
-
* target: target,
|
|
63
|
-
*
|
|
64
|
-
* // The algorithm will chose among these positions to meet the requirements such
|
|
65
|
-
* // as "limiter" element or "fitInViewport", set below. The positions are considered
|
|
66
|
-
* // in the order of the array.
|
|
67
|
-
* positions: [
|
|
68
|
-
* //
|
|
69
|
-
* // [ Target ]
|
|
70
|
-
* // +-----------------+
|
|
71
|
-
* // | Element |
|
|
72
|
-
* // +-----------------+
|
|
73
|
-
* //
|
|
74
|
-
* targetRect => ( {
|
|
75
|
-
* top: targetRect.bottom,
|
|
76
|
-
* left: targetRect.left,
|
|
77
|
-
* name: 'mySouthEastPosition'
|
|
78
|
-
* } ),
|
|
79
|
-
*
|
|
80
|
-
* //
|
|
81
|
-
* // +-----------------+
|
|
82
|
-
* // | Element |
|
|
83
|
-
* // +-----------------+
|
|
84
|
-
* // [ Target ]
|
|
85
|
-
* //
|
|
86
|
-
* ( targetRect, elementRect ) => ( {
|
|
87
|
-
* top: targetRect.top - elementRect.height,
|
|
88
|
-
* left: targetRect.left,
|
|
89
|
-
* name: 'myNorthEastPosition'
|
|
90
|
-
* } )
|
|
91
|
-
* ],
|
|
92
|
-
*
|
|
93
|
-
* // Find a position such guarantees the element remains within visible boundaries of <body>.
|
|
94
|
-
* limiter: document.body,
|
|
95
|
-
*
|
|
96
|
-
* // Find a position such guarantees the element remains within visible boundaries of the browser viewport.
|
|
97
|
-
* fitInViewport: true
|
|
98
|
-
* } );
|
|
99
|
-
*
|
|
100
|
-
* // The best position which fits into document.body and the viewport. May be useful
|
|
101
|
-
* // to set proper class on the `element`.
|
|
102
|
-
* console.log( name ); // -> "myNorthEastPosition"
|
|
103
|
-
*
|
|
104
|
-
* // Using the absolute coordinates which has been found to position the element
|
|
105
|
-
* // as in the diagram depicting the "myNorthEastPosition" position.
|
|
106
|
-
* element.style.top = top;
|
|
107
|
-
* element.style.left = left;
|
|
108
|
-
* ```
|
|
109
|
-
*
|
|
110
|
-
* @param options The input data and configuration of the helper.
|
|
111
|
-
*/
|
|
112
|
-
export function getOptimalPosition({ element, target, positions, limiter, fitInViewport, viewportOffsetConfig }) {
|
|
113
|
-
// If the {@link module:utils/dom/position~Options#target} is a function, use what it returns.
|
|
114
|
-
// https://github.com/ckeditor/ckeditor5-utils/issues/157
|
|
115
|
-
if (isFunction(target)) {
|
|
116
|
-
target = target();
|
|
117
|
-
}
|
|
118
|
-
// If the {@link module:utils/dom/position~Options#limiter} is a function, use what it returns.
|
|
119
|
-
// https://github.com/ckeditor/ckeditor5-ui/issues/260
|
|
120
|
-
if (isFunction(limiter)) {
|
|
121
|
-
limiter = limiter();
|
|
122
|
-
}
|
|
123
|
-
const positionedElementAncestor = getPositionedAncestor(element);
|
|
124
|
-
const constrainedViewportRect = getConstrainedViewportRect(viewportOffsetConfig);
|
|
125
|
-
const elementRect = new Rect(element);
|
|
126
|
-
const visibleTargetRect = getVisibleViewportIntersectionRect(target, constrainedViewportRect);
|
|
127
|
-
let bestPosition;
|
|
128
|
-
// @if CK_DEBUG_POSITION // const targetRect = new Rect( target );
|
|
129
|
-
// @if CK_DEBUG_POSITION // RectDrawer.clear();
|
|
130
|
-
// @if CK_DEBUG_POSITION // RectDrawer.draw( targetRect, TARGET_RECT_STYLE, 'Target' );
|
|
131
|
-
// @if CK_DEBUG_POSITION // if ( constrainedViewportRect ) {
|
|
132
|
-
// @if CK_DEBUG_POSITION // RectDrawer.draw( constrainedViewportRect, VIEWPORT_RECT_STYLE, 'Viewport' );
|
|
133
|
-
// @if CK_DEBUG_POSITION // }
|
|
134
|
-
// If the target got cropped by ancestors or went off the screen, positioning does not make any sense.
|
|
135
|
-
if (!visibleTargetRect || !constrainedViewportRect.getIntersection(visibleTargetRect)) {
|
|
136
|
-
return null;
|
|
137
|
-
}
|
|
138
|
-
// @if CK_DEBUG_POSITION // RectDrawer.draw( visibleTargetRect, VISIBLE_TARGET_RECT_STYLE, 'VisTgt' );
|
|
139
|
-
const positionOptions = {
|
|
140
|
-
targetRect: visibleTargetRect,
|
|
141
|
-
elementRect,
|
|
142
|
-
positionedElementAncestor,
|
|
143
|
-
viewportRect: constrainedViewportRect
|
|
144
|
-
};
|
|
145
|
-
// If there are no limits, just grab the very first position and be done with that drama.
|
|
146
|
-
if (!limiter && !fitInViewport) {
|
|
147
|
-
bestPosition = new PositionObject(positions[0], positionOptions);
|
|
148
|
-
}
|
|
149
|
-
else {
|
|
150
|
-
if (limiter) {
|
|
151
|
-
const visibleLimiterRect = getVisibleViewportIntersectionRect(limiter, constrainedViewportRect);
|
|
152
|
-
if (visibleLimiterRect) {
|
|
153
|
-
positionOptions.limiterRect = visibleLimiterRect;
|
|
154
|
-
// @if CK_DEBUG_POSITION // RectDrawer.draw( visibleLimiterRect, VISIBLE_LIMITER_RECT_STYLE, 'VisLim' );
|
|
155
|
-
}
|
|
156
|
-
}
|
|
157
|
-
// If there's no best position found, i.e. when all intersections have no area because
|
|
158
|
-
// rects have no width or height, then just return `null`
|
|
159
|
-
bestPosition = getBestPosition(positions, positionOptions);
|
|
160
|
-
}
|
|
161
|
-
return bestPosition;
|
|
162
|
-
}
|
|
163
|
-
/**
|
|
164
|
-
* Returns intersection of visible source `Rect` with Viewport `Rect`. In case when source `Rect` is not visible
|
|
165
|
-
* or there is no intersection between source `Rect` and Viewport `Rect`, `null` will be returned.
|
|
166
|
-
*/
|
|
167
|
-
function getVisibleViewportIntersectionRect(source, viewportRect) {
|
|
168
|
-
const visibleSourceRect = new Rect(source).getVisible();
|
|
169
|
-
if (!visibleSourceRect) {
|
|
170
|
-
return null;
|
|
171
|
-
}
|
|
172
|
-
return visibleSourceRect.getIntersection(viewportRect);
|
|
173
|
-
}
|
|
174
|
-
/**
|
|
175
|
-
* Returns a viewport `Rect` shrunk by the viewport offset config from all sides.
|
|
176
|
-
*/
|
|
177
|
-
function getConstrainedViewportRect(viewportOffsetConfig) {
|
|
178
|
-
viewportOffsetConfig = Object.assign({ top: 0, bottom: 0, left: 0, right: 0 }, viewportOffsetConfig);
|
|
179
|
-
const viewportRect = new Rect(global.window);
|
|
180
|
-
viewportRect.top += viewportOffsetConfig.top;
|
|
181
|
-
viewportRect.height -= viewportOffsetConfig.top;
|
|
182
|
-
viewportRect.bottom -= viewportOffsetConfig.bottom;
|
|
183
|
-
viewportRect.height -= viewportOffsetConfig.bottom;
|
|
184
|
-
return viewportRect;
|
|
185
|
-
}
|
|
186
|
-
/**
|
|
187
|
-
* For a given array of positioning functions, returns such that provides the best
|
|
188
|
-
* fit of the `elementRect` into the `limiterRect` and `viewportRect`.
|
|
189
|
-
*/
|
|
190
|
-
function getBestPosition(positions, options) {
|
|
191
|
-
const { elementRect } = options;
|
|
192
|
-
// This is when element is fully visible.
|
|
193
|
-
const elementRectArea = elementRect.getArea();
|
|
194
|
-
const positionInstances = positions
|
|
195
|
-
.map(positioningFunction => new PositionObject(positioningFunction, options))
|
|
196
|
-
// Some positioning functions may return `null` if they don't want to participate.
|
|
197
|
-
.filter(position => !!position.name);
|
|
198
|
-
let maxFitFactor = 0;
|
|
199
|
-
let bestPosition = null;
|
|
200
|
-
for (const position of positionInstances) {
|
|
201
|
-
const { limiterIntersectionArea, viewportIntersectionArea } = position;
|
|
202
|
-
// If a such position is found that element is fully contained by the limiter then, obviously,
|
|
203
|
-
// there will be no better one, so finishing.
|
|
204
|
-
if (limiterIntersectionArea === elementRectArea) {
|
|
205
|
-
// @if CK_DEBUG_POSITION // RectDrawer.draw( position._rect, CHOSEN_POSITION_RECT_STYLE, [
|
|
206
|
-
// @if CK_DEBUG_POSITION // position.name,
|
|
207
|
-
// @if CK_DEBUG_POSITION // '100% fit',
|
|
208
|
-
// @if CK_DEBUG_POSITION // ].join( '\n' ) );
|
|
209
|
-
return position;
|
|
210
|
-
}
|
|
211
|
-
// To maximize both viewport and limiter intersection areas we use distance on _viewportIntersectionArea
|
|
212
|
-
// and _limiterIntersectionArea plane (without sqrt because we are looking for max value).
|
|
213
|
-
const fitFactor = viewportIntersectionArea ** 2 + limiterIntersectionArea ** 2;
|
|
214
|
-
// @if CK_DEBUG_POSITION // RectDrawer.draw( position._rect, { opacity: .4 }, [
|
|
215
|
-
// @if CK_DEBUG_POSITION // position.name,
|
|
216
|
-
// @if CK_DEBUG_POSITION // 'Vi=' + Math.round( viewportIntersectionArea ),
|
|
217
|
-
// @if CK_DEBUG_POSITION // 'Li=' + Math.round( limiterIntersectionArea )
|
|
218
|
-
// @if CK_DEBUG_POSITION // ].join( '\n' ) );
|
|
219
|
-
if (fitFactor > maxFitFactor) {
|
|
220
|
-
maxFitFactor = fitFactor;
|
|
221
|
-
bestPosition = position;
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
// @if CK_DEBUG_POSITION // if ( bestPosition ) {
|
|
225
|
-
// @if CK_DEBUG_POSITION // RectDrawer.draw( bestPosition._rect, CHOSEN_POSITION_RECT_STYLE );
|
|
226
|
-
// @if CK_DEBUG_POSITION // }
|
|
227
|
-
return bestPosition;
|
|
228
|
-
}
|
|
229
|
-
/**
|
|
230
|
-
* A position class which instances are created and used by the {@link module:utils/dom/position~getOptimalPosition} helper.
|
|
231
|
-
*
|
|
232
|
-
* {@link module:utils/dom/position~Position#top} and {@link module:utils/dom/position~Position#left} properties of the position instance
|
|
233
|
-
* translate directly to the `top` and `left` properties in CSS "`position: absolute` coordinate system". If set on the positioned element
|
|
234
|
-
* in DOM, they will make it display it in the right place in the viewport.
|
|
235
|
-
*/
|
|
236
|
-
class PositionObject {
|
|
237
|
-
/**
|
|
238
|
-
* Creates an instance of the {@link module:utils/dom/position~PositionObject} class.
|
|
239
|
-
*
|
|
240
|
-
* @param positioningFunction function The function that defines the expected
|
|
241
|
-
* coordinates the positioned element should move to.
|
|
242
|
-
* @param options options object.
|
|
243
|
-
* @param options.elementRect The positioned element rect.
|
|
244
|
-
* @param options.targetRect The target element rect.
|
|
245
|
-
* @param options.viewportRect The viewport rect.
|
|
246
|
-
* @param options.limiterRect The limiter rect.
|
|
247
|
-
* @param options.positionedElementAncestor Nearest element ancestor element which CSS position is not "static".
|
|
248
|
-
*/
|
|
249
|
-
constructor(positioningFunction, options) {
|
|
250
|
-
const positioningFunctionOutput = positioningFunction(options.targetRect, options.elementRect, options.viewportRect, options.limiterRect);
|
|
251
|
-
// Nameless position for a function that didn't participate.
|
|
252
|
-
if (!positioningFunctionOutput) {
|
|
253
|
-
return;
|
|
254
|
-
}
|
|
255
|
-
const { left, top, name, config } = positioningFunctionOutput;
|
|
256
|
-
this.name = name;
|
|
257
|
-
this.config = config;
|
|
258
|
-
this._positioningFunctionCoordinates = { left, top };
|
|
259
|
-
this._options = options;
|
|
260
|
-
}
|
|
261
|
-
/**
|
|
262
|
-
* The left value in pixels in the CSS `position: absolute` coordinate system.
|
|
263
|
-
* Set it on the positioned element in DOM to move it to the position.
|
|
264
|
-
*/
|
|
265
|
-
get left() {
|
|
266
|
-
return this._absoluteRect.left;
|
|
267
|
-
}
|
|
268
|
-
/**
|
|
269
|
-
* The top value in pixels in the CSS `position: absolute` coordinate system.
|
|
270
|
-
* Set it on the positioned element in DOM to move it to the position.
|
|
271
|
-
*/
|
|
272
|
-
get top() {
|
|
273
|
-
return this._absoluteRect.top;
|
|
274
|
-
}
|
|
275
|
-
/**
|
|
276
|
-
* An intersection area between positioned element and limiter within viewport constraints.
|
|
277
|
-
*/
|
|
278
|
-
get limiterIntersectionArea() {
|
|
279
|
-
const limiterRect = this._options.limiterRect;
|
|
280
|
-
if (limiterRect) {
|
|
281
|
-
return limiterRect.getIntersectionArea(this._rect);
|
|
282
|
-
}
|
|
283
|
-
return 0;
|
|
284
|
-
}
|
|
285
|
-
/**
|
|
286
|
-
* An intersection area between positioned element and viewport.
|
|
287
|
-
*/
|
|
288
|
-
get viewportIntersectionArea() {
|
|
289
|
-
const viewportRect = this._options.viewportRect;
|
|
290
|
-
return viewportRect.getIntersectionArea(this._rect);
|
|
291
|
-
}
|
|
292
|
-
/**
|
|
293
|
-
* An already positioned element rect. A clone of the element rect passed to the constructor
|
|
294
|
-
* but placed in the viewport according to the positioning function.
|
|
295
|
-
*/
|
|
296
|
-
get _rect() {
|
|
297
|
-
if (this._cachedRect) {
|
|
298
|
-
return this._cachedRect;
|
|
299
|
-
}
|
|
300
|
-
this._cachedRect = this._options.elementRect.clone().moveTo(this._positioningFunctionCoordinates.left, this._positioningFunctionCoordinates.top);
|
|
301
|
-
return this._cachedRect;
|
|
302
|
-
}
|
|
303
|
-
/**
|
|
304
|
-
* An already absolutely positioned element rect. See ({@link #_rect}).
|
|
305
|
-
*/
|
|
306
|
-
get _absoluteRect() {
|
|
307
|
-
if (this._cachedAbsoluteRect) {
|
|
308
|
-
return this._cachedAbsoluteRect;
|
|
309
|
-
}
|
|
310
|
-
this._cachedAbsoluteRect = this._rect.toAbsoluteRect();
|
|
311
|
-
return this._cachedAbsoluteRect;
|
|
312
|
-
}
|
|
313
|
-
}
|
|
1
|
+
/**
|
|
2
|
+
* @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
|
+
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @module utils/dom/position
|
|
7
|
+
*/
|
|
8
|
+
import global from './global';
|
|
9
|
+
import Rect from './rect';
|
|
10
|
+
import getPositionedAncestor from './getpositionedancestor';
|
|
11
|
+
import { isFunction } from 'lodash-es';
|
|
12
|
+
// @if CK_DEBUG_POSITION // const {
|
|
13
|
+
// @if CK_DEBUG_POSITION // default: RectDrawer,
|
|
14
|
+
// @if CK_DEBUG_POSITION // diagonalStylesBlack,
|
|
15
|
+
// @if CK_DEBUG_POSITION // diagonalStylesGreen,
|
|
16
|
+
// @if CK_DEBUG_POSITION // diagonalStylesRed
|
|
17
|
+
// @if CK_DEBUG_POSITION // } = require( '@ckeditor/ckeditor5-utils/tests/_utils/rectdrawer' );
|
|
18
|
+
// @if CK_DEBUG_POSITION // const TARGET_RECT_STYLE = {
|
|
19
|
+
// @if CK_DEBUG_POSITION // outlineWidth: '2px', outlineStyle: 'dashed', outlineColor: 'blue', outlineOffset: '2px'
|
|
20
|
+
// @if CK_DEBUG_POSITION // };
|
|
21
|
+
// @if CK_DEBUG_POSITION // const VISIBLE_TARGET_RECT_STYLE = {
|
|
22
|
+
// @if CK_DEBUG_POSITION // ...diagonalStylesBlack,
|
|
23
|
+
// @if CK_DEBUG_POSITION // opacity: '1',
|
|
24
|
+
// @if CK_DEBUG_POSITION // backgroundColor: '#00000033',
|
|
25
|
+
// @if CK_DEBUG_POSITION // outlineWidth: '2px'
|
|
26
|
+
// @if CK_DEBUG_POSITION // };
|
|
27
|
+
// @if CK_DEBUG_POSITION // const VIEWPORT_RECT_STYLE = {
|
|
28
|
+
// @if CK_DEBUG_POSITION // outlineWidth: '2px',
|
|
29
|
+
// @if CK_DEBUG_POSITION // outlineOffset: '-2px',
|
|
30
|
+
// @if CK_DEBUG_POSITION // outlineStyle: 'solid',
|
|
31
|
+
// @if CK_DEBUG_POSITION // outlineColor: 'red'
|
|
32
|
+
// @if CK_DEBUG_POSITION // };
|
|
33
|
+
// @if CK_DEBUG_POSITION // const VISIBLE_LIMITER_RECT_STYLE = {
|
|
34
|
+
// @if CK_DEBUG_POSITION // ...diagonalStylesGreen,
|
|
35
|
+
// @if CK_DEBUG_POSITION // outlineWidth: '2px',
|
|
36
|
+
// @if CK_DEBUG_POSITION // outlineOffset: '-2px'
|
|
37
|
+
// @if CK_DEBUG_POSITION // };
|
|
38
|
+
// @if CK_DEBUG_POSITION // const ELEMENT_RECT_STYLE = {
|
|
39
|
+
// @if CK_DEBUG_POSITION // outlineWidth: '2px', outlineColor: 'orange', outlineOffset: '-2px'
|
|
40
|
+
// @if CK_DEBUG_POSITION // };
|
|
41
|
+
// @if CK_DEBUG_POSITION // const CHOSEN_POSITION_RECT_STYLE = {
|
|
42
|
+
// @if CK_DEBUG_POSITION // opacity: .5, outlineColor: 'magenta', backgroundColor: 'magenta'
|
|
43
|
+
// @if CK_DEBUG_POSITION // };
|
|
44
|
+
/**
|
|
45
|
+
* Calculates the `position: absolute` coordinates of a given element so it can be positioned with respect to the
|
|
46
|
+
* target in the visually most efficient way, taking various restrictions like viewport or limiter geometry
|
|
47
|
+
* into consideration.
|
|
48
|
+
*
|
|
49
|
+
* **Note**: If there are no position coordinates found that meet the requirements (arguments of this helper),
|
|
50
|
+
* `null` is returned.
|
|
51
|
+
*
|
|
52
|
+
* ```ts
|
|
53
|
+
* // The element which is to be positioned.
|
|
54
|
+
* const element = document.body.querySelector( '#toolbar' );
|
|
55
|
+
*
|
|
56
|
+
* // A target to which the element is positioned relatively.
|
|
57
|
+
* const target = document.body.querySelector( '#container' );
|
|
58
|
+
*
|
|
59
|
+
* // Finding the optimal coordinates for the positioning.
|
|
60
|
+
* const { left, top, name } = getOptimalPosition( {
|
|
61
|
+
* element: element,
|
|
62
|
+
* target: target,
|
|
63
|
+
*
|
|
64
|
+
* // The algorithm will chose among these positions to meet the requirements such
|
|
65
|
+
* // as "limiter" element or "fitInViewport", set below. The positions are considered
|
|
66
|
+
* // in the order of the array.
|
|
67
|
+
* positions: [
|
|
68
|
+
* //
|
|
69
|
+
* // [ Target ]
|
|
70
|
+
* // +-----------------+
|
|
71
|
+
* // | Element |
|
|
72
|
+
* // +-----------------+
|
|
73
|
+
* //
|
|
74
|
+
* targetRect => ( {
|
|
75
|
+
* top: targetRect.bottom,
|
|
76
|
+
* left: targetRect.left,
|
|
77
|
+
* name: 'mySouthEastPosition'
|
|
78
|
+
* } ),
|
|
79
|
+
*
|
|
80
|
+
* //
|
|
81
|
+
* // +-----------------+
|
|
82
|
+
* // | Element |
|
|
83
|
+
* // +-----------------+
|
|
84
|
+
* // [ Target ]
|
|
85
|
+
* //
|
|
86
|
+
* ( targetRect, elementRect ) => ( {
|
|
87
|
+
* top: targetRect.top - elementRect.height,
|
|
88
|
+
* left: targetRect.left,
|
|
89
|
+
* name: 'myNorthEastPosition'
|
|
90
|
+
* } )
|
|
91
|
+
* ],
|
|
92
|
+
*
|
|
93
|
+
* // Find a position such guarantees the element remains within visible boundaries of <body>.
|
|
94
|
+
* limiter: document.body,
|
|
95
|
+
*
|
|
96
|
+
* // Find a position such guarantees the element remains within visible boundaries of the browser viewport.
|
|
97
|
+
* fitInViewport: true
|
|
98
|
+
* } );
|
|
99
|
+
*
|
|
100
|
+
* // The best position which fits into document.body and the viewport. May be useful
|
|
101
|
+
* // to set proper class on the `element`.
|
|
102
|
+
* console.log( name ); // -> "myNorthEastPosition"
|
|
103
|
+
*
|
|
104
|
+
* // Using the absolute coordinates which has been found to position the element
|
|
105
|
+
* // as in the diagram depicting the "myNorthEastPosition" position.
|
|
106
|
+
* element.style.top = top;
|
|
107
|
+
* element.style.left = left;
|
|
108
|
+
* ```
|
|
109
|
+
*
|
|
110
|
+
* @param options The input data and configuration of the helper.
|
|
111
|
+
*/
|
|
112
|
+
export function getOptimalPosition({ element, target, positions, limiter, fitInViewport, viewportOffsetConfig }) {
|
|
113
|
+
// If the {@link module:utils/dom/position~Options#target} is a function, use what it returns.
|
|
114
|
+
// https://github.com/ckeditor/ckeditor5-utils/issues/157
|
|
115
|
+
if (isFunction(target)) {
|
|
116
|
+
target = target();
|
|
117
|
+
}
|
|
118
|
+
// If the {@link module:utils/dom/position~Options#limiter} is a function, use what it returns.
|
|
119
|
+
// https://github.com/ckeditor/ckeditor5-ui/issues/260
|
|
120
|
+
if (isFunction(limiter)) {
|
|
121
|
+
limiter = limiter();
|
|
122
|
+
}
|
|
123
|
+
const positionedElementAncestor = getPositionedAncestor(element);
|
|
124
|
+
const constrainedViewportRect = getConstrainedViewportRect(viewportOffsetConfig);
|
|
125
|
+
const elementRect = new Rect(element);
|
|
126
|
+
const visibleTargetRect = getVisibleViewportIntersectionRect(target, constrainedViewportRect);
|
|
127
|
+
let bestPosition;
|
|
128
|
+
// @if CK_DEBUG_POSITION // const targetRect = new Rect( target );
|
|
129
|
+
// @if CK_DEBUG_POSITION // RectDrawer.clear();
|
|
130
|
+
// @if CK_DEBUG_POSITION // RectDrawer.draw( targetRect, TARGET_RECT_STYLE, 'Target' );
|
|
131
|
+
// @if CK_DEBUG_POSITION // if ( constrainedViewportRect ) {
|
|
132
|
+
// @if CK_DEBUG_POSITION // RectDrawer.draw( constrainedViewportRect, VIEWPORT_RECT_STYLE, 'Viewport' );
|
|
133
|
+
// @if CK_DEBUG_POSITION // }
|
|
134
|
+
// If the target got cropped by ancestors or went off the screen, positioning does not make any sense.
|
|
135
|
+
if (!visibleTargetRect || !constrainedViewportRect.getIntersection(visibleTargetRect)) {
|
|
136
|
+
return null;
|
|
137
|
+
}
|
|
138
|
+
// @if CK_DEBUG_POSITION // RectDrawer.draw( visibleTargetRect, VISIBLE_TARGET_RECT_STYLE, 'VisTgt' );
|
|
139
|
+
const positionOptions = {
|
|
140
|
+
targetRect: visibleTargetRect,
|
|
141
|
+
elementRect,
|
|
142
|
+
positionedElementAncestor,
|
|
143
|
+
viewportRect: constrainedViewportRect
|
|
144
|
+
};
|
|
145
|
+
// If there are no limits, just grab the very first position and be done with that drama.
|
|
146
|
+
if (!limiter && !fitInViewport) {
|
|
147
|
+
bestPosition = new PositionObject(positions[0], positionOptions);
|
|
148
|
+
}
|
|
149
|
+
else {
|
|
150
|
+
if (limiter) {
|
|
151
|
+
const visibleLimiterRect = getVisibleViewportIntersectionRect(limiter, constrainedViewportRect);
|
|
152
|
+
if (visibleLimiterRect) {
|
|
153
|
+
positionOptions.limiterRect = visibleLimiterRect;
|
|
154
|
+
// @if CK_DEBUG_POSITION // RectDrawer.draw( visibleLimiterRect, VISIBLE_LIMITER_RECT_STYLE, 'VisLim' );
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
// If there's no best position found, i.e. when all intersections have no area because
|
|
158
|
+
// rects have no width or height, then just return `null`
|
|
159
|
+
bestPosition = getBestPosition(positions, positionOptions);
|
|
160
|
+
}
|
|
161
|
+
return bestPosition;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Returns intersection of visible source `Rect` with Viewport `Rect`. In case when source `Rect` is not visible
|
|
165
|
+
* or there is no intersection between source `Rect` and Viewport `Rect`, `null` will be returned.
|
|
166
|
+
*/
|
|
167
|
+
function getVisibleViewportIntersectionRect(source, viewportRect) {
|
|
168
|
+
const visibleSourceRect = new Rect(source).getVisible();
|
|
169
|
+
if (!visibleSourceRect) {
|
|
170
|
+
return null;
|
|
171
|
+
}
|
|
172
|
+
return visibleSourceRect.getIntersection(viewportRect);
|
|
173
|
+
}
|
|
174
|
+
/**
|
|
175
|
+
* Returns a viewport `Rect` shrunk by the viewport offset config from all sides.
|
|
176
|
+
*/
|
|
177
|
+
function getConstrainedViewportRect(viewportOffsetConfig) {
|
|
178
|
+
viewportOffsetConfig = Object.assign({ top: 0, bottom: 0, left: 0, right: 0 }, viewportOffsetConfig);
|
|
179
|
+
const viewportRect = new Rect(global.window);
|
|
180
|
+
viewportRect.top += viewportOffsetConfig.top;
|
|
181
|
+
viewportRect.height -= viewportOffsetConfig.top;
|
|
182
|
+
viewportRect.bottom -= viewportOffsetConfig.bottom;
|
|
183
|
+
viewportRect.height -= viewportOffsetConfig.bottom;
|
|
184
|
+
return viewportRect;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* For a given array of positioning functions, returns such that provides the best
|
|
188
|
+
* fit of the `elementRect` into the `limiterRect` and `viewportRect`.
|
|
189
|
+
*/
|
|
190
|
+
function getBestPosition(positions, options) {
|
|
191
|
+
const { elementRect } = options;
|
|
192
|
+
// This is when element is fully visible.
|
|
193
|
+
const elementRectArea = elementRect.getArea();
|
|
194
|
+
const positionInstances = positions
|
|
195
|
+
.map(positioningFunction => new PositionObject(positioningFunction, options))
|
|
196
|
+
// Some positioning functions may return `null` if they don't want to participate.
|
|
197
|
+
.filter(position => !!position.name);
|
|
198
|
+
let maxFitFactor = 0;
|
|
199
|
+
let bestPosition = null;
|
|
200
|
+
for (const position of positionInstances) {
|
|
201
|
+
const { limiterIntersectionArea, viewportIntersectionArea } = position;
|
|
202
|
+
// If a such position is found that element is fully contained by the limiter then, obviously,
|
|
203
|
+
// there will be no better one, so finishing.
|
|
204
|
+
if (limiterIntersectionArea === elementRectArea) {
|
|
205
|
+
// @if CK_DEBUG_POSITION // RectDrawer.draw( position._rect, CHOSEN_POSITION_RECT_STYLE, [
|
|
206
|
+
// @if CK_DEBUG_POSITION // position.name,
|
|
207
|
+
// @if CK_DEBUG_POSITION // '100% fit',
|
|
208
|
+
// @if CK_DEBUG_POSITION // ].join( '\n' ) );
|
|
209
|
+
return position;
|
|
210
|
+
}
|
|
211
|
+
// To maximize both viewport and limiter intersection areas we use distance on _viewportIntersectionArea
|
|
212
|
+
// and _limiterIntersectionArea plane (without sqrt because we are looking for max value).
|
|
213
|
+
const fitFactor = viewportIntersectionArea ** 2 + limiterIntersectionArea ** 2;
|
|
214
|
+
// @if CK_DEBUG_POSITION // RectDrawer.draw( position._rect, { opacity: .4 }, [
|
|
215
|
+
// @if CK_DEBUG_POSITION // position.name,
|
|
216
|
+
// @if CK_DEBUG_POSITION // 'Vi=' + Math.round( viewportIntersectionArea ),
|
|
217
|
+
// @if CK_DEBUG_POSITION // 'Li=' + Math.round( limiterIntersectionArea )
|
|
218
|
+
// @if CK_DEBUG_POSITION // ].join( '\n' ) );
|
|
219
|
+
if (fitFactor > maxFitFactor) {
|
|
220
|
+
maxFitFactor = fitFactor;
|
|
221
|
+
bestPosition = position;
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
// @if CK_DEBUG_POSITION // if ( bestPosition ) {
|
|
225
|
+
// @if CK_DEBUG_POSITION // RectDrawer.draw( bestPosition._rect, CHOSEN_POSITION_RECT_STYLE );
|
|
226
|
+
// @if CK_DEBUG_POSITION // }
|
|
227
|
+
return bestPosition;
|
|
228
|
+
}
|
|
229
|
+
/**
|
|
230
|
+
* A position class which instances are created and used by the {@link module:utils/dom/position~getOptimalPosition} helper.
|
|
231
|
+
*
|
|
232
|
+
* {@link module:utils/dom/position~Position#top} and {@link module:utils/dom/position~Position#left} properties of the position instance
|
|
233
|
+
* translate directly to the `top` and `left` properties in CSS "`position: absolute` coordinate system". If set on the positioned element
|
|
234
|
+
* in DOM, they will make it display it in the right place in the viewport.
|
|
235
|
+
*/
|
|
236
|
+
class PositionObject {
|
|
237
|
+
/**
|
|
238
|
+
* Creates an instance of the {@link module:utils/dom/position~PositionObject} class.
|
|
239
|
+
*
|
|
240
|
+
* @param positioningFunction function The function that defines the expected
|
|
241
|
+
* coordinates the positioned element should move to.
|
|
242
|
+
* @param options options object.
|
|
243
|
+
* @param options.elementRect The positioned element rect.
|
|
244
|
+
* @param options.targetRect The target element rect.
|
|
245
|
+
* @param options.viewportRect The viewport rect.
|
|
246
|
+
* @param options.limiterRect The limiter rect.
|
|
247
|
+
* @param options.positionedElementAncestor Nearest element ancestor element which CSS position is not "static".
|
|
248
|
+
*/
|
|
249
|
+
constructor(positioningFunction, options) {
|
|
250
|
+
const positioningFunctionOutput = positioningFunction(options.targetRect, options.elementRect, options.viewportRect, options.limiterRect);
|
|
251
|
+
// Nameless position for a function that didn't participate.
|
|
252
|
+
if (!positioningFunctionOutput) {
|
|
253
|
+
return;
|
|
254
|
+
}
|
|
255
|
+
const { left, top, name, config } = positioningFunctionOutput;
|
|
256
|
+
this.name = name;
|
|
257
|
+
this.config = config;
|
|
258
|
+
this._positioningFunctionCoordinates = { left, top };
|
|
259
|
+
this._options = options;
|
|
260
|
+
}
|
|
261
|
+
/**
|
|
262
|
+
* The left value in pixels in the CSS `position: absolute` coordinate system.
|
|
263
|
+
* Set it on the positioned element in DOM to move it to the position.
|
|
264
|
+
*/
|
|
265
|
+
get left() {
|
|
266
|
+
return this._absoluteRect.left;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* The top value in pixels in the CSS `position: absolute` coordinate system.
|
|
270
|
+
* Set it on the positioned element in DOM to move it to the position.
|
|
271
|
+
*/
|
|
272
|
+
get top() {
|
|
273
|
+
return this._absoluteRect.top;
|
|
274
|
+
}
|
|
275
|
+
/**
|
|
276
|
+
* An intersection area between positioned element and limiter within viewport constraints.
|
|
277
|
+
*/
|
|
278
|
+
get limiterIntersectionArea() {
|
|
279
|
+
const limiterRect = this._options.limiterRect;
|
|
280
|
+
if (limiterRect) {
|
|
281
|
+
return limiterRect.getIntersectionArea(this._rect);
|
|
282
|
+
}
|
|
283
|
+
return 0;
|
|
284
|
+
}
|
|
285
|
+
/**
|
|
286
|
+
* An intersection area between positioned element and viewport.
|
|
287
|
+
*/
|
|
288
|
+
get viewportIntersectionArea() {
|
|
289
|
+
const viewportRect = this._options.viewportRect;
|
|
290
|
+
return viewportRect.getIntersectionArea(this._rect);
|
|
291
|
+
}
|
|
292
|
+
/**
|
|
293
|
+
* An already positioned element rect. A clone of the element rect passed to the constructor
|
|
294
|
+
* but placed in the viewport according to the positioning function.
|
|
295
|
+
*/
|
|
296
|
+
get _rect() {
|
|
297
|
+
if (this._cachedRect) {
|
|
298
|
+
return this._cachedRect;
|
|
299
|
+
}
|
|
300
|
+
this._cachedRect = this._options.elementRect.clone().moveTo(this._positioningFunctionCoordinates.left, this._positioningFunctionCoordinates.top);
|
|
301
|
+
return this._cachedRect;
|
|
302
|
+
}
|
|
303
|
+
/**
|
|
304
|
+
* An already absolutely positioned element rect. See ({@link #_rect}).
|
|
305
|
+
*/
|
|
306
|
+
get _absoluteRect() {
|
|
307
|
+
if (this._cachedAbsoluteRect) {
|
|
308
|
+
return this._cachedAbsoluteRect;
|
|
309
|
+
}
|
|
310
|
+
this._cachedAbsoluteRect = this._rect.toAbsoluteRect();
|
|
311
|
+
return this._cachedAbsoluteRect;
|
|
312
|
+
}
|
|
313
|
+
}
|