@ckeditor/ckeditor5-utils 0.0.0-internal-20241017.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 (213) hide show
  1. package/CHANGELOG.md +4 -0
  2. package/LICENSE.md +21 -0
  3. package/README.md +22 -0
  4. package/dist/abortabledebounce.d.ts +21 -0
  5. package/dist/areconnectedthroughproperties.d.ts +15 -0
  6. package/dist/ckeditorerror.d.ts +127 -0
  7. package/dist/collection.d.ts +437 -0
  8. package/dist/comparearrays.d.ts +34 -0
  9. package/dist/config.d.ts +167 -0
  10. package/dist/count.d.ts +22 -0
  11. package/dist/crc32.d.ts +30 -0
  12. package/dist/delay.d.ts +23 -0
  13. package/dist/diff.d.ts +35 -0
  14. package/dist/difftochanges.d.ts +63 -0
  15. package/dist/dom/createelement.d.ts +61 -0
  16. package/dist/dom/emittermixin.d.ts +146 -0
  17. package/dist/dom/findclosestscrollableancestor.d.ts +15 -0
  18. package/dist/dom/getancestors.d.ts +21 -0
  19. package/dist/dom/getborderwidths.d.ts +28 -0
  20. package/dist/dom/getcommonancestor.d.ts +16 -0
  21. package/dist/dom/getdatafromelement.d.ts +18 -0
  22. package/dist/dom/getpositionedancestor.d.ts +14 -0
  23. package/dist/dom/getrangefrommouseevent.d.ts +21 -0
  24. package/dist/dom/global.d.ts +36 -0
  25. package/dist/dom/indexof.d.ts +18 -0
  26. package/dist/dom/insertat.d.ts +19 -0
  27. package/dist/dom/iscomment.d.ts +15 -0
  28. package/dist/dom/isnode.d.ts +15 -0
  29. package/dist/dom/isrange.d.ts +15 -0
  30. package/dist/dom/istext.d.ts +15 -0
  31. package/dist/dom/isvalidattributename.d.ts +14 -0
  32. package/dist/dom/isvisible.d.ts +19 -0
  33. package/dist/dom/iswindow.d.ts +15 -0
  34. package/dist/dom/position.d.ts +215 -0
  35. package/dist/dom/rect.d.ts +199 -0
  36. package/dist/dom/remove.d.ts +17 -0
  37. package/dist/dom/resizeobserver.d.ts +78 -0
  38. package/dist/dom/scroll.d.ts +77 -0
  39. package/dist/dom/setdatainelement.d.ts +18 -0
  40. package/dist/dom/tounit.d.ts +26 -0
  41. package/dist/elementreplacer.d.ts +35 -0
  42. package/dist/emittermixin.d.ts +316 -0
  43. package/dist/env.d.ts +144 -0
  44. package/dist/eventinfo.d.ts +62 -0
  45. package/dist/fastdiff.d.ts +116 -0
  46. package/dist/first.d.ts +15 -0
  47. package/dist/focustracker.d.ts +83 -0
  48. package/dist/index-content.css +4 -0
  49. package/dist/index-editor.css +4 -0
  50. package/dist/index.css +4 -0
  51. package/dist/index.d.ts +70 -0
  52. package/dist/index.js +5871 -0
  53. package/dist/index.js.map +1 -0
  54. package/dist/inserttopriorityarray.d.ts +34 -0
  55. package/dist/isiterable.d.ts +18 -0
  56. package/dist/keyboard.d.ts +130 -0
  57. package/dist/keystrokehandler.d.ts +101 -0
  58. package/dist/language.d.ts +21 -0
  59. package/dist/locale.d.ts +145 -0
  60. package/dist/mapsequal.d.ts +19 -0
  61. package/dist/mix.d.ts +89 -0
  62. package/dist/nth.d.ts +20 -0
  63. package/dist/objecttomap.d.ts +27 -0
  64. package/dist/observablemixin.d.ts +564 -0
  65. package/dist/parsebase64encodedobject.d.ts +15 -0
  66. package/dist/priorities.d.ts +37 -0
  67. package/dist/retry.d.ts +37 -0
  68. package/dist/splicearray.d.ts +30 -0
  69. package/dist/spy.d.ts +25 -0
  70. package/dist/toarray.d.ts +29 -0
  71. package/dist/tomap.d.ts +23 -0
  72. package/dist/translation-service.d.ts +178 -0
  73. package/dist/uid.d.ts +19 -0
  74. package/dist/unicode.d.ts +58 -0
  75. package/dist/version.d.ts +14 -0
  76. package/dist/wait.d.ts +20 -0
  77. package/package.json +36 -0
  78. package/src/abortabledebounce.d.ts +17 -0
  79. package/src/abortabledebounce.js +22 -0
  80. package/src/areconnectedthroughproperties.d.ts +11 -0
  81. package/src/areconnectedthroughproperties.js +73 -0
  82. package/src/ckeditorerror.d.ts +123 -0
  83. package/src/ckeditorerror.js +176 -0
  84. package/src/collection.d.ts +433 -0
  85. package/src/collection.js +583 -0
  86. package/src/comparearrays.d.ts +30 -0
  87. package/src/comparearrays.js +47 -0
  88. package/src/config.d.ts +163 -0
  89. package/src/config.js +163 -0
  90. package/src/count.d.ts +18 -0
  91. package/src/count.js +25 -0
  92. package/src/crc32.d.ts +26 -0
  93. package/src/crc32.js +61 -0
  94. package/src/delay.d.ts +19 -0
  95. package/src/delay.js +26 -0
  96. package/src/diff.d.ts +31 -0
  97. package/src/diff.js +115 -0
  98. package/src/difftochanges.d.ts +59 -0
  99. package/src/difftochanges.js +79 -0
  100. package/src/dom/createelement.d.ts +57 -0
  101. package/src/dom/createelement.js +40 -0
  102. package/src/dom/emittermixin.d.ts +142 -0
  103. package/src/dom/emittermixin.js +239 -0
  104. package/src/dom/findclosestscrollableancestor.d.ts +11 -0
  105. package/src/dom/findclosestscrollableancestor.js +31 -0
  106. package/src/dom/getancestors.d.ts +17 -0
  107. package/src/dom/getancestors.js +27 -0
  108. package/src/dom/getborderwidths.d.ts +24 -0
  109. package/src/dom/getborderwidths.js +24 -0
  110. package/src/dom/getcommonancestor.d.ts +12 -0
  111. package/src/dom/getcommonancestor.js +25 -0
  112. package/src/dom/getdatafromelement.d.ts +14 -0
  113. package/src/dom/getdatafromelement.js +20 -0
  114. package/src/dom/getpositionedancestor.d.ts +10 -0
  115. package/src/dom/getpositionedancestor.js +22 -0
  116. package/src/dom/getrangefrommouseevent.d.ts +17 -0
  117. package/src/dom/getrangefrommouseevent.js +33 -0
  118. package/src/dom/global.d.ts +32 -0
  119. package/src/dom/global.js +35 -0
  120. package/src/dom/indexof.d.ts +14 -0
  121. package/src/dom/indexof.js +21 -0
  122. package/src/dom/insertat.d.ts +15 -0
  123. package/src/dom/insertat.js +17 -0
  124. package/src/dom/iscomment.d.ts +11 -0
  125. package/src/dom/iscomment.js +14 -0
  126. package/src/dom/isnode.d.ts +11 -0
  127. package/src/dom/isnode.js +21 -0
  128. package/src/dom/isrange.d.ts +11 -0
  129. package/src/dom/isrange.js +13 -0
  130. package/src/dom/istext.d.ts +11 -0
  131. package/src/dom/istext.js +13 -0
  132. package/src/dom/isvalidattributename.d.ts +10 -0
  133. package/src/dom/isvalidattributename.js +22 -0
  134. package/src/dom/isvisible.d.ts +15 -0
  135. package/src/dom/isvisible.js +30 -0
  136. package/src/dom/iswindow.d.ts +11 -0
  137. package/src/dom/iswindow.js +22 -0
  138. package/src/dom/position.d.ts +211 -0
  139. package/src/dom/position.js +313 -0
  140. package/src/dom/rect.d.ts +195 -0
  141. package/src/dom/rect.js +474 -0
  142. package/src/dom/remove.d.ts +13 -0
  143. package/src/dom/remove.js +18 -0
  144. package/src/dom/resizeobserver.d.ts +74 -0
  145. package/src/dom/resizeobserver.js +127 -0
  146. package/src/dom/scroll.d.ts +73 -0
  147. package/src/dom/scroll.js +383 -0
  148. package/src/dom/setdatainelement.d.ts +14 -0
  149. package/src/dom/setdatainelement.js +20 -0
  150. package/src/dom/tounit.d.ts +22 -0
  151. package/src/dom/tounit.js +16 -0
  152. package/src/elementreplacer.d.ts +31 -0
  153. package/src/elementreplacer.js +43 -0
  154. package/src/emittermixin.d.ts +312 -0
  155. package/src/emittermixin.js +453 -0
  156. package/src/env.d.ts +140 -0
  157. package/src/env.js +145 -0
  158. package/src/eventinfo.d.ts +58 -0
  159. package/src/eventinfo.js +26 -0
  160. package/src/fastdiff.d.ts +112 -0
  161. package/src/fastdiff.js +248 -0
  162. package/src/first.d.ts +11 -0
  163. package/src/first.js +17 -0
  164. package/src/focustracker.d.ts +79 -0
  165. package/src/focustracker.js +101 -0
  166. package/src/index.d.ts +66 -0
  167. package/src/index.js +65 -0
  168. package/src/inserttopriorityarray.d.ts +30 -0
  169. package/src/inserttopriorityarray.js +21 -0
  170. package/src/isiterable.d.ts +14 -0
  171. package/src/isiterable.js +16 -0
  172. package/src/keyboard.d.ts +126 -0
  173. package/src/keyboard.js +252 -0
  174. package/src/keystrokehandler.d.ts +97 -0
  175. package/src/keystrokehandler.js +122 -0
  176. package/src/language.d.ts +17 -0
  177. package/src/language.js +20 -0
  178. package/src/locale.d.ts +141 -0
  179. package/src/locale.js +78 -0
  180. package/src/mapsequal.d.ts +15 -0
  181. package/src/mapsequal.js +27 -0
  182. package/src/mix.d.ts +85 -0
  183. package/src/mix.js +50 -0
  184. package/src/nth.d.ts +16 -0
  185. package/src/nth.js +24 -0
  186. package/src/objecttomap.d.ts +23 -0
  187. package/src/objecttomap.js +27 -0
  188. package/src/observablemixin.d.ts +560 -0
  189. package/src/observablemixin.js +580 -0
  190. package/src/parsebase64encodedobject.d.ts +11 -0
  191. package/src/parsebase64encodedobject.js +22 -0
  192. package/src/priorities.d.ts +33 -0
  193. package/src/priorities.js +23 -0
  194. package/src/retry.d.ts +33 -0
  195. package/src/retry.js +47 -0
  196. package/src/splicearray.d.ts +26 -0
  197. package/src/splicearray.js +40 -0
  198. package/src/spy.d.ts +21 -0
  199. package/src/spy.js +22 -0
  200. package/src/toarray.d.ts +25 -0
  201. package/src/toarray.js +7 -0
  202. package/src/tomap.d.ts +19 -0
  203. package/src/tomap.js +29 -0
  204. package/src/translation-service.d.ts +174 -0
  205. package/src/translation-service.js +209 -0
  206. package/src/uid.d.ts +15 -0
  207. package/src/uid.js +45 -0
  208. package/src/unicode.d.ts +54 -0
  209. package/src/unicode.js +85 -0
  210. package/src/version.d.ts +10 -0
  211. package/src/version.js +179 -0
  212. package/src/wait.d.ts +16 -0
  213. package/src/wait.js +29 -0
