@ckeditor/ckeditor5-utils 40.0.0 → 40.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +26 -26
- package/LICENSE.md +3 -3
- package/package.json +1 -1
- package/src/abortabledebounce.d.ts +17 -0
- package/src/abortabledebounce.js +22 -0
- package/src/areconnectedthroughproperties.d.ts +11 -11
- package/src/areconnectedthroughproperties.js +73 -73
- package/src/ckeditorerror.d.ts +123 -123
- package/src/ckeditorerror.js +176 -176
- package/src/collection.d.ts +433 -427
- package/src/collection.js +583 -575
- package/src/comparearrays.d.ts +30 -30
- package/src/comparearrays.js +47 -47
- package/src/config.d.ts +163 -163
- package/src/config.js +163 -162
- package/src/count.d.ts +18 -18
- package/src/count.js +24 -24
- package/src/delay.d.ts +19 -19
- package/src/delay.js +26 -26
- package/src/diff.d.ts +31 -31
- package/src/diff.js +115 -115
- package/src/difftochanges.d.ts +59 -59
- package/src/difftochanges.js +79 -79
- package/src/dom/createelement.d.ts +57 -57
- package/src/dom/createelement.js +40 -40
- package/src/dom/emittermixin.d.ts +142 -142
- package/src/dom/emittermixin.js +239 -239
- package/src/dom/findclosestscrollableancestor.d.ts +11 -11
- package/src/dom/findclosestscrollableancestor.js +31 -31
- package/src/dom/getancestors.d.ts +17 -17
- package/src/dom/getancestors.js +27 -27
- package/src/dom/getborderwidths.d.ts +24 -24
- package/src/dom/getborderwidths.js +24 -24
- package/src/dom/getcommonancestor.d.ts +12 -12
- package/src/dom/getcommonancestor.js +25 -25
- package/src/dom/getdatafromelement.d.ts +14 -14
- package/src/dom/getdatafromelement.js +20 -20
- package/src/dom/getpositionedancestor.d.ts +10 -10
- package/src/dom/getpositionedancestor.js +22 -22
- package/src/dom/global.d.ts +32 -32
- package/src/dom/global.js +35 -35
- package/src/dom/indexof.d.ts +14 -14
- package/src/dom/indexof.js +21 -21
- package/src/dom/insertat.d.ts +15 -15
- package/src/dom/insertat.js +17 -17
- package/src/dom/iscomment.d.ts +11 -11
- package/src/dom/iscomment.js +14 -14
- package/src/dom/isnode.d.ts +11 -11
- package/src/dom/isnode.js +21 -21
- package/src/dom/isrange.d.ts +11 -11
- package/src/dom/isrange.js +13 -13
- package/src/dom/istext.d.ts +11 -11
- package/src/dom/istext.js +13 -13
- package/src/dom/isvalidattributename.d.ts +10 -10
- package/src/dom/isvalidattributename.js +22 -22
- package/src/dom/isvisible.d.ts +18 -18
- package/src/dom/isvisible.js +20 -20
- package/src/dom/iswindow.d.ts +11 -11
- package/src/dom/iswindow.js +22 -22
- package/src/dom/position.d.ts +211 -211
- package/src/dom/position.js +313 -313
- package/src/dom/rect.d.ts +195 -195
- package/src/dom/rect.js +474 -474
- package/src/dom/remove.d.ts +13 -13
- package/src/dom/remove.js +18 -18
- package/src/dom/resizeobserver.d.ts +74 -74
- package/src/dom/resizeobserver.js +126 -126
- package/src/dom/scroll.d.ts +73 -73
- package/src/dom/scroll.js +383 -383
- package/src/dom/setdatainelement.d.ts +14 -14
- package/src/dom/setdatainelement.js +20 -20
- package/src/dom/tounit.d.ts +22 -22
- package/src/dom/tounit.js +16 -16
- package/src/elementreplacer.d.ts +31 -31
- package/src/elementreplacer.js +43 -43
- package/src/emittermixin.d.ts +312 -312
- package/src/emittermixin.js +453 -453
- package/src/env.d.ts +117 -117
- package/src/env.js +122 -122
- package/src/eventinfo.d.ts +58 -58
- package/src/eventinfo.js +26 -26
- package/src/fastdiff.d.ts +112 -112
- package/src/fastdiff.js +248 -248
- package/src/first.d.ts +11 -11
- package/src/first.js +17 -17
- package/src/focustracker.d.ts +75 -75
- package/src/focustracker.js +95 -95
- package/src/index.d.ts +64 -61
- package/src/index.js +63 -60
- package/src/inserttopriorityarray.d.ts +30 -30
- package/src/inserttopriorityarray.js +21 -21
- package/src/isiterable.d.ts +14 -14
- package/src/isiterable.js +16 -16
- package/src/keyboard.d.ts +126 -126
- package/src/keyboard.js +221 -221
- package/src/keystrokehandler.d.ts +87 -87
- package/src/keystrokehandler.js +122 -122
- package/src/language.d.ts +17 -17
- package/src/language.js +19 -19
- package/src/locale.d.ts +120 -120
- package/src/locale.js +76 -76
- package/src/mapsequal.d.ts +15 -15
- package/src/mapsequal.js +27 -27
- package/src/mix.d.ts +85 -85
- package/src/mix.js +50 -50
- package/src/nth.d.ts +16 -16
- package/src/nth.js +24 -24
- package/src/objecttomap.d.ts +23 -23
- package/src/objecttomap.js +27 -27
- package/src/observablemixin.d.ts +560 -560
- package/src/observablemixin.js +580 -580
- package/src/priorities.d.ts +33 -33
- package/src/priorities.js +23 -23
- package/src/retry.d.ts +33 -0
- package/src/retry.js +47 -0
- package/src/splicearray.d.ts +26 -26
- package/src/splicearray.js +40 -40
- package/src/spy.d.ts +21 -21
- package/src/spy.js +22 -22
- package/src/toarray.d.ts +25 -25
- package/src/toarray.js +7 -7
- package/src/tomap.d.ts +19 -19
- package/src/tomap.js +29 -29
- package/src/translation-service.d.ts +168 -168
- package/src/translation-service.js +198 -198
- package/src/uid.d.ts +15 -15
- package/src/uid.js +57 -57
- package/src/unicode.d.ts +54 -54
- package/src/unicode.js +85 -85
- package/src/verifylicense.d.ts +15 -15
- package/src/verifylicense.js +87 -87
- package/src/version.d.ts +10 -10
- package/src/version.js +153 -153
- package/src/wait.d.ts +16 -0
- package/src/wait.js +29 -0
package/src/dom/scroll.js
CHANGED
|
@@ -1,383 +1,383 @@
|
|
|
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/scroll
|
|
7
|
-
*/
|
|
8
|
-
import isRange from './isrange';
|
|
9
|
-
import Rect from './rect';
|
|
10
|
-
import isText from './istext';
|
|
11
|
-
/**
|
|
12
|
-
* Makes any page `HTMLElement` or `Range` (`target`) visible inside the browser viewport.
|
|
13
|
-
* This helper will scroll all `target` ancestors and the web browser viewport to reveal the target to
|
|
14
|
-
* the user. If the `target` is already visible, nothing will happen.
|
|
15
|
-
*
|
|
16
|
-
* @param options Additional configuration of the scrolling behavior.
|
|
17
|
-
* @param options.target A target, which supposed to become visible to the user.
|
|
18
|
-
* @param options.viewportOffset An offset from the edge of the viewport (in pixels)
|
|
19
|
-
* the `target` will be moved by if the viewport is scrolled. It enhances the user experience
|
|
20
|
-
* by keeping the `target` some distance from the edge of the viewport and thus making it easier to
|
|
21
|
-
* read or edit by the user.
|
|
22
|
-
* @param options.ancestorOffset An offset from the boundary of scrollable ancestors (if any)
|
|
23
|
-
* the `target` will be moved by if the viewport is scrolled. It enhances the user experience
|
|
24
|
-
* by keeping the `target` some distance from the edge of the ancestors and thus making it easier to
|
|
25
|
-
* read or edit by the user.
|
|
26
|
-
* @param options.alignToTop When set `true`, the helper will make sure the `target` is scrolled up
|
|
27
|
-
* to the top boundary of the viewport and/or scrollable ancestors if scrolled up. When not set
|
|
28
|
-
* (default), the `target` will be revealed by scrolling as little as possible. This option will
|
|
29
|
-
* not affect `targets` that must be scrolled down because they will appear at the top of the boundary
|
|
30
|
-
* anyway.
|
|
31
|
-
*
|
|
32
|
-
* ```
|
|
33
|
-
* scrollViewportToShowTarget() with scrollViewportToShowTarget() with
|
|
34
|
-
* Initial state alignToTop unset (default) alignToTop = true
|
|
35
|
-
*
|
|
36
|
-
* ┌────────────────────────────────┬─┐ ┌────────────────────────────────┬─┐ ┌────────────────────────────────┬─┐
|
|
37
|
-
* │ │▲│ │ │▲│ │ [ Target to be revealed ] │▲│
|
|
38
|
-
* │ │ │ │ │ │ │ │ │
|
|
39
|
-
* │ │█│ │ │ │ │ │ │
|
|
40
|
-
* │ │█│ │ │ │ │ │ │
|
|
41
|
-
* │ │ │ │ │█│ │ │ │
|
|
42
|
-
* │ │ │ │ │█│ │ │█│
|
|
43
|
-
* │ │ │ │ │ │ │ │█│
|
|
44
|
-
* │ │▼│ │ [ Target to be revealed ] │▼│ │ │▼│
|
|
45
|
-
* └────────────────────────────────┴─┘ └────────────────────────────────┴─┘ └────────────────────────────────┴─┘
|
|
46
|
-
*
|
|
47
|
-
*
|
|
48
|
-
* [ Target to be revealed ]
|
|
49
|
-
*```
|
|
50
|
-
*
|
|
51
|
-
* @param options.forceScroll When set `true`, the `target` will be aligned to the top of the viewport
|
|
52
|
-
* and scrollable ancestors whether it is already visible or not. This option will only work when `alignToTop`
|
|
53
|
-
* is `true`
|
|
54
|
-
*/
|
|
55
|
-
export function scrollViewportToShowTarget({ target, viewportOffset = 0, ancestorOffset = 0, alignToTop, forceScroll }) {
|
|
56
|
-
const targetWindow = getWindow(target);
|
|
57
|
-
let currentWindow = targetWindow;
|
|
58
|
-
let currentFrame = null;
|
|
59
|
-
viewportOffset = normalizeViewportOffset(viewportOffset);
|
|
60
|
-
// Iterate over all windows, starting from target's parent window up to window#top.
|
|
61
|
-
while (currentWindow) {
|
|
62
|
-
let firstAncestorToScroll;
|
|
63
|
-
// Let's scroll target's ancestors first to reveal it. Then, once the ancestor scrolls
|
|
64
|
-
// settled down, the algorithm can eventually scroll the viewport of the current window.
|
|
65
|
-
//
|
|
66
|
-
// Note: If the current window is target's **original** window (e.g. the first one),
|
|
67
|
-
// start scrolling the closest parent of the target. If not, scroll the closest parent
|
|
68
|
-
// of an iframe that resides in the current window.
|
|
69
|
-
if (currentWindow == targetWindow) {
|
|
70
|
-
firstAncestorToScroll = getParentElement(target);
|
|
71
|
-
}
|
|
72
|
-
else {
|
|
73
|
-
firstAncestorToScroll = getParentElement(currentFrame);
|
|
74
|
-
}
|
|
75
|
-
// Scroll the target's ancestors first. Once done, scrolling the viewport is easy.
|
|
76
|
-
scrollAncestorsToShowRect({
|
|
77
|
-
parent: firstAncestorToScroll,
|
|
78
|
-
getRect: () => {
|
|
79
|
-
// Note: If the target does not belong to the current window **directly**,
|
|
80
|
-
// i.e. it resides in an iframe belonging to the window, obtain the target's rect
|
|
81
|
-
// in the coordinates of the current window. By default, a Rect returns geometry
|
|
82
|
-
// relative to the current window's viewport. To make it work in a parent window,
|
|
83
|
-
// it must be shifted.
|
|
84
|
-
return getRectRelativeToWindow(target, currentWindow);
|
|
85
|
-
},
|
|
86
|
-
alignToTop,
|
|
87
|
-
ancestorOffset,
|
|
88
|
-
forceScroll
|
|
89
|
-
});
|
|
90
|
-
// Obtain the rect of the target after it has been scrolled within its ancestors.
|
|
91
|
-
// It's time to scroll the viewport.
|
|
92
|
-
const targetRect = getRectRelativeToWindow(target, currentWindow);
|
|
93
|
-
scrollWindowToShowRect({
|
|
94
|
-
window: currentWindow,
|
|
95
|
-
rect: targetRect,
|
|
96
|
-
viewportOffset,
|
|
97
|
-
alignToTop,
|
|
98
|
-
forceScroll
|
|
99
|
-
});
|
|
100
|
-
if (currentWindow.parent != currentWindow) {
|
|
101
|
-
// Keep the reference to the <iframe> element the "previous current window" was
|
|
102
|
-
// rendered within. It will be useful to re–calculate the rect of the target
|
|
103
|
-
// in the parent window's relative geometry. The target's rect must be shifted
|
|
104
|
-
// by it's iframe's position.
|
|
105
|
-
currentFrame = currentWindow.frameElement;
|
|
106
|
-
currentWindow = currentWindow.parent;
|
|
107
|
-
// If the current window has some parent but frameElement is inaccessible, then they have
|
|
108
|
-
// different domains/ports and, due to security reasons, accessing and scrolling
|
|
109
|
-
// the parent window won't be possible.
|
|
110
|
-
// See https://github.com/ckeditor/ckeditor5/issues/930.
|
|
111
|
-
if (!currentFrame) {
|
|
112
|
-
return;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
else {
|
|
116
|
-
currentWindow = null;
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
/**
|
|
121
|
-
* Makes any page `HTMLElement` or `Range` (target) visible within its scrollable ancestors,
|
|
122
|
-
* e.g. if they have `overflow: scroll` CSS style.
|
|
123
|
-
*
|
|
124
|
-
* @param target A target, which supposed to become visible to the user.
|
|
125
|
-
* @param ancestorOffset An offset between the target and the boundary of scrollable ancestors
|
|
126
|
-
* to be maintained while scrolling.
|
|
127
|
-
* @param limiterElement The outermost ancestor that should be scrolled. If specified, it can prevent
|
|
128
|
-
* scrolling the whole page.
|
|
129
|
-
*/
|
|
130
|
-
export function scrollAncestorsToShowTarget(target, ancestorOffset, limiterElement) {
|
|
131
|
-
const targetParent = getParentElement(target);
|
|
132
|
-
scrollAncestorsToShowRect({
|
|
133
|
-
parent: targetParent,
|
|
134
|
-
getRect: () => new Rect(target),
|
|
135
|
-
ancestorOffset,
|
|
136
|
-
limiterElement
|
|
137
|
-
});
|
|
138
|
-
}
|
|
139
|
-
/**
|
|
140
|
-
* Makes a given rect visible within its parent window.
|
|
141
|
-
*
|
|
142
|
-
* Note: Avoid the situation where the caret is still in the viewport, but totally
|
|
143
|
-
* at the edge of it. In such situation, if it moved beyond the viewport in the next
|
|
144
|
-
* action e.g. after paste, the scrolling would move it to the viewportOffset level
|
|
145
|
-
* and it all would look like the caret visually moved up/down:
|
|
146
|
-
*
|
|
147
|
-
* 1.
|
|
148
|
-
* ```
|
|
149
|
-
* | foo[]
|
|
150
|
-
* | <--- N px of space below the caret
|
|
151
|
-
* +---------------------------------...
|
|
152
|
-
* ```
|
|
153
|
-
*
|
|
154
|
-
* 2. *paste*
|
|
155
|
-
* 3.
|
|
156
|
-
* ```
|
|
157
|
-
* |
|
|
158
|
-
* |
|
|
159
|
-
* +-foo-----------------------------...
|
|
160
|
-
* bar[] <--- caret below viewport, scrolling...
|
|
161
|
-
* ```
|
|
162
|
-
*
|
|
163
|
-
* 4. *scrolling*
|
|
164
|
-
* 5.
|
|
165
|
-
* ```
|
|
166
|
-
* |
|
|
167
|
-
* | foo
|
|
168
|
-
* | bar[] <--- caret precisely at the edge
|
|
169
|
-
* +---------------------------------...
|
|
170
|
-
* ```
|
|
171
|
-
*
|
|
172
|
-
* To prevent this, this method checks the rects moved by the viewportOffset to cover
|
|
173
|
-
* the upper/lower edge of the viewport. It makes sure if the action repeats, there's
|
|
174
|
-
* no twitching – it's a purely visual improvement:
|
|
175
|
-
*
|
|
176
|
-
* 5. (after fix)
|
|
177
|
-
* ```
|
|
178
|
-
* |
|
|
179
|
-
* | foo
|
|
180
|
-
* | bar[]
|
|
181
|
-
* | <--- N px of space below the caret
|
|
182
|
-
* +---------------------------------...
|
|
183
|
-
* ```
|
|
184
|
-
*
|
|
185
|
-
* @param options Additional configuration of the scrolling behavior.
|
|
186
|
-
* @param options.window A window which is scrolled to reveal the rect.
|
|
187
|
-
* @param options.rect A rect which is to be revealed.
|
|
188
|
-
* @param options.viewportOffset An offset from the edge of the viewport (in pixels) the `rect` will be
|
|
189
|
-
* moved by if the viewport is scrolled.
|
|
190
|
-
* @param options.alignToTop When set `true`, the helper will make sure the `rect` is scrolled up
|
|
191
|
-
* to the top boundary of the viewport if scrolled up. When not set (default), the `rect` will be
|
|
192
|
-
* revealed by scrolling as little as possible. This option will not affect rects that must be scrolled
|
|
193
|
-
* down because they will appear at the top of the boundary anyway.
|
|
194
|
-
* @param options.forceScroll When set `true`, the `rect` will be aligned to the top of the viewport
|
|
195
|
-
* whether it is already visible or not. This option will only work when `alignToTop` is `true`
|
|
196
|
-
*/
|
|
197
|
-
function scrollWindowToShowRect({ window, rect, alignToTop, forceScroll, viewportOffset }) {
|
|
198
|
-
const targetShiftedDownRect = rect.clone().moveBy(0, viewportOffset.bottom);
|
|
199
|
-
const targetShiftedUpRect = rect.clone().moveBy(0, -viewportOffset.top);
|
|
200
|
-
const viewportRect = new Rect(window).excludeScrollbarsAndBorders();
|
|
201
|
-
const rects = [targetShiftedUpRect, targetShiftedDownRect];
|
|
202
|
-
const forceScrollToTop = alignToTop && forceScroll;
|
|
203
|
-
const allRectsFitInViewport = rects.every(rect => viewportRect.contains(rect));
|
|
204
|
-
let { scrollX, scrollY } = window;
|
|
205
|
-
const initialScrollX = scrollX;
|
|
206
|
-
const initialScrollY = scrollY;
|
|
207
|
-
if (forceScrollToTop) {
|
|
208
|
-
scrollY -= (viewportRect.top - rect.top) + viewportOffset.top;
|
|
209
|
-
}
|
|
210
|
-
else if (!allRectsFitInViewport) {
|
|
211
|
-
if (isAbove(targetShiftedUpRect, viewportRect)) {
|
|
212
|
-
scrollY -= viewportRect.top - rect.top + viewportOffset.top;
|
|
213
|
-
}
|
|
214
|
-
else if (isBelow(targetShiftedDownRect, viewportRect)) {
|
|
215
|
-
if (alignToTop) {
|
|
216
|
-
scrollY += rect.top - viewportRect.top - viewportOffset.top;
|
|
217
|
-
}
|
|
218
|
-
else {
|
|
219
|
-
scrollY += rect.bottom - viewportRect.bottom + viewportOffset.bottom;
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
}
|
|
223
|
-
if (!allRectsFitInViewport) {
|
|
224
|
-
// TODO: Web browsers scroll natively to place the target in the middle
|
|
225
|
-
// of the viewport. It's not a very popular case, though.
|
|
226
|
-
if (isLeftOf(rect, viewportRect)) {
|
|
227
|
-
scrollX -= viewportRect.left - rect.left + viewportOffset.left;
|
|
228
|
-
}
|
|
229
|
-
else if (isRightOf(rect, viewportRect)) {
|
|
230
|
-
scrollX += rect.right - viewportRect.right + viewportOffset.right;
|
|
231
|
-
}
|
|
232
|
-
}
|
|
233
|
-
if (scrollX != initialScrollX || scrollY !== initialScrollY) {
|
|
234
|
-
window.scrollTo(scrollX, scrollY);
|
|
235
|
-
}
|
|
236
|
-
}
|
|
237
|
-
/**
|
|
238
|
-
* Recursively scrolls element ancestors to visually reveal a rect.
|
|
239
|
-
*
|
|
240
|
-
* @param options Additional configuration of the scrolling behavior.
|
|
241
|
-
* @param options.parent The first parent ancestor to start scrolling.
|
|
242
|
-
* @param options.getRect A function which returns the Rect, which is to be revealed.
|
|
243
|
-
* @param options.ancestorOffset An offset from the boundary of scrollable ancestors (if any)
|
|
244
|
-
* the `Rect` instance will be moved by if the viewport is scrolled.
|
|
245
|
-
* @param options.alignToTop When set `true`, the helper will make sure the `Rect` instance is scrolled up
|
|
246
|
-
* to the top boundary of the scrollable ancestors if scrolled up. When not set (default), the `rect`
|
|
247
|
-
* will be revealed by scrolling as little as possible. This option will not affect rects that must be
|
|
248
|
-
* scrolled down because they will appear at the top of the boundary
|
|
249
|
-
* anyway.
|
|
250
|
-
* @param options.forceScroll When set `true`, the `rect` will be aligned to the top of scrollable ancestors
|
|
251
|
-
* whether it is already visible or not. This option will only work when `alignToTop` is `true`
|
|
252
|
-
* @param options.limiterElement The outermost ancestor that should be scrolled. Defaults to the `<body>` element.
|
|
253
|
-
*/
|
|
254
|
-
function scrollAncestorsToShowRect({ parent, getRect, alignToTop, forceScroll, ancestorOffset = 0, limiterElement }) {
|
|
255
|
-
const parentWindow = getWindow(parent);
|
|
256
|
-
const forceScrollToTop = alignToTop && forceScroll;
|
|
257
|
-
let parentRect, targetRect, targetFitsInTarget;
|
|
258
|
-
const limiter = limiterElement || parentWindow.document.body;
|
|
259
|
-
while (parent != limiter) {
|
|
260
|
-
targetRect = getRect();
|
|
261
|
-
parentRect = new Rect(parent).excludeScrollbarsAndBorders();
|
|
262
|
-
targetFitsInTarget = parentRect.contains(targetRect);
|
|
263
|
-
if (forceScrollToTop) {
|
|
264
|
-
parent.scrollTop -= (parentRect.top - targetRect.top) + ancestorOffset;
|
|
265
|
-
}
|
|
266
|
-
else if (!targetFitsInTarget) {
|
|
267
|
-
if (isAbove(targetRect, parentRect)) {
|
|
268
|
-
parent.scrollTop -= parentRect.top - targetRect.top + ancestorOffset;
|
|
269
|
-
}
|
|
270
|
-
else if (isBelow(targetRect, parentRect)) {
|
|
271
|
-
if (alignToTop) {
|
|
272
|
-
parent.scrollTop += targetRect.top - parentRect.top - ancestorOffset;
|
|
273
|
-
}
|
|
274
|
-
else {
|
|
275
|
-
parent.scrollTop += targetRect.bottom - parentRect.bottom + ancestorOffset;
|
|
276
|
-
}
|
|
277
|
-
}
|
|
278
|
-
}
|
|
279
|
-
if (!targetFitsInTarget) {
|
|
280
|
-
if (isLeftOf(targetRect, parentRect)) {
|
|
281
|
-
parent.scrollLeft -= parentRect.left - targetRect.left + ancestorOffset;
|
|
282
|
-
}
|
|
283
|
-
else if (isRightOf(targetRect, parentRect)) {
|
|
284
|
-
parent.scrollLeft += targetRect.right - parentRect.right + ancestorOffset;
|
|
285
|
-
}
|
|
286
|
-
}
|
|
287
|
-
parent = parent.parentNode;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
/**
|
|
291
|
-
* Determines if a given `Rect` extends beyond the bottom edge of the second `Rect`.
|
|
292
|
-
*/
|
|
293
|
-
function isBelow(firstRect, secondRect) {
|
|
294
|
-
return firstRect.bottom > secondRect.bottom;
|
|
295
|
-
}
|
|
296
|
-
/**
|
|
297
|
-
* Determines if a given `Rect` extends beyond the top edge of the second `Rect`.
|
|
298
|
-
*/
|
|
299
|
-
function isAbove(firstRect, secondRect) {
|
|
300
|
-
return firstRect.top < secondRect.top;
|
|
301
|
-
}
|
|
302
|
-
/**
|
|
303
|
-
* Determines if a given `Rect` extends beyond the left edge of the second `Rect`.
|
|
304
|
-
*/
|
|
305
|
-
function isLeftOf(firstRect, secondRect) {
|
|
306
|
-
return firstRect.left < secondRect.left;
|
|
307
|
-
}
|
|
308
|
-
/**
|
|
309
|
-
* Determines if a given `Rect` extends beyond the right edge of the second `Rect`.
|
|
310
|
-
*/
|
|
311
|
-
function isRightOf(firstRect, secondRect) {
|
|
312
|
-
return firstRect.right > secondRect.right;
|
|
313
|
-
}
|
|
314
|
-
/**
|
|
315
|
-
* Returns the closest window of an element or range.
|
|
316
|
-
*/
|
|
317
|
-
function getWindow(elementOrRange) {
|
|
318
|
-
if (isRange(elementOrRange)) {
|
|
319
|
-
return elementOrRange.startContainer.ownerDocument.defaultView;
|
|
320
|
-
}
|
|
321
|
-
else {
|
|
322
|
-
return elementOrRange.ownerDocument.defaultView;
|
|
323
|
-
}
|
|
324
|
-
}
|
|
325
|
-
/**
|
|
326
|
-
* Returns the closest parent of an element or DOM range.
|
|
327
|
-
*/
|
|
328
|
-
function getParentElement(elementOrRange) {
|
|
329
|
-
if (isRange(elementOrRange)) {
|
|
330
|
-
let parent = elementOrRange.commonAncestorContainer;
|
|
331
|
-
// If a Range is attached to the Text, use the closest element ancestor.
|
|
332
|
-
if (isText(parent)) {
|
|
333
|
-
parent = parent.parentNode;
|
|
334
|
-
}
|
|
335
|
-
return parent;
|
|
336
|
-
}
|
|
337
|
-
else {
|
|
338
|
-
return elementOrRange.parentNode;
|
|
339
|
-
}
|
|
340
|
-
}
|
|
341
|
-
/**
|
|
342
|
-
* Returns the rect of an element or range residing in an iframe.
|
|
343
|
-
* The result rect is relative to the geometry of the passed window instance.
|
|
344
|
-
*
|
|
345
|
-
* @param target Element or range which rect should be returned.
|
|
346
|
-
* @param relativeWindow A window the rect should be relative to.
|
|
347
|
-
*/
|
|
348
|
-
function getRectRelativeToWindow(target, relativeWindow) {
|
|
349
|
-
const targetWindow = getWindow(target);
|
|
350
|
-
const rect = new Rect(target);
|
|
351
|
-
if (targetWindow === relativeWindow) {
|
|
352
|
-
return rect;
|
|
353
|
-
}
|
|
354
|
-
else {
|
|
355
|
-
let currentWindow = targetWindow;
|
|
356
|
-
while (currentWindow != relativeWindow) {
|
|
357
|
-
const frame = currentWindow.frameElement;
|
|
358
|
-
const frameRect = new Rect(frame).excludeScrollbarsAndBorders();
|
|
359
|
-
rect.moveBy(frameRect.left, frameRect.top);
|
|
360
|
-
currentWindow = currentWindow.parent;
|
|
361
|
-
}
|
|
362
|
-
}
|
|
363
|
-
return rect;
|
|
364
|
-
}
|
|
365
|
-
/**
|
|
366
|
-
* A helper that explodes the `viewportOffset` configuration if defined as a plain number into an object
|
|
367
|
-
* with `top`, `bottom`, `left`, and `right` properties.
|
|
368
|
-
*
|
|
369
|
-
* If an object value is passed, this helper will pass it through.
|
|
370
|
-
*
|
|
371
|
-
* @param viewportOffset Viewport offset to be normalized.
|
|
372
|
-
*/
|
|
373
|
-
function normalizeViewportOffset(viewportOffset) {
|
|
374
|
-
if (typeof viewportOffset === 'number') {
|
|
375
|
-
return {
|
|
376
|
-
top: viewportOffset,
|
|
377
|
-
bottom: viewportOffset,
|
|
378
|
-
left: viewportOffset,
|
|
379
|
-
right: viewportOffset
|
|
380
|
-
};
|
|
381
|
-
}
|
|
382
|
-
return viewportOffset;
|
|
383
|
-
}
|
|
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/scroll
|
|
7
|
+
*/
|
|
8
|
+
import isRange from './isrange';
|
|
9
|
+
import Rect from './rect';
|
|
10
|
+
import isText from './istext';
|
|
11
|
+
/**
|
|
12
|
+
* Makes any page `HTMLElement` or `Range` (`target`) visible inside the browser viewport.
|
|
13
|
+
* This helper will scroll all `target` ancestors and the web browser viewport to reveal the target to
|
|
14
|
+
* the user. If the `target` is already visible, nothing will happen.
|
|
15
|
+
*
|
|
16
|
+
* @param options Additional configuration of the scrolling behavior.
|
|
17
|
+
* @param options.target A target, which supposed to become visible to the user.
|
|
18
|
+
* @param options.viewportOffset An offset from the edge of the viewport (in pixels)
|
|
19
|
+
* the `target` will be moved by if the viewport is scrolled. It enhances the user experience
|
|
20
|
+
* by keeping the `target` some distance from the edge of the viewport and thus making it easier to
|
|
21
|
+
* read or edit by the user.
|
|
22
|
+
* @param options.ancestorOffset An offset from the boundary of scrollable ancestors (if any)
|
|
23
|
+
* the `target` will be moved by if the viewport is scrolled. It enhances the user experience
|
|
24
|
+
* by keeping the `target` some distance from the edge of the ancestors and thus making it easier to
|
|
25
|
+
* read or edit by the user.
|
|
26
|
+
* @param options.alignToTop When set `true`, the helper will make sure the `target` is scrolled up
|
|
27
|
+
* to the top boundary of the viewport and/or scrollable ancestors if scrolled up. When not set
|
|
28
|
+
* (default), the `target` will be revealed by scrolling as little as possible. This option will
|
|
29
|
+
* not affect `targets` that must be scrolled down because they will appear at the top of the boundary
|
|
30
|
+
* anyway.
|
|
31
|
+
*
|
|
32
|
+
* ```
|
|
33
|
+
* scrollViewportToShowTarget() with scrollViewportToShowTarget() with
|
|
34
|
+
* Initial state alignToTop unset (default) alignToTop = true
|
|
35
|
+
*
|
|
36
|
+
* ┌────────────────────────────────┬─┐ ┌────────────────────────────────┬─┐ ┌────────────────────────────────┬─┐
|
|
37
|
+
* │ │▲│ │ │▲│ │ [ Target to be revealed ] │▲│
|
|
38
|
+
* │ │ │ │ │ │ │ │ │
|
|
39
|
+
* │ │█│ │ │ │ │ │ │
|
|
40
|
+
* │ │█│ │ │ │ │ │ │
|
|
41
|
+
* │ │ │ │ │█│ │ │ │
|
|
42
|
+
* │ │ │ │ │█│ │ │█│
|
|
43
|
+
* │ │ │ │ │ │ │ │█│
|
|
44
|
+
* │ │▼│ │ [ Target to be revealed ] │▼│ │ │▼│
|
|
45
|
+
* └────────────────────────────────┴─┘ └────────────────────────────────┴─┘ └────────────────────────────────┴─┘
|
|
46
|
+
*
|
|
47
|
+
*
|
|
48
|
+
* [ Target to be revealed ]
|
|
49
|
+
*```
|
|
50
|
+
*
|
|
51
|
+
* @param options.forceScroll When set `true`, the `target` will be aligned to the top of the viewport
|
|
52
|
+
* and scrollable ancestors whether it is already visible or not. This option will only work when `alignToTop`
|
|
53
|
+
* is `true`
|
|
54
|
+
*/
|
|
55
|
+
export function scrollViewportToShowTarget({ target, viewportOffset = 0, ancestorOffset = 0, alignToTop, forceScroll }) {
|
|
56
|
+
const targetWindow = getWindow(target);
|
|
57
|
+
let currentWindow = targetWindow;
|
|
58
|
+
let currentFrame = null;
|
|
59
|
+
viewportOffset = normalizeViewportOffset(viewportOffset);
|
|
60
|
+
// Iterate over all windows, starting from target's parent window up to window#top.
|
|
61
|
+
while (currentWindow) {
|
|
62
|
+
let firstAncestorToScroll;
|
|
63
|
+
// Let's scroll target's ancestors first to reveal it. Then, once the ancestor scrolls
|
|
64
|
+
// settled down, the algorithm can eventually scroll the viewport of the current window.
|
|
65
|
+
//
|
|
66
|
+
// Note: If the current window is target's **original** window (e.g. the first one),
|
|
67
|
+
// start scrolling the closest parent of the target. If not, scroll the closest parent
|
|
68
|
+
// of an iframe that resides in the current window.
|
|
69
|
+
if (currentWindow == targetWindow) {
|
|
70
|
+
firstAncestorToScroll = getParentElement(target);
|
|
71
|
+
}
|
|
72
|
+
else {
|
|
73
|
+
firstAncestorToScroll = getParentElement(currentFrame);
|
|
74
|
+
}
|
|
75
|
+
// Scroll the target's ancestors first. Once done, scrolling the viewport is easy.
|
|
76
|
+
scrollAncestorsToShowRect({
|
|
77
|
+
parent: firstAncestorToScroll,
|
|
78
|
+
getRect: () => {
|
|
79
|
+
// Note: If the target does not belong to the current window **directly**,
|
|
80
|
+
// i.e. it resides in an iframe belonging to the window, obtain the target's rect
|
|
81
|
+
// in the coordinates of the current window. By default, a Rect returns geometry
|
|
82
|
+
// relative to the current window's viewport. To make it work in a parent window,
|
|
83
|
+
// it must be shifted.
|
|
84
|
+
return getRectRelativeToWindow(target, currentWindow);
|
|
85
|
+
},
|
|
86
|
+
alignToTop,
|
|
87
|
+
ancestorOffset,
|
|
88
|
+
forceScroll
|
|
89
|
+
});
|
|
90
|
+
// Obtain the rect of the target after it has been scrolled within its ancestors.
|
|
91
|
+
// It's time to scroll the viewport.
|
|
92
|
+
const targetRect = getRectRelativeToWindow(target, currentWindow);
|
|
93
|
+
scrollWindowToShowRect({
|
|
94
|
+
window: currentWindow,
|
|
95
|
+
rect: targetRect,
|
|
96
|
+
viewportOffset,
|
|
97
|
+
alignToTop,
|
|
98
|
+
forceScroll
|
|
99
|
+
});
|
|
100
|
+
if (currentWindow.parent != currentWindow) {
|
|
101
|
+
// Keep the reference to the <iframe> element the "previous current window" was
|
|
102
|
+
// rendered within. It will be useful to re–calculate the rect of the target
|
|
103
|
+
// in the parent window's relative geometry. The target's rect must be shifted
|
|
104
|
+
// by it's iframe's position.
|
|
105
|
+
currentFrame = currentWindow.frameElement;
|
|
106
|
+
currentWindow = currentWindow.parent;
|
|
107
|
+
// If the current window has some parent but frameElement is inaccessible, then they have
|
|
108
|
+
// different domains/ports and, due to security reasons, accessing and scrolling
|
|
109
|
+
// the parent window won't be possible.
|
|
110
|
+
// See https://github.com/ckeditor/ckeditor5/issues/930.
|
|
111
|
+
if (!currentFrame) {
|
|
112
|
+
return;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
else {
|
|
116
|
+
currentWindow = null;
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/**
|
|
121
|
+
* Makes any page `HTMLElement` or `Range` (target) visible within its scrollable ancestors,
|
|
122
|
+
* e.g. if they have `overflow: scroll` CSS style.
|
|
123
|
+
*
|
|
124
|
+
* @param target A target, which supposed to become visible to the user.
|
|
125
|
+
* @param ancestorOffset An offset between the target and the boundary of scrollable ancestors
|
|
126
|
+
* to be maintained while scrolling.
|
|
127
|
+
* @param limiterElement The outermost ancestor that should be scrolled. If specified, it can prevent
|
|
128
|
+
* scrolling the whole page.
|
|
129
|
+
*/
|
|
130
|
+
export function scrollAncestorsToShowTarget(target, ancestorOffset, limiterElement) {
|
|
131
|
+
const targetParent = getParentElement(target);
|
|
132
|
+
scrollAncestorsToShowRect({
|
|
133
|
+
parent: targetParent,
|
|
134
|
+
getRect: () => new Rect(target),
|
|
135
|
+
ancestorOffset,
|
|
136
|
+
limiterElement
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Makes a given rect visible within its parent window.
|
|
141
|
+
*
|
|
142
|
+
* Note: Avoid the situation where the caret is still in the viewport, but totally
|
|
143
|
+
* at the edge of it. In such situation, if it moved beyond the viewport in the next
|
|
144
|
+
* action e.g. after paste, the scrolling would move it to the viewportOffset level
|
|
145
|
+
* and it all would look like the caret visually moved up/down:
|
|
146
|
+
*
|
|
147
|
+
* 1.
|
|
148
|
+
* ```
|
|
149
|
+
* | foo[]
|
|
150
|
+
* | <--- N px of space below the caret
|
|
151
|
+
* +---------------------------------...
|
|
152
|
+
* ```
|
|
153
|
+
*
|
|
154
|
+
* 2. *paste*
|
|
155
|
+
* 3.
|
|
156
|
+
* ```
|
|
157
|
+
* |
|
|
158
|
+
* |
|
|
159
|
+
* +-foo-----------------------------...
|
|
160
|
+
* bar[] <--- caret below viewport, scrolling...
|
|
161
|
+
* ```
|
|
162
|
+
*
|
|
163
|
+
* 4. *scrolling*
|
|
164
|
+
* 5.
|
|
165
|
+
* ```
|
|
166
|
+
* |
|
|
167
|
+
* | foo
|
|
168
|
+
* | bar[] <--- caret precisely at the edge
|
|
169
|
+
* +---------------------------------...
|
|
170
|
+
* ```
|
|
171
|
+
*
|
|
172
|
+
* To prevent this, this method checks the rects moved by the viewportOffset to cover
|
|
173
|
+
* the upper/lower edge of the viewport. It makes sure if the action repeats, there's
|
|
174
|
+
* no twitching – it's a purely visual improvement:
|
|
175
|
+
*
|
|
176
|
+
* 5. (after fix)
|
|
177
|
+
* ```
|
|
178
|
+
* |
|
|
179
|
+
* | foo
|
|
180
|
+
* | bar[]
|
|
181
|
+
* | <--- N px of space below the caret
|
|
182
|
+
* +---------------------------------...
|
|
183
|
+
* ```
|
|
184
|
+
*
|
|
185
|
+
* @param options Additional configuration of the scrolling behavior.
|
|
186
|
+
* @param options.window A window which is scrolled to reveal the rect.
|
|
187
|
+
* @param options.rect A rect which is to be revealed.
|
|
188
|
+
* @param options.viewportOffset An offset from the edge of the viewport (in pixels) the `rect` will be
|
|
189
|
+
* moved by if the viewport is scrolled.
|
|
190
|
+
* @param options.alignToTop When set `true`, the helper will make sure the `rect` is scrolled up
|
|
191
|
+
* to the top boundary of the viewport if scrolled up. When not set (default), the `rect` will be
|
|
192
|
+
* revealed by scrolling as little as possible. This option will not affect rects that must be scrolled
|
|
193
|
+
* down because they will appear at the top of the boundary anyway.
|
|
194
|
+
* @param options.forceScroll When set `true`, the `rect` will be aligned to the top of the viewport
|
|
195
|
+
* whether it is already visible or not. This option will only work when `alignToTop` is `true`
|
|
196
|
+
*/
|
|
197
|
+
function scrollWindowToShowRect({ window, rect, alignToTop, forceScroll, viewportOffset }) {
|
|
198
|
+
const targetShiftedDownRect = rect.clone().moveBy(0, viewportOffset.bottom);
|
|
199
|
+
const targetShiftedUpRect = rect.clone().moveBy(0, -viewportOffset.top);
|
|
200
|
+
const viewportRect = new Rect(window).excludeScrollbarsAndBorders();
|
|
201
|
+
const rects = [targetShiftedUpRect, targetShiftedDownRect];
|
|
202
|
+
const forceScrollToTop = alignToTop && forceScroll;
|
|
203
|
+
const allRectsFitInViewport = rects.every(rect => viewportRect.contains(rect));
|
|
204
|
+
let { scrollX, scrollY } = window;
|
|
205
|
+
const initialScrollX = scrollX;
|
|
206
|
+
const initialScrollY = scrollY;
|
|
207
|
+
if (forceScrollToTop) {
|
|
208
|
+
scrollY -= (viewportRect.top - rect.top) + viewportOffset.top;
|
|
209
|
+
}
|
|
210
|
+
else if (!allRectsFitInViewport) {
|
|
211
|
+
if (isAbove(targetShiftedUpRect, viewportRect)) {
|
|
212
|
+
scrollY -= viewportRect.top - rect.top + viewportOffset.top;
|
|
213
|
+
}
|
|
214
|
+
else if (isBelow(targetShiftedDownRect, viewportRect)) {
|
|
215
|
+
if (alignToTop) {
|
|
216
|
+
scrollY += rect.top - viewportRect.top - viewportOffset.top;
|
|
217
|
+
}
|
|
218
|
+
else {
|
|
219
|
+
scrollY += rect.bottom - viewportRect.bottom + viewportOffset.bottom;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
if (!allRectsFitInViewport) {
|
|
224
|
+
// TODO: Web browsers scroll natively to place the target in the middle
|
|
225
|
+
// of the viewport. It's not a very popular case, though.
|
|
226
|
+
if (isLeftOf(rect, viewportRect)) {
|
|
227
|
+
scrollX -= viewportRect.left - rect.left + viewportOffset.left;
|
|
228
|
+
}
|
|
229
|
+
else if (isRightOf(rect, viewportRect)) {
|
|
230
|
+
scrollX += rect.right - viewportRect.right + viewportOffset.right;
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
if (scrollX != initialScrollX || scrollY !== initialScrollY) {
|
|
234
|
+
window.scrollTo(scrollX, scrollY);
|
|
235
|
+
}
|
|
236
|
+
}
|
|
237
|
+
/**
|
|
238
|
+
* Recursively scrolls element ancestors to visually reveal a rect.
|
|
239
|
+
*
|
|
240
|
+
* @param options Additional configuration of the scrolling behavior.
|
|
241
|
+
* @param options.parent The first parent ancestor to start scrolling.
|
|
242
|
+
* @param options.getRect A function which returns the Rect, which is to be revealed.
|
|
243
|
+
* @param options.ancestorOffset An offset from the boundary of scrollable ancestors (if any)
|
|
244
|
+
* the `Rect` instance will be moved by if the viewport is scrolled.
|
|
245
|
+
* @param options.alignToTop When set `true`, the helper will make sure the `Rect` instance is scrolled up
|
|
246
|
+
* to the top boundary of the scrollable ancestors if scrolled up. When not set (default), the `rect`
|
|
247
|
+
* will be revealed by scrolling as little as possible. This option will not affect rects that must be
|
|
248
|
+
* scrolled down because they will appear at the top of the boundary
|
|
249
|
+
* anyway.
|
|
250
|
+
* @param options.forceScroll When set `true`, the `rect` will be aligned to the top of scrollable ancestors
|
|
251
|
+
* whether it is already visible or not. This option will only work when `alignToTop` is `true`
|
|
252
|
+
* @param options.limiterElement The outermost ancestor that should be scrolled. Defaults to the `<body>` element.
|
|
253
|
+
*/
|
|
254
|
+
function scrollAncestorsToShowRect({ parent, getRect, alignToTop, forceScroll, ancestorOffset = 0, limiterElement }) {
|
|
255
|
+
const parentWindow = getWindow(parent);
|
|
256
|
+
const forceScrollToTop = alignToTop && forceScroll;
|
|
257
|
+
let parentRect, targetRect, targetFitsInTarget;
|
|
258
|
+
const limiter = limiterElement || parentWindow.document.body;
|
|
259
|
+
while (parent != limiter) {
|
|
260
|
+
targetRect = getRect();
|
|
261
|
+
parentRect = new Rect(parent).excludeScrollbarsAndBorders();
|
|
262
|
+
targetFitsInTarget = parentRect.contains(targetRect);
|
|
263
|
+
if (forceScrollToTop) {
|
|
264
|
+
parent.scrollTop -= (parentRect.top - targetRect.top) + ancestorOffset;
|
|
265
|
+
}
|
|
266
|
+
else if (!targetFitsInTarget) {
|
|
267
|
+
if (isAbove(targetRect, parentRect)) {
|
|
268
|
+
parent.scrollTop -= parentRect.top - targetRect.top + ancestorOffset;
|
|
269
|
+
}
|
|
270
|
+
else if (isBelow(targetRect, parentRect)) {
|
|
271
|
+
if (alignToTop) {
|
|
272
|
+
parent.scrollTop += targetRect.top - parentRect.top - ancestorOffset;
|
|
273
|
+
}
|
|
274
|
+
else {
|
|
275
|
+
parent.scrollTop += targetRect.bottom - parentRect.bottom + ancestorOffset;
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
if (!targetFitsInTarget) {
|
|
280
|
+
if (isLeftOf(targetRect, parentRect)) {
|
|
281
|
+
parent.scrollLeft -= parentRect.left - targetRect.left + ancestorOffset;
|
|
282
|
+
}
|
|
283
|
+
else if (isRightOf(targetRect, parentRect)) {
|
|
284
|
+
parent.scrollLeft += targetRect.right - parentRect.right + ancestorOffset;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
parent = parent.parentNode;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
/**
|
|
291
|
+
* Determines if a given `Rect` extends beyond the bottom edge of the second `Rect`.
|
|
292
|
+
*/
|
|
293
|
+
function isBelow(firstRect, secondRect) {
|
|
294
|
+
return firstRect.bottom > secondRect.bottom;
|
|
295
|
+
}
|
|
296
|
+
/**
|
|
297
|
+
* Determines if a given `Rect` extends beyond the top edge of the second `Rect`.
|
|
298
|
+
*/
|
|
299
|
+
function isAbove(firstRect, secondRect) {
|
|
300
|
+
return firstRect.top < secondRect.top;
|
|
301
|
+
}
|
|
302
|
+
/**
|
|
303
|
+
* Determines if a given `Rect` extends beyond the left edge of the second `Rect`.
|
|
304
|
+
*/
|
|
305
|
+
function isLeftOf(firstRect, secondRect) {
|
|
306
|
+
return firstRect.left < secondRect.left;
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Determines if a given `Rect` extends beyond the right edge of the second `Rect`.
|
|
310
|
+
*/
|
|
311
|
+
function isRightOf(firstRect, secondRect) {
|
|
312
|
+
return firstRect.right > secondRect.right;
|
|
313
|
+
}
|
|
314
|
+
/**
|
|
315
|
+
* Returns the closest window of an element or range.
|
|
316
|
+
*/
|
|
317
|
+
function getWindow(elementOrRange) {
|
|
318
|
+
if (isRange(elementOrRange)) {
|
|
319
|
+
return elementOrRange.startContainer.ownerDocument.defaultView;
|
|
320
|
+
}
|
|
321
|
+
else {
|
|
322
|
+
return elementOrRange.ownerDocument.defaultView;
|
|
323
|
+
}
|
|
324
|
+
}
|
|
325
|
+
/**
|
|
326
|
+
* Returns the closest parent of an element or DOM range.
|
|
327
|
+
*/
|
|
328
|
+
function getParentElement(elementOrRange) {
|
|
329
|
+
if (isRange(elementOrRange)) {
|
|
330
|
+
let parent = elementOrRange.commonAncestorContainer;
|
|
331
|
+
// If a Range is attached to the Text, use the closest element ancestor.
|
|
332
|
+
if (isText(parent)) {
|
|
333
|
+
parent = parent.parentNode;
|
|
334
|
+
}
|
|
335
|
+
return parent;
|
|
336
|
+
}
|
|
337
|
+
else {
|
|
338
|
+
return elementOrRange.parentNode;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
/**
|
|
342
|
+
* Returns the rect of an element or range residing in an iframe.
|
|
343
|
+
* The result rect is relative to the geometry of the passed window instance.
|
|
344
|
+
*
|
|
345
|
+
* @param target Element or range which rect should be returned.
|
|
346
|
+
* @param relativeWindow A window the rect should be relative to.
|
|
347
|
+
*/
|
|
348
|
+
function getRectRelativeToWindow(target, relativeWindow) {
|
|
349
|
+
const targetWindow = getWindow(target);
|
|
350
|
+
const rect = new Rect(target);
|
|
351
|
+
if (targetWindow === relativeWindow) {
|
|
352
|
+
return rect;
|
|
353
|
+
}
|
|
354
|
+
else {
|
|
355
|
+
let currentWindow = targetWindow;
|
|
356
|
+
while (currentWindow != relativeWindow) {
|
|
357
|
+
const frame = currentWindow.frameElement;
|
|
358
|
+
const frameRect = new Rect(frame).excludeScrollbarsAndBorders();
|
|
359
|
+
rect.moveBy(frameRect.left, frameRect.top);
|
|
360
|
+
currentWindow = currentWindow.parent;
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
return rect;
|
|
364
|
+
}
|
|
365
|
+
/**
|
|
366
|
+
* A helper that explodes the `viewportOffset` configuration if defined as a plain number into an object
|
|
367
|
+
* with `top`, `bottom`, `left`, and `right` properties.
|
|
368
|
+
*
|
|
369
|
+
* If an object value is passed, this helper will pass it through.
|
|
370
|
+
*
|
|
371
|
+
* @param viewportOffset Viewport offset to be normalized.
|
|
372
|
+
*/
|
|
373
|
+
function normalizeViewportOffset(viewportOffset) {
|
|
374
|
+
if (typeof viewportOffset === 'number') {
|
|
375
|
+
return {
|
|
376
|
+
top: viewportOffset,
|
|
377
|
+
bottom: viewportOffset,
|
|
378
|
+
left: viewportOffset,
|
|
379
|
+
right: viewportOffset
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
return viewportOffset;
|
|
383
|
+
}
|