@formily-design/shared 1.0.0

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.
Files changed (98) hide show
  1. package/LICENSE.md +20 -0
  2. package/README.md +1 -0
  3. package/dist/designable.shared.umd.production.js +1 -0
  4. package/dist/designable.shared.umd.production.min.js +2051 -0
  5. package/esm/animation.d.ts +2 -0
  6. package/esm/animation.js +33 -0
  7. package/esm/array.d.ts +37 -0
  8. package/esm/array.js +111 -0
  9. package/esm/clone.d.ts +4 -0
  10. package/esm/clone.js +97 -0
  11. package/esm/compose.d.ts +1 -0
  12. package/esm/compose.js +7 -0
  13. package/esm/coordinate.d.ts +106 -0
  14. package/esm/coordinate.js +475 -0
  15. package/esm/element.d.ts +6 -0
  16. package/esm/element.js +137 -0
  17. package/esm/event.d.ts +96 -0
  18. package/esm/event.js +213 -0
  19. package/esm/globalThisPolyfill.d.ts +1 -0
  20. package/esm/globalThisPolyfill.js +22 -0
  21. package/esm/index.d.ts +17 -0
  22. package/esm/index.js +17 -0
  23. package/esm/instanceof.d.ts +1 -0
  24. package/esm/instanceof.js +11 -0
  25. package/esm/keycode.d.ts +147 -0
  26. package/esm/keycode.js +150 -0
  27. package/esm/lru.d.ts +73 -0
  28. package/esm/lru.js +293 -0
  29. package/esm/observer.d.ts +9 -0
  30. package/esm/observer.js +29 -0
  31. package/esm/request-idle.d.ts +10 -0
  32. package/esm/request-idle.js +8 -0
  33. package/esm/scroller.d.ts +10 -0
  34. package/esm/scroller.js +82 -0
  35. package/esm/subscribable.d.ts +14 -0
  36. package/esm/subscribable.js +48 -0
  37. package/esm/types.d.ts +13 -0
  38. package/esm/types.js +21 -0
  39. package/esm/uid.d.ts +1 -0
  40. package/esm/uid.js +9 -0
  41. package/lib/animation.d.ts +2 -0
  42. package/lib/animation.js +38 -0
  43. package/lib/array.d.ts +37 -0
  44. package/lib/array.js +125 -0
  45. package/lib/clone.d.ts +4 -0
  46. package/lib/clone.js +102 -0
  47. package/lib/compose.d.ts +1 -0
  48. package/lib/compose.js +11 -0
  49. package/lib/coordinate.d.ts +106 -0
  50. package/lib/coordinate.js +504 -0
  51. package/lib/element.d.ts +6 -0
  52. package/lib/element.js +145 -0
  53. package/lib/event.d.ts +96 -0
  54. package/lib/event.js +218 -0
  55. package/lib/globalThisPolyfill.d.ts +1 -0
  56. package/lib/globalThisPolyfill.js +25 -0
  57. package/lib/index.d.ts +17 -0
  58. package/lib/index.js +33 -0
  59. package/lib/instanceof.d.ts +1 -0
  60. package/lib/instanceof.js +15 -0
  61. package/lib/keycode.d.ts +147 -0
  62. package/lib/keycode.js +154 -0
  63. package/lib/lru.d.ts +73 -0
  64. package/lib/lru.js +297 -0
  65. package/lib/observer.d.ts +9 -0
  66. package/lib/observer.js +33 -0
  67. package/lib/request-idle.d.ts +10 -0
  68. package/lib/request-idle.js +13 -0
  69. package/lib/scroller.d.ts +10 -0
  70. package/lib/scroller.js +88 -0
  71. package/lib/subscribable.d.ts +14 -0
  72. package/lib/subscribable.js +52 -0
  73. package/lib/types.d.ts +13 -0
  74. package/lib/types.js +29 -0
  75. package/lib/uid.d.ts +1 -0
  76. package/lib/uid.js +12 -0
  77. package/package.json +32 -0
  78. package/rollup.config.js +3 -0
  79. package/src/animation.ts +38 -0
  80. package/src/array.ts +301 -0
  81. package/src/clone.ts +96 -0
  82. package/src/compose.ts +7 -0
  83. package/src/coordinate.ts +633 -0
  84. package/src/element.ts +144 -0
  85. package/src/event.ts +379 -0
  86. package/src/globalThisPolyfill.ts +19 -0
  87. package/src/index.ts +17 -0
  88. package/src/instanceof.ts +10 -0
  89. package/src/keycode.ts +150 -0
  90. package/src/lru.ts +322 -0
  91. package/src/observer.ts +38 -0
  92. package/src/request-idle.ts +22 -0
  93. package/src/scroller.ts +113 -0
  94. package/src/subscribable.ts +60 -0
  95. package/src/types.ts +27 -0
  96. package/src/uid.ts +10 -0
  97. package/tsconfig.build.json +10 -0
  98. package/tsconfig.json +5 -0
