@jupyterlab/ui-components 4.1.0-rc.0 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -114,13 +114,20 @@ export declare abstract class WindowedListModel implements WindowedList.IModel {
|
|
|
114
114
|
/**
|
|
115
115
|
* Get the scroll offset to display an item in the viewport.
|
|
116
116
|
*
|
|
117
|
-
* By default, the list will scroll as little as possible to ensure the item is fully visible
|
|
118
|
-
*
|
|
119
|
-
*
|
|
120
|
-
*
|
|
121
|
-
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
117
|
+
* By default, the list will scroll as little as possible to ensure the item is fully visible (`auto`).
|
|
118
|
+
* You can control the alignment of the item though by specifying a second alignment parameter.
|
|
119
|
+
* Acceptable values are:
|
|
120
|
+
*
|
|
121
|
+
* auto - Automatically align with the top or bottom minimising the amount scrolled,
|
|
122
|
+
* If `alignPreference` is given, follow such preferred alignment.
|
|
123
|
+
* If item is smaller than the viewport and fully visible, do not scroll at all.
|
|
124
|
+
* smart - If the item is significantly visible, don't scroll at all (regardless of whether it fits in the viewport).
|
|
125
|
+
* If the item is less than one viewport away, scroll so that it becomes fully visible (following the `auto` heuristics).
|
|
126
|
+
* If the item is more than one viewport away, scroll so that it is centered within the viewport (`center` if smaller than viewport, `top-center` otherwise).
|
|
127
|
+
* center - Align the middle of the item with the middle of the viewport (it only works well for items smaller than the viewport).
|
|
128
|
+
* top-center - Align the top of the item with the middle of the viewport (works well for items larger than the viewport).
|
|
129
|
+
* end - Align the bottom of the item to the bottom of the list.
|
|
130
|
+
* start - Align the top of item to the top of the list.
|
|
124
131
|
*
|
|
125
132
|
* An item is considered significantly visible if:
|
|
126
133
|
* - it overlaps with the viewport by the amount specified by `scrollDownThreshold` when below the viewport
|
|
@@ -129,9 +136,15 @@ export declare abstract class WindowedListModel implements WindowedList.IModel {
|
|
|
129
136
|
* @param index Item index
|
|
130
137
|
* @param align Where to align the item in the viewport
|
|
131
138
|
* @param margin The proportion of viewport to add when aligning with the top/bottom of the list.
|
|
139
|
+
* @param precomputed Precomputed values to use when windowing is disabled.
|
|
140
|
+
* @param alignPreference Allows to override the alignment of item when the `auto` heuristic decides that the item needs to be scrolled into view.
|
|
132
141
|
* @returns The needed scroll offset
|
|
133
142
|
*/
|
|
134
|
-
getOffsetForIndexAndAlignment(index: number, align?: WindowedList.ScrollToAlign, margin?: number
|
|
143
|
+
getOffsetForIndexAndAlignment(index: number, align?: WindowedList.ScrollToAlign, margin?: number, precomputed?: {
|
|
144
|
+
totalSize: number;
|
|
145
|
+
itemMetadata: WindowedList.ItemMetadata;
|
|
146
|
+
currentOffset: number;
|
|
147
|
+
}, alignPreference?: WindowedList.BaseScrollToAlignment): number;
|
|
135
148
|
/**
|
|
136
149
|
* Compute the items range to display.
|
|
137
150
|
*
|
|
@@ -269,19 +282,27 @@ export declare class WindowedList<T extends WindowedList.IModel = WindowedList.I
|
|
|
269
282
|
/**
|
|
270
283
|
* Scroll to the specified item.
|
|
271
284
|
*
|
|
272
|
-
* By default, the list will scroll as little as possible to ensure the item is visible
|
|
273
|
-
*
|
|
274
|
-
*
|
|
275
|
-
*
|
|
276
|
-
*
|
|
277
|
-
*
|
|
278
|
-
*
|
|
285
|
+
* By default, the list will scroll as little as possible to ensure the item is fully visible (`auto`).
|
|
286
|
+
* You can control the alignment of the item though by specifying a second alignment parameter.
|
|
287
|
+
* Acceptable values are:
|
|
288
|
+
*
|
|
289
|
+
* auto - Automatically align with the top or bottom minimising the amount scrolled,
|
|
290
|
+
* If `alignPreference` is given, follow such preferred alignment.
|
|
291
|
+
* If item is smaller than the viewport and fully visible, do not scroll at all.
|
|
292
|
+
* smart - If the item is significantly visible, don't scroll at all (regardless of whether it fits in the viewport).
|
|
293
|
+
* If the item is less than one viewport away, scroll so that it becomes fully visible (following the `auto` heuristics).
|
|
294
|
+
* If the item is more than one viewport away, scroll so that it is centered within the viewport (`center` if smaller than viewport, `top-center` otherwise).
|
|
295
|
+
* center - Align the middle of the item with the middle of the viewport (it only works well for items smaller than the viewport).
|
|
296
|
+
* top-center - Align the top of the item with the middle of the viewport (works well for items larger than the viewport).
|
|
297
|
+
* end - Align the bottom of the item to the bottom of the list.
|
|
298
|
+
* start - Align the top of item to the top of the list.
|
|
279
299
|
*
|
|
280
300
|
* @param index Item index to scroll to
|
|
281
301
|
* @param align Type of alignment
|
|
282
302
|
* @param margin In 'smart' mode the viewport proportion to add
|
|
303
|
+
* @param alignPreference Allows to override the alignment of item when the `auto` heuristic decides that the item needs to be scrolled into view.
|
|
283
304
|
*/
|
|
284
|
-
scrollToItem(index: number, align?: WindowedList.ScrollToAlign, margin?: number): Promise<void>;
|
|
305
|
+
scrollToItem(index: number, align?: WindowedList.ScrollToAlign, margin?: number, alignPreference?: WindowedList.BaseScrollToAlignment): Promise<void>;
|
|
285
306
|
/**
|
|
286
307
|
* A message handler invoked on an `'after-attach'` message.
|
|
287
308
|
*/
|
|
@@ -327,6 +348,10 @@ export declare class WindowedList<T extends WindowedList.IModel = WindowedList.I
|
|
|
327
348
|
* Turn off windowing related styles in the viewport.
|
|
328
349
|
*/
|
|
329
350
|
private _applyNoWindowingStyles;
|
|
351
|
+
/**
|
|
352
|
+
* Turn on windowing related styles in the viewport.
|
|
353
|
+
*/
|
|
354
|
+
private _applyWindowingStyles;
|
|
330
355
|
/**
|
|
331
356
|
* Remove listeners for viewport and contents (but not the virtual scrollbar).
|
|
332
357
|
*/
|
|
@@ -336,9 +361,32 @@ export declare class WindowedList<T extends WindowedList.IModel = WindowedList.I
|
|
|
336
361
|
*/
|
|
337
362
|
private _update;
|
|
338
363
|
/**
|
|
339
|
-
* Handle viewport
|
|
364
|
+
* Handle viewport area resize.
|
|
340
365
|
*/
|
|
341
|
-
private
|
|
366
|
+
private _onAreaResize;
|
|
367
|
+
/**
|
|
368
|
+
* Handle viewport content (i.e. items) resize.
|
|
369
|
+
*/
|
|
370
|
+
private _onItemResize;
|
|
371
|
+
/**
|
|
372
|
+
* Scroll to the item which was most recently requested.
|
|
373
|
+
*
|
|
374
|
+
* This method ensures that the app scrolls to the item even if a resize event
|
|
375
|
+
* occurs shortly after the scroll. Consider the following sequence of events:
|
|
376
|
+
*
|
|
377
|
+
* 1. User is at the nth cell, presses Shift+Enter (run current cell and
|
|
378
|
+
* advance to next)
|
|
379
|
+
* 2. App scrolls to the next (n+1) cell
|
|
380
|
+
* 3. The nth cell finishes running and renders the output, pushing the
|
|
381
|
+
* (n+1) cell down out of view
|
|
382
|
+
* 4. This triggers the resize observer, which calls this method and scrolls
|
|
383
|
+
* the (n+1) cell back into view
|
|
384
|
+
*
|
|
385
|
+
* On implementation level, this is ensured by scrolling to `this._scrollToItem`
|
|
386
|
+
* which is cleared after a short timeout once the scrolling settles
|
|
387
|
+
* (see `this._resetScrollToItem()`).
|
|
388
|
+
*/
|
|
389
|
+
private _scrollBackToItemOnResize;
|
|
342
390
|
/**
|
|
343
391
|
* Clear any outstanding timeout and enqueue scrolling to a new item.
|
|
344
392
|
*/
|
|
@@ -357,7 +405,8 @@ export declare class WindowedList<T extends WindowedList.IModel = WindowedList.I
|
|
|
357
405
|
private _needsUpdate;
|
|
358
406
|
private _outerElement;
|
|
359
407
|
private _resetScrollToItemTimeout;
|
|
360
|
-
private
|
|
408
|
+
private _areaResizeObserver;
|
|
409
|
+
private _itemsResizeObserver;
|
|
361
410
|
private _scrollbarElement;
|
|
362
411
|
private _scrollbarResizeObserver;
|
|
363
412
|
private _scrollRepaint;
|
|
@@ -504,20 +553,33 @@ export declare namespace WindowedList {
|
|
|
504
553
|
/**
|
|
505
554
|
* Get the scroll offset to display an item in the viewport.
|
|
506
555
|
*
|
|
507
|
-
* By default, the list will scroll as little as possible to ensure the item is visible
|
|
556
|
+
* By default, the list will scroll as little as possible to ensure the item is fully visible (`auto`).
|
|
557
|
+
* You can control the alignment of the item though by specifying a second alignment parameter.
|
|
558
|
+
* Acceptable values are:
|
|
508
559
|
*
|
|
509
|
-
* auto
|
|
510
|
-
*
|
|
511
|
-
*
|
|
512
|
-
*
|
|
513
|
-
*
|
|
560
|
+
* auto - Automatically align with the top or bottom minimising the amount scrolled,
|
|
561
|
+
* If `alignPreference` is given, follow such preferred alignment.
|
|
562
|
+
* If item is smaller than the viewport and fully visible, do not scroll at all.
|
|
563
|
+
* smart - If the item is significantly visible, don't scroll at all (regardless of whether it fits in the viewport).
|
|
564
|
+
* If the item is less than one viewport away, scroll so that it becomes fully visible (following the `auto` heuristics).
|
|
565
|
+
* If the item is more than one viewport away, scroll so that it is centered within the viewport (`center` if smaller than viewport, `top-center` otherwise).
|
|
566
|
+
* center - Align the middle of the item with the middle of the viewport (it only works well for items smaller than the viewport).
|
|
567
|
+
* top-center - Align the top of the item with the middle of the viewport (works well for items larger than the viewport).
|
|
568
|
+
* end - Align the bottom of the item to the bottom of the list.
|
|
569
|
+
* start - Align the top of item to the top of the list.
|
|
514
570
|
*
|
|
515
571
|
* @param index Item index
|
|
516
572
|
* @param align Where to align the item in the viewport
|
|
517
573
|
* @param margin In 'smart' mode the viewport proportion to add
|
|
574
|
+
* @param precomputed Precomputed values to use when windowing is disabled.
|
|
575
|
+
* @param alignPreference Allows to override the alignment of item when the `auto` heuristic decides that the item needs to be scrolled into view.
|
|
518
576
|
* @returns The needed scroll offset
|
|
519
577
|
*/
|
|
520
|
-
getOffsetForIndexAndAlignment(index: number, align?: ScrollToAlign, margin?: number
|
|
578
|
+
getOffsetForIndexAndAlignment(index: number, align?: ScrollToAlign, margin?: number, precomputed?: {
|
|
579
|
+
totalSize: number;
|
|
580
|
+
itemMetadata: WindowedList.ItemMetadata;
|
|
581
|
+
currentOffset: number;
|
|
582
|
+
}, alignPreference?: WindowedList.BaseScrollToAlignment): number;
|
|
521
583
|
/**
|
|
522
584
|
* Compute the items range to display.
|
|
523
585
|
*
|
|
@@ -691,9 +753,13 @@ export declare namespace WindowedList {
|
|
|
691
753
|
measured?: boolean;
|
|
692
754
|
};
|
|
693
755
|
/**
|
|
694
|
-
*
|
|
756
|
+
* Basic type of scroll alignment
|
|
757
|
+
*/
|
|
758
|
+
type BaseScrollToAlignment = 'center' | 'top-center' | 'start' | 'end';
|
|
759
|
+
/**
|
|
760
|
+
* Type of scroll alignment including `auto` and `smart`
|
|
695
761
|
*/
|
|
696
|
-
type ScrollToAlign = 'auto' | 'smart' |
|
|
762
|
+
type ScrollToAlign = 'auto' | 'smart' | BaseScrollToAlignment;
|
|
697
763
|
/**
|
|
698
764
|
* Widget range in view port
|
|
699
765
|
*/
|
|
@@ -9,11 +9,12 @@ import { Throttler } from '@lumino/polling';
|
|
|
9
9
|
import { Signal } from '@lumino/signaling';
|
|
10
10
|
import { PanelLayout, Widget } from '@lumino/widgets';
|
|
11
11
|
/**
|
|
12
|
-
*
|
|
12
|
+
* For how long after the scroll request should the target position
|
|
13
|
+
* be corrected to account for resize of other widgets?
|
|
13
14
|
*
|
|
14
|
-
*
|
|
15
|
+
* The time is given in milliseconds.
|
|
15
16
|
*/
|
|
16
|
-
const MAXIMUM_TIME_REMAINING =
|
|
17
|
+
const MAXIMUM_TIME_REMAINING = 100;
|
|
17
18
|
/*
|
|
18
19
|
* Feature detection
|
|
19
20
|
*
|
|
@@ -238,13 +239,20 @@ export class WindowedListModel {
|
|
|
238
239
|
/**
|
|
239
240
|
* Get the scroll offset to display an item in the viewport.
|
|
240
241
|
*
|
|
241
|
-
* By default, the list will scroll as little as possible to ensure the item is fully visible
|
|
242
|
+
* By default, the list will scroll as little as possible to ensure the item is fully visible (`auto`).
|
|
243
|
+
* You can control the alignment of the item though by specifying a second alignment parameter.
|
|
244
|
+
* Acceptable values are:
|
|
242
245
|
*
|
|
243
|
-
* auto
|
|
244
|
-
*
|
|
245
|
-
*
|
|
246
|
-
*
|
|
247
|
-
*
|
|
246
|
+
* auto - Automatically align with the top or bottom minimising the amount scrolled,
|
|
247
|
+
* If `alignPreference` is given, follow such preferred alignment.
|
|
248
|
+
* If item is smaller than the viewport and fully visible, do not scroll at all.
|
|
249
|
+
* smart - If the item is significantly visible, don't scroll at all (regardless of whether it fits in the viewport).
|
|
250
|
+
* If the item is less than one viewport away, scroll so that it becomes fully visible (following the `auto` heuristics).
|
|
251
|
+
* If the item is more than one viewport away, scroll so that it is centered within the viewport (`center` if smaller than viewport, `top-center` otherwise).
|
|
252
|
+
* center - Align the middle of the item with the middle of the viewport (it only works well for items smaller than the viewport).
|
|
253
|
+
* top-center - Align the top of the item with the middle of the viewport (works well for items larger than the viewport).
|
|
254
|
+
* end - Align the bottom of the item to the bottom of the list.
|
|
255
|
+
* start - Align the top of item to the top of the list.
|
|
248
256
|
*
|
|
249
257
|
* An item is considered significantly visible if:
|
|
250
258
|
* - it overlaps with the viewport by the amount specified by `scrollDownThreshold` when below the viewport
|
|
@@ -253,49 +261,62 @@ export class WindowedListModel {
|
|
|
253
261
|
* @param index Item index
|
|
254
262
|
* @param align Where to align the item in the viewport
|
|
255
263
|
* @param margin The proportion of viewport to add when aligning with the top/bottom of the list.
|
|
264
|
+
* @param precomputed Precomputed values to use when windowing is disabled.
|
|
265
|
+
* @param alignPreference Allows to override the alignment of item when the `auto` heuristic decides that the item needs to be scrolled into view.
|
|
256
266
|
* @returns The needed scroll offset
|
|
257
267
|
*/
|
|
258
|
-
getOffsetForIndexAndAlignment(index, align = 'auto', margin = 0) {
|
|
268
|
+
getOffsetForIndexAndAlignment(index, align = 'auto', margin = 0, precomputed, alignPreference) {
|
|
259
269
|
const boundedMargin = Math.min(Math.max(0.0, margin), 1.0);
|
|
260
270
|
const size = this._height;
|
|
261
|
-
const itemMetadata =
|
|
271
|
+
const itemMetadata = precomputed
|
|
272
|
+
? precomputed.itemMetadata
|
|
273
|
+
: this._getItemMetadata(index);
|
|
262
274
|
const scrollDownThreshold = this.scrollDownThreshold <= 1
|
|
263
275
|
? itemMetadata.size * this.scrollDownThreshold
|
|
264
276
|
: this.scrollDownThreshold;
|
|
265
277
|
const scrollUpThreshold = this.scrollUpThreshold <= 1
|
|
266
278
|
? itemMetadata.size * this.scrollUpThreshold
|
|
267
279
|
: this.scrollUpThreshold;
|
|
268
|
-
//
|
|
269
|
-
//
|
|
270
|
-
|
|
280
|
+
// When pre-computed values are not available (we are in windowing mode),
|
|
281
|
+
// `getEstimatedTotalSize` is called after ItemMetadata is computed
|
|
282
|
+
// to ensure it reflects actual measurements instead of just estimates.
|
|
283
|
+
const estimatedTotalSize = precomputed
|
|
284
|
+
? precomputed.totalSize
|
|
285
|
+
: this.getEstimatedTotalSize();
|
|
271
286
|
const topOffset = Math.max(0, Math.min(estimatedTotalSize - size, itemMetadata.offset));
|
|
272
287
|
const bottomOffset = Math.max(0, itemMetadata.offset - size + itemMetadata.size);
|
|
273
|
-
|
|
288
|
+
// Current offset (+/- padding) is the top edge of the viewport.
|
|
289
|
+
const currentOffset = precomputed
|
|
290
|
+
? precomputed.currentOffset
|
|
291
|
+
: this._scrollOffset;
|
|
292
|
+
const viewportPadding = this._windowingActive ? this.paddingTop : 0;
|
|
274
293
|
const itemTop = itemMetadata.offset;
|
|
275
294
|
const itemBottom = itemMetadata.offset + itemMetadata.size;
|
|
276
|
-
const bottomEdge = currentOffset -
|
|
277
|
-
const topEdge = currentOffset -
|
|
295
|
+
const bottomEdge = currentOffset - viewportPadding + size;
|
|
296
|
+
const topEdge = currentOffset - viewportPadding;
|
|
278
297
|
const crossingBottomEdge = bottomEdge > itemTop && bottomEdge < itemBottom;
|
|
279
298
|
const crossingTopEdge = topEdge > itemTop && topEdge < itemBottom;
|
|
299
|
+
const isFullyWithinViewport = bottomEdge > itemBottom && topEdge < itemTop;
|
|
280
300
|
if (align === 'smart') {
|
|
281
301
|
const edgeLessThanOneViewportAway = currentOffset >= bottomOffset - size &&
|
|
282
302
|
currentOffset <= topOffset + size;
|
|
283
303
|
const visiblePartBottom = bottomEdge - itemTop;
|
|
284
304
|
const hiddenPartTop = topEdge - itemTop;
|
|
285
|
-
if (
|
|
305
|
+
if (isFullyWithinViewport ||
|
|
306
|
+
(crossingBottomEdge && visiblePartBottom >= scrollDownThreshold) ||
|
|
286
307
|
(crossingTopEdge && hiddenPartTop < scrollUpThreshold)) {
|
|
287
|
-
return
|
|
308
|
+
return currentOffset;
|
|
288
309
|
}
|
|
289
310
|
else if (edgeLessThanOneViewportAway) {
|
|
290
311
|
// Possibly less than one viewport away, scroll so that it becomes visible (including the margin)
|
|
291
312
|
align = 'auto';
|
|
292
313
|
}
|
|
293
314
|
else {
|
|
294
|
-
// More than one viewport away, scroll so that it is centered within the list
|
|
295
|
-
//
|
|
296
|
-
// align with the
|
|
315
|
+
// More than one viewport away, scroll so that it is centered within the list:
|
|
316
|
+
// - if the item is smaller than viewport align the middle of the item with the middle of the viewport
|
|
317
|
+
// - if the item is larger than viewport align the top of the item with the middle of the viewport
|
|
297
318
|
if (itemMetadata.size > size) {
|
|
298
|
-
align = '
|
|
319
|
+
align = 'top-center';
|
|
299
320
|
}
|
|
300
321
|
else {
|
|
301
322
|
align = 'center';
|
|
@@ -303,9 +324,12 @@ export class WindowedListModel {
|
|
|
303
324
|
}
|
|
304
325
|
}
|
|
305
326
|
if (align === 'auto') {
|
|
306
|
-
if (
|
|
327
|
+
if (isFullyWithinViewport) {
|
|
307
328
|
// No need to change the position, return the current offset.
|
|
308
|
-
return
|
|
329
|
+
return currentOffset;
|
|
330
|
+
}
|
|
331
|
+
else if (alignPreference !== undefined) {
|
|
332
|
+
align = alignPreference;
|
|
309
333
|
}
|
|
310
334
|
else if (crossingBottomEdge || bottomEdge <= itemBottom) {
|
|
311
335
|
align = 'end';
|
|
@@ -316,13 +340,17 @@ export class WindowedListModel {
|
|
|
316
340
|
}
|
|
317
341
|
switch (align) {
|
|
318
342
|
case 'start':
|
|
319
|
-
// Align to the top edge.
|
|
320
|
-
return Math.max(0, topOffset - boundedMargin * size) +
|
|
343
|
+
// Align item top to the top edge.
|
|
344
|
+
return Math.max(0, topOffset - boundedMargin * size) + viewportPadding;
|
|
321
345
|
case 'end':
|
|
322
|
-
// Align to the bottom edge.
|
|
323
|
-
return bottomOffset + boundedMargin * size +
|
|
346
|
+
// Align item bottom to the bottom edge.
|
|
347
|
+
return bottomOffset + boundedMargin * size + viewportPadding;
|
|
324
348
|
case 'center':
|
|
325
|
-
|
|
349
|
+
// Align item centre to the middle of the viewport
|
|
350
|
+
return bottomOffset + (topOffset - bottomOffset) / 2;
|
|
351
|
+
case 'top-center':
|
|
352
|
+
// Align item top to the middle of the viewport
|
|
353
|
+
return topOffset - size / 2;
|
|
326
354
|
}
|
|
327
355
|
}
|
|
328
356
|
/**
|
|
@@ -589,7 +617,7 @@ export class WindowedList extends Widget {
|
|
|
589
617
|
this._innerElement = innerElement;
|
|
590
618
|
this._isScrolling = null;
|
|
591
619
|
this._outerElement = outerElement;
|
|
592
|
-
this.
|
|
620
|
+
this._itemsResizeObserver = null;
|
|
593
621
|
this._scrollbarElement = scrollbarElement;
|
|
594
622
|
this._scrollToItem = null;
|
|
595
623
|
this._scrollRepaint = null;
|
|
@@ -687,7 +715,7 @@ export class WindowedList extends Widget {
|
|
|
687
715
|
*/
|
|
688
716
|
scrollTo(scrollOffset) {
|
|
689
717
|
if (!this.viewModel.windowingActive) {
|
|
690
|
-
this.
|
|
718
|
+
this._outerElement.scrollTo({ top: scrollOffset });
|
|
691
719
|
return;
|
|
692
720
|
}
|
|
693
721
|
scrollOffset = Math.max(0, scrollOffset);
|
|
@@ -700,30 +728,27 @@ export class WindowedList extends Widget {
|
|
|
700
728
|
/**
|
|
701
729
|
* Scroll to the specified item.
|
|
702
730
|
*
|
|
703
|
-
* By default, the list will scroll as little as possible to ensure the item is visible
|
|
731
|
+
* By default, the list will scroll as little as possible to ensure the item is fully visible (`auto`).
|
|
732
|
+
* You can control the alignment of the item though by specifying a second alignment parameter.
|
|
733
|
+
* Acceptable values are:
|
|
704
734
|
*
|
|
705
|
-
* auto
|
|
706
|
-
*
|
|
707
|
-
*
|
|
708
|
-
*
|
|
709
|
-
*
|
|
735
|
+
* auto - Automatically align with the top or bottom minimising the amount scrolled,
|
|
736
|
+
* If `alignPreference` is given, follow such preferred alignment.
|
|
737
|
+
* If item is smaller than the viewport and fully visible, do not scroll at all.
|
|
738
|
+
* smart - If the item is significantly visible, don't scroll at all (regardless of whether it fits in the viewport).
|
|
739
|
+
* If the item is less than one viewport away, scroll so that it becomes fully visible (following the `auto` heuristics).
|
|
740
|
+
* If the item is more than one viewport away, scroll so that it is centered within the viewport (`center` if smaller than viewport, `top-center` otherwise).
|
|
741
|
+
* center - Align the middle of the item with the middle of the viewport (it only works well for items smaller than the viewport).
|
|
742
|
+
* top-center - Align the top of the item with the middle of the viewport (works well for items larger than the viewport).
|
|
743
|
+
* end - Align the bottom of the item to the bottom of the list.
|
|
744
|
+
* start - Align the top of item to the top of the list.
|
|
710
745
|
*
|
|
711
746
|
* @param index Item index to scroll to
|
|
712
747
|
* @param align Type of alignment
|
|
713
748
|
* @param margin In 'smart' mode the viewport proportion to add
|
|
749
|
+
* @param alignPreference Allows to override the alignment of item when the `auto` heuristic decides that the item needs to be scrolled into view.
|
|
714
750
|
*/
|
|
715
|
-
scrollToItem(index, align = 'auto', margin = 0.25) {
|
|
716
|
-
if (!this.viewModel.windowingActive) {
|
|
717
|
-
const widget = this.layout.widgets[index];
|
|
718
|
-
if (widget === null || widget === void 0 ? void 0 : widget.node.scrollIntoView) {
|
|
719
|
-
widget.node.scrollIntoView({
|
|
720
|
-
block: ['auto', 'smart'].includes(align)
|
|
721
|
-
? 'nearest'
|
|
722
|
-
: align
|
|
723
|
-
});
|
|
724
|
-
}
|
|
725
|
-
return Promise.resolve();
|
|
726
|
-
}
|
|
751
|
+
scrollToItem(index, align = 'auto', margin = 0.25, alignPreference) {
|
|
727
752
|
if (!this._isScrolling ||
|
|
728
753
|
this._scrollToItem === null ||
|
|
729
754
|
this._scrollToItem[0] !== index ||
|
|
@@ -732,10 +757,30 @@ export class WindowedList extends Widget {
|
|
|
732
757
|
this._isScrolling.reject('Scrolling to a new item is requested.');
|
|
733
758
|
}
|
|
734
759
|
this._isScrolling = new PromiseDelegate();
|
|
760
|
+
// Catch the internal reject, as otherwise this will
|
|
761
|
+
// result in an unhandled promise rejection in test.
|
|
762
|
+
this._isScrolling.promise.catch(console.debug);
|
|
735
763
|
}
|
|
736
|
-
this._scrollToItem = [index, align];
|
|
764
|
+
this._scrollToItem = [index, align, margin, alignPreference];
|
|
737
765
|
this._resetScrollToItem();
|
|
738
|
-
|
|
766
|
+
let precomputed = undefined;
|
|
767
|
+
if (!this.viewModel.windowingActive) {
|
|
768
|
+
const item = this._innerElement.querySelector(`[data-windowed-list-index="${index}"]`);
|
|
769
|
+
if (!item || !(item instanceof HTMLElement)) {
|
|
770
|
+
// Note: this can happen when scroll is requested when a cell is getting added
|
|
771
|
+
console.debug(`Element with index ${index} not found`);
|
|
772
|
+
return Promise.resolve();
|
|
773
|
+
}
|
|
774
|
+
precomputed = {
|
|
775
|
+
totalSize: this._outerElement.scrollHeight,
|
|
776
|
+
itemMetadata: {
|
|
777
|
+
offset: item.offsetTop,
|
|
778
|
+
size: item.clientHeight
|
|
779
|
+
},
|
|
780
|
+
currentOffset: this._outerElement.scrollTop
|
|
781
|
+
};
|
|
782
|
+
}
|
|
783
|
+
this.scrollTo(this.viewModel.getOffsetForIndexAndAlignment(Math.max(0, Math.min(index, this.viewModel.widgetCount - 1)), align, margin, precomputed, alignPreference));
|
|
739
784
|
return this._isScrolling.promise;
|
|
740
785
|
}
|
|
741
786
|
/**
|
|
@@ -744,13 +789,14 @@ export class WindowedList extends Widget {
|
|
|
744
789
|
onAfterAttach(msg) {
|
|
745
790
|
super.onAfterAttach(msg);
|
|
746
791
|
if (this.viewModel.windowingActive) {
|
|
747
|
-
this.
|
|
792
|
+
this._applyWindowingStyles();
|
|
748
793
|
}
|
|
749
794
|
else {
|
|
750
795
|
this._applyNoWindowingStyles();
|
|
751
796
|
}
|
|
797
|
+
this._addListeners();
|
|
752
798
|
this.viewModel.height = this.node.getBoundingClientRect().height;
|
|
753
|
-
const style = window.getComputedStyle(this.
|
|
799
|
+
const style = window.getComputedStyle(this._viewport);
|
|
754
800
|
this.viewModel.paddingTop = parseFloat(style.paddingTop);
|
|
755
801
|
this._scrollbarElement.addEventListener('pointerdown', this);
|
|
756
802
|
}
|
|
@@ -758,9 +804,7 @@ export class WindowedList extends Widget {
|
|
|
758
804
|
* A message handler invoked on an `'before-detach'` message.
|
|
759
805
|
*/
|
|
760
806
|
onBeforeDetach(msg) {
|
|
761
|
-
|
|
762
|
-
this._removeListeners();
|
|
763
|
-
}
|
|
807
|
+
this._removeListeners();
|
|
764
808
|
this._scrollbarElement.removeEventListener('pointerdown', this);
|
|
765
809
|
super.onBeforeDetach(msg);
|
|
766
810
|
}
|
|
@@ -810,14 +854,17 @@ export class WindowedList extends Widget {
|
|
|
810
854
|
onStateChanged(model, changes) {
|
|
811
855
|
switch (changes.name) {
|
|
812
856
|
case 'windowingActive':
|
|
857
|
+
this._removeListeners();
|
|
813
858
|
if (this.viewModel.windowingActive) {
|
|
814
|
-
this.
|
|
859
|
+
this._applyWindowingStyles();
|
|
815
860
|
this.onScroll({ currentTarget: this.node });
|
|
861
|
+
this._addListeners();
|
|
816
862
|
// Bail as onScroll will trigger update
|
|
817
863
|
return;
|
|
818
864
|
}
|
|
819
865
|
else {
|
|
820
|
-
this.
|
|
866
|
+
this._applyNoWindowingStyles();
|
|
867
|
+
this._addListeners();
|
|
821
868
|
}
|
|
822
869
|
break;
|
|
823
870
|
}
|
|
@@ -895,18 +942,25 @@ export class WindowedList extends Widget {
|
|
|
895
942
|
* Add listeners for viewport, contents and the virtual scrollbar.
|
|
896
943
|
*/
|
|
897
944
|
_addListeners() {
|
|
898
|
-
if (
|
|
899
|
-
|
|
945
|
+
if (this.viewModel.windowingActive) {
|
|
946
|
+
if (!this._itemsResizeObserver) {
|
|
947
|
+
this._itemsResizeObserver = new ResizeObserver(this._onItemResize.bind(this));
|
|
948
|
+
}
|
|
949
|
+
for (const widget of this.layout.widgets) {
|
|
950
|
+
this._itemsResizeObserver.observe(widget.node);
|
|
951
|
+
widget.disposed.connect(() => { var _a; return (_a = this._itemsResizeObserver) === null || _a === void 0 ? void 0 : _a.unobserve(widget.node); });
|
|
952
|
+
}
|
|
953
|
+
this._outerElement.addEventListener('scroll', this, passiveIfSupported);
|
|
954
|
+
this._scrollbarResizeObserver = new ResizeObserver(this._adjustDimensionsForScrollbar.bind(this));
|
|
955
|
+
this._scrollbarResizeObserver.observe(this._outerElement);
|
|
956
|
+
this._scrollbarResizeObserver.observe(this._scrollbarElement);
|
|
900
957
|
}
|
|
901
|
-
|
|
902
|
-
this.
|
|
903
|
-
|
|
958
|
+
else {
|
|
959
|
+
if (!this._areaResizeObserver) {
|
|
960
|
+
this._areaResizeObserver = new ResizeObserver(this._onAreaResize.bind(this));
|
|
961
|
+
this._areaResizeObserver.observe(this._innerElement);
|
|
962
|
+
}
|
|
904
963
|
}
|
|
905
|
-
this._outerElement.addEventListener('scroll', this, passiveIfSupported);
|
|
906
|
-
this._viewport.style.position = 'absolute';
|
|
907
|
-
this._scrollbarResizeObserver = new ResizeObserver(this._adjustDimensionsForScrollbar.bind(this));
|
|
908
|
-
this._scrollbarResizeObserver.observe(this._outerElement);
|
|
909
|
-
this._scrollbarResizeObserver.observe(this._scrollbarElement);
|
|
910
964
|
}
|
|
911
965
|
/**
|
|
912
966
|
* Turn off windowing related styles in the viewport.
|
|
@@ -915,17 +969,24 @@ export class WindowedList extends Widget {
|
|
|
915
969
|
this._viewport.style.position = 'relative';
|
|
916
970
|
this._viewport.style.top = '0px';
|
|
917
971
|
}
|
|
972
|
+
/**
|
|
973
|
+
* Turn on windowing related styles in the viewport.
|
|
974
|
+
*/
|
|
975
|
+
_applyWindowingStyles() {
|
|
976
|
+
this._viewport.style.position = 'absolute';
|
|
977
|
+
}
|
|
918
978
|
/**
|
|
919
979
|
* Remove listeners for viewport and contents (but not the virtual scrollbar).
|
|
920
980
|
*/
|
|
921
981
|
_removeListeners() {
|
|
922
|
-
var _a, _b;
|
|
982
|
+
var _a, _b, _c;
|
|
923
983
|
this._outerElement.removeEventListener('scroll', this);
|
|
924
|
-
(_a = this.
|
|
925
|
-
this.
|
|
926
|
-
(_b = this.
|
|
984
|
+
(_a = this._areaResizeObserver) === null || _a === void 0 ? void 0 : _a.disconnect();
|
|
985
|
+
this._areaResizeObserver = null;
|
|
986
|
+
(_b = this._itemsResizeObserver) === null || _b === void 0 ? void 0 : _b.disconnect();
|
|
987
|
+
this._itemsResizeObserver = null;
|
|
988
|
+
(_c = this._scrollbarResizeObserver) === null || _c === void 0 ? void 0 : _c.disconnect();
|
|
927
989
|
this._scrollbarResizeObserver = null;
|
|
928
|
-
this._applyNoWindowingStyles();
|
|
929
990
|
}
|
|
930
991
|
/**
|
|
931
992
|
* Update viewport and DOM state.
|
|
@@ -950,15 +1011,15 @@ export class WindowedList extends Widget {
|
|
|
950
1011
|
// Remove not needed widgets
|
|
951
1012
|
for (let itemIdx = nWidgets - 1; itemIdx >= 0; itemIdx--) {
|
|
952
1013
|
if (!toAdd.includes(this.layout.widgets[itemIdx])) {
|
|
953
|
-
(_a = this.
|
|
1014
|
+
(_a = this._itemsResizeObserver) === null || _a === void 0 ? void 0 : _a.unobserve(this.layout.widgets[itemIdx].node);
|
|
954
1015
|
this.layout.removeWidget(this.layout.widgets[itemIdx]);
|
|
955
1016
|
}
|
|
956
1017
|
}
|
|
957
1018
|
for (let index = 0; index < toAdd.length; index++) {
|
|
958
1019
|
const item = toAdd[index];
|
|
959
|
-
if (this.
|
|
960
|
-
this.
|
|
961
|
-
item.disposed.connect(() => { var _a; return (_a = this.
|
|
1020
|
+
if (this._itemsResizeObserver && !this.layout.widgets.includes(item)) {
|
|
1021
|
+
this._itemsResizeObserver.observe(item.node);
|
|
1022
|
+
item.disposed.connect(() => { var _a; return (_a = this._itemsResizeObserver) === null || _a === void 0 ? void 0 : _a.unobserve(item.node); });
|
|
962
1023
|
}
|
|
963
1024
|
// The widget may have moved due to drag-and-drop
|
|
964
1025
|
this.layout.insertWidget(index, item);
|
|
@@ -1001,9 +1062,15 @@ export class WindowedList extends Widget {
|
|
|
1001
1062
|
}
|
|
1002
1063
|
}
|
|
1003
1064
|
/**
|
|
1004
|
-
* Handle viewport
|
|
1065
|
+
* Handle viewport area resize.
|
|
1005
1066
|
*/
|
|
1006
|
-
|
|
1067
|
+
_onAreaResize(_entries) {
|
|
1068
|
+
this._scrollBackToItemOnResize();
|
|
1069
|
+
}
|
|
1070
|
+
/**
|
|
1071
|
+
* Handle viewport content (i.e. items) resize.
|
|
1072
|
+
*/
|
|
1073
|
+
_onItemResize(entries) {
|
|
1007
1074
|
this._resetScrollToItem();
|
|
1008
1075
|
if (this.isHidden || this.isParentHidden) {
|
|
1009
1076
|
return;
|
|
@@ -1022,15 +1089,37 @@ export class WindowedList extends Widget {
|
|
|
1022
1089
|
}
|
|
1023
1090
|
// If some sizes changed
|
|
1024
1091
|
if (this.viewModel.setWidgetSize(newSizes)) {
|
|
1092
|
+
this._scrollBackToItemOnResize();
|
|
1025
1093
|
// Update the list
|
|
1026
|
-
if (this._scrollToItem) {
|
|
1027
|
-
this.scrollToItem(...this._scrollToItem).catch(reason => {
|
|
1028
|
-
console.log(reason);
|
|
1029
|
-
});
|
|
1030
|
-
}
|
|
1031
1094
|
this.update();
|
|
1032
1095
|
}
|
|
1033
1096
|
}
|
|
1097
|
+
/**
|
|
1098
|
+
* Scroll to the item which was most recently requested.
|
|
1099
|
+
*
|
|
1100
|
+
* This method ensures that the app scrolls to the item even if a resize event
|
|
1101
|
+
* occurs shortly after the scroll. Consider the following sequence of events:
|
|
1102
|
+
*
|
|
1103
|
+
* 1. User is at the nth cell, presses Shift+Enter (run current cell and
|
|
1104
|
+
* advance to next)
|
|
1105
|
+
* 2. App scrolls to the next (n+1) cell
|
|
1106
|
+
* 3. The nth cell finishes running and renders the output, pushing the
|
|
1107
|
+
* (n+1) cell down out of view
|
|
1108
|
+
* 4. This triggers the resize observer, which calls this method and scrolls
|
|
1109
|
+
* the (n+1) cell back into view
|
|
1110
|
+
*
|
|
1111
|
+
* On implementation level, this is ensured by scrolling to `this._scrollToItem`
|
|
1112
|
+
* which is cleared after a short timeout once the scrolling settles
|
|
1113
|
+
* (see `this._resetScrollToItem()`).
|
|
1114
|
+
*/
|
|
1115
|
+
_scrollBackToItemOnResize() {
|
|
1116
|
+
if (!this._scrollToItem) {
|
|
1117
|
+
return;
|
|
1118
|
+
}
|
|
1119
|
+
this.scrollToItem(...this._scrollToItem).catch(reason => {
|
|
1120
|
+
console.log(reason);
|
|
1121
|
+
});
|
|
1122
|
+
}
|
|
1034
1123
|
/**
|
|
1035
1124
|
* Clear any outstanding timeout and enqueue scrolling to a new item.
|
|
1036
1125
|
*/
|