@thienvu18/designable-shared 2.0.0 → 2.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/esm/animation.d.ts +8 -2
- package/esm/animation.js +29 -30
- package/esm/array.d.ts +163 -37
- package/esm/array.js +104 -87
- package/esm/clone.d.ts +4 -4
- package/esm/clone.js +79 -86
- package/esm/compose.d.ts +3 -1
- package/esm/compose.js +6 -6
- package/esm/coordinate.d.ts +120 -76
- package/esm/coordinate.js +480 -411
- package/esm/element.d.ts +11 -6
- package/esm/element.js +123 -125
- package/esm/event.d.ts +162 -75
- package/esm/event.js +197 -197
- package/esm/globalThisPolyfill.d.ts +1 -1
- package/esm/globalThisPolyfill.js +14 -17
- package/esm/index.d.ts +17 -17
- package/esm/index.js +17 -17
- package/esm/instanceof.d.ts +1 -1
- package/esm/instanceof.js +9 -10
- package/esm/keycode.d.ts +145 -145
- package/esm/keycode.js +149 -149
- package/esm/lru.d.ts +50 -47
- package/esm/lru.js +236 -245
- package/esm/observer.d.ts +7 -7
- package/esm/observer.js +37 -40
- package/esm/request-idle.d.ts +9 -6
- package/esm/request-idle.js +6 -6
- package/esm/scroller.d.ts +24 -8
- package/esm/scroller.js +78 -75
- package/esm/subscribable.d.ts +10 -10
- package/esm/subscribable.js +39 -40
- package/esm/types.d.ts +13 -13
- package/esm/types.js +19 -20
- package/esm/uid.d.ts +1 -1
- package/esm/uid.js +7 -7
- package/lib/animation.d.ts +8 -2
- package/lib/animation.js +34 -35
- package/lib/array.d.ts +163 -37
- package/lib/array.js +129 -101
- package/lib/clone.d.ts +4 -4
- package/lib/clone.js +84 -91
- package/lib/compose.d.ts +3 -1
- package/lib/compose.js +10 -10
- package/lib/coordinate.d.ts +120 -76
- package/lib/coordinate.js +538 -440
- package/lib/element.d.ts +11 -6
- package/lib/element.js +139 -133
- package/lib/event.d.ts +162 -75
- package/lib/event.js +206 -202
- package/lib/globalThisPolyfill.d.ts +1 -1
- package/lib/globalThisPolyfill.js +17 -20
- package/lib/index.d.ts +17 -17
- package/lib/index.js +49 -33
- package/lib/instanceof.d.ts +1 -1
- package/lib/instanceof.js +13 -14
- package/lib/keycode.d.ts +145 -145
- package/lib/keycode.js +153 -153
- package/lib/lru.d.ts +50 -47
- package/lib/lru.js +240 -249
- package/lib/observer.d.ts +7 -7
- package/lib/observer.js +41 -44
- package/lib/request-idle.d.ts +9 -6
- package/lib/request-idle.js +14 -11
- package/lib/scroller.d.ts +24 -8
- package/lib/scroller.js +89 -81
- package/lib/subscribable.d.ts +10 -10
- package/lib/subscribable.js +43 -44
- package/lib/types.d.ts +13 -13
- package/lib/types.js +40 -28
- package/lib/uid.d.ts +1 -1
- package/lib/uid.js +11 -11
- package/package.json +15 -15
package/esm/observer.js
CHANGED
|
@@ -1,43 +1,40 @@
|
|
|
1
1
|
export class LayoutObserver {
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
});
|
|
18
|
-
this.connected = true;
|
|
19
|
-
};
|
|
20
|
-
this.disconnect = () => {
|
|
21
|
-
if (this.connected) {
|
|
22
|
-
this.resizeObserver?.disconnect();
|
|
23
|
-
this.performanceObserver?.disconnect();
|
|
24
|
-
this.mutationObserver?.disconnect();
|
|
25
|
-
}
|
|
26
|
-
this.connected = false;
|
|
27
|
-
};
|
|
28
|
-
if (typeof ResizeObserver !== 'undefined') {
|
|
29
|
-
this.resizeObserver = new ResizeObserver(() => observer());
|
|
30
|
-
}
|
|
31
|
-
if (typeof PerformanceObserver !== 'undefined') {
|
|
32
|
-
try {
|
|
33
|
-
this.performanceObserver = new PerformanceObserver(() => {
|
|
34
|
-
observer();
|
|
35
|
-
});
|
|
36
|
-
}
|
|
37
|
-
catch (e) { }
|
|
38
|
-
}
|
|
39
|
-
if (typeof MutationObserver !== 'undefined') {
|
|
40
|
-
this.mutationObserver = new MutationObserver(() => observer());
|
|
41
|
-
}
|
|
2
|
+
constructor(observer = () => {}) {
|
|
3
|
+
this.connected = false
|
|
4
|
+
this.observe = (target) => {
|
|
5
|
+
if (!target) return
|
|
6
|
+
this.resizeObserver?.observe(target)
|
|
7
|
+
try {
|
|
8
|
+
this.performanceObserver?.observe({
|
|
9
|
+
entryTypes: ['paint', 'element', 'layout-shift', 'event'],
|
|
10
|
+
})
|
|
11
|
+
} catch (e) {}
|
|
12
|
+
this.mutationObserver?.observe(target, {
|
|
13
|
+
attributeFilter: ['style'],
|
|
14
|
+
attributes: true,
|
|
15
|
+
})
|
|
16
|
+
this.connected = true
|
|
42
17
|
}
|
|
18
|
+
this.disconnect = () => {
|
|
19
|
+
if (this.connected) {
|
|
20
|
+
this.resizeObserver?.disconnect()
|
|
21
|
+
this.performanceObserver?.disconnect()
|
|
22
|
+
this.mutationObserver?.disconnect()
|
|
23
|
+
}
|
|
24
|
+
this.connected = false
|
|
25
|
+
}
|
|
26
|
+
if (typeof ResizeObserver !== 'undefined') {
|
|
27
|
+
this.resizeObserver = new ResizeObserver(() => observer())
|
|
28
|
+
}
|
|
29
|
+
if (typeof PerformanceObserver !== 'undefined') {
|
|
30
|
+
try {
|
|
31
|
+
this.performanceObserver = new PerformanceObserver(() => {
|
|
32
|
+
observer()
|
|
33
|
+
})
|
|
34
|
+
} catch (e) {}
|
|
35
|
+
}
|
|
36
|
+
if (typeof MutationObserver !== 'undefined') {
|
|
37
|
+
this.mutationObserver = new MutationObserver(() => observer())
|
|
38
|
+
}
|
|
39
|
+
}
|
|
43
40
|
}
|
package/esm/request-idle.d.ts
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
|
-
import 'requestidlecallback'
|
|
1
|
+
import 'requestidlecallback'
|
|
2
2
|
export interface IIdleDeadline {
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
didTimeout: boolean
|
|
4
|
+
timeRemaining: () => DOMHighResTimeStamp
|
|
5
5
|
}
|
|
6
6
|
export interface IdleCallbackOptions {
|
|
7
|
-
|
|
7
|
+
timeout?: number
|
|
8
8
|
}
|
|
9
|
-
export declare const requestIdle: (
|
|
10
|
-
|
|
9
|
+
export declare const requestIdle: (
|
|
10
|
+
callback: (params: IIdleDeadline) => void,
|
|
11
|
+
options?: IdleCallbackOptions
|
|
12
|
+
) => number
|
|
13
|
+
export declare const cancelIdle: (id: number) => void
|
package/esm/request-idle.js
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import 'requestidlecallback'
|
|
2
|
-
import { globalThisPolyfill } from './globalThisPolyfill'
|
|
1
|
+
import 'requestidlecallback'
|
|
2
|
+
import { globalThisPolyfill } from './globalThisPolyfill'
|
|
3
3
|
export const requestIdle = (callback, options) => {
|
|
4
|
-
|
|
5
|
-
}
|
|
4
|
+
return globalThisPolyfill['requestIdleCallback'](callback, options)
|
|
5
|
+
}
|
|
6
6
|
export const cancelIdle = (id) => {
|
|
7
|
-
|
|
8
|
-
}
|
|
7
|
+
globalThisPolyfill['cancelIdleCallback'](id)
|
|
8
|
+
}
|
package/esm/scroller.d.ts
CHANGED
|
@@ -1,10 +1,26 @@
|
|
|
1
|
-
import { IPoint } from './coordinate'
|
|
2
|
-
export type ScrollDirection = 'begin' | 'end'
|
|
1
|
+
import { IPoint } from './coordinate'
|
|
2
|
+
export type ScrollDirection = 'begin' | 'end'
|
|
3
3
|
export interface IAutoScrollBasicInfo {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
4
|
+
direction: ScrollDirection
|
|
5
|
+
speedFactor: number
|
|
6
|
+
speed: number
|
|
7
7
|
}
|
|
8
|
-
export declare const calcAutoScrollBasicInfo: (
|
|
9
|
-
|
|
10
|
-
|
|
8
|
+
export declare const calcAutoScrollBasicInfo: (
|
|
9
|
+
point: IPoint,
|
|
10
|
+
axis: 'x' | 'y',
|
|
11
|
+
viewport: DOMRect,
|
|
12
|
+
maxSpeed?: number
|
|
13
|
+
) => IAutoScrollBasicInfo | null
|
|
14
|
+
export declare const updateScrollValue: (
|
|
15
|
+
element: HTMLElement | Window,
|
|
16
|
+
axis: 'x' | 'y',
|
|
17
|
+
value: number,
|
|
18
|
+
callback?: (scrollValue: number) => void
|
|
19
|
+
) => void
|
|
20
|
+
export declare const scrollAnimate: (
|
|
21
|
+
element: HTMLElement | Window,
|
|
22
|
+
axis: 'x' | 'y',
|
|
23
|
+
direction: 'begin' | 'end',
|
|
24
|
+
speed: number,
|
|
25
|
+
callback?: (scrollValue: number) => void
|
|
26
|
+
) => () => void
|
package/esm/scroller.js
CHANGED
|
@@ -1,82 +1,85 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { isFn, isWindow } from './types'
|
|
3
|
-
const MAX_SPEED = 80
|
|
4
|
-
export const calcAutoScrollBasicInfo = (
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
1
|
+
import { calcSpeedFactor, createUniformSpeedAnimation } from './animation'
|
|
2
|
+
import { isFn, isWindow } from './types'
|
|
3
|
+
const MAX_SPEED = 80 // px/s
|
|
4
|
+
export const calcAutoScrollBasicInfo = (
|
|
5
|
+
point,
|
|
6
|
+
axis,
|
|
7
|
+
viewport,
|
|
8
|
+
maxSpeed = MAX_SPEED
|
|
9
|
+
) => {
|
|
10
|
+
const { left, right, top, bottom } = viewport
|
|
11
|
+
const { x, y } = point
|
|
12
|
+
let begin
|
|
13
|
+
let end
|
|
14
|
+
let pos
|
|
15
|
+
let speedFactor
|
|
16
|
+
if (axis === 'x') {
|
|
17
|
+
begin = left
|
|
18
|
+
end = right
|
|
19
|
+
pos = x
|
|
20
|
+
} else {
|
|
21
|
+
begin = top
|
|
22
|
+
end = bottom
|
|
23
|
+
pos = y
|
|
24
|
+
}
|
|
25
|
+
const scrollerSize = end - begin
|
|
26
|
+
const moveDistance = scrollerSize > 400 ? 100 : scrollerSize / 3
|
|
27
|
+
if (end - pos < moveDistance) {
|
|
28
|
+
return {
|
|
29
|
+
direction: 'end',
|
|
30
|
+
speedFactor,
|
|
31
|
+
speed: maxSpeed * calcSpeedFactor(end - pos, moveDistance),
|
|
15
32
|
}
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
33
|
+
} else if (pos - begin < moveDistance) {
|
|
34
|
+
return {
|
|
35
|
+
direction: 'begin',
|
|
36
|
+
speedFactor,
|
|
37
|
+
speed: maxSpeed * calcSpeedFactor(pos - begin, moveDistance),
|
|
20
38
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
return {
|
|
25
|
-
direction: 'end',
|
|
26
|
-
speedFactor,
|
|
27
|
-
speed: maxSpeed * calcSpeedFactor(end - pos, moveDistance),
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
else if (pos - begin < moveDistance) {
|
|
31
|
-
return {
|
|
32
|
-
direction: 'begin',
|
|
33
|
-
speedFactor,
|
|
34
|
-
speed: maxSpeed * calcSpeedFactor(pos - begin, moveDistance),
|
|
35
|
-
};
|
|
36
|
-
}
|
|
37
|
-
return null;
|
|
38
|
-
};
|
|
39
|
+
}
|
|
40
|
+
return null
|
|
41
|
+
}
|
|
39
42
|
export const updateScrollValue = (element, axis, value, callback) => {
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
callback(element.scrollLeft);
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
else {
|
|
51
|
-
if (element.scrollTop + value > element.scrollHeight)
|
|
52
|
-
return;
|
|
53
|
-
element.scrollTop += value;
|
|
54
|
-
if (isFn(callback)) {
|
|
55
|
-
callback(element.scrollTop);
|
|
56
|
-
}
|
|
57
|
-
}
|
|
43
|
+
if (element) {
|
|
44
|
+
if (!isWindow(element)) {
|
|
45
|
+
if (axis === 'x') {
|
|
46
|
+
if (element.scrollLeft + value > element.scrollWidth) return
|
|
47
|
+
element.scrollLeft += value
|
|
48
|
+
if (isFn(callback)) {
|
|
49
|
+
callback(element.scrollLeft)
|
|
58
50
|
}
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
});
|
|
65
|
-
}
|
|
66
|
-
else {
|
|
67
|
-
element.scrollBy({
|
|
68
|
-
top: value,
|
|
69
|
-
behavior: 'smooth',
|
|
70
|
-
});
|
|
71
|
-
}
|
|
72
|
-
if (isFn(callback)) {
|
|
73
|
-
callback(value);
|
|
74
|
-
}
|
|
51
|
+
} else {
|
|
52
|
+
if (element.scrollTop + value > element.scrollHeight) return
|
|
53
|
+
element.scrollTop += value
|
|
54
|
+
if (isFn(callback)) {
|
|
55
|
+
callback(element.scrollTop)
|
|
75
56
|
}
|
|
57
|
+
}
|
|
58
|
+
} else {
|
|
59
|
+
if (axis === 'x') {
|
|
60
|
+
element.scrollBy({
|
|
61
|
+
left: value,
|
|
62
|
+
behavior: 'smooth',
|
|
63
|
+
})
|
|
64
|
+
} else {
|
|
65
|
+
element.scrollBy({
|
|
66
|
+
top: value,
|
|
67
|
+
behavior: 'smooth',
|
|
68
|
+
})
|
|
69
|
+
}
|
|
70
|
+
if (isFn(callback)) {
|
|
71
|
+
callback(value)
|
|
72
|
+
}
|
|
76
73
|
}
|
|
77
|
-
}
|
|
74
|
+
}
|
|
75
|
+
}
|
|
78
76
|
export const scrollAnimate = (element, axis, direction, speed, callback) => {
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
77
|
+
return createUniformSpeedAnimation(speed, (delta) => {
|
|
78
|
+
updateScrollValue(
|
|
79
|
+
element,
|
|
80
|
+
axis,
|
|
81
|
+
direction === 'begin' ? 0 - delta : delta,
|
|
82
|
+
callback
|
|
83
|
+
)
|
|
84
|
+
})
|
|
85
|
+
}
|
package/esm/subscribable.d.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
|
-
declare const UNSUBSCRIBE_ID_SYMBOL: unique symbol
|
|
1
|
+
declare const UNSUBSCRIBE_ID_SYMBOL: unique symbol
|
|
2
2
|
export interface ISubscriber<Payload = any> {
|
|
3
|
-
|
|
3
|
+
(payload: Payload): void | boolean
|
|
4
4
|
}
|
|
5
5
|
export declare class Subscribable<ExtendsType = any> {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
private subscribers
|
|
7
|
+
dispatch<T extends ExtendsType = any>(event: T, context?: any): boolean
|
|
8
|
+
subscribe(subscriber: ISubscriber): {
|
|
9
|
+
(): void
|
|
10
|
+
[UNSUBSCRIBE_ID_SYMBOL]: number
|
|
11
|
+
}
|
|
12
|
+
unsubscribe: (id?: number | string | (() => void)) => void
|
|
13
13
|
}
|
|
14
|
-
export {}
|
|
14
|
+
export {}
|
package/esm/subscribable.js
CHANGED
|
@@ -1,48 +1,47 @@
|
|
|
1
|
-
import { isFn } from './types'
|
|
2
|
-
const UNSUBSCRIBE_ID_SYMBOL = Symbol('UNSUBSCRIBE_ID_SYMBOL')
|
|
1
|
+
import { isFn } from './types'
|
|
2
|
+
const UNSUBSCRIBE_ID_SYMBOL = Symbol('UNSUBSCRIBE_ID_SYMBOL')
|
|
3
3
|
export class Subscribable {
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
};
|
|
8
|
-
this.unsubscribe = (id) => {
|
|
9
|
-
if (id === undefined || id === null) {
|
|
10
|
-
for (const key in this.subscribers) {
|
|
11
|
-
this.unsubscribe(key);
|
|
12
|
-
}
|
|
13
|
-
return;
|
|
14
|
-
}
|
|
15
|
-
if (!isFn(id)) {
|
|
16
|
-
delete this.subscribers[id];
|
|
17
|
-
}
|
|
18
|
-
else {
|
|
19
|
-
delete this.subscribers[id[UNSUBSCRIBE_ID_SYMBOL]];
|
|
20
|
-
}
|
|
21
|
-
};
|
|
4
|
+
constructor() {
|
|
5
|
+
this.subscribers = {
|
|
6
|
+
index: 0,
|
|
22
7
|
}
|
|
23
|
-
|
|
24
|
-
|
|
8
|
+
this.unsubscribe = (id) => {
|
|
9
|
+
if (id === undefined || id === null) {
|
|
25
10
|
for (const key in this.subscribers) {
|
|
26
|
-
|
|
27
|
-
event['context'] = context;
|
|
28
|
-
if (this.subscribers[key](event) === false) {
|
|
29
|
-
interrupted = true;
|
|
30
|
-
}
|
|
31
|
-
}
|
|
11
|
+
this.unsubscribe(key)
|
|
32
12
|
}
|
|
33
|
-
return
|
|
13
|
+
return
|
|
14
|
+
}
|
|
15
|
+
if (!isFn(id)) {
|
|
16
|
+
delete this.subscribers[id]
|
|
17
|
+
} else {
|
|
18
|
+
delete this.subscribers[id[UNSUBSCRIBE_ID_SYMBOL]]
|
|
19
|
+
}
|
|
34
20
|
}
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
21
|
+
}
|
|
22
|
+
dispatch(event, context) {
|
|
23
|
+
let interrupted = false
|
|
24
|
+
for (const key in this.subscribers) {
|
|
25
|
+
if (isFn(this.subscribers[key])) {
|
|
26
|
+
event['context'] = context
|
|
27
|
+
if (this.subscribers[key](event) === false) {
|
|
28
|
+
interrupted = true
|
|
41
29
|
}
|
|
42
|
-
|
|
43
|
-
this.unsubscribe(id);
|
|
44
|
-
};
|
|
45
|
-
unsubscribe[UNSUBSCRIBE_ID_SYMBOL] = id;
|
|
46
|
-
return unsubscribe;
|
|
30
|
+
}
|
|
47
31
|
}
|
|
32
|
+
return interrupted ? false : true
|
|
33
|
+
}
|
|
34
|
+
subscribe(subscriber) {
|
|
35
|
+
let id
|
|
36
|
+
if (isFn(subscriber)) {
|
|
37
|
+
id = this.subscribers.index + 1
|
|
38
|
+
this.subscribers[id] = subscriber
|
|
39
|
+
this.subscribers.index++
|
|
40
|
+
}
|
|
41
|
+
const unsubscribe = () => {
|
|
42
|
+
this.unsubscribe(id)
|
|
43
|
+
}
|
|
44
|
+
unsubscribe[UNSUBSCRIBE_ID_SYMBOL] = id
|
|
45
|
+
return unsubscribe
|
|
46
|
+
}
|
|
48
47
|
}
|
package/esm/types.d.ts
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
export declare const getType: (obj: any) => any
|
|
2
|
-
export declare const isFn: (obj: unknown) => obj is (...args: any[]) => any
|
|
3
|
-
export declare const isWindow: (obj: unknown) => obj is Window
|
|
4
|
-
export declare const isHTMLElement: (obj: any) => obj is HTMLElement
|
|
5
|
-
export declare const isArr: (arg: any) => arg is any[]
|
|
6
|
-
export declare const isPlainObj: (obj: unknown) => obj is object
|
|
7
|
-
export declare const isStr: (obj: unknown) => obj is string
|
|
8
|
-
export declare const isBool: (obj: unknown) => obj is boolean
|
|
9
|
-
export declare const isNum: (obj: unknown) => obj is number
|
|
10
|
-
export declare const isObj: (val: unknown) => val is object
|
|
11
|
-
export declare const isRegExp: (obj: unknown) => obj is RegExp
|
|
12
|
-
export declare const isValid: (val: any) => boolean
|
|
13
|
-
export declare const isValidNumber: (val: any) => val is number
|
|
1
|
+
export declare const getType: (obj: any) => any
|
|
2
|
+
export declare const isFn: (obj: unknown) => obj is (...args: any[]) => any
|
|
3
|
+
export declare const isWindow: (obj: unknown) => obj is Window
|
|
4
|
+
export declare const isHTMLElement: (obj: any) => obj is HTMLElement
|
|
5
|
+
export declare const isArr: (arg: any) => arg is any[]
|
|
6
|
+
export declare const isPlainObj: (obj: unknown) => obj is object
|
|
7
|
+
export declare const isStr: (obj: unknown) => obj is string
|
|
8
|
+
export declare const isBool: (obj: unknown) => obj is boolean
|
|
9
|
+
export declare const isNum: (obj: unknown) => obj is number
|
|
10
|
+
export declare const isObj: (val: unknown) => val is object
|
|
11
|
+
export declare const isRegExp: (obj: unknown) => obj is RegExp
|
|
12
|
+
export declare const isValid: (val: any) => boolean
|
|
13
|
+
export declare const isValidNumber: (val: any) => val is number
|
package/esm/types.js
CHANGED
|
@@ -1,21 +1,20 @@
|
|
|
1
|
-
const isType = (type) => (obj) =>
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
export const isWindow = isType('Window');
|
|
1
|
+
const isType = (type) => (obj) =>
|
|
2
|
+
obj != null &&
|
|
3
|
+
(Array.isArray(type) ? type : [type]).some(
|
|
4
|
+
(t) => getType(obj) === `[object ${t}]`
|
|
5
|
+
)
|
|
6
|
+
export const getType = (obj) => Object.prototype.toString.call(obj)
|
|
7
|
+
export const isFn = isType(['Function', 'AsyncFunction', 'GeneratorFunction'])
|
|
8
|
+
export const isWindow = isType('Window')
|
|
10
9
|
export const isHTMLElement = (obj) => {
|
|
11
|
-
|
|
12
|
-
}
|
|
13
|
-
export const isArr = Array.isArray
|
|
14
|
-
export const isPlainObj = isType('Object')
|
|
15
|
-
export const isStr = isType('String')
|
|
16
|
-
export const isBool = isType('Boolean')
|
|
17
|
-
export const isNum = isType('Number')
|
|
18
|
-
export const isObj = (val) => typeof val === 'object'
|
|
19
|
-
export const isRegExp = isType('RegExp')
|
|
20
|
-
export const isValid = (val) => val !== null && val !== undefined
|
|
21
|
-
export const isValidNumber = (val) => !isNaN(val) && isNum(val)
|
|
10
|
+
return obj?.['nodeName'] || obj?.['tagName']
|
|
11
|
+
}
|
|
12
|
+
export const isArr = Array.isArray
|
|
13
|
+
export const isPlainObj = isType('Object')
|
|
14
|
+
export const isStr = isType('String')
|
|
15
|
+
export const isBool = isType('Boolean')
|
|
16
|
+
export const isNum = isType('Number')
|
|
17
|
+
export const isObj = (val) => typeof val === 'object'
|
|
18
|
+
export const isRegExp = isType('RegExp')
|
|
19
|
+
export const isValid = (val) => val !== null && val !== undefined
|
|
20
|
+
export const isValidNumber = (val) => !isNaN(val) && isNum(val)
|
package/esm/uid.d.ts
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export declare function uid(len?: number): string
|
|
1
|
+
export declare function uid(len?: number): string
|
package/esm/uid.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
let IDX = 36,
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
let IDX = 36,
|
|
2
|
+
HEX = ''
|
|
3
|
+
while (IDX--) HEX += IDX.toString(36)
|
|
4
4
|
export function uid(len) {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
5
|
+
let str = '',
|
|
6
|
+
num = len || 11
|
|
7
|
+
while (num--) str += HEX[(Math.random() * 36) | 0]
|
|
8
|
+
return str
|
|
9
9
|
}
|
package/lib/animation.d.ts
CHANGED
|
@@ -1,2 +1,8 @@
|
|
|
1
|
-
export declare const createUniformSpeedAnimation: (
|
|
2
|
-
|
|
1
|
+
export declare const createUniformSpeedAnimation: (
|
|
2
|
+
speed: number,
|
|
3
|
+
callback: (delta: number) => void
|
|
4
|
+
) => () => void
|
|
5
|
+
export declare const calcSpeedFactor: (
|
|
6
|
+
delta?: number,
|
|
7
|
+
threshold?: number
|
|
8
|
+
) => number
|
package/lib/animation.js
CHANGED
|
@@ -1,38 +1,37 @@
|
|
|
1
|
-
|
|
2
|
-
Object.defineProperty(exports,
|
|
3
|
-
exports.calcSpeedFactor = exports.createUniformSpeedAnimation = void 0
|
|
1
|
+
'use strict'
|
|
2
|
+
Object.defineProperty(exports, '__esModule', { value: true })
|
|
3
|
+
exports.calcSpeedFactor = exports.createUniformSpeedAnimation = void 0
|
|
4
4
|
const createUniformSpeedAnimation = (speed = 10, callback) => {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
exports.createUniformSpeedAnimation = createUniformSpeedAnimation;
|
|
5
|
+
let request = null
|
|
6
|
+
let startTime = null
|
|
7
|
+
const start = () => {
|
|
8
|
+
if (request) return
|
|
9
|
+
request = requestAnimationFrame((timestamp) => {
|
|
10
|
+
if (startTime === null) {
|
|
11
|
+
startTime = timestamp
|
|
12
|
+
}
|
|
13
|
+
const deltaTime = timestamp - startTime
|
|
14
|
+
const delta = (deltaTime / 1000) * speed
|
|
15
|
+
callback(delta)
|
|
16
|
+
request = null
|
|
17
|
+
start()
|
|
18
|
+
})
|
|
19
|
+
}
|
|
20
|
+
start()
|
|
21
|
+
return () => {
|
|
22
|
+
if (request) {
|
|
23
|
+
cancelAnimationFrame(request)
|
|
24
|
+
request = null
|
|
25
|
+
}
|
|
26
|
+
startTime = null
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.createUniformSpeedAnimation = createUniformSpeedAnimation
|
|
31
30
|
//越接近阈值,速度越小,越远离阈值,速度越大
|
|
32
31
|
const calcSpeedFactor = (delta = 0, threshold = Infinity) => {
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
}
|
|
38
|
-
exports.calcSpeedFactor = calcSpeedFactor
|
|
32
|
+
if (threshold >= delta) {
|
|
33
|
+
return (threshold - delta) / threshold
|
|
34
|
+
}
|
|
35
|
+
return 0
|
|
36
|
+
}
|
|
37
|
+
exports.calcSpeedFactor = calcSpeedFactor
|