@@ -0,0 +1,2 @@
1
+ export declare const createUniformSpeedAnimation: (speed: number, callback: (delta: number) => void) => () => void;
2
+ export declare const calcSpeedFactor: (delta?: number, threshold?: number) => number;
@@ -0,0 +1,33 @@
1
+ export const createUniformSpeedAnimation = (speed = 10, callback) => {
2
+ let request = null;
3
+ let startTime = null;
4
+ const start = () => {
5
+ if (request)
6
+ return;
7
+ request = requestAnimationFrame((timestamp) => {
8
+ if (startTime === null) {
9
+ startTime = timestamp;
10
+ }
11
+ const deltaTime = timestamp - startTime;
12
+ const delta = (deltaTime / 1000) * speed;
13
+ callback(delta);
14
+ request = null;
15
+ start();
16
+ });
17
+ };
18
+ start();
19
+ return () => {
20
+ if (request) {
21
+ cancelAnimationFrame(request);
22
+ request = null;
23
+ }
24
+ startTime = null;
25
+ };
26
+ };
27
+ //越接近阈值,速度越小,越远离阈值,速度越大
28
+ export const calcSpeedFactor = (delta = 0, threshold = Infinity) => {
29
+ if (threshold >= delta) {
30
+ return (threshold - delta) / threshold;
31
+ }
32
+ return 0;
33
+ };
package/esm/array.d.ts ADDED
@@ -0,0 +1,37 @@
1
+ type EachArrayIterator<T> = (currentValue: T, key: number) => void | boolean;
2
+ type EachStringIterator = (currentValue: string, key: number) => void | boolean;
3
+ type EachObjectIterator<T = any> = (currentValue: T, key: string) => void | boolean;
4
+ type MapArrayIterator<TItem, TResult> = (currentValue: TItem, key: number) => TResult;
5
+ type MapStringIterator<TResult> = (currentValue: string, key: number) => TResult;
6
+ type MapObjectIterator<TItem, TResult> = (currentValue: TItem, key: string) => TResult;
7
+ type MemoArrayIterator<T, U> = (previousValue: U, currentValue: T, key: number) => U;
8
+ type MemoStringIterator<T> = (previousValue: T, currentValue: string, key: number) => T;
9
+ type MemoObjectIterator<TValue, TResult> = (previousValue: TResult, currentValue: TValue, key: string) => TResult;
10
+ export declare const toArr: (val: any) => any[];
11
+ export declare function each(val: string, iterator: EachStringIterator, revert?: boolean): void;
12
+ export declare function each<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): void;
13
+ export declare function each<T extends {}, TValue = T[keyof T]>(val: T, iterator: EachObjectIterator<TValue>, revert?: boolean): void;
14
+ export declare function map<T>(val: string, iterator: MapStringIterator<T>, revert?: boolean): any;
15
+ export declare function map<TItem, TResult>(val: TItem[], iterator: MapArrayIterator<TItem, TResult>, revert?: boolean): any;
16
+ export declare function map<T extends {}, TResult>(val: T, iterator: MapObjectIterator<T[keyof T], TResult>, revert?: boolean): any;
17
+ export declare function reduce<T, U>(val: T[], iterator: MemoArrayIterator<T, U>, accumulator?: U, revert?: boolean): U;
18
+ export declare function reduce<T>(val: string, iterator: MemoStringIterator<T>, accumulator?: T, revert?: boolean): T;
19
+ export declare function reduce<T extends {}, TValue = T[keyof T], TResult = any>(val: T, iterator: MemoObjectIterator<TValue, TResult>, accumulator?: TResult, revert?: boolean): TResult;
20
+ export declare function every<T extends string>(val: T, iterator: EachStringIterator, revert?: boolean): boolean;
21
+ export declare function every<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): boolean;
22
+ export declare function every<T extends {}, TValue = T[keyof T]>(val: T, iterator: EachObjectIterator, revert?: boolean): boolean;
23
+ export declare function some<T extends string>(val: T, iterator: EachStringIterator, revert?: boolean): boolean;
24
+ export declare function some<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): boolean;
25
+ export declare function some<T extends {}, TValue = T[keyof T]>(val: T, iterator: EachObjectIterator, revert?: boolean): boolean;
26
+ export declare function findIndex<T extends string>(val: T, iterator: EachStringIterator, revert?: boolean): number;
27
+ export declare function findIndex<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): number;
28
+ export declare function findIndex<T extends {}, TValue = T[keyof T]>(val: T, iterator: EachObjectIterator, revert?: boolean): keyof T;
29
+ export declare function find<T extends string>(val: T, iterator: EachStringIterator, revert?: boolean): any;
30
+ export declare function find<T>(val: T[], iterator: EachArrayIterator<T>, revert?: boolean): T;
31
+ export declare function find<T extends {}, TValue = T[keyof T]>(val: T, iterator: EachObjectIterator, revert?: boolean): T[keyof T];
32
+ export declare function includes<T extends string>(val: T, searchElement: string, revert?: boolean): boolean;
33
+ export declare function includes<T>(val: T[], searchElement: T, revert?: boolean): boolean;
34
+ export declare function includesWith<T extends string>(val: T, search: (item: string) => boolean): boolean;
35
+ export declare function includesWith<T>(val: T[], search: (item: T) => boolean): boolean;
36
+ export declare const flat: <T>(array: Array<T | T[]>) => T[];
37
+ export {};
package/esm/array.js ADDED
@@ -0,0 +1,111 @@
1
+ import { isArr, isObj, isStr } from './types';
2
+ export const toArr = (val) => (isArr(val) ? val : val ? [val] : []);
3
+ export function each(val, iterator, revert) {
4
+ if (isArr(val) || isStr(val)) {
5
+ if (revert) {
6
+ for (let i = val.length - 1; i >= 0; i--) {
7
+ if (iterator(val[i], i) === false) {
8
+ return;
9
+ }
10
+ }
11
+ }
12
+ else {
13
+ for (let i = 0; i < val.length; i++) {
14
+ if (iterator(val[i], i) === false) {
15
+ return;
16
+ }
17
+ }
18
+ }
19
+ }
20
+ else if (isObj(val)) {
21
+ let key;
22
+ for (key in val) {
23
+ if (Object.hasOwnProperty.call(val, key)) {
24
+ if (iterator(val[key], key) === false) {
25
+ return;
26
+ }
27
+ }
28
+ }
29
+ }
30
+ }
31
+ export function map(val, iterator, revert) {
32
+ const res = isArr(val) || isStr(val) ? [] : {};
33
+ each(val, (item, key) => {
34
+ const value = iterator(item, key);
35
+ if (isArr(res)) {
36
+ ;
37
+ res.push(value);
38
+ }
39
+ else {
40
+ res[key] = value;
41
+ }
42
+ }, revert);
43
+ return res;
44
+ }
45
+ export function reduce(val, iterator, accumulator, revert) {
46
+ let result = accumulator;
47
+ each(val, (item, key) => {
48
+ result = iterator(result, item, key);
49
+ }, revert);
50
+ return result;
51
+ }
52
+ export function every(val, iterator, revert) {
53
+ let res = true;
54
+ each(val, (item, key) => {
55
+ if (!iterator(item, key)) {
56
+ res = false;
57
+ return false;
58
+ }
59
+ }, revert);
60
+ return res;
61
+ }
62
+ export function some(val, iterator, revert) {
63
+ let res = false;
64
+ each(val, (item, key) => {
65
+ if (iterator(item, key)) {
66
+ res = true;
67
+ return false;
68
+ }
69
+ }, revert);
70
+ return res;
71
+ }
72
+ export function findIndex(val, iterator, revert) {
73
+ let res = -1;
74
+ each(val, (item, key) => {
75
+ if (iterator(item, key)) {
76
+ res = key;
77
+ return false;
78
+ }
79
+ }, revert);
80
+ return res;
81
+ }
82
+ export function find(val, iterator, revert) {
83
+ let res;
84
+ each(val, (item, key) => {
85
+ if (iterator(item, key)) {
86
+ res = item;
87
+ return false;
88
+ }
89
+ }, revert);
90
+ return res;
91
+ }
92
+ export function includes(val, searchElement, revert) {
93
+ if (isStr(val))
94
+ return val.includes(searchElement);
95
+ return some(val, (item) => item === searchElement, revert);
96
+ }
97
+ export function includesWith(val, search) {
98
+ if (isArr(val)) {
99
+ return val.some((item) => search(item));
100
+ }
101
+ else {
102
+ return false;
103
+ }
104
+ }
105
+ export const flat = (array) => {
106
+ return toArr(array).reduce((buf, item) => {
107
+ if (isArr(item))
108
+ return buf.concat(flat(item));
109
+ return buf.concat(item);
110
+ }, []);
111
+ };
package/esm/clone.d.ts ADDED
@@ -0,0 +1,4 @@
1
+ type Filter = (value: any, key: string) => boolean;
2
+ export declare const shallowClone: (values: any) => any;
3
+ export declare const clone: (values: any, filter?: Filter) => any;
4
+ export {};
package/esm/clone.js ADDED
@@ -0,0 +1,97 @@
1
+ import { isFn } from './types';
2
+ import { instOf } from './instanceof';
3
+ const NATIVE_KEYS = [
4
+ ['Map', (map) => new Map(map)],
5
+ ['WeakMap', (map) => new WeakMap(map)],
6
+ ['WeakSet', (set) => new WeakSet(set)],
7
+ ['Set', (set) => new Set(set)],
8
+ ['Date', (date) => new Date(date)],
9
+ 'FileList',
10
+ 'File',
11
+ 'URL',
12
+ 'RegExp',
13
+ [
14
+ 'Promise',
15
+ (promise) => new Promise((resolve, reject) => promise.then(resolve, reject)),
16
+ ],
17
+ ];
18
+ const isNativeObject = (values) => {
19
+ for (let i = 0; i < NATIVE_KEYS.length; i++) {
20
+ const item = NATIVE_KEYS[i];
21
+ if (Array.isArray(item) && item[0]) {
22
+ if (instOf(values, item[0])) {
23
+ return item[1] ? item[1] : item[0];
24
+ }
25
+ }
26
+ else {
27
+ if (instOf(values, item)) {
28
+ return item;
29
+ }
30
+ }
31
+ }
32
+ };
33
+ export const shallowClone = (values) => {
34
+ let nativeClone;
35
+ if (Array.isArray(values)) {
36
+ return values.slice(0);
37
+ }
38
+ else if (isNativeObject(values)) {
39
+ nativeClone = isNativeObject(values);
40
+ return isFn(nativeClone) ? nativeClone(values) : values;
41
+ }
42
+ else if (typeof values === 'object' && !!values) {
43
+ return {
44
+ ...values,
45
+ };
46
+ }
47
+ };
48
+ export const clone = (values, filter) => {
49
+ let nativeClone;
50
+ if (Array.isArray(values)) {
51
+ return values.map((item) => clone(item, filter));
52
+ }
53
+ else if (isNativeObject(values)) {
54
+ nativeClone = isNativeObject(values);
55
+ return isFn(nativeClone) ? nativeClone(values) : values;
56
+ }
57
+ else if (typeof values === 'object' && !!values) {
58
+ if ('$$typeof' in values && '_owner' in values) {
59
+ return values;
60
+ }
61
+ if (values._isAMomentObject) {
62
+ return values;
63
+ }
64
+ if (values._isJSONSchemaObject) {
65
+ return values;
66
+ }
67
+ if (isFn(values.toJS)) {
68
+ return values;
69
+ }
70
+ if (isFn(values.toJSON)) {
71
+ return values;
72
+ }
73
+ if (Object.getOwnPropertySymbols(values || {}).length) {
74
+ return values;
75
+ }
76
+ const res = {};
77
+ for (const key in values) {
78
+ if (Object.hasOwnProperty.call(values, key)) {
79
+ if (isFn(filter)) {
80
+ if (filter(values[key], key)) {
81
+ res[key] = clone(values[key], filter);
82
+ }
83
+ else {
84
+ res[key] = values[key];
85
+ }
86
+ }
87
+ else {
88
+ res[key] = clone(values[key], filter);
89
+ }
90
+ }
91
+ }
92
+ return res;
93
+ }
94
+ else {
95
+ return values;
96
+ }
97
+ };
@@ -0,0 +1 @@
1
+ export declare const compose: (...fns: ((payload: any) => any)[]) => (payload: any) => any;
package/esm/compose.js ADDED
@@ -0,0 +1,7 @@
1
+ export const compose = (...fns) => {
2
+ return (payload) => {
3
+ return fns.reduce((buf, fn) => {
4
+ return fn(buf);
5
+ }, payload);
6
+ };
7
+ };
@@ -0,0 +1,106 @@
1
+ export interface IPoint {
2
+ x: number;
3
+ y: number;
4
+ }
5
+ export interface ISize {
6
+ width: number;
7
+ height: number;
8
+ }
9
+ export interface ILineSegment {
10
+ start: IPoint;
11
+ end: IPoint;
12
+ }
13
+ export interface IRectEdgeLines {
14
+ v: ILineSegment[];
15
+ h: ILineSegment[];
16
+ }
17
+ export declare function isRect(rect: any): rect is IRect;
18
+ export declare function isPoint(val: any): val is IPoint;
19
+ export declare function isLineSegment(val: any): val is ILineSegment;
20
+ export declare class Point implements IPoint {
21
+ x: number;
22
+ y: number;
23
+ constructor(x: number, y: number);
24
+ }
25
+ export declare class Rect implements IRect {
26
+ x: number;
27
+ y: number;
28
+ width: number;
29
+ height: number;
30
+ constructor(x: number, y: number, width: number, height: number);
31
+ get left(): number;
32
+ get right(): number;
33
+ get top(): number;
34
+ get bottom(): number;
35
+ }
36
+ export declare class LineSegment {
37
+ start: IPoint;
38
+ end: IPoint;
39
+ constructor(start: IPoint, end: IPoint);
40
+ }
41
+ export interface IRect {
42
+ x: number;
43
+ y: number;
44
+ width: number;
45
+ height: number;
46
+ }
47
+ export declare enum RectQuadrant {
48
+ Inner1 = "I1",//内部第一象限
49
+ Inner2 = "I2",//内部第二象限
50
+ Inner3 = "I3",//内部第三象限
51
+ Inner4 = "I4",//内部第四象限
52
+ Outer1 = "O1",//内部第五象限
53
+ Outer2 = "O2",//内部第六象限
54
+ Outer3 = "O3",//内部第七象限
55
+ Outer4 = "O4"
56
+ }
57
+ export interface IPointToRectRelative {
58
+ quadrant: RectQuadrant;
59
+ distance: number;
60
+ }
61
+ export declare function isPointInRect(point: IPoint, rect: IRect, sensitive?: boolean): boolean;
62
+ export declare function isEqualRect(target: IRect, source: IRect): boolean;
63
+ export declare function getRectPoints(source: IRect): Point[];
64
+ export declare function isRectInRect(target: IRect, source: IRect): boolean;
65
+ export declare function isCrossRectInRect(target: IRect, source: IRect): boolean;
66
+ /**
67
+ * 计算点在矩形的哪个象限
68
+ * @param point
69
+ * @param rect
70
+ */
71
+ export declare function calcQuadrantOfPointToRect(point: IPoint, rect: IRect): RectQuadrant;
72
+ export declare function calcDistanceOfPointToRect(point: IPoint, rect: IRect): number;
73
+ export declare function calcDistancePointToEdge(point: IPoint, rect: IRect): number;
74
+ export declare function isNearAfter(point: IPoint, rect: IRect, inline?: boolean): boolean;
75
+ /**
76
+ * 计算点鱼矩形的相对位置信息
77
+ * @param point
78
+ * @param rect
79
+ */
80
+ export declare function calcRelativeOfPointToRect(point: IPoint, rect: IRect): IPointToRectRelative;
81
+ export declare function calcBoundingRect(rects: IRect[]): Rect;
82
+ export declare function calcRectByStartEndPoint(startPoint: IPoint, endPoint: IPoint, scrollX?: number, scrollY?: number): Rect;
83
+ export declare function calcEdgeLinesOfRect(rect: IRect): IRectEdgeLines;
84
+ export declare function calcRectOfAxisLineSegment(line: ILineSegment): Rect;
85
+ export declare function calcSpaceBlockOfRect(target: IRect, source: IRect, type?: string): {
86
+ type: string;
87
+ distance: number;
88
+ rect: Rect;
89
+ };
90
+ export declare function calcExtendsLineSegmentOfRect(targetRect: Rect, referRect: Rect): {
91
+ start: {
92
+ x: number;
93
+ y: number;
94
+ };
95
+ end: {
96
+ x: number;
97
+ y: number;
98
+ };
99
+ };
100
+ export declare function calcOffsetOfSnapLineSegmentToEdge(line: ILineSegment, current: IRect): {
101
+ x: number;
102
+ y: number;
103
+ };
104
+ export declare function calcDistanceOfSnapLineToEdges(line: ILineSegment, edges: IRectEdgeLines): number;
105
+ export declare function calcCombineSnapLineSegment(target: ILineSegment, source: ILineSegment): ILineSegment;
106
+ export declare function calcClosestEdges(line: ILineSegment, edges: IRectEdgeLines): [number, ILineSegment];