123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118 |
- /* 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 | null, cb: (state: ScrollStateType) => void,
- ): ScrollStateType => {
- let rAF: number | null = null;
- const element = viewAreaElement as HTMLElement;
- const state = {
- right: true,
- down: true,
- lastX: element.scrollLeft,
- lastY: element.scrollTop,
- subscriber: {},
- };
- const debounceScroll = (): void => {
- if (rAF) {
- return;
- }
- // schedule an invocation of scroll for next animation frame.
- rAF = window.requestAnimationFrame(() => {
- rAF = null;
- const currentX = element.scrollLeft;
- const { lastX } = state;
- if (currentX !== lastX) {
- state.right = currentX > lastX;
- }
- state.lastX = currentX;
- const currentY = element.scrollTop;
- const { lastY } = state;
- if (currentY !== lastY) {
- state.down = currentY > lastY;
- }
- state.lastY = currentY;
- cb(state);
- });
- };
- const subscriber = fromEvent(element, 'scroll').pipe(
- throttleTime(200),
- auditTime(300),
- ).subscribe(debounceScroll);
- state.subscriber = subscriber;
- 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;
- };
- export const hexToRgb = (hex: string): any => {
- const result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
- return result ? {
- r: parseInt(result[1], 16),
- g: parseInt(result[2], 16),
- b: parseInt(result[3], 16),
- } : null;
- };
- export const downloadFileWithUri = (name: string, uri: string): void => {
- const ele = document.createElement('a');
- ele.download = name;
- ele.href = uri;
- document.body.appendChild(ele);
- ele.click();
- document.body.removeChild(ele);
- ele.remove();
- };
|