[improvement] rename packages dir to src (#3659)
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
/* eslint-disable no-empty */
|
||||
/* eslint-disable getter-return */
|
||||
/* eslint-disable import/no-mutable-exports */
|
||||
import { isServer } from '..';
|
||||
import { EventHanlder } from '../types';
|
||||
|
||||
export let supportsPassive = false;
|
||||
|
||||
if (!isServer) {
|
||||
try {
|
||||
const opts = {};
|
||||
Object.defineProperty(opts, 'passive', {
|
||||
get() {
|
||||
/* istanbul ignore next */
|
||||
supportsPassive = true;
|
||||
}
|
||||
});
|
||||
window.addEventListener('test-passive', null as any, opts);
|
||||
} catch (e) {}
|
||||
}
|
||||
|
||||
export function on(
|
||||
target: HTMLElement,
|
||||
event: string,
|
||||
handler: EventHanlder,
|
||||
passive = false
|
||||
) {
|
||||
if (!isServer) {
|
||||
target.addEventListener(
|
||||
event,
|
||||
handler,
|
||||
supportsPassive ? { capture: false, passive } : false
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
export function off(target: HTMLElement, event: string, handler: EventHanlder) {
|
||||
if (!isServer) {
|
||||
target.removeEventListener(event, handler);
|
||||
}
|
||||
}
|
||||
|
||||
export function stopPropagation(event: Event) {
|
||||
event.stopPropagation();
|
||||
}
|
||||
|
||||
export function preventDefault(event: Event, isStopPropagation?: boolean) {
|
||||
/* istanbul ignore else */
|
||||
if (typeof event.cancelable !== 'boolean' || event.cancelable) {
|
||||
event.preventDefault();
|
||||
}
|
||||
|
||||
if (isStopPropagation) {
|
||||
stopPropagation(event);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
/**
|
||||
* requestAnimationFrame polyfill
|
||||
*/
|
||||
|
||||
import { isServer } from '..';
|
||||
|
||||
let prev = Date.now();
|
||||
|
||||
/* istanbul ignore next */
|
||||
function fallback(fn: FrameRequestCallback): number {
|
||||
const curr = Date.now();
|
||||
const ms = Math.max(0, 16 - (curr - prev));
|
||||
const id = setTimeout(fn, ms);
|
||||
prev = curr + ms;
|
||||
return id;
|
||||
}
|
||||
|
||||
/* istanbul ignore next */
|
||||
const root = <Window>(isServer ? global : window);
|
||||
|
||||
/* istanbul ignore next */
|
||||
const iRaf = root.requestAnimationFrame || fallback;
|
||||
|
||||
/* istanbul ignore next */
|
||||
const iCancel = root.cancelAnimationFrame || root.clearTimeout;
|
||||
|
||||
export function raf(fn: FrameRequestCallback): number {
|
||||
return iRaf.call(root, fn);
|
||||
}
|
||||
|
||||
export function cancelRaf(id: number) {
|
||||
iCancel.call(root, id);
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
type ScrollElement = HTMLElement | Window;
|
||||
|
||||
// get nearest scroll element
|
||||
// http://w3help.org/zh-cn/causes/SD9013
|
||||
// http://stackoverflow.com/questions/17016740/onscroll-function-is-not-working-for-chrome
|
||||
export function getScrollEventTarget(element: HTMLElement, rootParent: ScrollElement = window) {
|
||||
let node = element;
|
||||
while (
|
||||
node &&
|
||||
node.tagName !== 'HTML' &&
|
||||
node.tagName !== 'BODY' &&
|
||||
node.nodeType === 1 &&
|
||||
node !== rootParent
|
||||
) {
|
||||
const { overflowY } = window.getComputedStyle(node);
|
||||
if (overflowY === 'scroll' || overflowY === 'auto') {
|
||||
return node;
|
||||
}
|
||||
node = <HTMLElement>node.parentNode;
|
||||
}
|
||||
return rootParent;
|
||||
}
|
||||
|
||||
export function getScrollTop(element: ScrollElement): number {
|
||||
return 'scrollTop' in element ? element.scrollTop : element.pageYOffset;
|
||||
}
|
||||
|
||||
export function setScrollTop(element: ScrollElement, value: number) {
|
||||
'scrollTop' in element ? (element.scrollTop = value) : element.scrollTo(element.scrollX, value);
|
||||
}
|
||||
|
||||
export function getRootScrollTop(): number {
|
||||
return window.pageYOffset || document.documentElement.scrollTop || document.body.scrollTop || 0;
|
||||
}
|
||||
|
||||
// get distance from element top to page top
|
||||
export function getElementTop(element: ScrollElement) {
|
||||
return (
|
||||
(element === window ? 0 : (<HTMLElement>element).getBoundingClientRect().top) +
|
||||
getScrollTop(window)
|
||||
);
|
||||
}
|
||||
|
||||
export function getVisibleHeight(element: ScrollElement) {
|
||||
return element === window
|
||||
? element.innerHeight
|
||||
: (<HTMLElement>element).getBoundingClientRect().height;
|
||||
}
|
||||
Reference in New Issue
Block a user