@@ -0,0 +1,211 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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
+ import Rect, { type RectSource } from './rect.js';
6
+ /**
7
+ * Calculates the `position: absolute` coordinates of a given element so it can be positioned with respect to the
8
+ * target in the visually most efficient way, taking various restrictions like viewport or limiter geometry
9
+ * into consideration.
10
+ *
11
+ * **Note**: If there are no position coordinates found that meet the requirements (arguments of this helper),
12
+ * `null` is returned.
13
+ *
14
+ * ```ts
15
+ * // The element which is to be positioned.
16
+ * const element = document.body.querySelector( '#toolbar' );
17
+ *
18
+ * // A target to which the element is positioned relatively.
19
+ * const target = document.body.querySelector( '#container' );
20
+ *
21
+ * // Finding the optimal coordinates for the positioning.
22
+ * const { left, top, name } = getOptimalPosition( {
23
+ * element: element,
24
+ * target: target,
25
+ *
26
+ * // The algorithm will chose among these positions to meet the requirements such
27
+ * // as "limiter" element or "fitInViewport", set below. The positions are considered
28
+ * // in the order of the array.
29
+ * positions: [
30
+ * //
31
+ * // [ Target ]
32
+ * // +-----------------+
33
+ * // | Element |
34
+ * // +-----------------+
35
+ * //
36
+ * targetRect => ( {
37
+ * top: targetRect.bottom,
38
+ * left: targetRect.left,
39
+ * name: 'mySouthEastPosition'
40
+ * } ),
41
+ *
42
+ * //
43
+ * // +-----------------+
44
+ * // | Element |
45
+ * // +-----------------+
46
+ * // [ Target ]
47
+ * //
48
+ * ( targetRect, elementRect ) => ( {
49
+ * top: targetRect.top - elementRect.height,
50
+ * left: targetRect.left,
51
+ * name: 'myNorthEastPosition'
52
+ * } )
53
+ * ],
54
+ *
55
+ * // Find a position such guarantees the element remains within visible boundaries of <body>.
56
+ * limiter: document.body,
57
+ *
58
+ * // Find a position such guarantees the element remains within visible boundaries of the browser viewport.
59
+ * fitInViewport: true
60
+ * } );
61
+ *
62
+ * // The best position which fits into document.body and the viewport. May be useful
63
+ * // to set proper class on the `element`.
64
+ * console.log( name ); // -> "myNorthEastPosition"
65
+ *
66
+ * // Using the absolute coordinates which has been found to position the element
67
+ * // as in the diagram depicting the "myNorthEastPosition" position.
68
+ * element.style.top = top;
69
+ * element.style.left = left;
70
+ * ```
71
+ *
72
+ * @param options The input data and configuration of the helper.
73
+ */
74
+ export declare function getOptimalPosition({ element, target, positions, limiter, fitInViewport, viewportOffsetConfig }: Options): DomPoint | null;
75
+ /**
76
+ * A position object which instances are created and used by the {@link module:utils/dom/position~getOptimalPosition} helper.
77
+ *
78
+ * {@link module:utils/dom/position~DomPoint#top} and {@link module:utils/dom/position~DomPoint#left} properties of the position instance
79
+ * translate directly to the `top` and `left` properties in CSS "`position: absolute` coordinate system". If set on the positioned element
80
+ * in DOM, they will make it display it in the right place in the viewport.
81
+ */
82
+ export interface DomPoint {
83
+ /**
84
+ * Position name.
85
+ */
86
+ readonly name?: string;
87
+ /**
88
+ * Additional position configuration, as passed from the {@link module:utils/dom/position~PositioningFunction positioning function}.
89
+ *
90
+ * This object can be use, for instance, to pass through presentation options used by the consumer of the
91
+ * {@link module:utils/dom/position~getOptimalPosition} helper.
92
+ */
93
+ readonly config?: object;
94
+ /**
95
+ * The left value in pixels in the CSS `position: absolute` coordinate system.
96
+ * Set it on the positioned element in DOM to move it to the position.
97
+ */
98
+ readonly left: number;
99
+ /**
100
+ * The top value in pixels in the CSS `position: absolute` coordinate system.
101
+ * Set it on the positioned element in DOM to move it to the position.
102
+ */
103
+ readonly top: number;
104
+ }
105
+ /**
106
+ * The `getOptimalPosition()` helper options.
107
+ */
108
+ export interface Options {
109
+ /**
110
+ * Element that is to be positioned.
111
+ */
112
+ readonly element: HTMLElement;
113
+ /**
114
+ * Target with respect to which the `element` is to be positioned.
115
+ */
116
+ readonly target: RectSource | (() => RectSource);
117
+ /**
118
+ * An array of positioning functions.
119
+ *
120
+ * **Note**: Positioning functions are processed in the order of preference. The first function that works
121
+ * in the current environment (e.g. offers the complete fit in the viewport geometry) will be picked by
122
+ * `getOptimalPosition()`.
123
+ *
124
+ * **Note**: Any positioning function returning `null` is ignored.
125
+ */
126
+ readonly positions: ReadonlyArray<PositioningFunction>;
127
+ /**
128
+ * When set, the algorithm will chose position which fits the most in the
129
+ * limiter's bounding rect.
130
+ */
131
+ readonly limiter?: RectSource | (() => (RectSource | null)) | null;
132
+ /**
133
+ * When set, the algorithm will chose such a position which fits `element`
134
+ * the most inside visible viewport.
135
+ */
136
+ readonly fitInViewport?: boolean;
137
+ /**
138
+ * Viewport offset config object. It restricts the visible viewport available to the `getOptimalPosition()` from each side.
139
+ *
140
+ * ```ts
141
+ * {
142
+ * top: 50,
143
+ * right: 50,
144
+ * bottom: 50,
145
+ * left: 50
146
+ * }
147
+ * ```
148
+ */
149
+ readonly viewportOffsetConfig?: {
150
+ readonly top?: number;
151
+ readonly right?: number;
152
+ readonly bottom?: number;
153
+ readonly left?: number;
154
+ };
155
+ }
156
+ /**
157
+ * A positioning function which, based on positioned element and target {@link module:utils/dom/rect~Rect Rects}, returns rect coordinates
158
+ * representing the geometrical relation between them. Used by the {@link module:utils/dom/position~getOptimalPosition} helper.
159
+ *
160
+ * ```ts
161
+ * // This simple position will place the element directly under the target, in the middle:
162
+ * //
163
+ * // [ Target ]
164
+ * // +-----------------+
165
+ * // | Element |
166
+ * // +-----------------+
167
+ * //
168
+ * const position = ( targetRect, elementRect, [ viewportRect ] ) => ( {
169
+ * top: targetRect.bottom,
170
+ * left: targetRect.left + targetRect.width / 2 - elementRect.width / 2,
171
+ * name: 'bottomMiddle',
172
+ *
173
+ * // Note: The config is optional.
174
+ * config: {
175
+ * zIndex: '999'
176
+ * }
177
+ * } );
178
+ * ```
179
+ *
180
+ * @param elementRect The rect of the element to be positioned.
181
+ * @param targetRect The rect of the target the element (its rect) is relatively positioned to.
182
+ * @param viewportRect The rect of the visual browser viewport.
183
+ * @returns When the function returns `null`, it will not be considered by {@link module:utils/dom/position~getOptimalPosition}.
184
+ */
185
+ export type PositioningFunction = (elementRect: Rect, targetRect: Rect, viewportRect: Rect, limiterRect?: Rect) => PositioningFunctionResult | null;
186
+ /**
187
+ * The result of {@link module:utils/dom/position~PositioningFunction}.
188
+ */
189
+ export interface PositioningFunctionResult {
190
+ /**
191
+ * The `top` value of the element rect that would represent the position.
192
+ */
193
+ top: number;
194
+ /**
195
+ * The `left` value of the element rect that would represent the position.
196
+ */
197
+ left: number;
198
+ /**
199
+ * The name of the position. It helps the user of the {@link module:utils/dom/position~getOptimalPosition}
200
+ * helper to recognize different positioning function results. It will pass through to the {@link module:utils/dom/position~DomPoint}
201
+ * returned by the helper.
202
+ */
203
+ name?: string;
204
+ /**
205
+ * An optional configuration that will pass-through the {@link module:utils/dom/position~getOptimalPosition} helper
206
+ * to the {@link module:utils/dom/position~DomPoint} returned by this helper.
207
+ * This configuration may, for instance, let the user of {@link module:utils/dom/position~getOptimalPosition} know that this particular
208
+ * position comes with a certain presentation.
209
+ */
210
+ config?: object;
211
+ }
@@ -0,0 +1,313 @@
1
+ /**
2
+ * @license Copyright (c) 2003-2024, 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.js';
9
+ import Rect from './rect.js';
10
+ import getPositionedAncestor from './getpositionedancestor.js';
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
+ }