1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495 |
- /* eslint-disable no-underscore-dangle */
- import { fromEvent } from 'rxjs';
- import {
- auditTime,
- throttleTime,
- } from 'rxjs/operators';
- import { ScrollStateType } from '../constants/type';
- export const objIsEmpty = (obj: Record<string, any>): boolean => !Object.keys(obj).length;
- 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;
- }
- // schedule an invocation of scroll for next animation frame.
- rAF = window.requestAnimationFrame(() => {
- rAF = null;
- const currentX = viewAreaElement.scrollLeft;
- const { lastX } = state;
- if (currentX !== lastX) {
- state.right = currentX > lastX;
- }
- state.lastX = currentX;
- const currentY = viewAreaElement.scrollTop;
- const { lastY } = state;
- if (currentY !== lastY) {
- state.down = currentY > lastY;
- }
- state.lastY = currentY;
- cb(state);
- });
- };
- fromEvent(viewAreaElement, 'scroll').pipe(
- auditTime(300),
- throttleTime(200),
- ).subscribe(debounceScroll);
- return state;
- };
- 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')
- ) {
- if (parent.dataset._scaleY) {
- offsetY /= parseInt(parent.dataset._scaleY, 10);
- }
- offsetY += parent.offsetTop;
- parent = parent.offsetParent as HTMLElement;
- if (!parent) {
- return; // no need to scroll
- }
- }
- if (spot) {
- if (spot.top !== undefined) {
- offsetY += spot.top;
- }
- }
- parent.scrollTop = offsetY;
- };
- export const scaleCheck = (scale: number): number => {
- if (typeof scale === 'number' && scale >= 50 && scale <= 250) {
- return Math.round(scale * 100) / 10000;
- }
- if (scale < 50) {
- return 0.5;
- }
- return 2.5;
- };
|