@ckeditor/ckeditor5-utils 37.0.0-alpha.2 → 37.0.0-alpha.3

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ckeditor/ckeditor5-utils",
3
- "version": "37.0.0-alpha.2",
3
+ "version": "37.0.0-alpha.3",
4
4
  "description": "Miscellaneous utilities used by CKEditor 5.",
5
5
  "keywords": [
6
6
  "ckeditor",
@@ -14,10 +14,10 @@
14
14
  "lodash-es": "^4.17.15"
15
15
  },
16
16
  "devDependencies": {
17
- "@ckeditor/ckeditor5-build-classic": "^37.0.0-alpha.2",
18
- "@ckeditor/ckeditor5-editor-classic": "^37.0.0-alpha.2",
19
- "@ckeditor/ckeditor5-core": "^37.0.0-alpha.2",
20
- "@ckeditor/ckeditor5-engine": "^37.0.0-alpha.2",
17
+ "@ckeditor/ckeditor5-build-classic": "^37.0.0-alpha.3",
18
+ "@ckeditor/ckeditor5-editor-classic": "^37.0.0-alpha.3",
19
+ "@ckeditor/ckeditor5-core": "^37.0.0-alpha.3",
20
+ "@ckeditor/ckeditor5-engine": "^37.0.0-alpha.3",
21
21
  "@types/lodash-es": "^4.17.6",
22
22
  "typescript": "^4.8.4"
23
23
  },
@@ -2,26 +2,65 @@
2
2
  * @license Copyright (c) 2003-2023, CKSource Holding sp. z o.o. All rights reserved.
3
3
  * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
4
4
  */
5
+ type IfTrue<T> = T extends true ? true : never;
5
6
  /**
6
7
  * Makes any page `HTMLElement` or `Range` (`target`) visible inside the browser viewport.
7
8
  * This helper will scroll all `target` ancestors and the web browser viewport to reveal the target to
8
9
  * the user. If the `target` is already visible, nothing will happen.
9
10
  *
10
- * @param options
11
+ * @param options Additional configuration of the scrolling behavior.
11
12
  * @param options.target A target, which supposed to become visible to the user.
12
13
  * @param options.viewportOffset An offset from the edge of the viewport (in pixels)
13
- * the `target` will be moved by when the viewport is scrolled. It enhances the user experience
14
+ * the `target` will be moved by if the viewport is scrolled. It enhances the user experience
14
15
  * by keeping the `target` some distance from the edge of the viewport and thus making it easier to
15
16
  * read or edit by the user.
17
+ * @param options.ancestorOffset An offset from the boundary of scrollable ancestors (if any)
18
+ * the `target` will be moved by if the viewport is scrolled. It enhances the user experience
19
+ * by keeping the `target` some distance from the edge of the ancestors and thus making it easier to
20
+ * read or edit by the user.
21
+ * @param options.alignToTop When set `true`, the helper will make sure the `target` is scrolled up
22
+ * to the top boundary of the viewport and/or scrollable ancestors if scrolled up. When not set
23
+ * (default), the `target` will be revealed by scrolling as little as possible. This option will
24
+ * not affect `targets` that must be scrolled down because they will appear at the top of the boundary
25
+ * anyway.
26
+ *
27
+ * ```
28
+ * scrollViewportToShowTarget() with scrollViewportToShowTarget() with
29
+ * Initial state alignToTop unset (default) alignToTop = true
30
+ *
31
+ * ┌────────────────────────────────┬─┐ ┌────────────────────────────────┬─┐ ┌────────────────────────────────┬─┐
32
+ * │ │▲│ │ │▲│ │ [ Target to be revealed ] │▲│
33
+ * │ │ │ │ │ │ │ │ │
34
+ * │ │█│ │ │ │ │ │ │
35
+ * │ │█│ │ │ │ │ │ │
36
+ * │ │ │ │ │█│ │ │ │
37
+ * │ │ │ │ │█│ │ │█│
38
+ * │ │ │ │ │ │ │ │█│
39
+ * │ │▼│ │ [ Target to be revealed ] │▼│ │ │▼│
40
+ * └────────────────────────────────┴─┘ └────────────────────────────────┴─┘ └────────────────────────────────┴─┘
41
+ *
42
+ *
43
+ * [ Target to be revealed ]
44
+ *```
45
+ *
46
+ * @param options.forceScroll When set `true`, the `target` will be aligned to the top of the viewport
47
+ * and scrollable ancestors whether it is already visible or not. This option will only work when `alignToTop`
48
+ * is `true`
16
49
  */
