@ckeditor/ckeditor5-utils 39.0.2 → 40.0.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.
Files changed (131) hide show
  1. package/package.json +1 -1
  2. package/src/areconnectedthroughproperties.d.ts +11 -11
  3. package/src/areconnectedthroughproperties.js +73 -73
  4. package/src/ckeditorerror.d.ts +123 -123
  5. package/src/ckeditorerror.js +176 -176
  6. package/src/collection.d.ts +427 -427
  7. package/src/collection.js +575 -575
  8. package/src/comparearrays.d.ts +30 -30
  9. package/src/comparearrays.js +47 -47
  10. package/src/config.d.ts +163 -163
  11. package/src/config.js +162 -162
  12. package/src/count.d.ts +18 -18
  13. package/src/count.js +24 -24
  14. package/src/delay.d.ts +19 -19
  15. package/src/delay.js +26 -26
  16. package/src/diff.d.ts +31 -31
  17. package/src/diff.js +115 -115
  18. package/src/difftochanges.d.ts +59 -59
  19. package/src/difftochanges.js +79 -79
  20. package/src/dom/createelement.d.ts +57 -57
  21. package/src/dom/createelement.js +40 -40
  22. package/src/dom/emittermixin.d.ts +142 -142
  23. package/src/dom/emittermixin.js +239 -239
  24. package/src/dom/findclosestscrollableancestor.d.ts +11 -11
  25. package/src/dom/findclosestscrollableancestor.js +31 -31
  26. package/src/dom/getancestors.d.ts +17 -17
  27. package/src/dom/getancestors.js +27 -27
  28. package/src/dom/getborderwidths.d.ts +24 -24
  29. package/src/dom/getborderwidths.js +24 -24
  30. package/src/dom/getcommonancestor.d.ts +12 -12
  31. package/src/dom/getcommonancestor.js +25 -25
  32. package/src/dom/getdatafromelement.d.ts +14 -14
  33. package/src/dom/getdatafromelement.js +20 -20
  34. package/src/dom/getpositionedancestor.d.ts +10 -10
  35. package/src/dom/getpositionedancestor.js +22 -22
  36. package/src/dom/global.d.ts +32 -32
  37. package/src/dom/global.js +35 -35
  38. package/src/dom/indexof.d.ts +14 -14
  39. package/src/dom/indexof.js +21 -21
  40. package/src/dom/insertat.d.ts +15 -15
  41. package/src/dom/insertat.js +17 -17
  42. package/src/dom/iscomment.d.ts +11 -11
  43. package/src/dom/iscomment.js +14 -14
  44. package/src/dom/isnode.d.ts +11 -11
  45. package/src/dom/isnode.js +21 -21
  46. package/src/dom/isrange.d.ts +11 -11
  47. package/src/dom/isrange.js +13 -13
  48. package/src/dom/istext.d.ts +11 -11
  49. package/src/dom/istext.js +13 -13
  50. package/src/dom/isvalidattributename.d.ts +10 -10
  51. package/src/dom/isvalidattributename.js +22 -22
  52. package/src/dom/isvisible.d.ts +18 -18
  53. package/src/dom/isvisible.js +20 -20
  54. package/src/dom/iswindow.d.ts +11 -11
  55. package/src/dom/iswindow.js +22 -22
  56. package/src/dom/position.d.ts +211 -208
  57. package/src/dom/position.js +313 -305
  58. package/src/dom/rect.d.ts +195 -191
  59. package/src/dom/rect.js +474 -388
  60. package/src/dom/remove.d.ts +13 -13
  61. package/src/dom/remove.js +18 -18
  62. package/src/dom/resizeobserver.d.ts +74 -74
  63. package/src/dom/resizeobserver.js +126 -126
  64. package/src/dom/scroll.d.ts +73 -73
  65. package/src/dom/scroll.js +383 -383
  66. package/src/dom/setdatainelement.d.ts +14 -14
  67. package/src/dom/setdatainelement.js +20 -20
  68. package/src/dom/tounit.d.ts +22 -22
  69. package/src/dom/tounit.js +16 -16
  70. package/src/elementreplacer.d.ts +31 -31
  71. package/src/elementreplacer.js +43 -43
  72. package/src/emittermixin.d.ts +312 -312
  73. package/src/emittermixin.js +453 -453
  74. package/src/env.d.ts +117 -117
  75. package/src/env.js +122 -122
  76. package/src/eventinfo.d.ts +58 -58
  77. package/src/eventinfo.js +26 -26
  78. package/src/fastdiff.d.ts +112 -112
  79. package/src/fastdiff.js +248 -248
  80. package/src/first.d.ts +11 -11
  81. package/src/first.js +17 -17
  82. package/src/focustracker.d.ts +75 -75
  83. package/src/focustracker.js +95 -95
  84. package/src/index.d.ts +61 -62
  85. package/src/index.js +60 -61
  86. package/src/inserttopriorityarray.d.ts +30 -30
  87. package/src/inserttopriorityarray.js +21 -21
  88. package/src/isiterable.d.ts +14 -14
  89. package/src/isiterable.js +16 -16
  90. package/src/keyboard.d.ts +126 -126
  91. package/src/keyboard.js +221 -221
  92. package/src/keystrokehandler.d.ts +87 -87
  93. package/src/keystrokehandler.js +122 -122
  94. package/src/language.d.ts +17 -17
  95. package/src/language.js +19 -19
  96. package/src/locale.d.ts +120 -120
  97. package/src/locale.js +76 -76
  98. package/src/mapsequal.d.ts +15 -15
  99. package/src/mapsequal.js +27 -27
  100. package/src/mix.d.ts +85 -85
  101. package/src/mix.js +50 -50
  102. package/src/nth.d.ts +16 -16
  103. package/src/nth.js +24 -24
  104. package/src/objecttomap.d.ts +23 -23
  105. package/src/objecttomap.js +27 -27
  106. package/src/observablemixin.d.ts +560 -560
  107. package/src/observablemixin.js +580 -580
  108. package/src/priorities.d.ts +33 -33
  109. package/src/priorities.js +23 -23
  110. package/src/splicearray.d.ts +26 -26
  111. package/src/splicearray.js +40 -40
  112. package/src/spy.d.ts +21 -21
  113. package/src/spy.js +22 -22
  114. package/src/toarray.d.ts +25 -25
  115. package/src/toarray.js +7 -7
  116. package/src/tomap.d.ts +19 -19
  117. package/src/tomap.js +29 -29
  118. package/src/translation-service.d.ts +168 -168
  119. package/src/translation-service.js +198 -198
  120. package/src/uid.d.ts +15 -15
  121. package/src/uid.js +57 -57
  122. package/src/unicode.d.ts +54 -54
  123. package/src/unicode.js +85 -85
  124. package/src/verifylicense.d.ts +15 -15
  125. package/src/verifylicense.js +87 -87
  126. package/src/version.d.ts +10 -10
  127. package/src/version.js +153 -153
  128. package/src/dom/getelementsintersectionrect.d.ts +0 -14
  129. package/src/dom/getelementsintersectionrect.js +0 -43
  130. package/src/dom/getscrollableancestors.d.ts +0 -14
  131. package/src/dom/getscrollableancestors.js +0 -28
