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AAe,CAAC,CAAC;aACvE;SACF;QAED,IAAI,CAAC,UAAU,GAAG,UAAU,CAAC,IAAI,CAAC,gBAAgB,EAAE,sBAAsB,CAAC,CAAC;IAC9E,CAAC;IAEO,gCAAW,GAAnB;QACE,IAAI,IAAI,CAAC,UAAU,EAAE;YACnB,YAAY,CAAC,IAAI,CAAC,UAAU,CAAC,CAAC;YAC9B,OAAO,IAAI,CAAC,UAAU,CAAC;SACxB;IACH,CAAC;IACH,iBAAC;AAAD,CAAC,AA7HD,IA6HC","sourcesContent":["import { EventGroup } from './EventGroup';\nimport { findScrollableParent } from './scroll';\nimport { getRect } from './dom/getRect';\nimport { IRectangle } from './IRectangle';\n\ndeclare function setTimeout(cb: Function, delay: number): number;\n\nconst SCROLL_ITERATION_DELAY = 16;\nconst SCROLL_GUTTER = 100;\nconst MAX_SCROLL_VELOCITY = 15;\n\n/**\n * AutoScroll simply hooks up mouse events given a parent element, and scrolls the container\n * up/down depending on how close the mouse is to the top/bottom of the container.\n *\n * Once you don't want autoscroll any more, just dispose the helper and it will unhook events.\n *\n * @public\n * {@docCategory AutoScroll}\n */\nexport class AutoScroll {\n private _events: EventGroup;\n private _scrollableParent: HTMLElement | null;\n private _scrollRect: IRectangle | undefined;\n private _scrollVelocity: number;\n private _isVerticalScroll: boolean;\n private _timeoutId: number;\n\n constructor(element: HTMLElement) {\n this._events = new EventGroup(this);\n this._scrollableParent = findScrollableParent(element) as HTMLElement;\n\n this._incrementScroll = this._incrementScroll.bind(this);\n this._scrollRect = getRect(this._scrollableParent);\n\n // eslint-disable-next-line @typescript-eslint/no-explicit-any\n if (this._scrollableParent === (window as any)) {\n this._scrollableParent = document.body;\n }\n\n if (this._scrollableParent) {\n this._events.on(window, 'mousemove', this._onMouseMove, true);\n this._events.on(window, 'touchmove', this._onTouchMove, true);\n }\n }\n\n public dispose(): void {\n this._events.dispose();\n this._stopScroll();\n }\n\n private _onMouseMove(ev: MouseEvent): void {\n this._computeScrollVelocity(ev);\n }\n\n private _onTouchMove(ev: TouchEvent): void {\n if (ev.touches.length > 0) {\n this._computeScrollVelocity(ev);\n }\n }\n\n private _computeScrollVelocity(ev: MouseEvent | TouchEvent): void {\n if (!this._scrollRect) {\n return;\n }\n\n let clientX: number;\n let clientY: number;\n if ('clientX' in ev) {\n clientX = ev.clientX;\n clientY = ev.clientY;\n } else {\n clientX = ev.touches[0].clientX;\n clientY = ev.touches[0].clientY;\n }\n\n let scrollRectTop = this._scrollRect.top;\n let scrollRectLeft = this._scrollRect.left;\n let scrollClientBottom = scrollRectTop + this._scrollRect.height - SCROLL_GUTTER;\n let scrollClientRight = scrollRectLeft + this._scrollRect.width - SCROLL_GUTTER;\n\n // variables to use for alternating scroll direction\n let scrollRect;\n let clientDirection;\n let scrollClient;\n\n // if either of these conditions are met we are scrolling vertically else horizontally\n if (clientY < scrollRectTop + SCROLL_GUTTER || clientY > scrollClientBottom) {\n clientDirection = clientY;\n scrollRect = scrollRectTop;\n scrollClient = scrollClientBottom;\n this._isVerticalScroll = true;\n } else {\n clientDirection = clientX;\n scrollRect = scrollRectLeft;\n scrollClient = scrollClientRight;\n this._isVerticalScroll = false;\n }\n\n // calculate scroll velocity and direction\n if (clientDirection! < scrollRect + SCROLL_GUTTER) {\n this._scrollVelocity = Math.max(\n -MAX_SCROLL_VELOCITY,\n -MAX_SCROLL_VELOCITY * ((SCROLL_GUTTER - (clientDirection - scrollRect)) / SCROLL_GUTTER),\n );\n } else if (clientDirection > scrollClient) {\n this._scrollVelocity = Math.min(\n MAX_SCROLL_VELOCITY,\n MAX_SCROLL_VELOCITY * ((clientDirection - scrollClient) / SCROLL_GUTTER),\n );\n } else {\n this._scrollVelocity = 0;\n }\n\n if (this._scrollVelocity) {\n this._startScroll();\n } else {\n this._stopScroll();\n }\n }\n\n private _startScroll(): void {\n if (!this._timeoutId) {\n this._incrementScroll();\n }\n }\n\n private _incrementScroll(): void {\n if (this._scrollableParent) {\n if (this._isVerticalScroll) {\n this._scrollableParent.scrollTop += Math.round(this._scrollVelocity);\n } else {\n this._scrollableParent.scrollLeft += Math.round(this._scrollVelocity);\n }\n }\n\n this._timeoutId = setTimeout(this._incrementScroll, SCROLL_ITERATION_DELAY);\n }\n\n private _stopScroll(): void {\n if (this._timeoutId) {\n clearTimeout(this._timeoutId);\n delete this._timeoutId;\n }\n }\n}\n"]} |