17
- export declare function scrollViewportToShowTarget({ target, viewportOffset }: {
50
+ export declare function scrollViewportToShowTarget<T extends boolean, U extends IfTrue<T>>({ target, viewportOffset, ancestorOffset, alignToTop, forceScroll }: {
18
51
  readonly target: HTMLElement | Range;
19
52
  readonly viewportOffset?: number;
53
+ readonly ancestorOffset?: number;
54
+ readonly alignToTop?: T;
55
+ readonly forceScroll?: U;
20
56
  }): void;
21
57
  /**
22
58
  * Makes any page `HTMLElement` or `Range` (target) visible within its scrollable ancestors,
23
59
  * e.g. if they have `overflow: scroll` CSS style.
24
60
  *
25
61
  * @param target A target, which supposed to become visible to the user.
62
+ * @param ancestorOffset An offset between the target and the boundary of scrollable ancestors
63
+ * to be maintained while scrolling.
26
64
  */
27
- export declare function scrollAncestorsToShowTarget(target: HTMLElement | Range): void;
65
+ export declare function scrollAncestorsToShowTarget(target: HTMLElement | Range, ancestorOffset?: number): void;
66
+ export {};
package/src/dom/scroll.js CHANGED
@@ -13,14 +13,46 @@ import isText from './istext';
13
13
  * This helper will scroll all `target` ancestors and the web browser viewport to reveal the target to
14
14
  * the user. If the `target` is already visible, nothing will happen.
15
15
  *
16
- * @param options
16
+ * @param options Additional configuration of the scrolling behavior.
17
17
  * @param options.target A target, which supposed to become visible to the user.
18
18
  * @param options.viewportOffset An offset from the edge of the viewport (in pixels)
19
- * the `target` will be moved by when the viewport is scrolled. It enhances the user experience
19
+ * the `target` will be moved by if the viewport is scrolled. It enhances the user experience
20
20
  * by keeping the `target` some distance from the edge of the viewport and thus making it easier to
21
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`
22
54
  */
23
- export function scrollViewportToShowTarget({ target, viewportOffset = 0 }) {
55
+ export function scrollViewportToShowTarget({ target, viewportOffset = 0, ancestorOffset = 0, alignToTop, forceScroll }) {
24
56
  const targetWindow = getWindow(target);
25
57
  let currentWindow = targetWindow;
26
58
  let currentFrame = null;
@@ -40,18 +72,30 @@ export function scrollViewportToShowTarget({ target, viewportOffset = 0 }) {
40
72
  firstAncestorToScroll = getParentElement(currentFrame);
41
73
  }
42
74
  // Scroll the target's ancestors first. Once done, scrolling the viewport is easy.
43
- scrollAncestorsToShowRect(firstAncestorToScroll, () => {
44
- // Note: If the target does not belong to the current window **directly**,
45
- // i.e. it resides in an iframe belonging to the window, obtain the target's rect
46
- // in the coordinates of the current window. By default, a Rect returns geometry
47
- // relative to the current window's viewport. To make it work in a parent window,
48
- // it must be shifted.
49
- return getRectRelativeToWindow(target, currentWindow);
75
+ scrollAncestorsToShowRect({
76
+ parent: firstAncestorToScroll,
77
+ getRect: () => {
78
+ // Note: If the target does not belong to the current window **directly**,
79
+ // i.e. it resides in an iframe belonging to the window, obtain the target's rect
80
+ // in the coordinates of the current window. By default, a Rect returns geometry
81
+ // relative to the current window's viewport. To make it work in a parent window,
82
+ // it must be shifted.
83
+ return getRectRelativeToWindow(target, currentWindow);
84
+ },
85
+ alignToTop,
86
+ ancestorOffset,
87
+ forceScroll
50
88
  });
51
89
  // Obtain the rect of the target after it has been scrolled within its ancestors.
52
90
  // It's time to scroll the viewport.
53
91
  const targetRect = getRectRelativeToWindow(target, currentWindow);
54
- scrollWindowToShowRect(currentWindow, targetRect, viewportOffset);
92
+ scrollWindowToShowRect({
93
+ window: currentWindow,
94
+ rect: targetRect,
95
+ viewportOffset,
96
+ alignToTop,
97
+ forceScroll
98
+ });
55
99
  if (currentWindow.parent != currentWindow) {
56
100
  // Keep the reference to the <iframe> element the "previous current window" was
57
101
  // rendered within. It will be useful to re–calculate the rect of the target
@@ -77,11 +121,15 @@ export function scrollViewportToShowTarget({ target, viewportOffset = 0 }) {
77
121
  * e.g. if they have `overflow: scroll` CSS style.
78
122
  *
79
123
  * @param target A target, which supposed to become visible to the user.
124
+ * @param ancestorOffset An offset between the target and the boundary of scrollable ancestors
125
+ * to be maintained while scrolling.
80
126
  */
81
- export function scrollAncestorsToShowTarget(target) {
127
+ export function scrollAncestorsToShowTarget(target, ancestorOffset) {
82
128
  const targetParent = getParentElement(target);
83
- scrollAncestorsToShowRect(targetParent, () => {
84
- return new Rect(target);
129
+ scrollAncestorsToShowRect({
130
+ parent: targetParent,
131
+ getRect: () => new Rect(target),
132
+ ancestorOffset
85
133
  });
86
134
  }
87
135
  /**
@@ -130,23 +178,45 @@ export function scrollAncestorsToShowTarget(target) {
130
178
  * +---------------------------------...
131
179
  * ```
132
180
  *
133
- * @param window A window which is scrolled to reveal the rect.
134
- * @param rect A rect which is to be revealed.
135
- * @param viewportOffset See scrollViewportToShowTarget.
181
+ * @param options Additional configuration of the scrolling behavior.
182
+ * @param options.window A window which is scrolled to reveal the rect.
183
+ * @param options.rect A rect which is to be revealed.
184
+ * @param options.viewportOffset An offset from the edge of the viewport (in pixels) the `rect` will be
185
+ * moved by if the viewport is scrolled.
186
+ * @param options.alignToTop When set `true`, the helper will make sure the `rect` is scrolled up
187
+ * to the top boundary of the viewport if scrolled up. When not set (default), the `rect` will be
188
+ * revealed by scrolling as little as possible. This option will not affect rects that must be scrolled
189
+ * down because they will appear at the top of the boundary anyway.
190
+ * @param options.forceScroll When set `true`, the `rect` will be aligned to the top of the viewport
191
+ * whether it is already visible or not. This option will only work when `alignToTop` is `true`
136
192
  */
137
- function scrollWindowToShowRect(window, rect, viewportOffset) {
193
+ function scrollWindowToShowRect({ window, rect, alignToTop, forceScroll, viewportOffset }) {
138
194
  const targetShiftedDownRect = rect.clone().moveBy(0, viewportOffset);
139
195
  const targetShiftedUpRect = rect.clone().moveBy(0, -viewportOffset);
140
196
  const viewportRect = new Rect(window).excludeScrollbarsAndBorders();
141
197
  const rects = [targetShiftedUpRect, targetShiftedDownRect];
142
- if (!rects.every(rect => viewportRect.contains(rect))) {
143
- let { scrollX, scrollY } = window;
198
+ const forceScrollToTop = alignToTop && forceScroll;
199
+ const allRectsFitInViewport = rects.every(rect => viewportRect.contains(rect));
200
+ let { scrollX, scrollY } = window;
201
+ const initialScrollX = scrollX;
202
+ const initialScrollY = scrollY;
203
+ if (forceScrollToTop) {
204
+ scrollY -= (viewportRect.top - rect.top) + viewportOffset;
205
+ }
206
+ else if (!allRectsFitInViewport) {
144
207
  if (isAbove(targetShiftedUpRect, viewportRect)) {
145
208
  scrollY -= viewportRect.top - rect.top + viewportOffset;
146
209
  }
147
210
  else if (isBelow(targetShiftedDownRect, viewportRect)) {
148
- scrollY += rect.bottom - viewportRect.bottom + viewportOffset;
211
+ if (alignToTop) {
212
+ scrollY += rect.top - viewportRect.top - viewportOffset;
213
+ }
214
+ else {
215
+ scrollY += rect.bottom - viewportRect.bottom + viewportOffset;
216
+ }
149
217
  }
218
+ }
219
+ if (!allRectsFitInViewport) {
150
220
  // TODO: Web browsers scroll natively to place the target in the middle
151
221
  // of the viewport. It's not a very popular case, though.
152
222
  if (isLeftOf(rect, viewportRect)) {
@@ -155,33 +225,57 @@ function scrollWindowToShowRect(window, rect, viewportOffset) {
155
225
  else if (isRightOf(rect, viewportRect)) {
156
226
  scrollX += rect.right - viewportRect.right + viewportOffset;
157
227
  }
228
+ }
229
+ if (scrollX != initialScrollX || scrollY !== initialScrollY) {
158
230
  window.scrollTo(scrollX, scrollY);
159
231
  }
160
232
  }
161
233
  /**
162
234
  * Recursively scrolls element ancestors to visually reveal a rect.
163
235
  *
164
- * @param parent A parent The first ancestors to start scrolling.
165
- * @param getRect A function which returns the Rect, which is to be revealed.
236
+ * @param options Additional configuration of the scrolling behavior.
237
+ * @param options.parent The first parent ancestor to start scrolling.
238
+ * @param options.getRect A function which returns the Rect, which is to be revealed.
239
+ * @param options.ancestorOffset An offset from the boundary of scrollable ancestors (if any)
240
+ * the `Rect` instance will be moved by if the viewport is scrolled.
241
+ * @param options.alignToTop When set `true`, the helper will make sure the `Rect` instance is scrolled up
242
+ * to the top boundary of the scrollable ancestors if scrolled up. When not set (default), the `rect`
243
+ * will be revealed by scrolling as little as possible. This option will not affect rects that must be
244
+ * scrolled down because they will appear at the top of the boundary
245
+ * anyway.
246
+ * @param options.forceScroll When set `true`, the `rect` will be aligned to the top of scrollable ancestors
247
+ * whether it is already visible or not. This option will only work when `alignToTop` is `true`
166
248
  */
167
- function scrollAncestorsToShowRect(parent, getRect) {
249
+ function scrollAncestorsToShowRect({ parent, getRect, alignToTop, forceScroll, ancestorOffset = 0 }) {
168
250
  const parentWindow = getWindow(parent);
169
- let parentRect, targetRect;
251
+ const forceScrollToTop = alignToTop && forceScroll;
252
+ let parentRect, targetRect, targetFitsInTarget;
170
253
  while (parent != parentWindow.document.body) {
171
254
  targetRect = getRect();
172
255
  parentRect = new Rect(parent).excludeScrollbarsAndBorders();
173
- if (!parentRect.contains(targetRect)) {
256
+ targetFitsInTarget = parentRect.contains(targetRect);
257
+ if (forceScrollToTop) {
258
+ parent.scrollTop -= (parentRect.top - targetRect.top) + ancestorOffset;
259
+ }
260
+ else if (!targetFitsInTarget) {
174
261
  if (isAbove(targetRect, parentRect)) {
175
- parent.scrollTop -= parentRect.top - targetRect.top;
262
+ parent.scrollTop -= parentRect.top - targetRect.top + ancestorOffset;
176
263
  }
177
264
  else if (isBelow(targetRect, parentRect)) {
178
- parent.scrollTop += targetRect.bottom - parentRect.bottom;
265
+ if (alignToTop) {
266
+ parent.scrollTop += targetRect.top - parentRect.top - ancestorOffset;
267
+ }
268
+ else {
269
+ parent.scrollTop += targetRect.bottom - parentRect.bottom + ancestorOffset;
270
+ }
179
271
  }
272
+ }
273
+ if (!targetFitsInTarget) {
180
274
  if (isLeftOf(targetRect, parentRect)) {
181
- parent.scrollLeft -= parentRect.left - targetRect.left;
275
+ parent.scrollLeft -= parentRect.left - targetRect.left + ancestorOffset;
182
276
  }
183
277
  else if (isRightOf(targetRect, parentRect)) {
184
- parent.scrollLeft += targetRect.right - parentRect.right;
278
+ parent.scrollLeft += targetRect.right - parentRect.right + ancestorOffset;
185
279
  }
186
280
  }
187
281
  parent = parent.parentNode;