refactor: some util by ts
parent
ac863a6a11
commit
93b64061db
|
@ -425,97 +425,98 @@ const KeyCode = {
|
|||
* WIN_IME
|
||||
*/
|
||||
WIN_IME: 229,
|
||||
};
|
||||
|
||||
/*
|
||||
whether text and modified key is entered at the same time.
|
||||
*/
|
||||
KeyCode.isTextModifyingKeyEvent = function isTextModifyingKeyEvent(e) {
|
||||
const keyCode = e.keyCode;
|
||||
if (
|
||||
(e.altKey && !e.ctrlKey) ||
|
||||
e.metaKey ||
|
||||
// Function keys don't generate text
|
||||
(keyCode >= KeyCode.F1 && keyCode <= KeyCode.F12)
|
||||
) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// The following keys are quite harmless, even in combination with
|
||||
// CTRL, ALT or SHIFT.
|
||||
switch (keyCode) {
|
||||
case KeyCode.ALT:
|
||||
case KeyCode.CAPS_LOCK:
|
||||
case KeyCode.CONTEXT_MENU:
|
||||
case KeyCode.CTRL:
|
||||
case KeyCode.DOWN:
|
||||
case KeyCode.END:
|
||||
case KeyCode.ESC:
|
||||
case KeyCode.HOME:
|
||||
case KeyCode.INSERT:
|
||||
case KeyCode.LEFT:
|
||||
case KeyCode.MAC_FF_META:
|
||||
case KeyCode.META:
|
||||
case KeyCode.NUMLOCK:
|
||||
case KeyCode.NUM_CENTER:
|
||||
case KeyCode.PAGE_DOWN:
|
||||
case KeyCode.PAGE_UP:
|
||||
case KeyCode.PAUSE:
|
||||
case KeyCode.PRINT_SCREEN:
|
||||
case KeyCode.RIGHT:
|
||||
case KeyCode.SHIFT:
|
||||
case KeyCode.UP:
|
||||
case KeyCode.WIN_KEY:
|
||||
case KeyCode.WIN_KEY_RIGHT:
|
||||
// ======================== Function ========================
|
||||
/**
|
||||
* whether text and modified key is entered at the same time.
|
||||
*/
|
||||
isTextModifyingKeyEvent: function isTextModifyingKeyEvent(e: KeyboardEvent) {
|
||||
const { keyCode } = e;
|
||||
if (
|
||||
(e.altKey && !e.ctrlKey) ||
|
||||
e.metaKey ||
|
||||
// Function keys don't generate text
|
||||
(keyCode >= KeyCode.F1 && keyCode <= KeyCode.F12)
|
||||
) {
|
||||
return false;
|
||||
default:
|
||||
}
|
||||
|
||||
// The following keys are quite harmless, even in combination with
|
||||
// CTRL, ALT or SHIFT.
|
||||
switch (keyCode) {
|
||||
case KeyCode.ALT:
|
||||
case KeyCode.CAPS_LOCK:
|
||||
case KeyCode.CONTEXT_MENU:
|
||||
case KeyCode.CTRL:
|
||||
case KeyCode.DOWN:
|
||||
case KeyCode.END:
|
||||
case KeyCode.ESC:
|
||||
case KeyCode.HOME:
|
||||
case KeyCode.INSERT:
|
||||
case KeyCode.LEFT:
|
||||
case KeyCode.MAC_FF_META:
|
||||
case KeyCode.META:
|
||||
case KeyCode.NUMLOCK:
|
||||
case KeyCode.NUM_CENTER:
|
||||
case KeyCode.PAGE_DOWN:
|
||||
case KeyCode.PAGE_UP:
|
||||
case KeyCode.PAUSE:
|
||||
case KeyCode.PRINT_SCREEN:
|
||||
case KeyCode.RIGHT:
|
||||
case KeyCode.SHIFT:
|
||||
case KeyCode.UP:
|
||||
case KeyCode.WIN_KEY:
|
||||
case KeyCode.WIN_KEY_RIGHT:
|
||||
return false;
|
||||
default:
|
||||
return true;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* whether character is entered.
|
||||
*/
|
||||
isCharacterKey: function isCharacterKey(keyCode: number) {
|
||||
if (keyCode >= KeyCode.ZERO && keyCode <= KeyCode.NINE) {
|
||||
return true;
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
/*
|
||||
whether character is entered.
|
||||
*/
|
||||
KeyCode.isCharacterKey = function isCharacterKey(keyCode) {
|
||||
if (keyCode >= KeyCode.ZERO && keyCode <= KeyCode.NINE) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (keyCode >= KeyCode.NUM_ZERO && keyCode <= KeyCode.NUM_MULTIPLY) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (keyCode >= KeyCode.A && keyCode <= KeyCode.Z) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Safari sends zero key code for non-latin characters.
|
||||
if (window.navigation.userAgent.indexOf('WebKit') !== -1 && keyCode === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (keyCode) {
|
||||
case KeyCode.SPACE:
|
||||
case KeyCode.QUESTION_MARK:
|
||||
case KeyCode.NUM_PLUS:
|
||||
case KeyCode.NUM_MINUS:
|
||||
case KeyCode.NUM_PERIOD:
|
||||
case KeyCode.NUM_DIVISION:
|
||||
case KeyCode.SEMICOLON:
|
||||
case KeyCode.DASH:
|
||||
case KeyCode.EQUALS:
|
||||
case KeyCode.COMMA:
|
||||
case KeyCode.PERIOD:
|
||||
case KeyCode.SLASH:
|
||||
case KeyCode.APOSTROPHE:
|
||||
case KeyCode.SINGLE_QUOTE:
|
||||
case KeyCode.OPEN_SQUARE_BRACKET:
|
||||
case KeyCode.BACKSLASH:
|
||||
case KeyCode.CLOSE_SQUARE_BRACKET:
|
||||
if (keyCode >= KeyCode.NUM_ZERO && keyCode <= KeyCode.NUM_MULTIPLY) {
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if (keyCode >= KeyCode.A && keyCode <= KeyCode.Z) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Safari sends zero key code for non-latin characters.
|
||||
if (window.navigator.userAgent.indexOf('WebKit') !== -1 && keyCode === 0) {
|
||||
return true;
|
||||
}
|
||||
|
||||
switch (keyCode) {
|
||||
case KeyCode.SPACE:
|
||||
case KeyCode.QUESTION_MARK:
|
||||
case KeyCode.NUM_PLUS:
|
||||
case KeyCode.NUM_MINUS:
|
||||
case KeyCode.NUM_PERIOD:
|
||||
case KeyCode.NUM_DIVISION:
|
||||
case KeyCode.SEMICOLON:
|
||||
case KeyCode.DASH:
|
||||
case KeyCode.EQUALS:
|
||||
case KeyCode.COMMA:
|
||||
case KeyCode.PERIOD:
|
||||
case KeyCode.SLASH:
|
||||
case KeyCode.APOSTROPHE:
|
||||
case KeyCode.SINGLE_QUOTE:
|
||||
case KeyCode.OPEN_SQUARE_BRACKET:
|
||||
case KeyCode.BACKSLASH:
|
||||
case KeyCode.CLOSE_SQUARE_BRACKET:
|
||||
return true;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
export default KeyCode;
|
|
@ -1,10 +0,0 @@
|
|||
export default {
|
||||
methods: {
|
||||
setState(state, callback) {
|
||||
Object.assign(this.$data, state);
|
||||
this.$nextTick(() => {
|
||||
callback && callback();
|
||||
});
|
||||
},
|
||||
},
|
||||
};
|
|
@ -1,8 +1,8 @@
|
|||
import { isArray, isString, isObject } from './util';
|
||||
function classNames() {
|
||||
let classes = [];
|
||||
for (let i = 0; i < arguments.length; i++) {
|
||||
const value = arguments[i];
|
||||
function classNames(...args: any[]) {
|
||||
const classes = [];
|
||||
for (let i = 0; i < args.length; i++) {
|
||||
const value = args[i];
|
||||
if (!value) continue;
|
||||
if (isString(value)) {
|
||||
classes.push(value);
|
|
@ -1,4 +1,4 @@
|
|||
export function easeInOutCubic(t, b, c, d) {
|
||||
export function easeInOutCubic(t: number, b: number, c: number, d: number) {
|
||||
const cc = c - b;
|
||||
t /= d / 2;
|
||||
if (t < 1) {
|
|
@ -1,14 +1,10 @@
|
|||
/* eslint-disable no-undef */
|
||||
// Browser environment sniffing
|
||||
export const inBrowser = typeof window !== 'undefined';
|
||||
export const inWeex = typeof WXEnvironment !== 'undefined' && !!WXEnvironment.platform;
|
||||
export const weexPlatform = inWeex && WXEnvironment.platform.toLowerCase();
|
||||
export const UA = inBrowser && window.navigator.userAgent.toLowerCase();
|
||||
export const isIE = UA && /msie|trident/.test(UA);
|
||||
export const isIE9 = UA && UA.indexOf('msie 9.0') > 0;
|
||||
export const isEdge = UA && UA.indexOf('edge/') > 0;
|
||||
export const isAndroid = (UA && UA.indexOf('android') > 0) || weexPlatform === 'android';
|
||||
export const isIOS = (UA && /iphone|ipad|ipod|ios/.test(UA)) || weexPlatform === 'ios';
|
||||
export const isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge;
|
||||
export const isPhantomJS = UA && /phantomjs/.test(UA);
|
||||
export const isFF = UA && UA.match(/firefox\/(\d+)/);
|
|
@ -1,4 +1,5 @@
|
|||
const isNumeric = value => {
|
||||
const isNumeric = (value: any): boolean => {
|
||||
return !isNaN(parseFloat(value)) && isFinite(value);
|
||||
};
|
||||
|
||||
export default isNumeric;
|
|
@ -1,4 +1,4 @@
|
|||
const isValid = value => {
|
||||
const isValid = (value: any): boolean => {
|
||||
return value !== undefined && value !== null && value !== '';
|
||||
};
|
||||
export default isValid;
|
|
@ -25,16 +25,22 @@ const propList = `${attributes} ${eventsName}`.split(/[\s\n]+/);
|
|||
const ariaPrefix = 'aria-';
|
||||
const dataPrefix = 'data-';
|
||||
|
||||
function match(key, prefix) {
|
||||
function match(key: string, prefix: string) {
|
||||
return key.indexOf(prefix) === 0;
|
||||
}
|
||||
|
||||
export interface PickConfig {
|
||||
aria?: boolean;
|
||||
data?: boolean;
|
||||
attr?: boolean;
|
||||
}
|
||||
|
||||
/**
|
||||
* Picker props from exist props with filter
|
||||
* @param props Passed props
|
||||
* @param ariaOnly boolean | { aria?: boolean; data?: boolean; attr?: boolean; } filter config
|
||||
*/
|
||||
export default function pickAttrs(props, ariaOnly = false) {
|
||||
export default function pickAttrs(props: object, ariaOnly: boolean | PickConfig = false) {
|
||||
let mergedConfig;
|
||||
if (ariaOnly === false) {
|
||||
mergedConfig = {
|
|
@ -1,12 +1,16 @@
|
|||
import raf from 'raf';
|
||||
|
||||
interface RafMap {
|
||||
[id: number]: number;
|
||||
}
|
||||
|
||||
let id = 0;
|
||||
const ids = {};
|
||||
const ids: RafMap = {};
|
||||
|
||||
// Support call raf with delay specified frame
|
||||
export default function wrapperRaf(callback, delayFrames = 1) {
|
||||
const myId = id++;
|
||||
let restFrames = delayFrames;
|
||||
export default function wrapperRaf(callback: () => void, delayFrames = 1): number {
|
||||
const myId: number = id++;
|
||||
let restFrames: number = delayFrames;
|
||||
|
||||
function internalCallback() {
|
||||
restFrames -= 1;
|
||||
|
@ -24,9 +28,11 @@ export default function wrapperRaf(callback, delayFrames = 1) {
|
|||
return myId;
|
||||
}
|
||||
|
||||
wrapperRaf.cancel = function(pid) {
|
||||
wrapperRaf.cancel = function cancel(pid?: number) {
|
||||
if (pid === undefined) return;
|
||||
|
||||
raf.cancel(ids[pid]);
|
||||
delete ids[pid];
|
||||
};
|
||||
|
||||
wrapperRaf.ids = ids; // export this for test usage
|
|
@ -2,16 +2,16 @@ import raf from 'raf';
|
|||
import getScroll from './getScroll';
|
||||
import { easeInOutCubic } from './easings';
|
||||
|
||||
// interface ScrollToOptions {
|
||||
// /** Scroll container, default as window */
|
||||
// getContainer?: () => HTMLElement | Window;
|
||||
// /** Scroll end callback */
|
||||
// callback?: () => any;
|
||||
// /** Animation duration, default as 450 */
|
||||
// duration?: number;
|
||||
// }
|
||||
interface ScrollToOptions {
|
||||
/** Scroll container, default as window */
|
||||
getContainer?: () => HTMLElement | Window;
|
||||
/** Scroll end callback */
|
||||
callback?: () => any;
|
||||
/** Animation duration, default as 450 */
|
||||
duration?: number;
|
||||
}
|
||||
|
||||
export default function scrollTo(y, options = {}) {
|
||||
export default function scrollTo(y: number, options: ScrollToOptions = {}) {
|
||||
const { getContainer = () => window, callback, duration = 450 } = options;
|
||||
|
||||
const container = getContainer();
|
||||
|
@ -25,7 +25,7 @@ export default function scrollTo(y, options = {}) {
|
|||
if (container === window) {
|
||||
window.scrollTo(window.pageXOffset, nextScrollTop);
|
||||
} else {
|
||||
container.scrollTop = nextScrollTop;
|
||||
(container as HTMLElement).scrollTop = nextScrollTop;
|
||||
}
|
||||
if (time < duration) {
|
||||
raf(frameFunc);
|
|
@ -1,12 +1,17 @@
|
|||
import { CSSProperties } from 'vue';
|
||||
|
||||
/**
|
||||
* Easy to set element style, return previous style
|
||||
* IE browser compatible(IE browser doesn't merge overflow style, need to set it separately)
|
||||
* https://github.com/ant-design/ant-design/issues/19393
|
||||
*
|
||||
*/
|
||||
function setStyle(style, options = {}) {
|
||||
export interface SetStyleOptions {
|
||||
element?: HTMLElement;
|
||||
}
|
||||
function setStyle(style: CSSProperties, options: SetStyleOptions = {}): CSSProperties {
|
||||
const { element = document.body } = options;
|
||||
const oldStyle = {};
|
||||
const oldStyle: CSSProperties = {};
|
||||
|
||||
const styleKeys = Object.keys(style);
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
const isStyleSupport = styleName => {
|
||||
const isStyleSupport = (styleName: string | Array<string>): boolean => {
|
||||
if (typeof window !== 'undefined' && window.document && window.document.documentElement) {
|
||||
const styleNameList = Array.isArray(styleName) ? styleName : [styleName];
|
||||
const { documentElement } = window.document;
|
|
@ -1,31 +1,33 @@
|
|||
import raf from 'raf';
|
||||
|
||||
export default function throttleByAnimationFrame(fn) {
|
||||
let requestId;
|
||||
export default function throttleByAnimationFrame(fn: (...args: any[]) => void) {
|
||||
let requestId: number | null;
|
||||
|
||||
const later = args => () => {
|
||||
const later = (args: any[]) => () => {
|
||||
requestId = null;
|
||||
fn(...args);
|
||||
};
|
||||
|
||||
const throttled = (...args) => {
|
||||
const throttled = (...args: any[]) => {
|
||||
if (requestId == null) {
|
||||
requestId = raf(later(args));
|
||||
}
|
||||
};
|
||||
|
||||
throttled.cancel = () => raf.cancel(requestId);
|
||||
(throttled as any).cancel = () => raf.cancel(requestId!);
|
||||
|
||||
return throttled;
|
||||
}
|
||||
|
||||
export function throttleByAnimationFrameDecorator() {
|
||||
return function(target, key, descriptor) {
|
||||
// eslint-disable-next-line func-names
|
||||
return function(target: any, key: string, descriptor: any) {
|
||||
const fn = descriptor.value;
|
||||
let definingProperty = false;
|
||||
return {
|
||||
configurable: true,
|
||||
get() {
|
||||
// eslint-disable-next-line no-prototype-builtins
|
||||
if (definingProperty || this === target.prototype || this.hasOwnProperty(key)) {
|
||||
return fn;
|
||||
}
|
|
@ -1,4 +1,4 @@
|
|||
export default function triggerEvent(el, type) {
|
||||
export default function triggerEvent(el: Element, type: string) {
|
||||
if ('createEvent' in document) {
|
||||
// modern browsers, IE9+
|
||||
const e = document.createEvent('HTMLEvents');
|
Loading…
Reference in New Issue