virtual-scroller 1.8.0 → 1.9.1
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/.gitlab-ci.yml +1 -1
- package/CHANGELOG.md +57 -0
- package/README.md +337 -160
- package/bundle/virtual-scroller-dom.js +1 -1
- package/bundle/virtual-scroller-dom.js.map +1 -1
- package/bundle/virtual-scroller-react.js +1 -1
- package/bundle/virtual-scroller-react.js.map +1 -1
- package/bundle/virtual-scroller.js +1 -1
- package/bundle/virtual-scroller.js.map +1 -1
- package/commonjs/BeforeResize.js +23 -27
- package/commonjs/BeforeResize.js.map +1 -1
- package/commonjs/DOM/Engine.js +7 -7
- package/commonjs/DOM/Engine.js.map +1 -1
- package/commonjs/DOM/ItemsContainer.js +1 -1
- package/commonjs/DOM/ItemsContainer.js.map +1 -1
- package/commonjs/DOM/ListTopOffsetWatcher.js +15 -9
- package/commonjs/DOM/ListTopOffsetWatcher.js.map +1 -1
- package/commonjs/DOM/ScrollableContainer.js +28 -28
- package/commonjs/DOM/ScrollableContainer.js.map +1 -1
- package/commonjs/DOM/VirtualScroller.js +20 -17
- package/commonjs/DOM/VirtualScroller.js.map +1 -1
- package/commonjs/DOM/tbody.js +16 -10
- package/commonjs/DOM/tbody.js.map +1 -1
- package/commonjs/ItemHeights.js +23 -17
- package/commonjs/ItemHeights.js.map +1 -1
- package/commonjs/Layout.js +15 -13
- package/commonjs/Layout.js.map +1 -1
- package/commonjs/Layout.test.js +8 -3
- package/commonjs/Layout.test.js.map +1 -1
- package/commonjs/{ListHeightChangeWatcher.js → ListHeightMeasurement.js} +26 -28
- package/commonjs/ListHeightMeasurement.js.map +1 -0
- package/commonjs/Resize.js +38 -28
- package/commonjs/Resize.js.map +1 -1
- package/commonjs/Scroll.js +28 -44
- package/commonjs/Scroll.js.map +1 -1
- package/commonjs/VirtualScroller.columns.js +43 -0
- package/commonjs/VirtualScroller.columns.js.map +1 -0
- package/commonjs/VirtualScroller.constructor.js +408 -0
- package/commonjs/VirtualScroller.constructor.js.map +1 -0
- package/commonjs/VirtualScroller.items.js +305 -0
- package/commonjs/VirtualScroller.items.js.map +1 -0
- package/commonjs/VirtualScroller.js +132 -1872
- package/commonjs/VirtualScroller.js.map +1 -1
- package/commonjs/VirtualScroller.layout.js +562 -0
- package/commonjs/VirtualScroller.layout.js.map +1 -0
- package/commonjs/VirtualScroller.onRender.js +357 -0
- package/commonjs/VirtualScroller.onRender.js.map +1 -0
- package/commonjs/VirtualScroller.resize.js +186 -0
- package/commonjs/VirtualScroller.resize.js.map +1 -0
- package/commonjs/VirtualScroller.state.js +301 -0
- package/commonjs/VirtualScroller.state.js.map +1 -0
- package/commonjs/VirtualScroller.verticalSpacing.js +65 -0
- package/commonjs/VirtualScroller.verticalSpacing.js.map +1 -0
- package/commonjs/getItemCoordinates.js.map +1 -1
- package/commonjs/getItemsDiff.js.map +1 -1
- package/commonjs/getVerticalSpacing.js.map +1 -1
- package/commonjs/package.json +5 -0
- package/commonjs/react/VirtualScroller.js +184 -618
- package/commonjs/react/VirtualScroller.js.map +1 -1
- package/commonjs/react/useClassName.js +26 -0
- package/commonjs/react/useClassName.js.map +1 -0
- package/commonjs/react/useHandleItemsChange.js +116 -0
- package/commonjs/react/useHandleItemsChange.js.map +1 -0
- package/commonjs/react/useInstanceMethods.js +37 -0
- package/commonjs/react/useInstanceMethods.js.map +1 -0
- package/commonjs/react/useItemKeys.js +60 -0
- package/commonjs/react/useItemKeys.js.map +1 -0
- package/commonjs/react/useOnItemHeightChange.js +32 -0
- package/commonjs/react/useOnItemHeightChange.js.map +1 -0
- package/commonjs/react/useOnItemStateChange.js +32 -0
- package/commonjs/react/useOnItemStateChange.js.map +1 -0
- package/commonjs/react/useState.js +140 -0
- package/commonjs/react/useState.js.map +1 -0
- package/commonjs/react/useStyle.js +29 -0
- package/commonjs/react/useStyle.js.map +1 -0
- package/commonjs/react/useVirtualScroller.js +62 -0
- package/commonjs/react/useVirtualScroller.js.map +1 -0
- package/commonjs/react/useVirtualScrollerStartStop.js +20 -0
- package/commonjs/react/useVirtualScrollerStartStop.js.map +1 -0
- package/commonjs/test/Engine.js +23 -0
- package/commonjs/test/Engine.js.map +1 -0
- package/commonjs/test/ItemsContainer.js +127 -0
- package/commonjs/test/ItemsContainer.js.map +1 -0
- package/commonjs/test/ScrollableContainer.js +130 -0
- package/commonjs/test/ScrollableContainer.js.map +1 -0
- package/commonjs/test/VirtualScroller.js +281 -0
- package/commonjs/test/VirtualScroller.js.map +1 -0
- package/commonjs/utility/debounce.js +2 -2
- package/commonjs/utility/debounce.js.map +1 -1
- package/commonjs/utility/debug.js.map +1 -1
- package/commonjs/utility/getStateSnapshot.js +2 -2
- package/commonjs/utility/getStateSnapshot.js.map +1 -1
- package/commonjs/utility/px.js.map +1 -1
- package/commonjs/utility/px.test.js +1 -1
- package/commonjs/utility/px.test.js.map +1 -1
- package/commonjs/utility/shallowEqual.js +1 -1
- package/commonjs/utility/shallowEqual.js.map +1 -1
- package/commonjs/utility/throttle.js.map +1 -1
- package/dom/index.cjs +4 -0
- package/dom/index.cjs.js +9 -0
- package/dom/index.d.ts +6 -4
- package/dom/index.js +1 -1
- package/dom/package.json +10 -4
- package/index.cjs +4 -0
- package/index.cjs.js +9 -0
- package/index.d.ts +30 -15
- package/index.js +1 -1
- package/modules/BeforeResize.js +22 -27
- package/modules/BeforeResize.js.map +1 -1
- package/modules/DOM/Engine.js +6 -6
- package/modules/DOM/Engine.js.map +1 -1
- package/modules/DOM/ItemsContainer.js +1 -1
- package/modules/DOM/ItemsContainer.js.map +1 -1
- package/modules/DOM/ListTopOffsetWatcher.js +15 -9
- package/modules/DOM/ListTopOffsetWatcher.js.map +1 -1
- package/modules/DOM/ScrollableContainer.js +28 -28
- package/modules/DOM/ScrollableContainer.js.map +1 -1
- package/modules/DOM/VirtualScroller.js +19 -16
- package/modules/DOM/VirtualScroller.js.map +1 -1
- package/modules/DOM/tbody.js +11 -9
- package/modules/DOM/tbody.js.map +1 -1
- package/modules/ItemHeights.js +22 -16
- package/modules/ItemHeights.js.map +1 -1
- package/modules/Layout.js +14 -12
- package/modules/Layout.js.map +1 -1
- package/modules/Layout.test.js +8 -3
- package/modules/Layout.test.js.map +1 -1
- package/modules/{ListHeightChangeWatcher.js → ListHeightMeasurement.js} +25 -27
- package/modules/ListHeightMeasurement.js.map +1 -0
- package/modules/Resize.js +38 -28
- package/modules/Resize.js.map +1 -1
- package/modules/Scroll.js +28 -44
- package/modules/Scroll.js.map +1 -1
- package/modules/VirtualScroller.columns.js +36 -0
- package/modules/VirtualScroller.columns.js.map +1 -0
- package/modules/VirtualScroller.constructor.js +371 -0
- package/modules/VirtualScroller.constructor.js.map +1 -0
- package/modules/VirtualScroller.items.js +288 -0
- package/modules/VirtualScroller.items.js.map +1 -0
- package/modules/VirtualScroller.js +132 -1860
- package/modules/VirtualScroller.js.map +1 -1
- package/modules/VirtualScroller.layout.js +549 -0
- package/modules/VirtualScroller.layout.js.map +1 -0
- package/modules/VirtualScroller.onRender.js +337 -0
- package/modules/VirtualScroller.onRender.js.map +1 -0
- package/modules/VirtualScroller.resize.js +176 -0
- package/modules/VirtualScroller.resize.js.map +1 -0
- package/modules/VirtualScroller.state.js +283 -0
- package/modules/VirtualScroller.state.js.map +1 -0
- package/modules/VirtualScroller.verticalSpacing.js +54 -0
- package/modules/VirtualScroller.verticalSpacing.js.map +1 -0
- package/modules/getItemCoordinates.js.map +1 -1
- package/modules/getItemsDiff.js.map +1 -1
- package/modules/getVerticalSpacing.js.map +1 -1
- package/modules/react/VirtualScroller.js +176 -625
- package/modules/react/VirtualScroller.js.map +1 -1
- package/modules/react/useClassName.js +18 -0
- package/modules/react/useClassName.js.map +1 -0
- package/modules/react/useHandleItemsChange.js +108 -0
- package/modules/react/useHandleItemsChange.js.map +1 -0
- package/modules/react/useInstanceMethods.js +28 -0
- package/modules/react/useInstanceMethods.js.map +1 -0
- package/modules/react/useItemKeys.js +52 -0
- package/modules/react/useItemKeys.js.map +1 -0
- package/modules/react/useOnItemHeightChange.js +24 -0
- package/modules/react/useOnItemHeightChange.js.map +1 -0
- package/modules/react/useOnItemStateChange.js +24 -0
- package/modules/react/useOnItemStateChange.js.map +1 -0
- package/modules/react/useState.js +132 -0
- package/modules/react/useState.js.map +1 -0
- package/modules/react/useStyle.js +19 -0
- package/modules/react/useStyle.js.map +1 -0
- package/modules/react/useVirtualScroller.js +51 -0
- package/modules/react/useVirtualScroller.js.map +1 -0
- package/modules/react/useVirtualScrollerStartStop.js +12 -0
- package/modules/react/useVirtualScrollerStartStop.js.map +1 -0
- package/modules/test/Engine.js +11 -0
- package/modules/test/Engine.js.map +1 -0
- package/modules/test/ItemsContainer.js +120 -0
- package/modules/test/ItemsContainer.js.map +1 -0
- package/modules/test/ScrollableContainer.js +123 -0
- package/modules/test/ScrollableContainer.js.map +1 -0
- package/modules/test/VirtualScroller.js +270 -0
- package/modules/test/VirtualScroller.js.map +1 -0
- package/modules/utility/debounce.js +2 -2
- package/modules/utility/debounce.js.map +1 -1
- package/modules/utility/debug.js.map +1 -1
- package/modules/utility/getStateSnapshot.js +2 -2
- package/modules/utility/getStateSnapshot.js.map +1 -1
- package/modules/utility/px.js.map +1 -1
- package/modules/utility/px.test.js +1 -1
- package/modules/utility/px.test.js.map +1 -1
- package/modules/utility/shallowEqual.js +1 -1
- package/modules/utility/shallowEqual.js.map +1 -1
- package/modules/utility/throttle.js.map +1 -1
- package/package.json +46 -23
- package/react/index.cjs +4 -0
- package/react/index.cjs.js +9 -0
- package/react/index.d.ts +10 -9
- package/react/index.js +1 -1
- package/react/package.json +10 -4
- package/rollup.config.mjs +62 -0
- package/runnable/create-commonjs-package-json.js +11 -0
- package/source/BeforeResize.js +16 -21
- package/source/DOM/Engine.js +8 -10
- package/source/DOM/ListTopOffsetWatcher.js +13 -8
- package/source/DOM/ScrollableContainer.js +23 -21
- package/source/DOM/VirtualScroller.js +27 -11
- package/source/DOM/tbody.js +30 -21
- package/source/ItemHeights.js +19 -14
- package/source/Layout.js +12 -9
- package/source/Layout.test.js +8 -3
- package/source/{ListHeightChangeWatcher.js → ListHeightMeasurement.js} +21 -20
- package/source/Resize.js +41 -25
- package/source/Scroll.js +27 -35
- package/source/VirtualScroller.columns.js +26 -0
- package/source/VirtualScroller.constructor.js +336 -0
- package/source/VirtualScroller.items.js +302 -0
- package/source/VirtualScroller.js +144 -1872
- package/source/VirtualScroller.layout.js +539 -0
- package/source/VirtualScroller.onRender.js +345 -0
- package/source/VirtualScroller.resize.js +189 -0
- package/source/VirtualScroller.state.js +284 -0
- package/source/VirtualScroller.verticalSpacing.js +51 -0
- package/source/react/VirtualScroller.js +243 -587
- package/source/react/useClassName.js +14 -0
- package/source/react/useHandleItemsChange.js +115 -0
- package/source/react/useInstanceMethods.js +25 -0
- package/source/react/useItemKeys.js +59 -0
- package/source/react/useOnItemHeightChange.js +28 -0
- package/source/react/useOnItemStateChange.js +28 -0
- package/source/react/useState.js +114 -0
- package/source/react/useStyle.js +20 -0
- package/source/react/useVirtualScroller.js +59 -0
- package/source/react/useVirtualScrollerStartStop.js +12 -0
- package/source/test/Engine.js +11 -0
- package/source/test/ItemsContainer.js +87 -0
- package/source/test/ScrollableContainer.js +88 -0
- package/source/test/VirtualScroller.js +232 -0
- package/source/utility/debounce.js +2 -2
- package/source/utility/px.test.js +1 -1
- package/babel.config.js +0 -25
- package/babel.js +0 -5
- package/commonjs/ListHeightChangeWatcher.js.map +0 -1
- package/dom/index.commonjs.js +0 -4
- package/index.commonjs.js +0 -4
- package/modules/ListHeightChangeWatcher.js.map +0 -1
- package/react/index.commonjs.js +0 -4
package/index.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
export type ItemHeight = number | undefined;
|
|
2
2
|
export type ItemState = any | undefined;
|
|
3
3
|
|
|
4
|
+
interface BeforeResizeState {
|
|
5
|
+
itemHeights: number[];
|
|
6
|
+
columnsCount: number;
|
|
7
|
+
verticalSpacing: number;
|
|
8
|
+
}
|
|
9
|
+
|
|
4
10
|
export interface State<Item> {
|
|
5
11
|
items: Item[];
|
|
6
12
|
firstShownItemIndex: number;
|
|
@@ -11,6 +17,8 @@ export interface State<Item> {
|
|
|
11
17
|
itemHeights: ItemHeight[];
|
|
12
18
|
verticalSpacing?: number;
|
|
13
19
|
columnsCount?: number;
|
|
20
|
+
scrollableContainerWidth?: number;
|
|
21
|
+
beforeResize?: BeforeResizeState;
|
|
14
22
|
}
|
|
15
23
|
|
|
16
24
|
export interface ItemsContainer<Element> {
|
|
@@ -34,34 +42,40 @@ export interface ScrollableContainer<Element> {
|
|
|
34
42
|
|
|
35
43
|
export interface Engine<Element> {
|
|
36
44
|
createItemsContainer(getItemsContainerElement: () => Element): ItemsContainer<Element>;
|
|
37
|
-
createScrollableContainer(
|
|
45
|
+
createScrollableContainer(getScrollableContainer: () => Element, getItemsContainerElement: () => Element): ScrollableContainer<Element>;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
interface ScrollableContainerArgument {
|
|
49
|
+
getWidth(): number;
|
|
38
50
|
}
|
|
39
51
|
|
|
40
|
-
export interface
|
|
52
|
+
export interface VirtualScrollerCommonOptions<Element, Item> {
|
|
41
53
|
bypass?: boolean;
|
|
42
|
-
onStateChange?(newState: State<Item
|
|
54
|
+
onStateChange?(newState: State<Item>);
|
|
43
55
|
measureItemsBatchSize?: number;
|
|
44
56
|
estimatedItemHeight?: number;
|
|
45
57
|
initialScrollPosition?: number;
|
|
46
58
|
onScrollPositionChange?(scrollY: number): void;
|
|
47
59
|
onItemInitialRender?(item: Item): void;
|
|
48
60
|
getItemId?(item: Item): any;
|
|
49
|
-
getColumnsCount?(scrollableContainer:
|
|
61
|
+
getColumnsCount?(scrollableContainer: ScrollableContainerArgument): number;
|
|
50
62
|
}
|
|
51
63
|
|
|
52
|
-
|
|
64
|
+
interface Options<Element, Item> extends VirtualScrollerCommonOptions<Element, Item> {
|
|
53
65
|
state?: State<Item>;
|
|
54
|
-
|
|
66
|
+
render?(state: State<Item>, previousState?: State<Item>): void;
|
|
67
|
+
engine?: Engine<Element>;
|
|
68
|
+
tbody?: boolean;
|
|
69
|
+
scrollableContainer?: Element;
|
|
70
|
+
getScrollableContainer?(): Element;
|
|
55
71
|
}
|
|
56
72
|
|
|
57
|
-
interface
|
|
73
|
+
interface UseStateOptions<Item> {
|
|
58
74
|
getState?(): State<Item>;
|
|
59
|
-
|
|
60
|
-
engine?: Engine<Element>;
|
|
61
|
-
tbody?: boolean;
|
|
75
|
+
updateState?(stateUpdate: Partial<State<Item>>): void;
|
|
62
76
|
}
|
|
63
77
|
|
|
64
|
-
export interface
|
|
78
|
+
export interface SetItemsOptions {
|
|
65
79
|
preserveScrollPositionOnPrependItems?: boolean;
|
|
66
80
|
}
|
|
67
81
|
|
|
@@ -72,13 +86,14 @@ export default class VirtualScroller<Element, Item> {
|
|
|
72
86
|
options?: Options<Element, Item>
|
|
73
87
|
);
|
|
74
88
|
|
|
75
|
-
|
|
89
|
+
start(): void;
|
|
76
90
|
stop(): void;
|
|
77
|
-
willUpdateState(newState: State<Item>, prevState: State<Item>): void;
|
|
78
|
-
didUpdateState(prevState: State<Item>): void;
|
|
79
91
|
updateLayout(): void;
|
|
80
|
-
|
|
92
|
+
onRender(): void;
|
|
93
|
+
setItems(newItems: Item[], options?: SetItemsOptions): void;
|
|
81
94
|
onItemHeightChange(i: number): void;
|
|
82
95
|
onItemStateChange(i: number, itemState?: object): void;
|
|
83
96
|
getItemScrollPosition(i: number): number | undefined;
|
|
97
|
+
getInitialState(): State<Item>;
|
|
98
|
+
useState(options: UseStateOptions<Item>): void;
|
|
84
99
|
}
|
package/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export { default } from './modules/VirtualScroller'
|
|
1
|
+
export { default } from './modules/VirtualScroller.js'
|
package/modules/BeforeResize.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object);
|
|
1
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
2
2
|
|
|
3
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i]
|
|
3
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
4
4
|
|
|
5
5
|
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
|
6
6
|
|
|
@@ -8,9 +8,9 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
|
|
|
8
8
|
|
|
9
9
|
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
10
10
|
|
|
11
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
11
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
12
12
|
|
|
13
|
-
import log from './utility/debug';
|
|
13
|
+
import log from './utility/debug.js';
|
|
14
14
|
|
|
15
15
|
var BeforeResize = /*#__PURE__*/function () {
|
|
16
16
|
function BeforeResize(_ref) {
|
|
@@ -23,27 +23,12 @@ var BeforeResize = /*#__PURE__*/function () {
|
|
|
23
23
|
this.getState = getState;
|
|
24
24
|
this.getVerticalSpacing = getVerticalSpacing;
|
|
25
25
|
this.getColumnsCount = getColumnsCount;
|
|
26
|
-
}
|
|
27
|
-
// "Before resize" state property is cleaned up when all "before resize" item heights
|
|
28
|
-
// have been re-measured in an asynchronous `this.setState({ beforeResize: undefined })` call.
|
|
29
|
-
// If `VirtualScroller` state was snapshotted externally before that `this.setState()` call
|
|
30
|
-
// has been applied, then "before resize" property might have not been cleaned up properly.
|
|
31
|
-
|
|
26
|
+
}
|
|
32
27
|
|
|
33
28
|
_createClass(BeforeResize, [{
|
|
34
|
-
key: "
|
|
35
|
-
value: function
|
|
36
|
-
|
|
37
|
-
if (state.beforeResize) {
|
|
38
|
-
if (state.beforeResize.itemHeights.length === 0) {
|
|
39
|
-
state.beforeResize = undefined;
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
if (state.beforeResize) {
|
|
44
|
-
this._includesBeforeResizeInState = true;
|
|
45
|
-
}
|
|
46
|
-
}
|
|
29
|
+
key: "initializeFromState",
|
|
30
|
+
value: function initializeFromState(state) {
|
|
31
|
+
this._includesBeforeResizeInState = Boolean(state.beforeResize);
|
|
47
32
|
} // Cleans up "before resize" item heights and adjusts the scroll position accordingly.
|
|
48
33
|
//
|
|
49
34
|
// Hypothetically, it could also wait for the user to stop scrolling and only then
|
|
@@ -63,7 +48,7 @@ var BeforeResize = /*#__PURE__*/function () {
|
|
|
63
48
|
|
|
64
49
|
}, {
|
|
65
50
|
key: "cleanUpBeforeResizeItemHeights",
|
|
66
|
-
value: function cleanUpBeforeResizeItemHeights(
|
|
51
|
+
value: function cleanUpBeforeResizeItemHeights() {
|
|
67
52
|
var _this$getState = this.getState(),
|
|
68
53
|
firstShownItemIndex = _this$getState.firstShownItemIndex,
|
|
69
54
|
lastShownItemIndex = _this$getState.lastShownItemIndex,
|
|
@@ -146,9 +131,9 @@ var BeforeResize = /*#__PURE__*/function () {
|
|
|
146
131
|
cleanedUpBeforeResizeItemRowsHeight += _rowHeight;
|
|
147
132
|
cleanedUpBeforeResizeItemRowsHeight += beforeResize.verticalSpacing;
|
|
148
133
|
k++;
|
|
149
|
-
} // Schedule an asynchronous `this.
|
|
134
|
+
} // Schedule an asynchronous `this.updateState()` call that will update
|
|
150
135
|
// `beforeResize` property of `state`. Ideally, it should be updated
|
|
151
|
-
// immediately, but since `this.
|
|
136
|
+
// immediately, but since `this.updateState()` calls are asynchronous,
|
|
152
137
|
// the code updates just the underlying `beforeResize.itemHeights`
|
|
153
138
|
// array immediately instead, which is still a hack but still a lesser one.
|
|
154
139
|
|
|
@@ -166,7 +151,7 @@ var BeforeResize = /*#__PURE__*/function () {
|
|
|
166
151
|
}
|
|
167
152
|
} // Immediately update `beforeResize.itemHeights`
|
|
168
153
|
// so that the component isn't left in an inconsistent state
|
|
169
|
-
// before a `this.
|
|
154
|
+
// before a `this.updateState()` call below is applied.
|
|
170
155
|
|
|
171
156
|
|
|
172
157
|
beforeResize.itemHeights.splice(firstShownItemIndex, beforeResize.itemHeights.length - firstShownItemIndex); // Return the "scroll by" amount that would correct the scroll position.
|
|
@@ -307,4 +292,14 @@ function equalizeItemHeights(itemHeights, maxItemsCount, columnsCount) {
|
|
|
307
292
|
|
|
308
293
|
return itemHeights.slice(0, maxItemsCount);
|
|
309
294
|
}
|
|
295
|
+
|
|
296
|
+
export function cleanUpBeforeResizeState(state) {
|
|
297
|
+
if (state.beforeResize) {
|
|
298
|
+
if (state.beforeResize.itemHeights.length === 0) {
|
|
299
|
+
state.beforeResize = undefined;
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
return state;
|
|
304
|
+
}
|
|
310
305
|
//# sourceMappingURL=BeforeResize.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../source/BeforeResize.js"],"names":["log","BeforeResize","getState","getVerticalSpacing","getColumnsCount","state","beforeResize","itemHeights","length","undefined","_includesBeforeResizeInState","prevState","firstShownItemIndex","lastShownItemIndex","newlyShownItemRowsHeight","prevFirstReMeasuredItemsRowIndex","Math","floor","newlyShownItemsToIndex","min","i","rowHeight","columnIndex","itemHeight","getAverageItemHeight","max","cleanedUpBeforeResizeItemRowsHeight","maxParticipatingBeforeResizeItemsCount","participatingBeforeResizeItemRowsCount","ceil","columnsCount","firstCleanedUpBeforeResizeItemsRowIndex","k","verticalSpacing","firstDroppedBeforeResizeItemIndex","lastDroppedBeforeResizeItemIndex","slice","splice","scrollBy","newFirstShownItemIndex","newColumnsCount","prevBeforeResize","prevBeforeResizeItemsCount","prevBeforeResizeBeforeItemsHeight","prevBeforeResizeItemRowsCount","rowIndex","newBeforeResizeAdditionalBeforeItemsHeight","newBeforeResizeBeforeItemsHeight","newBeforeResizeBeforeItemRowsCount","Array","fill","concat","equalizeItemHeights","maxItemsCount"],"mappings":";;;;;;;;;;;;AAAA,OAAOA,GAAP,MAAgB,iBAAhB;;IAEqBC,Y;AACpB,8BAIG;AAAA,QAHFC,QAGE,QAHFA,QAGE;AAAA,QAFFC,kBAEE,QAFFA,kBAEE;AAAA,QADFC,eACE,QADFA,eACE;;AAAA;;AACF,SAAKF,QAAL,GAAgBA,QAAhB;AACA,SAAKC,kBAAL,GAA0BA,kBAA1B;AACA,SAAKC,eAAL,GAAuBA,eAAvB;AACA,G,CAED;AACA;AACA;AACA;AACA;;;;;WACA,wBAAeC,KAAf,EAAsB;AACrB,UAAIA,KAAJ,EAAW;AACV,YAAIA,KAAK,CAACC,YAAV,EAAwB;AACvB,cAAID,KAAK,CAACC,YAAN,CAAmBC,WAAnB,CAA+BC,MAA/B,KAA0C,CAA9C,EAAiD;AAChDH,YAAAA,KAAK,CAACC,YAAN,GAAqBG,SAArB;AACA;AACD;;A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W,CAACsC,QAAQ,GAAGf,YAAX,GAA0BE,CAA3B,CAA/B,CAAZ;AACAA,MAAAA,CAAC;AACD,KAP8C,CAS/C;;;AACAA,IAAAA,CAAC,GAAG,CAAJ;;AACA,WAAOA,CAAC,GAAGF,YAAX,EAAyB;AACxBvB,MAAAA,WAAW,CAACsC,QAAQ,GAAGf,YAAX,GAA0BE,CAA3B,CAAX,GAA2CX,SAA3C;AACAW,MAAAA,CAAC;AACD,KAd8C,CAgB/C;;;AACAa,IAAAA,QAAQ;AACR;;AAED,SAAOtC,WAAW,CAAC6B,KAAZ,CAAkB,CAAlB,EAAqBiB,aAArB,CAAP;AACA","sourcesContent":["import log from './utility/debug'\r\n\r\nexport default class BeforeResize {\r\n\tconstructor({\r\n\t\tgetState,\r\n\t\tgetVerticalSpacing,\r\n\t\tgetColumnsCount\r\n\t}) {\r\n\t\tthis.getState = getState\r\n\t\tthis.getVerticalSpacing = getVerticalSpacing\r\n\t\tthis.getColumnsCount = getColumnsCount\r\n\t}\r\n\r\n\t// Possibly clean up \"before resize\" property in state.\r\n\t// \"Before resize\" state property is cleaned up when all \"before resize\" item heights\r\n\t// have been re-measured in an asynchronous `this.setState({ beforeResize: undefined })` call.\r\n\t// If `VirtualScroller` state was snapshotted externally before that `this.setState()` call\r\n\t// has been applied, then \"before resize\" property might have not been cleaned up properly.\r\n\tonInitialState(state) {\r\n\t\tif (state) {\r\n\t\t\tif (state.beforeResize) {\r\n\t\t\t\tif (state.beforeResize.itemHeights.length === 0) {\r\n\t\t\t\t\tstate.beforeResize = undefined\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\tif (state.beforeResize) {\r\n\t\t\t\tthis._includesBeforeResizeInState = true\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t// Cleans up \"before resize\" item heights and adjusts the scroll position accordingly.\r\n\t//\r\n\t// Hypothetically, it could also wait for the user to stop scrolling and only then\r\n\t// adjust the scroll position. The rationale is that if `window.scrollTo()` is called\r\n\t// while the user is scrolling, the user would occasionally experience \"lost\" mouse wheel\r\n\t// events when scrolling with a mouse wheel.\r\n\t//\r\n\t// Seems like Twitter's website waits for the user to stop scrolling before applying\r\n\t// the scroll position correction after a window resize. This library could do that too,\r\n\t// but that would require rewriting \"before items height\" top padding calculation\r\n\t// so that it doesn't re-calculate it on every re-render and instead does so incrementally,\r\n\t// and then, when the user stops, it re-calculates it from scratch removing the error\r\n\t// and adjusting the scroll position accordingly so that there's no \"jump of content\".\r\n\t//\r\n\t// But, seems like it works fine as it is and there's no need to rewrite anything.\r\n\t//\r\n\tcleanUpBeforeResizeItemHeights(prevState) {\r\n\t\tconst {\r\n\t\t\tfirstShownItemIndex,\r\n\t\t\tlastShownItemIndex,\r\n\t\t\titemHeights,\r\n\t\t\tbeforeResize\r\n\t\t} = this.getState()\r\n\r\n\t\t// If there're \"before resize\" properties in `state`\r\n\t\t// then it means that the corresponding items are waiting to be\r\n\t\t// re-measured after container resize. Since the resize,\r\n\t\t// some of those non-re-measured items might have just been measured,\r\n\t\t// so see if that's true, and if it is, remove those now-obsolete\r\n\t\t// \"before resize\" item heights and ajust the scroll position\r\n\t\t// so that there's no \"content jumping\".\r\n\r\n\t\tif (beforeResize) {\r\n\t\t\t// If the user has scrolled up to reveal a previously hidden item\r\n\t\t\t// that has not yet been re-measured after a previous resize.\r\n\t\t\tif (firstShownItemIndex < beforeResize.itemHeights.length) {\r\n\t\t\t\tlog('~ Clean up \"before resize\" item heights and correct scroll position ~')\r\n\r\n\t\t\t\t// Some of the \"before\" items have been un-hidden and re-measured.\r\n\t\t\t\t// Un-hiding those items would result in a \"jump of content\"\r\n\t\t\t\t// because \"before resize\" heights of those un-hidden items\r\n\t\t\t\t// could (and most likely will) be different from the current ones,\r\n\t\t\t\t// or because \"before resize\" columns count is different from\r\n\t\t\t\t// the current one.\r\n\t\t\t\t// To prevent a \"jump of content\", calculate the scroll position\r\n\t\t\t\t// difference and adjust the scroll position.\r\n\r\n\t\t\t\t// The height of the item rows that have transitioned\r\n\t\t\t\t// from hidden to shown.\r\n\t\t\t\tlet newlyShownItemRowsHeight = 0\r\n\r\n\t\t\t\t// Some of the `itemHeights` between the current `firstShownItemIndex` and\r\n\t\t\t\t// the previous `firstShownItemIndex` could stay `undefined` if the user\r\n\t\t\t\t// scrolled \"abruptly\": for example, by using a `window.scrollTo()` call.\r\n\t\t\t\t// In that case, the items below the visible ones won't be rendered and measured.\r\n\t\t\t\t// In such case, limit the items being iterated over to the current `lastShownItemIndex`\r\n\t\t\t\t// rather than the previous `firstShownItemIndex`.\r\n\t\t\t\tconst prevFirstReMeasuredItemsRowIndex = Math.floor(beforeResize.itemHeights.length / this.getColumnsCount())\r\n\t\t\t\tconst newlyShownItemsToIndex = Math.min(\r\n\t\t\t\t\tprevFirstReMeasuredItemsRowIndex * this.getColumnsCount() - 1,\r\n\t\t\t\t\tlastShownItemIndex\r\n\t\t\t\t)\r\n\r\n\t\t\t\tlet i = firstShownItemIndex\r\n\t\t\t\twhile (i <= newlyShownItemsToIndex) {\r\n\t\t\t\t\t// Calculate newly shown row height.\r\n\t\t\t\t\tlet rowHeight = 0\r\n\t\t\t\t\tlet columnIndex = 0\r\n\t\t\t\t\twhile (columnIndex < this.getColumnsCount() && i <= newlyShownItemsToIndex) {\r\n\t\t\t\t\t\tlet itemHeight = itemHeights[i]\r\n\t\t\t\t\t\tif (itemHeight === undefined) {\r\n\t\t\t\t\t\t\t// `itemHeight` can only be `undefined` when not `beforeResize`.\r\n\t\t\t\t\t\t\t// Use the current \"average item height\" as a substitute.\r\n\t\t\t\t\t\t\titemHeight = this.getAverageItemHeight()\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\trowHeight = Math.max(rowHeight, itemHeight)\r\n\t\t\t\t\t\ti++\r\n\t\t\t\t\t\tcolumnIndex++\r\n\t\t\t\t\t}\r\n\t\t\t\t\t// Append to the total \"newly shown item rows height\".\r\n\t\t\t\t\tnewlyShownItemRowsHeight += rowHeight\r\n\t\t\t\t\tnewlyShownItemRowsHeight += this.getVerticalSpacing()\r\n\t\t\t\t}\r\n\r\n\t\t\t\t// The height of the \"before resize\" item rows\r\n\t\t\t\t// that will be \"cleaned up\" in this function call.\r\n\t\t\t\tlet cleanedUpBeforeResizeItemRowsHeight = 0\r\n\r\n\t\t\t\t// Some of the `beforeResize` item rows might have been skipped if the user\r\n\t\t\t\t// scrolled up \"abruptly\": for example, by using a `window.scrollTo()` call.\r\n\t\t\t\t// In that case, the \"before resize\" items below the bottom border of the screen\r\n\t\t\t\t// shouldn't be accounted for when calculating the scrollbar adjustment shift\r\n\t\t\t\t// because items after `lastShownItemIndex` aren't participating in the calculation\r\n\t\t\t\t// of `newlyShownItemRowsHeight`.\r\n\t\t\t\tconst maxParticipatingBeforeResizeItemsCount = Math.min(beforeResize.itemHeights.length, lastShownItemIndex + 1)\r\n\t\t\t\tconst participatingBeforeResizeItemRowsCount = Math.ceil(maxParticipatingBeforeResizeItemsCount / beforeResize.columnsCount)\r\n\r\n\t\t\t\tconst firstCleanedUpBeforeResizeItemsRowIndex = firstShownItemIndex === 0\r\n\t\t\t\t\t? 0\r\n\t\t\t\t\t: Math.floor((firstShownItemIndex - 1) / beforeResize.columnsCount) + 1\r\n\r\n\t\t\t\tlet k = firstCleanedUpBeforeResizeItemsRowIndex\r\n\t\t\t\twhile (k < participatingBeforeResizeItemRowsCount) {\r\n\t\t\t\t\tconst rowHeight = beforeResize.itemHeights[k * beforeResize.columnsCount]\r\n\t\t\t\t\tcleanedUpBeforeResizeItemRowsHeight += rowHeight\r\n\t\t\t\t\tcleanedUpBeforeResizeItemRowsHeight += beforeResize.verticalSpacing\r\n\t\t\t\t\tk++\r\n\t\t\t\t}\r\n\r\n\t\t\t\t// Schedule an asynchronous `this.setState()` call that will update\r\n\t\t\t\t// `beforeResize` property of `state`. Ideally, it should be updated\r\n\t\t\t\t// immediately, but since `this.setState()` calls are asynchronous,\r\n\t\t\t\t// the code updates just the underlying `beforeResize.itemHeights`\r\n\t\t\t\t// array immediately instead, which is still a hack but still a lesser one.\r\n\t\t\t\tif (firstShownItemIndex === 0) {\r\n\t\t\t\t\tlog('Drop all \"before resize\" item heights')\r\n\t\t\t\t} else {\r\n\t\t\t\t\tconst firstDroppedBeforeResizeItemIndex = firstShownItemIndex\r\n\t\t\t\t\tconst lastDroppedBeforeResizeItemIndex = beforeResize.itemHeights.length - 1\r\n\t\t\t\t\tif (firstDroppedBeforeResizeItemIndex === lastDroppedBeforeResizeItemIndex) {\r\n\t\t\t\t\t\tlog('For item index', firstDroppedBeforeResizeItemIndex, '— drop \"before resize\" height', beforeResize.itemHeights[firstDroppedBeforeResizeItemIndex], )\r\n\t\t\t\t\t} else {\r\n\t\t\t\t\t\tlog('For item indexes from', firstDroppedBeforeResizeItemIndex, 'to', lastDroppedBeforeResizeItemIndex, '— drop \"before resize\" heights', beforeResize.itemHeights.slice(firstDroppedBeforeResizeItemIndex))\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\r\n\t\t\t\t// Immediately update `beforeResize.itemHeights`\r\n\t\t\t\t// so that the component isn't left in an inconsistent state\r\n\t\t\t\t// before a `this.setState()` call below is applied.\r\n\t\t\t\tbeforeResize.itemHeights.splice(\r\n\t\t\t\t\tfirstShownItemIndex,\r\n\t\t\t\t\tbeforeResize.itemHeights.length - firstShownItemIndex\r\n\t\t\t\t)\r\n\r\n\t\t\t\t// Return the \"scroll by\" amount that would correct the scroll position.\r\n\t\t\t\t// Also return a state update.\r\n\t\t\t\treturn {\r\n\t\t\t\t\tscrollBy: newlyShownItemRowsHeight - cleanedUpBeforeResizeItemRowsHeight,\r\n\t\t\t\t\tbeforeResize: firstShownItemIndex === 0 ? undefined : {\r\n\t\t\t\t\t\t// Simply change the \"reference\" to `beforeResize` while leaving\r\n\t\t\t\t\t\t// its contents unchanged. That simply indicates that it has been updated:\r\n\t\t\t\t\t\t// `beforeResize.itemHeights` array length has been changed \"directly\".\r\n\t\t\t\t\t\t...beforeResize\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t// Snapshots \"before resize\" values in order to preserve the currently\r\n\t// shown items' vertical position on screen so that there's no \"content jumping\".\r\n\t//\r\n\t// `newFirstShownItemIndex` is `> 0`.\r\n\t//\r\n\tsnapshotBeforeResizeItemHeights({\r\n\t\tfirstShownItemIndex,\r\n\t\tnewFirstShownItemIndex,\r\n\t\tnewColumnsCount\r\n\t}) {\r\n\t\tconst columnsCount = this.getColumnsCount()\r\n\t\tconst verticalSpacing = this.getVerticalSpacing()\r\n\r\n\t\tthis._includesBeforeResizeInState = true\r\n\r\n\t\tconst {\r\n\t\t\tbeforeResize: prevBeforeResize,\r\n\t\t\titemHeights\r\n\t\t} = this.getState()\r\n\r\n\t\tconst prevBeforeResizeItemsCount = prevBeforeResize\r\n\t\t\t? prevBeforeResize.itemHeights.length\r\n\t\t\t: 0\r\n\r\n\t\t// If there already are \"before resize\" values in `state`\r\n\t\t// then it means that those should be merged with the new ones.\r\n\t\t//\r\n\t\t// `beforeResize.itemHeights` could be empty in an edge case\r\n\t\t// when there's a pending state update that sets `beforeResize`\r\n\t\t// to `undefined`, and in that case empty `beforeResize.itemHeights`\r\n\t\t// signals about that type of a situation.\r\n\t\t//\r\n\t\tif (prevBeforeResizeItemsCount > 0) {\r\n\t\t\t// Because the \"previous\" before resize values might have been captured\r\n\t\t\t// for a window width corresponding to a layout with a different columns count\r\n\t\t\t// and different vertical spacing, re-calculate those item heights as if\r\n\t\t\t// they corresponded to the current columns count and current vertical spacing,\r\n\t\t\t// since \"previous\" and \"new\" before resize item heights are gonna be merged.\r\n\t\t\tif (\r\n\t\t\t\tprevBeforeResize.columnsCount !== columnsCount ||\r\n\t\t\t\tprevBeforeResize.verticalSpacing !== verticalSpacing\r\n\t\t\t) {\r\n\t\t\t\tlet prevBeforeResizeBeforeItemsHeight = 0\r\n\r\n\t\t\t\tconst prevBeforeResizeItemRowsCount = Math.ceil(prevBeforeResizeItemsCount / prevBeforeResize.columnsCount)\r\n\t\t\t\tlet rowIndex = 0\r\n\t\t\t\twhile (rowIndex < prevBeforeResizeItemRowsCount) {\r\n\t\t\t\t\t// Since all \"before resize\" item heights are equal within a row,\r\n\t\t\t\t\t// the height of the first \"before resize\" item in a row is that row's height.\r\n\t\t\t\t\tconst rowHeight = prevBeforeResize.itemHeights[rowIndex * prevBeforeResize.columnsCount]\r\n\t\t\t\t\tprevBeforeResizeBeforeItemsHeight += rowHeight\r\n\t\t\t\t\tprevBeforeResizeBeforeItemsHeight += prevBeforeResize.verticalSpacing\r\n\t\t\t\t\trowIndex++\r\n\t\t\t\t}\r\n\r\n\t\t\t\tlet newBeforeResizeAdditionalBeforeItemsHeight = 0\r\n\t\t\t\tlet i = firstShownItemIndex\r\n\t\t\t\twhile (i < newFirstShownItemIndex) {\r\n\t\t\t\t\tlet rowHeight = 0\r\n\t\t\t\t\tlet k = 0\r\n\t\t\t\t\twhile (k < columnsCount && i < newFirstShownItemIndex) {\r\n\t\t\t\t\t\trowHeight = Math.max(rowHeight, itemHeights[i])\r\n\t\t\t\t\t\tk++\r\n\t\t\t\t\t\ti++\r\n\t\t\t\t\t}\r\n\t\t\t\t\tnewBeforeResizeAdditionalBeforeItemsHeight += rowHeight\r\n\t\t\t\t\tnewBeforeResizeAdditionalBeforeItemsHeight += verticalSpacing\r\n\t\t\t\t}\r\n\r\n\t\t\t\tconst newBeforeResizeBeforeItemsHeight = prevBeforeResizeBeforeItemsHeight + newBeforeResizeAdditionalBeforeItemsHeight\r\n\t\t\t\tconst newBeforeResizeBeforeItemRowsCount = Math.ceil(newFirstShownItemIndex / columnsCount)\r\n\r\n\t\t\t\treturn new Array(newFirstShownItemIndex).fill(\r\n\t\t\t\t\t// Re-calculate \"before resize\" item heights so that \"previous\" and \"new\" ones\r\n\t\t\t\t\t// correspond to the same (new) columns count.\r\n\t\t\t\t\t// Also don't occasionally set item heights to `< 0`.\r\n\t\t\t\t\tMath.max(0, newBeforeResizeBeforeItemsHeight / newBeforeResizeBeforeItemRowsCount - verticalSpacing)\r\n\t\t\t\t)\r\n\t\t\t} else {\r\n\t\t\t\t// Add new item heights to the previously snapshotted ones.\r\n\t\t\t\treturn prevBeforeResize.itemHeights.concat(\r\n\t\t\t\t\tequalizeItemHeights(\r\n\t\t\t\t\t\titemHeights,\r\n\t\t\t\t\t\tnewFirstShownItemIndex,\r\n\t\t\t\t\t\tcolumnsCount\r\n\t\t\t\t\t).slice(prevBeforeResize.itemHeights.length)\r\n\t\t\t\t)\r\n\t\t\t}\r\n\t\t} else {\r\n\t\t\treturn equalizeItemHeights(\r\n\t\t\t\titemHeights,\r\n\t\t\t\tnewFirstShownItemIndex,\r\n\t\t\t\tcolumnsCount\r\n\t\t\t)\r\n\t\t}\r\n\t}\r\n\r\n\tshouldIncludeBeforeResizeValuesInState() {\r\n\t\treturn this._includesBeforeResizeInState\r\n\t}\r\n}\r\n\r\n// Equalizes all item heights within a given row, for each row.\r\n//\r\n// The reason is that `beforeResize.itemHeights` is not necessarily divisible by\r\n// `beforeResize.columnsCount`, which would result in varying last row height\r\n// as items get removed from `beforeResize.itemHeights` as the user scrolls up.\r\n//\r\n// By equalizing all item heights within a given row, for each row, such \"jumping\"\r\n// last \"before resize\" row height is prevented when the user scrolls up.\r\n//\r\nfunction equalizeItemHeights(itemHeights, maxItemsCount, columnsCount) {\r\n\titemHeights = itemHeights.slice(0, Math.ceil(maxItemsCount / columnsCount) * columnsCount)\r\n\r\n\tlet rowIndex = 0\r\n\twhile (rowIndex * columnsCount < maxItemsCount) {\r\n\t\t// Calculate row height.\r\n\t\tlet rowHeight = 0\r\n\t\tlet k = 0\r\n\t\twhile (k < columnsCount) {\r\n\t\t\trowHeight = Math.max(rowHeight, itemHeights[rowIndex * columnsCount + k])\r\n\t\t\tk++\r\n\t\t}\r\n\r\n\t\t// Equalize all item heights within the row.\r\n\t\tk = 0\r\n\t\twhile (k < columnsCount) {\r\n\t\t\titemHeights[rowIndex * columnsCount + k] = rowHeight\r\n\t\t\tk++\r\n\t\t}\r\n\r\n\t\t// Proceed with the next row.\r\n\t\trowIndex++\r\n\t}\r\n\r\n\treturn itemHeights.slice(0, maxItemsCount)\r\n}"],"file":"BeforeResize.js"}
|
|
1
|
+
{"version":3,"file":"BeforeResize.js","names":["log","BeforeResize","getState","getVerticalSpacing","getColumnsCount","state","_includesBeforeResizeInState","Boolean","beforeResize","firstShownItemIndex","lastShownItemIndex","itemHeights","length","newlyShownItemRowsHeight","prevFirstReMeasuredItemsRowIndex","Math","floor","newlyShownItemsToIndex","min","i","rowHeight","columnIndex","itemHeight","undefined","getAverageItemHeight","max","cleanedUpBeforeResizeItemRowsHeight","maxParticipatingBeforeResizeItemsCount","participatingBeforeResizeItemRowsCount","ceil","columnsCount","firstCleanedUpBeforeResizeItemsRowIndex","k","verticalSpacing","firstDroppedBeforeResizeItemIndex","lastDroppedBeforeResizeItemIndex","slice","splice","scrollBy","newFirstShownItemIndex","newColumnsCount","prevBeforeResize","prevBeforeResizeItemsCount","prevBeforeResizeBeforeItemsHeight","prevBeforeResizeItemRowsCount","rowIndex","newBeforeResizeAdditionalBeforeItemsHeight","newBeforeResizeBeforeItemsHeight","newBeforeResizeBeforeItemRowsCount","Array","fill","concat","equalizeItemHeights","maxItemsCount","cleanUpBeforeResizeState"],"sources":["../source/BeforeResize.js"],"sourcesContent":["import log from './utility/debug.js'\r\n\r\nexport default class BeforeResize {\r\n\tconstructor({\r\n\t\tgetState,\r\n\t\tgetVerticalSpacing,\r\n\t\tgetColumnsCount\r\n\t}) {\r\n\t\tthis.getState = getState\r\n\t\tthis.getVerticalSpacing = getVerticalSpacing\r\n\t\tthis.getColumnsCount = getColumnsCount\r\n\t}\r\n\r\n\tinitializeFromState(state) {\r\n\t\tthis._includesBeforeResizeInState = Boolean(state.beforeResize)\r\n\t}\r\n\r\n\t// Cleans up \"before resize\" item heights and adjusts the scroll position accordingly.\r\n\t//\r\n\t// Hypothetically, it could also wait for the user to stop scrolling and only then\r\n\t// adjust the scroll position. The rationale is that if `window.scrollTo()` is called\r\n\t// while the user is scrolling, the user would occasionally experience \"lost\" mouse wheel\r\n\t// events when scrolling with a mouse wheel.\r\n\t//\r\n\t// Seems like Twitter's website waits for the user to stop scrolling before applying\r\n\t// the scroll position correction after a window resize. This library could do that too,\r\n\t// but that would require rewriting \"before items height\" top padding calculation\r\n\t// so that it doesn't re-calculate it on every re-render and instead does so incrementally,\r\n\t// and then, when the user stops, it re-calculates it from scratch removing the error\r\n\t// and adjusting the scroll position accordingly so that there's no \"jump of content\".\r\n\t//\r\n\t// But, seems like it works fine as it is and there's no need to rewrite anything.\r\n\t//\r\n\tcleanUpBeforeResizeItemHeights() {\r\n\t\tconst {\r\n\t\t\tfirstShownItemIndex,\r\n\t\t\tlastShownItemIndex,\r\n\t\t\titemHeights,\r\n\t\t\tbeforeResize\r\n\t\t} = this.getState()\r\n\r\n\t\t// If there're \"before resize\" properties in `state`\r\n\t\t// then it means that the corresponding items are waiting to be\r\n\t\t// re-measured after container resize. Since the resize,\r\n\t\t// some of those non-re-measured items might have just been measured,\r\n\t\t// so see if that's true, and if it is, remove those now-obsolete\r\n\t\t// \"before resize\" item heights and ajust the scroll position\r\n\t\t// so that there's no \"content jumping\".\r\n\r\n\t\tif (beforeResize) {\r\n\t\t\t// If the user has scrolled up to reveal a previously hidden item\r\n\t\t\t// that has not yet been re-measured after a previous resize.\r\n\t\t\tif (firstShownItemIndex < beforeResize.itemHeights.length) {\r\n\t\t\t\tlog('~ Clean up \"before resize\" item heights and correct scroll position ~')\r\n\r\n\t\t\t\t// Some of the \"before\" items have been un-hidden and re-measured.\r\n\t\t\t\t// Un-hiding those items would result in a \"jump of content\"\r\n\t\t\t\t// because \"before resize\" heights of those un-hidden items\r\n\t\t\t\t// could (and most likely will) be different from the current ones,\r\n\t\t\t\t// or because \"before resize\" columns count is different from\r\n\t\t\t\t// the current one.\r\n\t\t\t\t// To prevent a \"jump of content\", calculate the scroll position\r\n\t\t\t\t// difference and adjust the scroll position.\r\n\r\n\t\t\t\t// The height of the item rows that have transitioned\r\n\t\t\t\t// from hidden to shown.\r\n\t\t\t\tlet newlyShownItemRowsHeight = 0\r\n\r\n\t\t\t\t// Some of the `itemHeights` between the current `firstShownItemIndex` and\r\n\t\t\t\t// the previous `firstShownItemIndex` could stay `undefined` if the user\r\n\t\t\t\t// scrolled \"abruptly\": for example, by using a `window.scrollTo()` call.\r\n\t\t\t\t// In that case, the items below the visible ones won't be rendered and measured.\r\n\t\t\t\t// In such case, limit the items being iterated over to the current `lastShownItemIndex`\r\n\t\t\t\t// rather than the previous `firstShownItemIndex`.\r\n\t\t\t\tconst prevFirstReMeasuredItemsRowIndex = Math.floor(beforeResize.itemHeights.length / this.getColumnsCount())\r\n\t\t\t\tconst newlyShownItemsToIndex = Math.min(\r\n\t\t\t\t\tprevFirstReMeasuredItemsRowIndex * this.getColumnsCount() - 1,\r\n\t\t\t\t\tlastShownItemIndex\r\n\t\t\t\t)\r\n\r\n\t\t\t\tlet i = firstShownItemIndex\r\n\t\t\t\twhile (i <= newlyShownItemsToIndex) {\r\n\t\t\t\t\t// Calculate newly shown row height.\r\n\t\t\t\t\tlet rowHeight = 0\r\n\t\t\t\t\tlet columnIndex = 0\r\n\t\t\t\t\twhile (columnIndex < this.getColumnsCount() && i <= newlyShownItemsToIndex) {\r\n\t\t\t\t\t\tlet itemHeight = itemHeights[i]\r\n\t\t\t\t\t\tif (itemHeight === undefined) {\r\n\t\t\t\t\t\t\t// `itemHeight` can only be `undefined` when not `beforeResize`.\r\n\t\t\t\t\t\t\t// Use the current \"average item height\" as a substitute.\r\n\t\t\t\t\t\t\titemHeight = this.getAverageItemHeight()\r\n\t\t\t\t\t\t}\r\n\t\t\t\t\t\trowHeight = Math.max(rowHeight, itemHeight)\r\n\t\t\t\t\t\ti++\r\n\t\t\t\t\t\tcolumnIndex++\r\n\t\t\t\t\t}\r\n\t\t\t\t\t// Append to the total \"newly shown item rows height\".\r\n\t\t\t\t\tnewlyShownItemRowsHeight += rowHeight\r\n\t\t\t\t\tnewlyShownItemRowsHeight += this.getVerticalSpacing()\r\n\t\t\t\t}\r\n\r\n\t\t\t\t// The height of the \"before resize\" item rows\r\n\t\t\t\t// that will be \"cleaned up\" in this function call.\r\n\t\t\t\tlet cleanedUpBeforeResizeItemRowsHeight = 0\r\n\r\n\t\t\t\t// Some of the `beforeResize` item rows might have been skipped if the user\r\n\t\t\t\t// scrolled up \"abruptly\": for example, by using a `window.scrollTo()` call.\r\n\t\t\t\t// In that case, the \"before resize\" items below the bottom border of the screen\r\n\t\t\t\t// shouldn't be accounted for when calculating the scrollbar adjustment shift\r\n\t\t\t\t// because items after `lastShownItemIndex` aren't participating in the calculation\r\n\t\t\t\t// of `newlyShownItemRowsHeight`.\r\n\t\t\t\tconst maxParticipatingBeforeResizeItemsCount = Math.min(beforeResize.itemHeights.length, lastShownItemIndex + 1)\r\n\t\t\t\tconst participatingBeforeResizeItemRowsCount = Math.ceil(maxParticipatingBeforeResizeItemsCount / beforeResize.columnsCount)\r\n\r\n\t\t\t\tconst firstCleanedUpBeforeResizeItemsRowIndex = firstShownItemIndex === 0\r\n\t\t\t\t\t? 0\r\n\t\t\t\t\t: Math.floor((firstShownItemIndex - 1) / beforeResize.columnsCount) + 1\r\n\r\n\t\t\t\tlet k = firstCleanedUpBeforeResizeItemsRowIndex\r\n\t\t\t\twhile (k < participatingBeforeResizeItemRowsCount) {\r\n\t\t\t\t\tconst rowHeight = beforeResize.itemHeights[k * beforeResize.columnsCount]\r\n\t\t\t\t\tcleanedUpBeforeResizeItemRowsHeight += rowHeight\r\n\t\t\t\t\tcleanedUpBeforeResizeItemRowsHeight += beforeResize.verticalSpacing\r\n\t\t\t\t\tk++\r\n\t\t\t\t}\r\n\r\n\t\t\t\t// Schedule an asynchronous `this.updateState()` call that will update\r\n\t\t\t\t// `beforeResize` property of `state`. Ideally, it should be updated\r\n\t\t\t\t// immediately, but since `this.updateState()` calls are asynchronous,\r\n\t\t\t\t// the code updates just the underlying `beforeResize.itemHeights`\r\n\t\t\t\t// array immediately instead, which is still a hack but still a lesser one.\r\n\t\t\t\tif (firstShownItemIndex === 0) {\r\n\t\t\t\t\tlog('Drop all \"before resize\" item heights')\r\n\t\t\t\t} else {\r\n\t\t\t\t\tconst firstDroppedBeforeResizeItemIndex = firstShownItemIndex\r\n\t\t\t\t\tconst lastDroppedBeforeResizeItemIndex = beforeResize.itemHeights.length - 1\r\n\t\t\t\t\tif (firstDroppedBeforeResizeItemIndex === lastDroppedBeforeResizeItemIndex) {\r\n\t\t\t\t\t\tlog('For item index', firstDroppedBeforeResizeItemIndex, '— drop \"before resize\" height', beforeResize.itemHeights[firstDroppedBeforeResizeItemIndex], )\r\n\t\t\t\t\t} else {\r\n\t\t\t\t\t\tlog('For item indexes from', firstDroppedBeforeResizeItemIndex, 'to', lastDroppedBeforeResizeItemIndex, '— drop \"before resize\" heights', beforeResize.itemHeights.slice(firstDroppedBeforeResizeItemIndex))\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\r\n\t\t\t\t// Immediately update `beforeResize.itemHeights`\r\n\t\t\t\t// so that the component isn't left in an inconsistent state\r\n\t\t\t\t// before a `this.updateState()` call below is applied.\r\n\t\t\t\tbeforeResize.itemHeights.splice(\r\n\t\t\t\t\tfirstShownItemIndex,\r\n\t\t\t\t\tbeforeResize.itemHeights.length - firstShownItemIndex\r\n\t\t\t\t)\r\n\r\n\t\t\t\t// Return the \"scroll by\" amount that would correct the scroll position.\r\n\t\t\t\t// Also return a state update.\r\n\t\t\t\treturn {\r\n\t\t\t\t\tscrollBy: newlyShownItemRowsHeight - cleanedUpBeforeResizeItemRowsHeight,\r\n\t\t\t\t\tbeforeResize: firstShownItemIndex === 0 ? undefined : {\r\n\t\t\t\t\t\t// Simply change the \"reference\" to `beforeResize` while leaving\r\n\t\t\t\t\t\t// its contents unchanged. That simply indicates that it has been updated:\r\n\t\t\t\t\t\t// `beforeResize.itemHeights` array length has been changed \"directly\".\r\n\t\t\t\t\t\t...beforeResize\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\t// Snapshots \"before resize\" values in order to preserve the currently\r\n\t// shown items' vertical position on screen so that there's no \"content jumping\".\r\n\t//\r\n\t// `newFirstShownItemIndex` is `> 0`.\r\n\t//\r\n\tsnapshotBeforeResizeItemHeights({\r\n\t\tfirstShownItemIndex,\r\n\t\tnewFirstShownItemIndex,\r\n\t\tnewColumnsCount\r\n\t}) {\r\n\t\tconst columnsCount = this.getColumnsCount()\r\n\t\tconst verticalSpacing = this.getVerticalSpacing()\r\n\r\n\t\tthis._includesBeforeResizeInState = true\r\n\r\n\t\tconst {\r\n\t\t\tbeforeResize: prevBeforeResize,\r\n\t\t\titemHeights\r\n\t\t} = this.getState()\r\n\r\n\t\tconst prevBeforeResizeItemsCount = prevBeforeResize\r\n\t\t\t? prevBeforeResize.itemHeights.length\r\n\t\t\t: 0\r\n\r\n\t\t// If there already are \"before resize\" values in `state`\r\n\t\t// then it means that those should be merged with the new ones.\r\n\t\t//\r\n\t\t// `beforeResize.itemHeights` could be empty in an edge case\r\n\t\t// when there's a pending state update that sets `beforeResize`\r\n\t\t// to `undefined`, and in that case empty `beforeResize.itemHeights`\r\n\t\t// signals about that type of a situation.\r\n\t\t//\r\n\t\tif (prevBeforeResizeItemsCount > 0) {\r\n\t\t\t// Because the \"previous\" before resize values might have been captured\r\n\t\t\t// for a window width corresponding to a layout with a different columns count\r\n\t\t\t// and different vertical spacing, re-calculate those item heights as if\r\n\t\t\t// they corresponded to the current columns count and current vertical spacing,\r\n\t\t\t// since \"previous\" and \"new\" before resize item heights are gonna be merged.\r\n\t\t\tif (\r\n\t\t\t\tprevBeforeResize.columnsCount !== columnsCount ||\r\n\t\t\t\tprevBeforeResize.verticalSpacing !== verticalSpacing\r\n\t\t\t) {\r\n\t\t\t\tlet prevBeforeResizeBeforeItemsHeight = 0\r\n\r\n\t\t\t\tconst prevBeforeResizeItemRowsCount = Math.ceil(prevBeforeResizeItemsCount / prevBeforeResize.columnsCount)\r\n\t\t\t\tlet rowIndex = 0\r\n\t\t\t\twhile (rowIndex < prevBeforeResizeItemRowsCount) {\r\n\t\t\t\t\t// Since all \"before resize\" item heights are equal within a row,\r\n\t\t\t\t\t// the height of the first \"before resize\" item in a row is that row's height.\r\n\t\t\t\t\tconst rowHeight = prevBeforeResize.itemHeights[rowIndex * prevBeforeResize.columnsCount]\r\n\t\t\t\t\tprevBeforeResizeBeforeItemsHeight += rowHeight\r\n\t\t\t\t\tprevBeforeResizeBeforeItemsHeight += prevBeforeResize.verticalSpacing\r\n\t\t\t\t\trowIndex++\r\n\t\t\t\t}\r\n\r\n\t\t\t\tlet newBeforeResizeAdditionalBeforeItemsHeight = 0\r\n\t\t\t\tlet i = firstShownItemIndex\r\n\t\t\t\twhile (i < newFirstShownItemIndex) {\r\n\t\t\t\t\tlet rowHeight = 0\r\n\t\t\t\t\tlet k = 0\r\n\t\t\t\t\twhile (k < columnsCount && i < newFirstShownItemIndex) {\r\n\t\t\t\t\t\trowHeight = Math.max(rowHeight, itemHeights[i])\r\n\t\t\t\t\t\tk++\r\n\t\t\t\t\t\ti++\r\n\t\t\t\t\t}\r\n\t\t\t\t\tnewBeforeResizeAdditionalBeforeItemsHeight += rowHeight\r\n\t\t\t\t\tnewBeforeResizeAdditionalBeforeItemsHeight += verticalSpacing\r\n\t\t\t\t}\r\n\r\n\t\t\t\tconst newBeforeResizeBeforeItemsHeight = prevBeforeResizeBeforeItemsHeight + newBeforeResizeAdditionalBeforeItemsHeight\r\n\t\t\t\tconst newBeforeResizeBeforeItemRowsCount = Math.ceil(newFirstShownItemIndex / columnsCount)\r\n\r\n\t\t\t\treturn new Array(newFirstShownItemIndex).fill(\r\n\t\t\t\t\t// Re-calculate \"before resize\" item heights so that \"previous\" and \"new\" ones\r\n\t\t\t\t\t// correspond to the same (new) columns count.\r\n\t\t\t\t\t// Also don't occasionally set item heights to `< 0`.\r\n\t\t\t\t\tMath.max(0, newBeforeResizeBeforeItemsHeight / newBeforeResizeBeforeItemRowsCount - verticalSpacing)\r\n\t\t\t\t)\r\n\t\t\t} else {\r\n\t\t\t\t// Add new item heights to the previously snapshotted ones.\r\n\t\t\t\treturn prevBeforeResize.itemHeights.concat(\r\n\t\t\t\t\tequalizeItemHeights(\r\n\t\t\t\t\t\titemHeights,\r\n\t\t\t\t\t\tnewFirstShownItemIndex,\r\n\t\t\t\t\t\tcolumnsCount\r\n\t\t\t\t\t).slice(prevBeforeResize.itemHeights.length)\r\n\t\t\t\t)\r\n\t\t\t}\r\n\t\t} else {\r\n\t\t\treturn equalizeItemHeights(\r\n\t\t\t\titemHeights,\r\n\t\t\t\tnewFirstShownItemIndex,\r\n\t\t\t\tcolumnsCount\r\n\t\t\t)\r\n\t\t}\r\n\t}\r\n\r\n\tshouldIncludeBeforeResizeValuesInState() {\r\n\t\treturn this._includesBeforeResizeInState\r\n\t}\r\n}\r\n\r\n// Equalizes all item heights within a given row, for each row.\r\n//\r\n// The reason is that `beforeResize.itemHeights` is not necessarily divisible by\r\n// `beforeResize.columnsCount`, which would result in varying last row height\r\n// as items get removed from `beforeResize.itemHeights` as the user scrolls up.\r\n//\r\n// By equalizing all item heights within a given row, for each row, such \"jumping\"\r\n// last \"before resize\" row height is prevented when the user scrolls up.\r\n//\r\nfunction equalizeItemHeights(itemHeights, maxItemsCount, columnsCount) {\r\n\titemHeights = itemHeights.slice(0, Math.ceil(maxItemsCount / columnsCount) * columnsCount)\r\n\r\n\tlet rowIndex = 0\r\n\twhile (rowIndex * columnsCount < maxItemsCount) {\r\n\t\t// Calculate row height.\r\n\t\tlet rowHeight = 0\r\n\t\tlet k = 0\r\n\t\twhile (k < columnsCount) {\r\n\t\t\trowHeight = Math.max(rowHeight, itemHeights[rowIndex * columnsCount + k])\r\n\t\t\tk++\r\n\t\t}\r\n\r\n\t\t// Equalize all item heights within the row.\r\n\t\tk = 0\r\n\t\twhile (k < columnsCount) {\r\n\t\t\titemHeights[rowIndex * columnsCount + k] = rowHeight\r\n\t\t\tk++\r\n\t\t}\r\n\r\n\t\t// Proceed with the next row.\r\n\t\trowIndex++\r\n\t}\r\n\r\n\treturn itemHeights.slice(0, maxItemsCount)\r\n}\r\n\r\nexport function cleanUpBeforeResizeState(state) {\r\n\tif (state.beforeResize) {\r\n\t\tif (state.beforeResize.itemHeights.length === 0) {\r\n\t\t\tstate.beforeResize = undefined\r\n\t\t}\r\n\t}\r\n\treturn state\r\n}"],"mappings":";;;;;;;;;;;;AAAA,OAAOA,GAAP,MAAgB,oBAAhB;;IAEqBC,Y;EACpB,4BAIG;IAAA,IAHFC,QAGE,QAHFA,QAGE;IAAA,IAFFC,kBAEE,QAFFA,kBAEE;IAAA,IADFC,eACE,QADFA,eACE;;IAAA;;IACF,KAAKF,QAAL,GAAgBA,QAAhB;IACA,KAAKC,kBAAL,GAA0BA,kBAA1B;IACA,KAAKC,eAAL,GAAuBA,eAAvB;EACA;;;;WAED,6BAAoBC,KAApB,EAA2B;MAC1B,KAAKC,4BAAL,GAAoCC,OAAO,CAACF,KAAK,CAACG,YAAP,CAA3C;IACA,C,CAED;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;IACA;;;;WACA,0CAAiC;MAChC,qBAKI,KAAKN,QAAL,EALJ;MAAA,IACCO,mBADD,kBACCA,mBADD;MAAA,IAECC,kBAFD,kBAECA,kBAFD;MAAA,IAGCC,WAHD,kBAGCA,WAHD;MAAA,IAICH,YAJD,kBAICA,YAJD,CADgC,CAQhC;MACA;MACA;MACA;MACA;MACA;MACA;;;MAEA,IAAIA,YAAJ,EAAkB;QACjB;QACA;QACA,IAAIC,mBAAmB,GAAGD,YAAY,CAACG,WAAb,CAAyBC,MAAnD,EAA2D;UAC1DZ,GAAG,CAAC,uEAAD,CAAH,CAD0D,CAG1D;UACA;UACA;UACA;UACA;UACA;UACA;UACA;UAEA;UACA;;UACA,IAAIa,wBAAwB,GAAG,CAA/B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,WAAW,CAACyB,KAAZ,CAAkB,CAAlB,EAAqBrB,IAAI,CAACc,IAAL,CAAUwB,aAAa,GAAGvB,YAA1B,IAA0CA,YAA/D,CAAd;EAEA,IAAIe,QAAQ,GAAG,CAAf;;EACA,OAAOA,QAAQ,GAAGf,YAAX,GAA0BuB,aAAjC,EAAgD;IAC/C;IACA,IAAIjC,SAAS,GAAG,CAAhB;IACA,IAAIY,CAAC,GAAG,CAAR;;IACA,OAAOA,CAAC,GAAGF,YAAX,EAAyB;MACxBV,SAAS,GAAGL,IAAI,CAACU,GAAL,CAASL,SAAT,EAAoBT,WAAW,CAACkC,QAAQ,GAAGf,YAAX,GAA0BE,CAA3B,CAA/B,CAAZ;MACAA,CAAC;IACD,CAP8C,CAS/C;;;IACAA,CAAC,GAAG,CAAJ;;IACA,OAAOA,CAAC,GAAGF,YAAX,EAAyB;MACxBnB,WAAW,CAACkC,QAAQ,GAAGf,YAAX,GAA0BE,CAA3B,CAAX,GAA2CZ,SAA3C;MACAY,CAAC;IACD,CAd8C,CAgB/C;;;IACAa,QAAQ;EACR;;EAED,OAAOlC,WAAW,CAACyB,KAAZ,CAAkB,CAAlB,EAAqBiB,aAArB,CAAP;AACA;;AAED,OAAO,SAASC,wBAAT,CAAkCjD,KAAlC,EAAyC;EAC/C,IAAIA,KAAK,CAACG,YAAV,EAAwB;IACvB,IAAIH,KAAK,CAACG,YAAN,CAAmBG,WAAnB,CAA+BC,MAA/B,KAA0C,CAA9C,EAAiD;MAChDP,KAAK,CAACG,YAAN,GAAqBe,SAArB;IACA;EACD;;EACD,OAAOlB,KAAP;AACA"}
|
package/modules/DOM/Engine.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import ItemsContainer from './ItemsContainer';
|
|
2
|
-
import ScrollableContainer, { ScrollableWindowContainer } from './ScrollableContainer';
|
|
3
|
-
import ListTopOffsetWatcher from './ListTopOffsetWatcher';
|
|
1
|
+
import ItemsContainer from './ItemsContainer.js';
|
|
2
|
+
import ScrollableContainer, { ScrollableWindowContainer } from './ScrollableContainer.js';
|
|
3
|
+
import ListTopOffsetWatcher from './ListTopOffsetWatcher.js';
|
|
4
4
|
export default {
|
|
5
5
|
createItemsContainer: function createItemsContainer(getItemsContainerElement) {
|
|
6
6
|
return new ItemsContainer(getItemsContainerElement);
|
|
@@ -8,9 +8,9 @@ export default {
|
|
|
8
8
|
// Creates a `scrollableContainer`.
|
|
9
9
|
// On client side, `scrollableContainer` is always created.
|
|
10
10
|
// On server side, `scrollableContainer` is not created (and not used).
|
|
11
|
-
createScrollableContainer: function createScrollableContainer(
|
|
12
|
-
if (
|
|
13
|
-
return new ScrollableContainer(
|
|
11
|
+
createScrollableContainer: function createScrollableContainer(getScrollableContainerElement, getItemsContainerElement) {
|
|
12
|
+
if (getScrollableContainerElement) {
|
|
13
|
+
return new ScrollableContainer(getScrollableContainerElement, getItemsContainerElement);
|
|
14
14
|
} else if (typeof window !== 'undefined') {
|
|
15
15
|
return new ScrollableWindowContainer(getItemsContainerElement);
|
|
16
16
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"Engine.js","names":["ItemsContainer","ScrollableContainer","ScrollableWindowContainer","ListTopOffsetWatcher","createItemsContainer","getItemsContainerElement","createScrollableContainer","getScrollableContainerElement","window","watchListTopOffset","getListTopOffset","onListTopOffsetChange"],"sources":["../../source/DOM/Engine.js"],"sourcesContent":["import ItemsContainer from './ItemsContainer.js'\r\nimport ScrollableContainer, { ScrollableWindowContainer } from './ScrollableContainer.js'\r\nimport ListTopOffsetWatcher from './ListTopOffsetWatcher.js'\r\n\r\nexport default {\r\n\tcreateItemsContainer(getItemsContainerElement) {\r\n\t\treturn new ItemsContainer(getItemsContainerElement)\r\n\t},\r\n\r\n\t// Creates a `scrollableContainer`.\r\n\t// On client side, `scrollableContainer` is always created.\r\n\t// On server side, `scrollableContainer` is not created (and not used).\r\n\tcreateScrollableContainer(getScrollableContainerElement, getItemsContainerElement) {\r\n\t\tif (getScrollableContainerElement) {\r\n\t\t\treturn new ScrollableContainer(getScrollableContainerElement, getItemsContainerElement)\r\n\t\t} else if (typeof window !== 'undefined') {\r\n\t\t\treturn new ScrollableWindowContainer(getItemsContainerElement)\r\n\t\t}\r\n\t},\r\n\r\n\twatchListTopOffset({\r\n\t\tgetListTopOffset,\r\n\t\tonListTopOffsetChange\r\n\t}) {\r\n\t\treturn new ListTopOffsetWatcher({\r\n\t\t\tgetListTopOffset,\r\n\t\t\tonListTopOffsetChange\r\n\t\t})\r\n\t}\r\n}"],"mappings":"AAAA,OAAOA,cAAP,MAA2B,qBAA3B;AACA,OAAOC,mBAAP,IAA8BC,yBAA9B,QAA+D,0BAA/D;AACA,OAAOC,oBAAP,MAAiC,2BAAjC;AAEA,eAAe;EACdC,oBADc,gCACOC,wBADP,EACiC;IAC9C,OAAO,IAAIL,cAAJ,CAAmBK,wBAAnB,CAAP;EACA,CAHa;EAKd;EACA;EACA;EACAC,yBARc,qCAQYC,6BARZ,EAQ2CF,wBAR3C,EAQqE;IAClF,IAAIE,6BAAJ,EAAmC;MAClC,OAAO,IAAIN,mBAAJ,CAAwBM,6BAAxB,EAAuDF,wBAAvD,CAAP;IACA,CAFD,MAEO,IAAI,OAAOG,MAAP,KAAkB,WAAtB,EAAmC;MACzC,OAAO,IAAIN,yBAAJ,CAA8BG,wBAA9B,CAAP;IACA;EACD,CAda;EAgBdI,kBAhBc,oCAmBX;IAAA,IAFFC,gBAEE,QAFFA,gBAEE;IAAA,IADFC,qBACE,QADFA,qBACE;IACF,OAAO,IAAIR,oBAAJ,CAAyB;MAC/BO,gBAAgB,EAAhBA,gBAD+B;MAE/BC,qBAAqB,EAArBA;IAF+B,CAAzB,CAAP;EAIA;AAxBa,CAAf"}
|
|
@@ -2,7 +2,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
|
|
|
2
2
|
|
|
3
3
|
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
4
4
|
|
|
5
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
5
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
6
6
|
|
|
7
7
|
var ItemsContainer = /*#__PURE__*/function () {
|
|
8
8
|
/**
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"ItemsContainer.js","names":["ItemsContainer","getElement","renderedElementIndex","childNodes","getBoundingClientRect","top","height","firstChild","removeChild"],"sources":["../../source/DOM/ItemsContainer.js"],"sourcesContent":["export default class ItemsContainer {\r\n\t/**\r\n\t * Constructs a new \"container\" from an element.\r\n\t * @param {function} getElement\r\n\t */\r\n\tconstructor(getElement) {\r\n\t\tthis.getElement = getElement\r\n\t}\r\n\r\n\t/**\r\n\t * Returns an item element's \"top offset\", relative to the items `container`'s top edge.\r\n\t * @param {number} renderedElementIndex — An index of an item relative to the \"first shown item index\". For example, if the list is showing items from index 8 to index 12 then `renderedElementIndex = 0` would mean the item at index `8`.\r\n\t * @return {number}\r\n\t */\r\n\tgetNthRenderedItemTopOffset(renderedElementIndex) {\r\n\t\treturn this.getElement().childNodes[renderedElementIndex].getBoundingClientRect().top - this.getElement().getBoundingClientRect().top\r\n\t}\r\n\r\n\t/**\r\n\t * Returns an item element's height.\r\n\t * @param {number} renderedElementIndex — An index of an item relative to the \"first shown item index\". For example, if the list is showing items from index 8 to index 12 then `renderedElementIndex = 0` would mean the item at index `8`.\r\n\t * @return {number}\r\n\t */\r\n\tgetNthRenderedItemHeight(renderedElementIndex) {\r\n\t\t// `offsetHeight` is not precise enough (doesn't return fractional pixels).\r\n\t\t// return this.getElement().childNodes[renderedElementIndex].offsetHeight\r\n\t\treturn this.getElement().childNodes[renderedElementIndex].getBoundingClientRect().height\r\n\t}\r\n\r\n\t/**\r\n\t * Returns items container height.\r\n\t * @return {number}\r\n\t */\r\n\tgetHeight() {\r\n\t\t// `offsetHeight` is not precise enough (doesn't return fractional pixels).\r\n\t\t// return this.getElement().offsetHeight\r\n\t\treturn this.getElement().getBoundingClientRect().height\r\n\t}\r\n\r\n\t/**\r\n\t * Removes all item elements of an items container.\r\n\t */\r\n\tclear() {\r\n\t\twhile (this.getElement().firstChild) {\r\n\t\t\tthis.getElement().removeChild(this.getElement().firstChild)\r\n\t\t}\r\n\t}\r\n}"],"mappings":";;;;;;IAAqBA,c;EACpB;AACD;AACA;AACA;EACC,wBAAYC,UAAZ,EAAwB;IAAA;;IACvB,KAAKA,UAAL,GAAkBA,UAAlB;EACA;EAED;AACD;AACA;AACA;AACA;;;;;WACC,qCAA4BC,oBAA5B,EAAkD;MACjD,OAAO,KAAKD,UAAL,GAAkBE,UAAlB,CAA6BD,oBAA7B,EAAmDE,qBAAnD,GAA2EC,GAA3E,GAAiF,KAAKJ,UAAL,GAAkBG,qBAAlB,GAA0CC,GAAlI;IACA;IAED;AACD;AACA;AACA;AACA;;;;WACC,kCAAyBH,oBAAzB,EAA+C;MAC9C;MACA;MACA,OAAO,KAAKD,UAAL,GAAkBE,UAAlB,CAA6BD,oBAA7B,EAAmDE,qBAAnD,GAA2EE,MAAlF;IACA;IAED;AACD;AACA;AACA;;;;WACC,qBAAY;MACX;MACA;MACA,OAAO,KAAKL,UAAL,GAAkBG,qBAAlB,GAA0CE,MAAjD;IACA;IAED;AACD;AACA;;;;WACC,iBAAQ;MACP,OAAO,KAAKL,UAAL,GAAkBM,UAAzB,EAAqC;QACpC,KAAKN,UAAL,GAAkBO,WAAlB,CAA8B,KAAKP,UAAL,GAAkBM,UAAhD;MACA;IACD;;;;;;SA9CmBP,c"}
|
|
@@ -2,7 +2,7 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Cons
|
|
|
2
2
|
|
|
3
3
|
function _defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } }
|
|
4
4
|
|
|
5
|
-
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; }
|
|
5
|
+
function _createClass(Constructor, protoProps, staticProps) { if (protoProps) _defineProperties(Constructor.prototype, protoProps); if (staticProps) _defineProperties(Constructor, staticProps); Object.defineProperty(Constructor, "prototype", { writable: false }); return Constructor; }
|
|
6
6
|
|
|
7
7
|
// For some weird reason, in Chrome, `setTimeout()` would lag up to a second (or more) behind.
|
|
8
8
|
// Turns out, Chrome developers have deprecated `setTimeout()` API entirely without asking anyone.
|
|
@@ -20,18 +20,19 @@ var WATCH_LIST_TOP_OFFSET_MAX_DURATION = 3000; // `VirtualScroller` calls `this.
|
|
|
20
20
|
// returns "incorrect" `top` position because the styles haven't been applied yet.
|
|
21
21
|
//
|
|
22
22
|
// For example, consider a page:
|
|
23
|
+
//
|
|
23
24
|
// <div class="page">
|
|
24
25
|
// <nav class="sidebar">...</nav>
|
|
25
26
|
// <main>...</main>
|
|
26
27
|
// </div>
|
|
27
28
|
//
|
|
28
|
-
// The sidebar is styled as `position: fixed`, but until
|
|
29
|
-
// the page styles have been applied it's gonna be a regular `<div/>`
|
|
29
|
+
// The sidebar is styled as `position: fixed`, but, until
|
|
30
|
+
// the page styles have been applied, it's gonna be a regular `<div/>`
|
|
30
31
|
// meaning that `<main/>` will be rendered below the sidebar
|
|
31
|
-
// and will appear offscreen and so it will only render the first item.
|
|
32
|
+
// and will appear offscreen, and so it will only render the first item.
|
|
32
33
|
//
|
|
33
|
-
// Then, the page styles are loaded and applied and the sidebar
|
|
34
|
-
// is now `position: fixed
|
|
34
|
+
// Then, the page styles are loaded and applied, and the sidebar
|
|
35
|
+
// is now `position: fixed`, so `<main/>` is now rendered at the top of the page,
|
|
35
36
|
// but `VirtualScroller`'s `.render()` has already been called
|
|
36
37
|
// and it won't re-render until the user scrolls or the window is resized.
|
|
37
38
|
//
|
|
@@ -86,13 +87,18 @@ var ListTopOffsetWatcher = /*#__PURE__*/function () {
|
|
|
86
87
|
}, {
|
|
87
88
|
key: "start",
|
|
88
89
|
value: function start() {
|
|
89
|
-
this.
|
|
90
|
+
this._isActive = true;
|
|
90
91
|
this.watchListTopOffset();
|
|
91
92
|
}
|
|
93
|
+
}, {
|
|
94
|
+
key: "isStarted",
|
|
95
|
+
value: function isStarted() {
|
|
96
|
+
return this._isActive;
|
|
97
|
+
}
|
|
92
98
|
}, {
|
|
93
99
|
key: "stop",
|
|
94
100
|
value: function stop() {
|
|
95
|
-
this.
|
|
101
|
+
this._isActive = false;
|
|
96
102
|
|
|
97
103
|
if (this.watchListTopOffsetTimer) {
|
|
98
104
|
clearTimeout(this.watchListTopOffsetTimer);
|
|
@@ -109,7 +115,7 @@ var ListTopOffsetWatcher = /*#__PURE__*/function () {
|
|
|
109
115
|
var check = function check() {
|
|
110
116
|
// If `VirtualScroller` has been unmounted
|
|
111
117
|
// while `setTimeout()` was waiting, then exit.
|
|
112
|
-
if (!_this.
|
|
118
|
+
if (!_this._isActive) {
|
|
113
119
|
return;
|
|
114
120
|
} // Skip comparing `top` coordinate of the list
|
|
115
121
|
// when this function is called for the first time.
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"file":"ListTopOffsetWatcher.js","names":["setTimeout","clearTimeout","WATCH_LIST_TOP_OFFSET_INTERVAL","WATCH_LIST_TOP_OFFSET_MAX_DURATION","ListTopOffsetWatcher","getListTopOffset","onListTopOffsetChange","listTopOffset","listTopOffsetInsideScrollableContainer","undefined","start","_isActive","watchListTopOffset","watchListTopOffsetTimer","startedAt","Date","now","check"],"sources":["../../source/DOM/ListTopOffsetWatcher.js"],"sourcesContent":["// For some weird reason, in Chrome, `setTimeout()` would lag up to a second (or more) behind.\r\n// Turns out, Chrome developers have deprecated `setTimeout()` API entirely without asking anyone.\r\n// Replacing `setTimeout()` with `requestAnimationFrame()` can work around that Chrome bug.\r\n// https://github.com/bvaughn/react-virtualized/issues/722\r\nimport { setTimeout, clearTimeout } from 'request-animation-frame-timeout'\r\n\r\n// Refreshing two times every seconds seems reasonable.\r\nconst WATCH_LIST_TOP_OFFSET_INTERVAL = 500\r\n\r\n// Refreshing for 3 seconds after the initial page load seems reasonable.\r\nconst WATCH_LIST_TOP_OFFSET_MAX_DURATION = 3000\r\n\r\n// `VirtualScroller` calls `this.layout.layOut()` on mount,\r\n// but if the page styles are applied after `VirtualScroller` mounts\r\n// (for example, if styles are applied via javascript, like Webpack does)\r\n// then the list might not render correctly and it will only show the first item.\r\n// The reason is that in that case calling `.getListTopOffset()` on mount\r\n// returns \"incorrect\" `top` position because the styles haven't been applied yet.\r\n//\r\n// For example, consider a page:\r\n//\r\n// <div class=\"page\">\r\n// <nav class=\"sidebar\">...</nav>\r\n// <main>...</main>\r\n// </div>\r\n//\r\n// The sidebar is styled as `position: fixed`, but, until\r\n// the page styles have been applied, it's gonna be a regular `<div/>`\r\n// meaning that `<main/>` will be rendered below the sidebar\r\n// and will appear offscreen, and so it will only render the first item.\r\n//\r\n// Then, the page styles are loaded and applied, and the sidebar\r\n// is now `position: fixed`, so `<main/>` is now rendered at the top of the page,\r\n// but `VirtualScroller`'s `.render()` has already been called\r\n// and it won't re-render until the user scrolls or the window is resized.\r\n//\r\n// This type of a bug doesn't seem to occur in production, but it can appear\r\n// in development mode when using Webpack. The workaround `VirtualScroller`\r\n// implements for such cases is calling `.getListTopOffset()`\r\n// on the list container DOM element periodically (every second) to check\r\n// if the `top` coordinate has changed as a result of CSS being applied:\r\n// if it has then it recalculates the shown item indexes.\r\n//\r\n// Maybe this bug could occur in production when using Webpack chunks.\r\n// That depends on how a style of a chunk is added to the page:\r\n// if it's added via `javascript` after the page has been rendered\r\n// then this workaround will also work for that case.\r\n//\r\n// Another example would be a page having a really tall expanded \"accordion\"\r\n// section, below which a `VirtualScroller` list resides. If the user un-expands\r\n// such expanded \"accordion\" section, the list would become visible but\r\n// it wouldn't get re-rendered because no `scroll` event has occured,\r\n// and the list only re-renders automatically on `scroll` events.\r\n// To work around such cases, call `virtualScroller.updateLayout()` method manually.\r\n// The workaround below could be extended to refresh the list's top coordinate\r\n// indefinitely and at higher intervals, but why waste CPU time on that.\r\n// There doesn't seem to be any DOM API for tracking an element's top position.\r\n// There is `IntersectionObserver` API but it doesn't exactly do that.\r\n//\r\nexport default class ListTopOffsetWatcher {\r\n\tconstructor({\r\n\t\tgetListTopOffset,\r\n\t\tonListTopOffsetChange\r\n\t}) {\r\n\t\tthis.getListTopOffset = getListTopOffset\r\n\t\tthis.onListTopOffsetChange = onListTopOffsetChange\r\n\t}\r\n\r\n\tonListTopOffset(listTopOffset) {\r\n\t\tif (this.listTopOffsetInsideScrollableContainer === undefined) {\r\n\t\t\t// Start periodical checks of the list's top offset\r\n\t\t\t// in order to perform a re-layout in case it changes.\r\n\t\t\t// See the comments in `ListTopOffsetWatcher.js` file\r\n\t\t\t// on why can the list's top offset change, and in which circumstances.\r\n\t\t\tthis.start()\r\n\t\t}\r\n\t\tthis.listTopOffsetInsideScrollableContainer = listTopOffset\r\n\t}\r\n\r\n\tstart() {\r\n\t\tthis._isActive = true\r\n\t\tthis.watchListTopOffset()\r\n\t}\r\n\r\n\tisStarted() {\r\n\t\treturn this._isActive\r\n\t}\r\n\r\n\tstop() {\r\n\t\tthis._isActive = false\r\n\r\n\t\tif (this.watchListTopOffsetTimer) {\r\n\t\t\tclearTimeout(this.watchListTopOffsetTimer)\r\n\t\t\tthis.watchListTopOffsetTimer = undefined\r\n\t\t}\r\n\t}\r\n\r\n\twatchListTopOffset() {\r\n\t\tconst startedAt = Date.now()\r\n\t\tconst check = () => {\r\n\t\t\t// If `VirtualScroller` has been unmounted\r\n\t\t\t// while `setTimeout()` was waiting, then exit.\r\n\t\t\tif (!this._isActive) {\r\n\t\t\t\treturn\r\n\t\t\t}\r\n\t\t\t// Skip comparing `top` coordinate of the list\r\n\t\t\t// when this function is called for the first time.\r\n\t\t\tif (this.listTopOffsetInsideScrollableContainer !== undefined) {\r\n\t\t\t\t// Calling `this.getListTopOffset()` on an element\r\n\t\t\t\t// runs about 0.003 milliseconds on a modern desktop CPU,\r\n\t\t\t\t// so I guess it's fine calling it twice a second.\r\n\t\t\t\tif (this.getListTopOffset() !== this.listTopOffsetInsideScrollableContainer) {\r\n\t\t\t\t\tthis.onListTopOffsetChange()\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t\t// Compare `top` coordinate of the list twice a second\r\n\t\t\t// to find out if it has changed as a result of loading CSS styles.\r\n\t\t\t// The total duration of 3 seconds would be enough for any styles to load, I guess.\r\n\t\t\t// There could be other cases changing the `top` coordinate\r\n\t\t\t// of the list (like collapsing an \"accordeon\" panel above the list\r\n\t\t\t// without scrolling the page), but those cases should be handled\r\n\t\t\t// by manually calling `.updateLayout()` instance method on `VirtualScroller` instance.\r\n\t\t\tif (Date.now() - startedAt < WATCH_LIST_TOP_OFFSET_MAX_DURATION) {\r\n\t\t\t\tthis.watchListTopOffsetTimer = setTimeout(check, WATCH_LIST_TOP_OFFSET_INTERVAL)\r\n\t\t\t}\r\n\t\t}\r\n\t\t// Run the cycle.\r\n\t\tcheck()\r\n\t}\r\n}"],"mappings":";;;;;;AAAA;AACA;AACA;AACA;AACA,SAASA,UAAT,EAAqBC,YAArB,QAAyC,iCAAzC,C,CAEA;;AACA,IAAMC,8BAA8B,GAAG,GAAvC,C,CAEA;;AACA,IAAMC,kCAAkC,GAAG,IAA3C,C,CAEA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;AACA;A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