@ckeditor/ckeditor5-utils 34.2.0 → 35.1.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 +324 -0
- package/LICENSE.md +1 -1
- package/package.json +19 -8
- package/src/areconnectedthroughproperties.js +54 -71
- package/src/ckeditorerror.js +92 -114
- package/src/collection.js +594 -762
- package/src/comparearrays.js +22 -28
- package/src/config.js +193 -223
- package/src/count.js +8 -12
- package/src/diff.js +85 -110
- package/src/difftochanges.js +47 -57
- package/src/dom/createelement.js +17 -25
- package/src/dom/emittermixin.js +202 -263
- package/src/dom/getancestors.js +9 -13
- package/src/dom/getborderwidths.js +10 -13
- package/src/dom/getcommonancestor.js +9 -15
- package/src/dom/getdatafromelement.js +5 -9
- package/src/dom/getpositionedancestor.js +9 -14
- package/src/dom/global.js +15 -4
- package/src/dom/indexof.js +7 -11
- package/src/dom/insertat.js +2 -4
- package/src/dom/iscomment.js +2 -5
- package/src/dom/isnode.js +10 -12
- package/src/dom/isrange.js +2 -4
- package/src/dom/istext.js +2 -4
- package/src/dom/isvisible.js +2 -4
- package/src/dom/iswindow.js +11 -16
- package/src/dom/position.js +220 -410
- package/src/dom/rect.js +335 -414
- package/src/dom/remove.js +5 -8
- package/src/dom/resizeobserver.js +109 -342
- package/src/dom/scroll.js +151 -183
- package/src/dom/setdatainelement.js +5 -9
- package/src/dom/tounit.js +10 -12
- package/src/elementreplacer.js +30 -44
- package/src/emittermixin.js +368 -634
- package/src/env.js +109 -116
- package/src/eventinfo.js +12 -65
- package/src/fastdiff.js +96 -128
- package/src/first.js +8 -12
- package/src/focustracker.js +77 -133
- package/src/index.js +0 -9
- package/src/inserttopriorityarray.js +9 -30
- package/src/isiterable.js +2 -4
- package/src/keyboard.js +117 -196
- package/src/keystrokehandler.js +72 -88
- package/src/language.js +9 -15
- package/src/locale.js +61 -158
- package/src/mapsequal.js +12 -17
- package/src/mix.js +17 -16
- package/src/nth.js +8 -11
- package/src/objecttomap.js +7 -11
- package/src/observablemixin.js +474 -778
- package/src/priorities.js +20 -32
- package/src/spy.js +3 -6
- package/src/toarray.js +2 -13
- package/src/tomap.js +8 -10
- package/src/translation-service.js +57 -93
- package/src/uid.js +34 -38
- package/src/unicode.js +28 -43
- package/src/version.js +134 -143
package/src/dom/position.js
CHANGED
|
@@ -2,19 +2,15 @@
|
|
|
2
2
|
* @license Copyright (c) 2003-2022, CKSource Holding sp. z o.o. All rights reserved.
|
|
3
3
|
* For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
|
|
4
4
|
*/
|
|
5
|
-
|
|
6
5
|
/**
|
|
7
6
|
* @module utils/dom/position
|
|
8
7
|
*/
|
|
9
|
-
|
|
10
8
|
import global from './global';
|
|
11
9
|
import Rect from './rect';
|
|
12
10
|
import getPositionedAncestor from './getpositionedancestor';
|
|
13
11
|
import getBorderWidths from './getborderwidths';
|
|
14
12
|
import { isFunction } from 'lodash-es';
|
|
15
|
-
|
|
16
13
|
// @if CK_DEBUG_POSITION // import { RectDrawer } from '@ckeditor/ckeditor5-minimap/src/utils';
|
|
17
|
-
|
|
18
14
|
/**
|
|
19
15
|
* Calculates the `position: absolute` coordinates of a given element so it can be positioned with respect to the
|
|
20
16
|
* target in the visually most efficient way, taking various restrictions like viewport or limiter geometry
|
|
@@ -79,127 +75,102 @@ import { isFunction } from 'lodash-es';
|
|
|
79
75
|
* @param {module:utils/dom/position~Options} options The input data and configuration of the helper.
|
|
80
76
|
* @returns {module:utils/dom/position~Position}
|
|
81
77
|
*/
|
|
82
|
-
export function getOptimalPosition(
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
// @if CK_DEBUG_POSITION // }
|
|
120
|
-
|
|
121
|
-
Object.assign( positionOptions, { limiterRect, viewportRect } );
|
|
122
|
-
|
|
123
|
-
// If there's no best position found, i.e. when all intersections have no area because
|
|
124
|
-
// rects have no width or height, then just use the first available position.
|
|
125
|
-
bestPosition = getBestPosition( positions, positionOptions ) || new Position( positions[ 0 ], positionOptions );
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
return bestPosition;
|
|
78
|
+
export function getOptimalPosition({ element, target, positions, limiter, fitInViewport, viewportOffsetConfig }) {
|
|
79
|
+
// If the {@link module:utils/dom/position~Options#target} is a function, use what it returns.
|
|
80
|
+
// https://github.com/ckeditor/ckeditor5-utils/issues/157
|
|
81
|
+
if (isFunction(target)) {
|
|
82
|
+
target = target();
|
|
83
|
+
}
|
|
84
|
+
// If the {@link module:utils/dom/position~Options#limiter} is a function, use what it returns.
|
|
85
|
+
// https://github.com/ckeditor/ckeditor5-ui/issues/260
|
|
86
|
+
if (isFunction(limiter)) {
|
|
87
|
+
limiter = limiter();
|
|
88
|
+
}
|
|
89
|
+
const positionedElementAncestor = getPositionedAncestor(element);
|
|
90
|
+
const elementRect = new Rect(element);
|
|
91
|
+
const targetRect = new Rect(target);
|
|
92
|
+
let bestPosition;
|
|
93
|
+
// @if CK_DEBUG_POSITION // RectDrawer.clear();
|
|
94
|
+
// @if CK_DEBUG_POSITION // RectDrawer.draw( targetRect, { outlineWidth: '5px' }, 'Target' );
|
|
95
|
+
const viewportRect = fitInViewport && getConstrainedViewportRect(viewportOffsetConfig) || null;
|
|
96
|
+
const positionOptions = { targetRect, elementRect, positionedElementAncestor, viewportRect };
|
|
97
|
+
// If there are no limits, just grab the very first position and be done with that drama.
|
|
98
|
+
if (!limiter && !fitInViewport) {
|
|
99
|
+
bestPosition = new PositionObject(positions[0], positionOptions);
|
|
100
|
+
}
|
|
101
|
+
else {
|
|
102
|
+
const limiterRect = limiter && new Rect(limiter).getVisible();
|
|
103
|
+
// @if CK_DEBUG_POSITION // if ( viewportRect ) {
|
|
104
|
+
// @if CK_DEBUG_POSITION // RectDrawer.draw( viewportRect, { outlineWidth: '5px' }, 'Viewport' );
|
|
105
|
+
// @if CK_DEBUG_POSITION // }
|
|
106
|
+
// @if CK_DEBUG_POSITION // if ( limiter ) {
|
|
107
|
+
// @if CK_DEBUG_POSITION // RectDrawer.draw( limiterRect, { outlineWidth: '5px', outlineColor: 'green' }, 'Visible limiter' );
|
|
108
|
+
// @if CK_DEBUG_POSITION // }
|
|
109
|
+
Object.assign(positionOptions, { limiterRect, viewportRect });
|
|
110
|
+
// If there's no best position found, i.e. when all intersections have no area because
|
|
111
|
+
// rects have no width or height, then just use the first available position.
|
|
112
|
+
bestPosition = getBestPosition(positions, positionOptions) || new PositionObject(positions[0], positionOptions);
|
|
113
|
+
}
|
|
114
|
+
return bestPosition;
|
|
129
115
|
}
|
|
130
|
-
|
|
131
116
|
// Returns a viewport `Rect` shrunk by the viewport offset config from all sides.
|
|
132
117
|
//
|
|
133
118
|
// @private
|
|
134
119
|
// @param {Object} An object containing viewportOffset config.
|
|
135
|
-
// @returns {utils/dom/rect~Rect} A shrunken rect of the viewport.
|
|
136
|
-
function getConstrainedViewportRect(
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
viewportRect.height -= viewportOffsetConfig.bottom;
|
|
145
|
-
|
|
146
|
-
return viewportRect;
|
|
120
|
+
// @returns {module:utils/dom/rect~Rect} A shrunken rect of the viewport.
|
|
121
|
+
function getConstrainedViewportRect(viewportOffsetConfig) {
|
|
122
|
+
viewportOffsetConfig = Object.assign({ top: 0, bottom: 0, left: 0, right: 0 }, viewportOffsetConfig);
|
|
123
|
+
const viewportRect = new Rect(global.window);
|
|
124
|
+
viewportRect.top += viewportOffsetConfig.top;
|
|
125
|
+
viewportRect.height -= viewportOffsetConfig.top;
|
|
126
|
+
viewportRect.bottom -= viewportOffsetConfig.bottom;
|
|
127
|
+
viewportRect.height -= viewportOffsetConfig.bottom;
|
|
128
|
+
return viewportRect;
|
|
147
129
|
}
|
|
148
|
-
|
|
149
130
|
// For a given array of positioning functions, returns such that provides the best
|
|
150
131
|
// fit of the `elementRect` into the `limiterRect` and `viewportRect`.
|
|
151
132
|
//
|
|
152
133
|
// @private
|
|
153
134
|
//
|
|
154
|
-
// @param {Object} options
|
|
155
135
|
// @param {module:utils/dom/position~Options#positions} positions Functions returning
|
|
156
136
|
// {@link module:utils/dom/position~Position}to be checked, in the order of preference.
|
|
157
137
|
// @param {Object} options
|
|
158
|
-
// @param {utils/dom/rect~Rect} options.
|
|
159
|
-
// @param {utils/dom/rect~Rect} options.
|
|
160
|
-
//
|
|
161
|
-
// @param {utils/dom/rect~Rect} options.limiterRect
|
|
162
|
-
// @param {
|
|
138
|
+
// @param {module:utils/dom/rect~Rect} options.elementRect The positioned element rect.
|
|
139
|
+
// @param {module:utils/dom/rect~Rect} options.targetRect The target element rect.
|
|
140
|
+
// @param {module:utils/dom/rect~Rect} options.viewportRect The viewport rect.
|
|
141
|
+
// @param {module:utils/dom/rect~Rect} [options.limiterRect] The limiter rect.
|
|
142
|
+
// @param {HTMLElement|null} [options.positionedElementAncestor] Nearest element ancestor element which CSS position is not "static".
|
|
163
143
|
//
|
|
164
|
-
// @returns {
|
|
165
|
-
function getBestPosition(
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
if ( fitFactor > maxFitFactor ) {
|
|
193
|
-
maxFitFactor = fitFactor;
|
|
194
|
-
bestPosition = position;
|
|
195
|
-
}
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
return bestPosition;
|
|
144
|
+
// @returns {module:utils/dom/position~Position|null} An array containing the name of the position and it's rect.
|
|
145
|
+
function getBestPosition(positions, options) {
|
|
146
|
+
const { elementRect } = options;
|
|
147
|
+
// This is when element is fully visible.
|
|
148
|
+
const elementRectArea = elementRect.getArea();
|
|
149
|
+
const positionInstances = positions
|
|
150
|
+
.map(positioningFunction => new PositionObject(positioningFunction, options))
|
|
151
|
+
// Some positioning functions may return `null` if they don't want to participate.
|
|
152
|
+
.filter(position => !!position.name);
|
|
153
|
+
let maxFitFactor = 0;
|
|
154
|
+
let bestPosition = null;
|
|
155
|
+
for (const position of positionInstances) {
|
|
156
|
+
const { limiterIntersectionArea, viewportIntersectionArea } = position;
|
|
157
|
+
// If a such position is found that element is fully contained by the limiter then, obviously,
|
|
158
|
+
// there will be no better one, so finishing.
|
|
159
|
+
if (limiterIntersectionArea === elementRectArea) {
|
|
160
|
+
return position;
|
|
161
|
+
}
|
|
162
|
+
// To maximize both viewport and limiter intersection areas we use distance on _viewportIntersectionArea
|
|
163
|
+
// and _limiterIntersectionArea plane (without sqrt because we are looking for max value).
|
|
164
|
+
const fitFactor = viewportIntersectionArea ** 2 + limiterIntersectionArea ** 2;
|
|
165
|
+
if (fitFactor > maxFitFactor) {
|
|
166
|
+
maxFitFactor = fitFactor;
|
|
167
|
+
bestPosition = position;
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
return bestPosition;
|
|
199
171
|
}
|
|
200
|
-
|
|
201
|
-
//
|
|
202
|
-
// new Rect coordinates that make up for the position and the scroll of the ancestor.
|
|
172
|
+
// For a given absolute Rect coordinates object and a positioned element ancestor, it updates its
|
|
173
|
+
// coordinates that make up for the position and the scroll of the ancestor.
|
|
203
174
|
//
|
|
204
175
|
// This is necessary because while Rects (and DOMRects) are relative to the browser's viewport, their coordinates
|
|
205
176
|
// are used in real–life to position elements with `position: absolute`, which are scoped by any positioned
|
|
@@ -207,312 +178,151 @@ function getBestPosition( positions, options ) {
|
|
|
207
178
|
//
|
|
208
179
|
// @private
|
|
209
180
|
//
|
|
210
|
-
// @param {utils/dom/rect~Rect} rect A rect with absolute rect coordinates.
|
|
211
|
-
// @param {Number} rect.top
|
|
212
|
-
// @param {Number} rect.left
|
|
181
|
+
// @param {module:utils/dom/rect~Rect} rect A rect with absolute rect coordinates.
|
|
213
182
|
// @param {HTMLElement} positionedElementAncestor An ancestor element that should be considered.
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
// If there's some positioned ancestor of the panel, then its `Rect` includes its CSS `borderWidth`
|
|
241
|
-
// while `position: absolute` positioning does not consider it.
|
|
242
|
-
// E.g. `{ position: absolute, top: 0, left: 0 }` means upper left corner of the element,
|
|
243
|
-
// not upper-left corner of its border.
|
|
244
|
-
moveX -= ancestorBorderWidths.left;
|
|
245
|
-
moveY -= ancestorBorderWidths.top;
|
|
246
|
-
|
|
247
|
-
rect.moveBy( moveX, moveY );
|
|
183
|
+
function shiftRectToCompensatePositionedAncestor(rect, positionedElementAncestor) {
|
|
184
|
+
const ancestorPosition = getRectForAbsolutePositioning(new Rect(positionedElementAncestor));
|
|
185
|
+
const ancestorBorderWidths = getBorderWidths(positionedElementAncestor);
|
|
186
|
+
let moveX = 0;
|
|
187
|
+
let moveY = 0;
|
|
188
|
+
// (https://github.com/ckeditor/ckeditor5-ui-default/issues/126)
|
|
189
|
+
// If there's some positioned ancestor of the panel, then its `Rect` must be taken into
|
|
190
|
+
// consideration. `Rect` is always relative to the viewport while `position: absolute` works
|
|
191
|
+
// with respect to that positioned ancestor.
|
|
192
|
+
moveX -= ancestorPosition.left;
|
|
193
|
+
moveY -= ancestorPosition.top;
|
|
194
|
+
// (https://github.com/ckeditor/ckeditor5-utils/issues/139)
|
|
195
|
+
// If there's some positioned ancestor of the panel, not only its position must be taken into
|
|
196
|
+
// consideration (see above) but also its internal scrolls. Scroll have an impact here because `Rect`
|
|
197
|
+
// is relative to the viewport (it doesn't care about scrolling), while `position: absolute`
|
|
198
|
+
// must compensate that scrolling.
|
|
199
|
+
moveX += positionedElementAncestor.scrollLeft;
|
|
200
|
+
moveY += positionedElementAncestor.scrollTop;
|
|
201
|
+
// (https://github.com/ckeditor/ckeditor5-utils/issues/139)
|
|
202
|
+
// If there's some positioned ancestor of the panel, then its `Rect` includes its CSS `borderWidth`
|
|
203
|
+
// while `position: absolute` positioning does not consider it.
|
|
204
|
+
// E.g. `{ position: absolute, top: 0, left: 0 }` means upper left corner of the element,
|
|
205
|
+
// not upper-left corner of its border.
|
|
206
|
+
moveX -= ancestorBorderWidths.left;
|
|
207
|
+
moveY -= ancestorBorderWidths.top;
|
|
208
|
+
rect.moveBy(moveX, moveY);
|
|
248
209
|
}
|
|
249
|
-
|
|
250
210
|
// DOMRect (also Rect) works in a scroll–independent geometry but `position: absolute` doesn't.
|
|
251
211
|
// This function converts Rect to `position: absolute` coordinates.
|
|
252
212
|
//
|
|
253
213
|
// @private
|
|
254
|
-
// @param {utils/dom/rect~Rect} rect A rect to be converted.
|
|
255
|
-
// @returns {
|
|
256
|
-
function getRectForAbsolutePositioning(
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
return rect.clone().moveBy( scrollX, scrollY );
|
|
214
|
+
// @param {module:utils/dom/rect~Rect} rect A rect to be converted.
|
|
215
|
+
// @returns {module:utils/dom/rect~Rect} Object containing `left` and `top` properties, in absolute coordinates.
|
|
216
|
+
function getRectForAbsolutePositioning(rect) {
|
|
217
|
+
const { scrollX, scrollY } = global.window;
|
|
218
|
+
return rect.clone().moveBy(scrollX, scrollY);
|
|
260
219
|
}
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
-
|
|
267
|
-
|
|
268
|
-
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
* @readonly
|
|
370
|
-
* @private
|
|
371
|
-
* @type {Number}
|
|
372
|
-
*/
|
|
373
|
-
get _viewportIntersectionArea() {
|
|
374
|
-
const viewportRect = this._options.viewportRect;
|
|
375
|
-
|
|
376
|
-
if ( viewportRect ) {
|
|
377
|
-
return viewportRect.getIntersectionArea( this._rect );
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
return 0;
|
|
381
|
-
}
|
|
382
|
-
|
|
383
|
-
/**
|
|
384
|
-
* An already positioned element rect. A clone of the element rect passed to the constructor
|
|
385
|
-
* but placed in the viewport according to the positioning function.
|
|
386
|
-
*
|
|
387
|
-
* @private
|
|
388
|
-
* @type {module:utils/dom/rect~Rect}
|
|
389
|
-
*/
|
|
390
|
-
get _rect() {
|
|
391
|
-
if ( this._cachedRect ) {
|
|
392
|
-
return this._cachedRect;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
this._cachedRect = this._options.elementRect.clone().moveTo(
|
|
396
|
-
this._positioningFunctionCorrdinates.left,
|
|
397
|
-
this._positioningFunctionCorrdinates.top
|
|
398
|
-
);
|
|
399
|
-
|
|
400
|
-
return this._cachedRect;
|
|
401
|
-
}
|
|
402
|
-
|
|
403
|
-
/**
|
|
404
|
-
* An already absolutely positioned element rect. See ({@link #_rect}).
|
|
405
|
-
*
|
|
406
|
-
* @private
|
|
407
|
-
* @type {module:utils/dom/rect~Rect}
|
|
408
|
-
*/
|
|
409
|
-
get _absoluteRect() {
|
|
410
|
-
if ( this._cachedAbsoluteRect ) {
|
|
411
|
-
return this._cachedAbsoluteRect;
|
|
412
|
-
}
|
|
413
|
-
|
|
414
|
-
this._cachedAbsoluteRect = getRectForAbsolutePositioning( this._rect );
|
|
415
|
-
|
|
416
|
-
if ( this._options.positionedElementAncestor ) {
|
|
417
|
-
shiftRectToCompensatePositionedAncestor( this._cachedAbsoluteRect, this._options.positionedElementAncestor );
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
return this._cachedAbsoluteRect;
|
|
421
|
-
}
|
|
220
|
+
// A position class which instances are created and used by the {@link module:utils/dom/position~getOptimalPosition} helper.
|
|
221
|
+
//
|
|
222
|
+
// {@link module:utils/dom/position~Position#top} and {@link module:utils/dom/position~Position#left} properties of the position instance
|
|
223
|
+
// translate directly to the `top` and `left` properties in CSS "`position: absolute` coordinate system". If set on the positioned element
|
|
224
|
+
// in DOM, they will make it display it in the right place in the viewport.
|
|
225
|
+
// @private
|
|
226
|
+
// @implements {Position}
|
|
227
|
+
class PositionObject {
|
|
228
|
+
// Creates an instance of the {@link module:utils/dom/position~PositionObject} class.
|
|
229
|
+
//
|
|
230
|
+
// @param {module:utils/dom/position~PositioningFunction} positioningFunction function The function that defines the expected
|
|
231
|
+
// coordinates the positioned element should move to.
|
|
232
|
+
// @param {Object} [options] options object.
|
|
233
|
+
// @param {module:utils/dom/rect~Rect} options.elementRect The positioned element rect.
|
|
234
|
+
// @param {module:utils/dom/rect~Rect} options.targetRect The target element rect.
|
|
235
|
+
// @param {module:utils/dom/rect~Rect|null} options.viewportRect The viewport rect.
|
|
236
|
+
// @param {module:utils/dom/rect~Rect} [options.limiterRect] The limiter rect.
|
|
237
|
+
// @param {HTMLElement|null} [options.positionedElementAncestor] Nearest element ancestor element which CSS position is not "static".
|
|
238
|
+
constructor(positioningFunction, options) {
|
|
239
|
+
const positioningFunctionOutput = positioningFunction(options.targetRect, options.elementRect, options.viewportRect);
|
|
240
|
+
// Nameless position for a function that didn't participate.
|
|
241
|
+
if (!positioningFunctionOutput) {
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
const { left, top, name, config } = positioningFunctionOutput;
|
|
245
|
+
this.name = name;
|
|
246
|
+
this.config = config;
|
|
247
|
+
this._positioningFunctionCorrdinates = { left, top };
|
|
248
|
+
this._options = options;
|
|
249
|
+
}
|
|
250
|
+
// The left value in pixels in the CSS `position: absolute` coordinate system.
|
|
251
|
+
// Set it on the positioned element in DOM to move it to the position.
|
|
252
|
+
//
|
|
253
|
+
// @readonly
|
|
254
|
+
// @type {Number}
|
|
255
|
+
get left() {
|
|
256
|
+
return this._absoluteRect.left;
|
|
257
|
+
}
|
|
258
|
+
// The top value in pixels in the CSS `position: absolute` coordinate system.
|
|
259
|
+
// Set it on the positioned element in DOM to move it to the position.
|
|
260
|
+
//
|
|
261
|
+
// @readonly
|
|
262
|
+
// @type {Number}
|
|
263
|
+
get top() {
|
|
264
|
+
return this._absoluteRect.top;
|
|
265
|
+
}
|
|
266
|
+
// An intersection area between positioned element and limiter within viewport constraints.
|
|
267
|
+
//
|
|
268
|
+
// @readonly
|
|
269
|
+
// @type {Number}
|
|
270
|
+
get limiterIntersectionArea() {
|
|
271
|
+
const limiterRect = this._options.limiterRect;
|
|
272
|
+
if (limiterRect) {
|
|
273
|
+
const viewportRect = this._options.viewportRect;
|
|
274
|
+
if (viewportRect) {
|
|
275
|
+
// Consider only the part of the limiter which is visible in the viewport. So the limiter is getting limited.
|
|
276
|
+
const limiterViewportIntersectRect = limiterRect.getIntersection(viewportRect);
|
|
277
|
+
if (limiterViewportIntersectRect) {
|
|
278
|
+
// If the limiter is within the viewport, then check the intersection between that part of the
|
|
279
|
+
// limiter and actual position.
|
|
280
|
+
return limiterViewportIntersectRect.getIntersectionArea(this._rect);
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
else {
|
|
284
|
+
return limiterRect.getIntersectionArea(this._rect);
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
return 0;
|
|
288
|
+
}
|
|
289
|
+
// An intersection area between positioned element and viewport.
|
|
290
|
+
//
|
|
291
|
+
// @readonly
|
|
292
|
+
// @type {Number}
|
|
293
|
+
get viewportIntersectionArea() {
|
|
294
|
+
const viewportRect = this._options.viewportRect;
|
|
295
|
+
if (viewportRect) {
|
|
296
|
+
return viewportRect.getIntersectionArea(this._rect);
|
|
297
|
+
}
|
|
298
|
+
return 0;
|
|
299
|
+
}
|
|
300
|
+
// An already positioned element rect. A clone of the element rect passed to the constructor
|
|
301
|
+
// but placed in the viewport according to the positioning function.
|
|
302
|
+
//
|
|
303
|
+
// @private
|
|
304
|
+
// @readonly
|
|
305
|
+
// @type {module:utils/dom/rect~Rect}
|
|
306
|
+
get _rect() {
|
|
307
|
+
if (this._cachedRect) {
|
|
308
|
+
return this._cachedRect;
|
|
309
|
+
}
|
|
310
|
+
this._cachedRect = this._options.elementRect.clone().moveTo(this._positioningFunctionCorrdinates.left, this._positioningFunctionCorrdinates.top);
|
|
311
|
+
return this._cachedRect;
|
|
312
|
+
}
|
|
313
|
+
// An already absolutely positioned element rect. See ({@link #_rect}).
|
|
314
|
+
//
|
|
315
|
+
// @private
|
|
316
|
+
// @readonly
|
|
317
|
+
// @type {module:utils/dom/rect~Rect}
|
|
318
|
+
get _absoluteRect() {
|
|
319
|
+
if (this._cachedAbsoluteRect) {
|
|
320
|
+
return this._cachedAbsoluteRect;
|
|
321
|
+
}
|
|
322
|
+
this._cachedAbsoluteRect = getRectForAbsolutePositioning(this._rect);
|
|
323
|
+
if (this._options.positionedElementAncestor) {
|
|
324
|
+
shiftRectToCompensatePositionedAncestor(this._cachedAbsoluteRect, this._options.positionedElementAncestor);
|
|
325
|
+
}
|
|
326
|
+
return this._cachedAbsoluteRect;
|
|
327
|
+
}
|
|
422
328
|
}
|
|
423
|
-
|
|
424
|
-
/**
|
|
425
|
-
* The `getOptimalPosition()` helper options.
|
|
426
|
-
*
|
|
427
|
-
* @interface module:utils/dom/position~Options
|
|
428
|
-
*/
|
|
429
|
-
|
|
430
|
-
/**
|
|
431
|
-
* Element that is to be positioned.
|
|
432
|
-
*
|
|
433
|
-
* @member {HTMLElement} #element
|
|
434
|
-
*/
|
|
435
|
-
|
|
436
|
-
/**
|
|
437
|
-
* Target with respect to which the `element` is to be positioned.
|
|
438
|
-
*
|
|
439
|
-
* @member {HTMLElement|Range|Window|ClientRect|DOMRect|module:utils/dom/rect~Rect|Object|Function} #target
|
|
440
|
-
*/
|
|
441
|
-
|
|
442
|
-
/**
|
|
443
|
-
* An array of positioning functions.
|
|
444
|
-
*
|
|
445
|
-
* **Note**: Positioning functions are processed in the order of preference. The first function that works
|
|
446
|
-
* in the current environment (e.g. offers the complete fit in the viewport geometry) will be picked by
|
|
447
|
-
* `getOptimalPosition()`.
|
|
448
|
-
*
|
|
449
|
-
* **Note**: Any positioning function returning `null` is ignored.
|
|
450
|
-
*
|
|
451
|
-
* @member {Array.<module:utils/dom/position~positioningFunction>} #positions
|
|
452
|
-
*/
|
|
453
|
-
|
|
454
|
-
/**
|
|
455
|
-
* When set, the algorithm will chose position which fits the most in the
|
|
456
|
-
* limiter's bounding rect.
|
|
457
|
-
*
|
|
458
|
-
* @member {HTMLElement|Range|Window|ClientRect|DOMRect|module:utils/dom/rect~Rect|Object|Function} #limiter
|
|
459
|
-
*/
|
|
460
|
-
|
|
461
|
-
/**
|
|
462
|
-
* When set, the algorithm will chose such a position which fits `element`
|
|
463
|
-
* the most inside visible viewport.
|
|
464
|
-
*
|
|
465
|
-
* @member {Boolean} #fitInViewport
|
|
466
|
-
*/
|
|
467
|
-
|
|
468
|
-
/**
|
|
469
|
-
* Viewport offset config object. It restricts the visible viewport available to the `getOptimalPosition()` from each side.
|
|
470
|
-
*
|
|
471
|
-
* {
|
|
472
|
-
* top: 50,
|
|
473
|
-
* right: 50,
|
|
474
|
-
* bottom: 50,
|
|
475
|
-
* left: 50
|
|
476
|
-
* }
|
|
477
|
-
*
|
|
478
|
-
* @member {Object} #viewportOffsetConfig
|
|
479
|
-
*/
|
|
480
|
-
|
|
481
|
-
/**
|
|
482
|
-
* A positioning function which, based on positioned element and target {@link module:utils/dom/rect~Rect Rects}, returns rect coordinates
|
|
483
|
-
* representing the geometrical relation between them. Used by the {@link module:utils/dom/position~getOptimalPosition} helper.
|
|
484
|
-
*
|
|
485
|
-
* // This simple position will place the element directly under the target, in the middle:
|
|
486
|
-
* //
|
|
487
|
-
* // [ Target ]
|
|
488
|
-
* // +-----------------+
|
|
489
|
-
* // | Element |
|
|
490
|
-
* // +-----------------+
|
|
491
|
-
* //
|
|
492
|
-
* const position = ( targetRect, elementRect, [ viewportRect ] ) => ( {
|
|
493
|
-
* top: targetRect.bottom,
|
|
494
|
-
* left: targetRect.left + targetRect.width / 2 - elementRect.width / 2,
|
|
495
|
-
* name: 'bottomMiddle',
|
|
496
|
-
*
|
|
497
|
-
* // Note: The config is optional.
|
|
498
|
-
* config: {
|
|
499
|
-
* zIndex: '999'
|
|
500
|
-
* }
|
|
501
|
-
* } );
|
|
502
|
-
*
|
|
503
|
-
* @callback module:utils/dom/position~positioningFunction
|
|
504
|
-
* @param {module:utils/dom/rect~Rect} elementRect The rect of the element to be positioned.
|
|
505
|
-
* @param {module:utils/dom/rect~Rect} targetRect The rect of the target the element (its rect) is relatively positioned to.
|
|
506
|
-
* @param {module:utils/dom/rect~Rect} viewportRect The rect of the visual browser viewport.
|
|
507
|
-
* @returns {Object|null} return When the function returns `null`, it will not be considered by
|
|
508
|
-
* {@link module:utils/dom/position~getOptimalPosition}.
|
|
509
|
-
* @returns {Number} return.top The `top` value of the element rect that would represent the position.
|
|
510
|
-
* @returns {Number} return.left The `left` value of the element rect that would represent the position.
|
|
511
|
-
* @returns {Number} return.name The name of the position. It helps the user of the {@link module:utils/dom/position~getOptimalPosition}
|
|
512
|
-
* helper to recognize different positioning function results. It will pass through to the {@link module:utils/dom/position~Position}
|
|
513
|
-
* returned by the helper.
|
|
514
|
-
* @returns {Number} [return.config] An optional configuration that will pass-through the
|
|
515
|
-
* {@link module:utils/dom/position~getOptimalPosition} helper to the {@link module:utils/dom/position~Position} returned by this helper.
|
|
516
|
-
* This configuration may, for instance, let the user of {@link module:utils/dom/position~getOptimalPosition} know that this particular
|
|
517
|
-
* position comes with a certain presentation.
|
|
518
|
-
*/
|