|
@@ -1,3 +1,4 @@
|
|
|
+/* eslint-disable no-underscore-dangle */
|
|
|
import { fromEvent } from 'rxjs';
|
|
|
import {
|
|
|
auditTime,
|
|
@@ -6,10 +7,20 @@ import {
|
|
|
|
|
|
import { ScrollStateType } from '../constants/type';
|
|
|
|
|
|
-export const objIsEmpty = (obj: Object) => !Object.keys(obj).length;
|
|
|
+export const objIsEmpty = (obj: Record<string, any>): boolean => !Object.keys(obj).length;
|
|
|
|
|
|
-export const watchScroll = (viewAreaElement: HTMLElement, cb: (state: ScrollStateType) => void) => {
|
|
|
- const debounceScroll = () => {
|
|
|
+export const watchScroll = (
|
|
|
+ viewAreaElement: HTMLElement, cb: (state: ScrollStateType) => void,
|
|
|
+): ScrollStateType => {
|
|
|
+ let rAF: number | null = null;
|
|
|
+ const state = {
|
|
|
+ right: true,
|
|
|
+ down: true,
|
|
|
+ lastX: viewAreaElement.scrollLeft,
|
|
|
+ lastY: viewAreaElement.scrollTop,
|
|
|
+ };
|
|
|
+
|
|
|
+ const debounceScroll = (): void => {
|
|
|
if (rAF) {
|
|
|
return;
|
|
|
}
|
|
@@ -17,14 +28,14 @@ export const watchScroll = (viewAreaElement: HTMLElement, cb: (state: ScrollStat
|
|
|
rAF = window.requestAnimationFrame(() => {
|
|
|
rAF = null;
|
|
|
|
|
|
- let currentX = viewAreaElement.scrollLeft;
|
|
|
- let lastX = state.lastX;
|
|
|
+ const currentX = viewAreaElement.scrollLeft;
|
|
|
+ const { lastX } = state;
|
|
|
if (currentX !== lastX) {
|
|
|
state.right = currentX > lastX;
|
|
|
}
|
|
|
state.lastX = currentX;
|
|
|
- let currentY = viewAreaElement.scrollTop;
|
|
|
- let lastY = state.lastY;
|
|
|
+ const currentY = viewAreaElement.scrollTop;
|
|
|
+ const { lastY } = state;
|
|
|
if (currentY !== lastY) {
|
|
|
state.down = currentY > lastY;
|
|
|
}
|
|
@@ -33,14 +44,6 @@ export const watchScroll = (viewAreaElement: HTMLElement, cb: (state: ScrollStat
|
|
|
});
|
|
|
};
|
|
|
|
|
|
- let state = {
|
|
|
- right: true,
|
|
|
- down: true,
|
|
|
- lastX: viewAreaElement.scrollLeft,
|
|
|
- lastY: viewAreaElement.scrollTop,
|
|
|
- };
|
|
|
- let rAF: number | null = null;
|
|
|
-
|
|
|
fromEvent(viewAreaElement, 'scroll').pipe(
|
|
|
auditTime(300),
|
|
|
throttleTime(200),
|
|
@@ -49,22 +52,24 @@ export const watchScroll = (viewAreaElement: HTMLElement, cb: (state: ScrollStat
|
|
|
return state;
|
|
|
};
|
|
|
|
|
|
-export const scrollIntoView = (element: HTMLElement, spot?: {top: number}, skipOverflowHiddenElements: boolean = false) => {
|
|
|
- let parent: HTMLElement = <HTMLElement>element.offsetParent;
|
|
|
+export const scrollIntoView = (
|
|
|
+ element: HTMLElement, spot?: {top: number}, skipOverflowHiddenElements = false,
|
|
|
+): void => {
|
|
|
+ let parent: HTMLElement = element.offsetParent as HTMLElement;
|
|
|
let offsetY = element.offsetTop + element.clientTop;
|
|
|
|
|
|
if (!parent) {
|
|
|
return; // no need to scroll
|
|
|
}
|
|
|
- while ((parent.clientHeight === parent.scrollHeight &&
|
|
|
- parent.clientWidth === parent.scrollWidth) ||
|
|
|
- (skipOverflowHiddenElements &&
|
|
|
- getComputedStyle(parent).overflow === 'hidden')) {
|
|
|
+ while (
|
|
|
+ (parent.clientHeight === parent.scrollHeight && parent.clientWidth === parent.scrollWidth)
|
|
|
+ || (skipOverflowHiddenElements && getComputedStyle(parent).overflow === 'hidden')
|
|
|
+ ) {
|
|
|
if (parent.dataset._scaleY) {
|
|
|
- offsetY /= parseInt(parent.dataset._scaleY);
|
|
|
+ offsetY /= parseInt(parent.dataset._scaleY, 10);
|
|
|
}
|
|
|
offsetY += parent.offsetTop;
|
|
|
- parent = <HTMLElement>parent.offsetParent;
|
|
|
+ parent = parent.offsetParent as HTMLElement;
|
|
|
if (!parent) {
|
|
|
return; // no need to scroll
|
|
|
}
|