@@ -1,305 +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 getBorderWidths from './getborderwidths';
12
- import { isFunction } from 'lodash-es';
13
- // @if CK_DEBUG_POSITION // const RectDrawer = require( '@ckeditor/ckeditor5-utils/tests/_utils/rectdrawer' ).default
14
- /**
15
- * Calculates the `position: absolute` coordinates of a given element so it can be positioned with respect to the
16
- * target in the visually most efficient way, taking various restrictions like viewport or limiter geometry
17
- * into consideration.
18
- *
19
- * ```ts
20
- * // The element which is to be positioned.
21
- * const element = document.body.querySelector( '#toolbar' );
22
- *
23
- * // A target to which the element is positioned relatively.
24
- * const target = document.body.querySelector( '#container' );
25
- *
26
- * // Finding the optimal coordinates for the positioning.
27
- * const { left, top, name } = getOptimalPosition( {
28
- * element: element,
29
- * target: target,
30
- *
31
- * // The algorithm will chose among these positions to meet the requirements such
32
- * // as "limiter" element or "fitInViewport", set below. The positions are considered
33
- * // in the order of the array.
34
- * positions: [
35
- * //
36
- * // [ Target ]
37
- * // +-----------------+
38
- * // | Element |
39
- * // +-----------------+
40
- * //
41
- * targetRect => ( {
42
- * top: targetRect.bottom,
43
- * left: targetRect.left,
44
- * name: 'mySouthEastPosition'
45
- * } ),
46
- *
47
- * //
48
- * // +-----------------+
49
- * // | Element |
50
- * // +-----------------+
51
- * // [ Target ]
52
- * //
53
- * ( targetRect, elementRect ) => ( {
54
- * top: targetRect.top - elementRect.height,
55
- * left: targetRect.left,
56
- * name: 'myNorthEastPosition'
57
- * } )
58
- * ],
59
- *
60
- * // Find a position such guarantees the element remains within visible boundaries of <body>.
61
- * limiter: document.body,
62
- *
63
- * // Find a position such guarantees the element remains within visible boundaries of the browser viewport.
64
- * fitInViewport: true
65
- * } );
66
- *
67
- * // The best position which fits into document.body and the viewport. May be useful
68
- * // to set proper class on the `element`.
69
- * console.log( name ); // -> "myNorthEastPosition"
70
- *
71
- * // Using the absolute coordinates which has been found to position the element
72
- * // as in the diagram depicting the "myNorthEastPosition" position.
73
- * element.style.top = top;
74
- * element.style.left = left;
75
- * ```
76
- *
77
- * @param options The input data and configuration of the helper.
78
- */
79
- export function getOptimalPosition({ element, target, positions, limiter, fitInViewport, viewportOffsetConfig }) {
80
- // If the {@link module:utils/dom/position~Options#target} is a function, use what it returns.
81
- // https://github.com/ckeditor/ckeditor5-utils/issues/157
82
- if (isFunction(target)) {
83
- target = target();
84
- }
85
- // If the {@link module:utils/dom/position~Options#limiter} is a function, use what it returns.
86
- // https://github.com/ckeditor/ckeditor5-ui/issues/260
87
- if (isFunction(limiter)) {
88
- limiter = limiter();
89
- }
90
- const positionedElementAncestor = getPositionedAncestor(element);
91
- const elementRect = new Rect(element);
92
- const targetRect = new Rect(target);
93
- let bestPosition;
94
- // @if CK_DEBUG_POSITION // RectDrawer.clear();
95
- // @if CK_DEBUG_POSITION // RectDrawer.draw( targetRect, { outlineWidth: '5px' }, 'Target' );
96
- const viewportRect = fitInViewport && getConstrainedViewportRect(viewportOffsetConfig) || null;
97
- const positionOptions = { targetRect, elementRect, positionedElementAncestor, viewportRect };
98
- // If there are no limits, just grab the very first position and be done with that drama.
99
- if (!limiter && !fitInViewport) {
100
- bestPosition = new PositionObject(positions[0], positionOptions);
101
- }
102
- else {
103
- const limiterRect = limiter && new Rect(limiter).getVisible();
104
- // @if CK_DEBUG_POSITION // if ( viewportRect ) {
105
- // @if CK_DEBUG_POSITION // RectDrawer.draw( viewportRect, { outlineWidth: '5px' }, 'Viewport' );
106
- // @if CK_DEBUG_POSITION // }
107
- // @if CK_DEBUG_POSITION // if ( limiter ) {
108
- // @if CK_DEBUG_POSITION // RectDrawer.draw( limiterRect, { outlineWidth: '5px', outlineColor: 'green' }, 'Visible limiter' );
109
- // @if CK_DEBUG_POSITION // }
110
- Object.assign(positionOptions, { limiterRect, viewportRect });
111
- // If there's no best position found, i.e. when all intersections have no area because
112
- // rects have no width or height, then just use the first available position.
113
- bestPosition = getBestPosition(positions, positionOptions) || new PositionObject(positions[0], positionOptions);
114
- }
115
- return bestPosition;
116
- }
117
- /**
118
- * Returns a viewport `Rect` shrunk by the viewport offset config from all sides.
119
- */
120
- function getConstrainedViewportRect(viewportOffsetConfig) {
121
- viewportOffsetConfig = Object.assign({ top: 0, bottom: 0, left: 0, right: 0 }, viewportOffsetConfig);
122
- const viewportRect = new Rect(global.window);
123
- viewportRect.top += viewportOffsetConfig.top;
124
- viewportRect.height -= viewportOffsetConfig.top;
125
- viewportRect.bottom -= viewportOffsetConfig.bottom;
126
- viewportRect.height -= viewportOffsetConfig.bottom;
127
- return viewportRect;
128
- }
129
- /**
130
- * For a given array of positioning functions, returns such that provides the best
131
- * fit of the `elementRect` into the `limiterRect` and `viewportRect`.
132
- */
133
- function getBestPosition(positions, options) {
134
- const { elementRect } = options;
135
- // This is when element is fully visible.
136
- const elementRectArea = elementRect.getArea();
137
- const positionInstances = positions
138
- .map(positioningFunction => new PositionObject(positioningFunction, options))
139
- // Some positioning functions may return `null` if they don't want to participate.
140
- .filter(position => !!position.name);
141
- let maxFitFactor = 0;
142
- let bestPosition = null;
143
- for (const position of positionInstances) {
144
- const { limiterIntersectionArea, viewportIntersectionArea } = position;
145
- // If a such position is found that element is fully contained by the limiter then, obviously,
146
- // there will be no better one, so finishing.
147
- if (limiterIntersectionArea === elementRectArea) {
148
- return position;
149
- }
150
- // To maximize both viewport and limiter intersection areas we use distance on _viewportIntersectionArea
151
- // and _limiterIntersectionArea plane (without sqrt because we are looking for max value).
152
- const fitFactor = viewportIntersectionArea ** 2 + limiterIntersectionArea ** 2;
153
- if (fitFactor > maxFitFactor) {
154
- maxFitFactor = fitFactor;
155
- bestPosition = position;
156
- }
157
- }
158
- return bestPosition;
159
- }
160
- /**
161
- * For a given absolute Rect coordinates object and a positioned element ancestor, it updates its
162
- * coordinates that make up for the position and the scroll of the ancestor.
163
- *
164
- * This is necessary because while Rects (and DOMRects) are relative to the browser's viewport, their coordinates
165
- * are used in real–life to position elements with `position: absolute`, which are scoped by any positioned
166
- * (and scrollable) ancestors.
167
- */
168
- function shiftRectToCompensatePositionedAncestor(rect, positionedElementAncestor) {
169
- const ancestorPosition = getRectForAbsolutePositioning(new Rect(positionedElementAncestor));
170
- const ancestorBorderWidths = getBorderWidths(positionedElementAncestor);
171
- let moveX = 0;
172
- let moveY = 0;
173
- // (https://github.com/ckeditor/ckeditor5-ui-default/issues/126)
174
- // If there's some positioned ancestor of the panel, then its `Rect` must be taken into
175
- // consideration. `Rect` is always relative to the viewport while `position: absolute` works
176
- // with respect to that positioned ancestor.
177
- moveX -= ancestorPosition.left;
178
- moveY -= ancestorPosition.top;
179
- // (https://github.com/ckeditor/ckeditor5-utils/issues/139)
180
- // If there's some positioned ancestor of the panel, not only its position must be taken into
181
- // consideration (see above) but also its internal scrolls. Scroll have an impact here because `Rect`
182
- // is relative to the viewport (it doesn't care about scrolling), while `position: absolute`
183
- // must compensate that scrolling.
184
- moveX += positionedElementAncestor.scrollLeft;
185
- moveY += positionedElementAncestor.scrollTop;
186
- // (https://github.com/ckeditor/ckeditor5-utils/issues/139)
187
- // If there's some positioned ancestor of the panel, then its `Rect` includes its CSS `borderWidth`
188
- // while `position: absolute` positioning does not consider it.
189
- // E.g. `{ position: absolute, top: 0, left: 0 }` means upper left corner of the element,
190
- // not upper-left corner of its border.
191
- moveX -= ancestorBorderWidths.left;
192
- moveY -= ancestorBorderWidths.top;
193
- rect.moveBy(moveX, moveY);
194
- }
195
- /**
196
- * DOMRect (also Rect) works in a scroll–independent geometry but `position: absolute` doesn't.
197
- * This function converts Rect to `position: absolute` coordinates.
198
- */
199
- function getRectForAbsolutePositioning(rect) {
200
- const { scrollX, scrollY } = global.window;
201
- return rect.clone().moveBy(scrollX, scrollY);
202
- }
203
- /**
204
- * A position class which instances are created and used by the {@link module:utils/dom/position~getOptimalPosition} helper.
205
- *
206
- * {@link module:utils/dom/position~Position#top} and {@link module:utils/dom/position~Position#left} properties of the position instance
207
- * translate directly to the `top` and `left` properties in CSS "`position: absolute` coordinate system". If set on the positioned element
208
- * in DOM, they will make it display it in the right place in the viewport.
209
- */
210
- class PositionObject {
211
- /**
212
- * Creates an instance of the {@link module:utils/dom/position~PositionObject} class.
213
- *
214
- * @param positioningFunction function The function that defines the expected
215
- * coordinates the positioned element should move to.
216
- * @param options options object.
217
- * @param options.elementRect The positioned element rect.
218
- * @param options.targetRect The target element rect.
219
- * @param options.viewportRect The viewport rect.
220
- * @param options.limiterRect The limiter rect.
221
- * @param options.positionedElementAncestor Nearest element ancestor element which CSS position is not "static".
222
- */
223
- constructor(positioningFunction, options) {
224
- const positioningFunctionOutput = positioningFunction(options.targetRect, options.elementRect, options.viewportRect);
225
- // Nameless position for a function that didn't participate.
226
- if (!positioningFunctionOutput) {
227
- return;
228
- }
229
- const { left, top, name, config } = positioningFunctionOutput;
230
- this.name = name;
231
- this.config = config;
232
- this._positioningFunctionCorrdinates = { left, top };
233
- this._options = options;
234
- }
235
- /**
236
- * The left value in pixels in the CSS `position: absolute` coordinate system.
237
- * Set it on the positioned element in DOM to move it to the position.
238
- */
239
- get left() {
240
- return this._absoluteRect.left;
241
- }
242
- /**
243
- * The top value in pixels in the CSS `position: absolute` coordinate system.
244
- * Set it on the positioned element in DOM to move it to the position.
245
- */
246
- get top() {
247
- return this._absoluteRect.top;
248
- }
249
- /**
250
- * An intersection area between positioned element and limiter within viewport constraints.
251
- */
252
- get limiterIntersectionArea() {
253
- const limiterRect = this._options.limiterRect;
254
- if (limiterRect) {
255
- const viewportRect = this._options.viewportRect;
256
- if (viewportRect) {
257
- // Consider only the part of the limiter which is visible in the viewport. So the limiter is getting limited.
258
- const limiterViewportIntersectRect = limiterRect.getIntersection(viewportRect);
259
- if (limiterViewportIntersectRect) {
260
- // If the limiter is within the viewport, then check the intersection between that part of the
261
- // limiter and actual position.
262
- return limiterViewportIntersectRect.getIntersectionArea(this._rect);
263
- }
264
- }
265
- else {
266
- return limiterRect.getIntersectionArea(this._rect);
267
- }
268
- }
269
- return 0;
270
- }
271
- /**
272
- * An intersection area between positioned element and viewport.
273
- */
274
- get viewportIntersectionArea() {
275
- const viewportRect = this._options.viewportRect;
276
- if (viewportRect) {
277
- return viewportRect.getIntersectionArea(this._rect);
278
- }
279
- return 0;
280
- }
281
- /**
282
- * An already positioned element rect. A clone of the element rect passed to the constructor
283
- * but placed in the viewport according to the positioning function.
284
- */
285
- get _rect() {
286
- if (this._cachedRect) {
287
- return this._cachedRect;
288
- }
289
- this._cachedRect = this._options.elementRect.clone().moveTo(this._positioningFunctionCorrdinates.left, this._positioningFunctionCorrdinates.top);
290
- return this._cachedRect;
291
- }
292
- /**
293
- * An already absolutely positioned element rect. See ({@link #_rect}).
294
- */
295
- get _absoluteRect() {
296
- if (this._cachedAbsoluteRect) {
297
- return this._cachedAbsoluteRect;
298
- }
299
- this._cachedAbsoluteRect = getRectForAbsolutePositioning(this._rect);
300
- if (this._options.positionedElementAncestor) {
301
- shiftRectToCompensatePositionedAncestor(this._cachedAbsoluteRect, this._options.positionedElementAncestor);
302
- }
303
- return this._cachedAbsoluteRect;
304
- }
305
- }
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
+ }