pixuireactcomponents 1.5.11 → 1.5.13
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/index.d.ts +1 -0
- package/index.js +1 -0
- package/package.json +1 -1
- package/src/components/react/app/listDrag/listDrag.d.ts +27 -0
- package/src/components/react/app/listDrag/listDrag.js +150 -0
- package/src/components/react/app/scrollBar/ScrollBar.js +14 -19
- package/src/js_components/index.d.ts +1 -0
- package/src/js_components/index.js +11 -0
- package/src/js_components/src/HMSETHelper.d.ts +27 -0
- package/src/js_components/src/HMSETHelper.js +297 -0
- package/src/js_components/src/frameAnimationJs.d.ts +32 -0
- package/src/js_components/src/frameAnimationJs.js +125 -0
package/index.d.ts
CHANGED
|
@@ -18,5 +18,6 @@ export { tools } from "./src/components/tools/tools";
|
|
|
18
18
|
export { PreloadImg } from "./src/components/react/app/preloadImg/PreloadImg";
|
|
19
19
|
export { LoadChecker } from "./src/components/tools/LoadChecker";
|
|
20
20
|
export { AntiRepeat } from "./src/components/tools/antiRepeat";
|
|
21
|
+
export { DragList } from "./src/components/react/app/listDrag/listDrag";
|
|
21
22
|
export { Slider, SliderProps } from "./src/components/react/app/slider/Slider";
|
|
22
23
|
export { ImgPreLoader_base64, PreLoadPic_base64 } from "./src/components/tools/ImgPreLoader";
|
package/index.js
CHANGED
|
@@ -23,3 +23,4 @@ export { tools } from './src/components/tools/tools';
|
|
|
23
23
|
export { PreloadImg } from './src/components/react/app/preloadImg/PreloadImg';
|
|
24
24
|
export { LoadChecker } from './src/components/tools/LoadChecker';
|
|
25
25
|
export { AntiRepeat } from './src/components/tools/antiRepeat';
|
|
26
|
+
export { DragList } from './src/components/react/app/listDrag/listDrag';
|
package/package.json
CHANGED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { h, Component } from 'preact';
|
|
2
|
+
interface DragListProps {
|
|
3
|
+
items: string[];
|
|
4
|
+
}
|
|
5
|
+
interface DragListState {
|
|
6
|
+
items: string[];
|
|
7
|
+
draggingIndex: number | null;
|
|
8
|
+
dragOverIndex: number | null;
|
|
9
|
+
dragStartX: number;
|
|
10
|
+
dragStartY: number;
|
|
11
|
+
dragOffsetX: number;
|
|
12
|
+
dragOffsetY: number;
|
|
13
|
+
width: number;
|
|
14
|
+
height: number;
|
|
15
|
+
hoverIndex: number | null;
|
|
16
|
+
}
|
|
17
|
+
export declare class DragList extends Component<DragListProps, DragListState> {
|
|
18
|
+
constructor(props: DragListProps);
|
|
19
|
+
handleMouseDown: (index: number, e: MouseEvent) => void;
|
|
20
|
+
handleMouseMove: (e: MouseEvent) => void;
|
|
21
|
+
handleMouseUp: () => void;
|
|
22
|
+
handleMouseEnter: (index: number) => void;
|
|
23
|
+
handleMouseLeave: () => void;
|
|
24
|
+
getDragOverIndex: (clientX: number, clientY: number) => number | null;
|
|
25
|
+
render(): h.JSX.Element;
|
|
26
|
+
}
|
|
27
|
+
export {};
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
var __extends = (this && this.__extends) || (function () {
|
|
2
|
+
var extendStatics = function (d, b) {
|
|
3
|
+
extendStatics = Object.setPrototypeOf ||
|
|
4
|
+
({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
|
5
|
+
function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
|
|
6
|
+
return extendStatics(d, b);
|
|
7
|
+
};
|
|
8
|
+
return function (d, b) {
|
|
9
|
+
if (typeof b !== "function" && b !== null)
|
|
10
|
+
throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
|
|
11
|
+
extendStatics(d, b);
|
|
12
|
+
function __() { this.constructor = d; }
|
|
13
|
+
d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
|
14
|
+
};
|
|
15
|
+
})();
|
|
16
|
+
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
17
|
+
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
18
|
+
if (ar || !(i in from)) {
|
|
19
|
+
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
20
|
+
ar[i] = from[i];
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
return to.concat(ar || Array.prototype.slice.call(from));
|
|
24
|
+
};
|
|
25
|
+
import { h, Component } from 'preact';
|
|
26
|
+
var DragList = /** @class */ (function (_super) {
|
|
27
|
+
__extends(DragList, _super);
|
|
28
|
+
function DragList(props) {
|
|
29
|
+
var _this = _super.call(this, props) || this;
|
|
30
|
+
_this.handleMouseDown = function (index, e) {
|
|
31
|
+
var rect = e.target.getBoundingClientRect();
|
|
32
|
+
_this.setState({
|
|
33
|
+
draggingIndex: index,
|
|
34
|
+
dragStartX: e.clientX,
|
|
35
|
+
dragStartY: e.clientY,
|
|
36
|
+
dragOffsetX: e.clientX - rect.left,
|
|
37
|
+
dragOffsetY: e.clientY - rect.top,
|
|
38
|
+
width: rect.width,
|
|
39
|
+
height: rect.height,
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
_this.handleMouseMove = function (e) {
|
|
43
|
+
var draggingIndex = _this.state.draggingIndex;
|
|
44
|
+
if (draggingIndex !== null) {
|
|
45
|
+
var dragOverIndex = _this.getDragOverIndex(e.clientX, e.clientY);
|
|
46
|
+
_this.setState({
|
|
47
|
+
dragOverIndex: dragOverIndex,
|
|
48
|
+
dragStartX: e.clientX,
|
|
49
|
+
dragStartY: e.clientY,
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
_this.handleMouseUp = function () {
|
|
54
|
+
var _a = _this.state, draggingIndex = _a.draggingIndex, dragOverIndex = _a.dragOverIndex, items = _a.items;
|
|
55
|
+
if (draggingIndex !== null && dragOverIndex !== null) {
|
|
56
|
+
var newItems = __spreadArray([], items, true);
|
|
57
|
+
var draggedItem = newItems.splice(draggingIndex, 1)[0];
|
|
58
|
+
newItems.splice(dragOverIndex, 0, draggedItem);
|
|
59
|
+
_this.setState({
|
|
60
|
+
items: newItems,
|
|
61
|
+
draggingIndex: null,
|
|
62
|
+
dragOverIndex: null,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
else {
|
|
66
|
+
_this.setState({
|
|
67
|
+
draggingIndex: null,
|
|
68
|
+
dragOverIndex: null,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
};
|
|
72
|
+
_this.handleMouseEnter = function (index) {
|
|
73
|
+
_this.setState({ hoverIndex: index });
|
|
74
|
+
};
|
|
75
|
+
_this.handleMouseLeave = function () {
|
|
76
|
+
_this.setState({ hoverIndex: null });
|
|
77
|
+
};
|
|
78
|
+
_this.getDragOverIndex = function (clientX, clientY) {
|
|
79
|
+
var _a = _this.state, draggingIndex = _a.draggingIndex, width = _a.width, height = _a.height, hoverIndex = _a.hoverIndex;
|
|
80
|
+
if (draggingIndex === null)
|
|
81
|
+
return null;
|
|
82
|
+
var element = document.elementFromPoint ? document.elementFromPoint(clientX, clientY) : null;
|
|
83
|
+
if (element == null) {
|
|
84
|
+
return hoverIndex;
|
|
85
|
+
}
|
|
86
|
+
if (element && element.className.includes('drag-item')) {
|
|
87
|
+
var targetIndex = Array.from(element.parentNode.children).indexOf(element);
|
|
88
|
+
if (targetIndex === draggingIndex)
|
|
89
|
+
return null;
|
|
90
|
+
var targetRect = element.getBoundingClientRect();
|
|
91
|
+
var draggingRect = {
|
|
92
|
+
left: clientX - _this.state.dragOffsetX,
|
|
93
|
+
top: clientY - _this.state.dragOffsetY,
|
|
94
|
+
right: clientX - _this.state.dragOffsetX + width,
|
|
95
|
+
bottom: clientY - _this.state.dragOffsetY + height,
|
|
96
|
+
};
|
|
97
|
+
var overlapX = Math.min(draggingRect.right, targetRect.right) - Math.max(draggingRect.left, targetRect.left);
|
|
98
|
+
var overlapY = Math.min(draggingRect.bottom, targetRect.bottom) - Math.max(draggingRect.top, targetRect.top);
|
|
99
|
+
var overlapRatioX = overlapX / width;
|
|
100
|
+
var overlapRatioY = overlapY / height;
|
|
101
|
+
if (overlapRatioX >= 0.5 && overlapRatioY >= 0.5) {
|
|
102
|
+
return targetIndex;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return null;
|
|
106
|
+
};
|
|
107
|
+
_this.state = {
|
|
108
|
+
items: props.items,
|
|
109
|
+
draggingIndex: null,
|
|
110
|
+
dragOverIndex: null,
|
|
111
|
+
dragStartX: 0,
|
|
112
|
+
dragStartY: 0,
|
|
113
|
+
dragOffsetX: 0,
|
|
114
|
+
dragOffsetY: 0,
|
|
115
|
+
width: 0,
|
|
116
|
+
height: 0,
|
|
117
|
+
hoverIndex: null,
|
|
118
|
+
};
|
|
119
|
+
return _this;
|
|
120
|
+
}
|
|
121
|
+
DragList.prototype.render = function () {
|
|
122
|
+
var _this = this;
|
|
123
|
+
var _a = this.state, items = _a.items, width = _a.width, height = _a.height, draggingIndex = _a.draggingIndex, dragOverIndex = _a.dragOverIndex, dragStartX = _a.dragStartX, dragStartY = _a.dragStartY, dragOffsetX = _a.dragOffsetX, dragOffsetY = _a.dragOffsetY, hoverIndex = _a.hoverIndex;
|
|
124
|
+
return (h("div", { style: { display: 'flex', flexWrap: 'wrap', flexDirection: 'row', alignItems: 'center', justifyContent: 'center' } },
|
|
125
|
+
items.map(function (item, index) { return (h("div", { key: index, draggable: true, style: {
|
|
126
|
+
padding: '8px',
|
|
127
|
+
border: '1px solid #ccc',
|
|
128
|
+
cursor: 'grab',
|
|
129
|
+
transition: 'transform 0.2s ease',
|
|
130
|
+
margin: '0.1rem',
|
|
131
|
+
position: 'relative',
|
|
132
|
+
opacity: index === draggingIndex ? 0.5 : 1,
|
|
133
|
+
zIndex: index === draggingIndex ? 1 : 'auto',
|
|
134
|
+
backgroundColor: index === dragOverIndex ? '#f0f0f0' : '#fff',
|
|
135
|
+
}, onDragStart: function (e) { return _this.handleMouseDown(index, e); }, onDrag: _this.handleMouseMove, onDragEnd: _this.handleMouseUp, onMouseEnter: function () { return _this.handleMouseEnter(index); }, onMouseLeave: _this.handleMouseLeave }, item)); }),
|
|
136
|
+
draggingIndex !== null && (h("div", { style: {
|
|
137
|
+
position: 'fixed',
|
|
138
|
+
width: width,
|
|
139
|
+
height: height,
|
|
140
|
+
left: dragStartX - dragOffsetX,
|
|
141
|
+
top: dragStartY - dragOffsetY,
|
|
142
|
+
pointerEvents: 'none',
|
|
143
|
+
backgroundColor: '#f0f0f0',
|
|
144
|
+
padding: '8px',
|
|
145
|
+
border: '1px solid #ccc',
|
|
146
|
+
} }, items[draggingIndex]))));
|
|
147
|
+
};
|
|
148
|
+
return DragList;
|
|
149
|
+
}(Component));
|
|
150
|
+
export { DragList };
|
|
@@ -127,30 +127,25 @@ export var ScrollBar = function (props) {
|
|
|
127
127
|
};
|
|
128
128
|
// 拖动滑块时,页面跟着移动
|
|
129
129
|
var sBlockScrolling = function (event) {
|
|
130
|
+
var y = 0;
|
|
130
131
|
if (event.clientY - clientYtoBlockTop < sFrame.getBoundingClientRect().top) {
|
|
131
|
-
|
|
132
|
-
setSBlockY(0);
|
|
132
|
+
y = 0;
|
|
133
133
|
}
|
|
134
134
|
else if (event.clientY - clientYtoBlockTop + sBlock.getBoundingClientRect().height > sFrame.getBoundingClientRect().bottom) {
|
|
135
|
-
|
|
136
|
-
setSBlockY(sFrame.getBoundingClientRect().height - sBlock.getBoundingClientRect().height);
|
|
135
|
+
y = sFrame.getBoundingClientRect().height - sBlock.getBoundingClientRect().height;
|
|
137
136
|
}
|
|
138
137
|
else {
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
var rate = slideMarginTop / (sframe_height - sblock_height);
|
|
151
|
-
var content_scrollTop = (content_height - swindow_height) * rate;
|
|
152
|
-
sWindow.scrollTop = content_scrollTop;
|
|
153
|
-
}
|
|
138
|
+
y = event.clientY - clientYtoBlockTop - sFrame.getBoundingClientRect().top;
|
|
139
|
+
}
|
|
140
|
+
setSBlockY(y);
|
|
141
|
+
// //移动 content
|
|
142
|
+
var sblock_height = sBlock.getBoundingClientRect().height;
|
|
143
|
+
var sframe_height = sFrame.getBoundingClientRect().height;
|
|
144
|
+
var swindow_height = sWindow.getBoundingClientRect().height;
|
|
145
|
+
var content_height = sWindow.scrollHeight;
|
|
146
|
+
var rate = y / (sframe_height - sblock_height);
|
|
147
|
+
var content_scrollTop = (content_height - swindow_height) * rate;
|
|
148
|
+
sWindow.scrollTop = content_scrollTop;
|
|
154
149
|
};
|
|
155
150
|
useImperativeHandle(cRef, function () { return ({
|
|
156
151
|
scrollTo: scrollTo,
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { frameAnimationJs, SpritePlugin, HMSETHelper, ktxConverter };
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var frameAnimationJs = require('./dist/frameAnimationJs').frameAnimationJs;
|
|
3
|
+
var SpritePlugin = require('./dist/sprite').SpritePlugin;
|
|
4
|
+
var HMSETHelper = require('./dist/HMSETHelper').HMSETHelper;
|
|
5
|
+
var ktxConverter = require('./dist/ktxConverter').ktxConverter;
|
|
6
|
+
module.exports = {
|
|
7
|
+
frameAnimationJs: frameAnimationJs,
|
|
8
|
+
SpritePlugin: SpritePlugin,
|
|
9
|
+
HMSETHelper: HMSETHelper,
|
|
10
|
+
ktxConverter: ktxConverter,
|
|
11
|
+
};
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
type HMSETData = {
|
|
2
|
+
[key: string]: HMSETData;
|
|
3
|
+
} | Array<HMSETData> | string | number | boolean | null | undefined;
|
|
4
|
+
/**
|
|
5
|
+
* 将obj转换为可以存储在MHSET中的数组,接受的数据结构为object和array之间的组合,key中不能包含 > [ ] 字符
|
|
6
|
+
*/
|
|
7
|
+
declare const HMSETHelper: {
|
|
8
|
+
/**展开obj */
|
|
9
|
+
ObjToHM: (obj: HMSETData) => string[];
|
|
10
|
+
/**将展开的obj数组恢复为obj */
|
|
11
|
+
HMToObj: typeof unflattenObject;
|
|
12
|
+
/**增量更新用,比较两个obj的差异,返回一个数组,HMSET oldData, HMSET diff => newData */
|
|
13
|
+
DiffObjToHM: (oldData: HMSETData, newData: HMSETData) => string[];
|
|
14
|
+
};
|
|
15
|
+
declare function hasCycle(obj: HMSETData): boolean;
|
|
16
|
+
declare function flattenObject(obj: HMSETData, prefix?: string, res?: string[]): string[];
|
|
17
|
+
declare function parseValue(value: string): null | boolean | number | [] | {};
|
|
18
|
+
type nestObj = HMSETData[] | {
|
|
19
|
+
[key: string]: HMSETData;
|
|
20
|
+
};
|
|
21
|
+
declare function keys2obj(keys: string[], value: string): nestObj;
|
|
22
|
+
declare function isNull(obj: HMSETData): boolean;
|
|
23
|
+
declare function getType(obj: HMSETData): string;
|
|
24
|
+
declare const mergeObj: (obj1: HMSETData, obj2: HMSETData) => HMSETData;
|
|
25
|
+
declare function unflattenObject(arr: string[]): {} | null;
|
|
26
|
+
declare const runTest: () => void;
|
|
27
|
+
declare function generateRandomObject(depth: number): HMSETData;
|
|
@@ -0,0 +1,297 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* 将obj转换为可以存储在MHSET中的数组,接受的数据结构为object和array之间的组合,key中不能包含 > [ ] 字符
|
|
4
|
+
*/
|
|
5
|
+
var HMSETHelper = {
|
|
6
|
+
/**展开obj */
|
|
7
|
+
ObjToHM: function (obj) {
|
|
8
|
+
if (hasCycle(obj)) {
|
|
9
|
+
console.error("obj has cycle");
|
|
10
|
+
return [];
|
|
11
|
+
}
|
|
12
|
+
return flattenObject(obj);
|
|
13
|
+
},
|
|
14
|
+
/**将展开的obj数组恢复为obj */
|
|
15
|
+
HMToObj: unflattenObject,
|
|
16
|
+
/**增量更新用,比较两个obj的差异,返回一个数组,HMSET oldData, HMSET diff => newData */
|
|
17
|
+
DiffObjToHM: function (oldData, newData) {
|
|
18
|
+
var oldTile = flattenObject(oldData);
|
|
19
|
+
var newTile = flattenObject(newData);
|
|
20
|
+
var res = [];
|
|
21
|
+
for (var i = 0; i < newTile.length; i += 2) {
|
|
22
|
+
var key = newTile[i];
|
|
23
|
+
var value = newTile[i + 1];
|
|
24
|
+
var index = oldTile.indexOf(key);
|
|
25
|
+
if (index === -1) {
|
|
26
|
+
res.push(key, value);
|
|
27
|
+
}
|
|
28
|
+
else {
|
|
29
|
+
//old有这个,检查是否有更新以后去掉
|
|
30
|
+
if (oldTile[index + 1] !== value) {
|
|
31
|
+
res.push(key, value);
|
|
32
|
+
}
|
|
33
|
+
oldTile.splice(index, 2);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
return res;
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
//检测obj是否有循环引用
|
|
40
|
+
function hasCycle(obj) {
|
|
41
|
+
var visited = new Set();
|
|
42
|
+
function bfs(obj) {
|
|
43
|
+
var queue = [obj];
|
|
44
|
+
while (queue.length > 0) {
|
|
45
|
+
var cur = queue.shift();
|
|
46
|
+
if (getType(cur) === "string" || getType(cur) === "number" || getType(cur) === "boolean" || getType(cur) === "null") {
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (visited.has(cur)) {
|
|
50
|
+
console.error("has cycle", cur);
|
|
51
|
+
return true;
|
|
52
|
+
}
|
|
53
|
+
if (Array.isArray(cur)) {
|
|
54
|
+
queue.push.apply(queue, cur);
|
|
55
|
+
}
|
|
56
|
+
else if (getType(cur) === "object") {
|
|
57
|
+
cur = cur;
|
|
58
|
+
for (var key in cur) {
|
|
59
|
+
queue.push(cur[key]);
|
|
60
|
+
}
|
|
61
|
+
visited.add(cur);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
return false;
|
|
65
|
+
}
|
|
66
|
+
return bfs(obj);
|
|
67
|
+
}
|
|
68
|
+
function flattenObject(obj, prefix, res) {
|
|
69
|
+
if (prefix === void 0) { prefix = ""; }
|
|
70
|
+
if (res === void 0) { res = []; }
|
|
71
|
+
if (getType(obj) === "string") {
|
|
72
|
+
res.push(prefix, "\"".concat(obj, "\""));
|
|
73
|
+
}
|
|
74
|
+
else if (getType(obj) === "number" || getType(obj) === "boolean") {
|
|
75
|
+
obj = obj;
|
|
76
|
+
res.push(prefix, obj.toString());
|
|
77
|
+
}
|
|
78
|
+
else if (isNull(obj)) {
|
|
79
|
+
res.push(prefix, "null");
|
|
80
|
+
}
|
|
81
|
+
else if (getType(obj) === "array") {
|
|
82
|
+
obj = obj;
|
|
83
|
+
if (obj.length === 0) {
|
|
84
|
+
res.push(prefix, "[]");
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
obj.forEach(function (item, index) {
|
|
88
|
+
flattenObject(item, prefix ? "".concat(prefix, ">[").concat(index, "]") : "[".concat(index, "]"), res);
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
else if (getType(obj) === "object") {
|
|
93
|
+
obj = obj;
|
|
94
|
+
if (Object.keys(obj).length === 0) {
|
|
95
|
+
res.push(prefix, "{}");
|
|
96
|
+
}
|
|
97
|
+
else {
|
|
98
|
+
Object.keys(obj).forEach(function (key) {
|
|
99
|
+
obj = obj;
|
|
100
|
+
flattenObject(obj[key], prefix ? "".concat(prefix, ">").concat(key) : key, res);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
return res;
|
|
105
|
+
}
|
|
106
|
+
function parseValue(value) {
|
|
107
|
+
if (value.startsWith('"') && value.endsWith('"')) {
|
|
108
|
+
return value.slice(1, -1);
|
|
109
|
+
}
|
|
110
|
+
switch (value) {
|
|
111
|
+
case "null":
|
|
112
|
+
return null;
|
|
113
|
+
case "true":
|
|
114
|
+
return true;
|
|
115
|
+
case "false":
|
|
116
|
+
return false;
|
|
117
|
+
case "[]":
|
|
118
|
+
return [];
|
|
119
|
+
case "{}":
|
|
120
|
+
return {};
|
|
121
|
+
default:
|
|
122
|
+
return parseFloat(value);
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
function keys2obj(keys, value) {
|
|
126
|
+
var res = keys[0].substring(0, 1) === "[" ? [] : {};
|
|
127
|
+
if (keys[0].substring(0, 1) === "[") {
|
|
128
|
+
var idx = parseInt(keys[0].slice(1, -1));
|
|
129
|
+
res[idx] = keys.length === 1 ? parseValue(value) : keys2obj(keys.slice(1), value);
|
|
130
|
+
}
|
|
131
|
+
else {
|
|
132
|
+
res[keys[0]] = keys.length === 1 ? parseValue(value) : keys2obj(keys.slice(1), value);
|
|
133
|
+
}
|
|
134
|
+
return res;
|
|
135
|
+
}
|
|
136
|
+
function isNull(obj) {
|
|
137
|
+
return obj == null || obj == undefined;
|
|
138
|
+
}
|
|
139
|
+
function getType(obj) {
|
|
140
|
+
if (isNull(obj)) {
|
|
141
|
+
return "null";
|
|
142
|
+
}
|
|
143
|
+
if (Array.isArray(obj)) {
|
|
144
|
+
return "array";
|
|
145
|
+
}
|
|
146
|
+
return typeof obj;
|
|
147
|
+
}
|
|
148
|
+
var mergeObj = function (obj1, obj2) {
|
|
149
|
+
//merge two obj to obj1
|
|
150
|
+
if (getType(obj1) !== getType(obj2)) {
|
|
151
|
+
console.error("obj1 and obj2 type is different");
|
|
152
|
+
return obj1;
|
|
153
|
+
}
|
|
154
|
+
if (getType(obj1) === "array") {
|
|
155
|
+
obj1 = obj1;
|
|
156
|
+
obj2 = obj2;
|
|
157
|
+
for (var i = 0; i < Math.max(obj1.length, obj2.length); i++) {
|
|
158
|
+
if ((isNull(obj1[i]) && !isNull(obj2[i])) || (isNull(obj1[i]) && isNull(obj2[i]))) {
|
|
159
|
+
obj1[i] = obj2[i];
|
|
160
|
+
}
|
|
161
|
+
else if (!isNull(obj1[i]) && isNull(obj2[i])) {
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
mergeObj(obj1[i], obj2[i]);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
else if (getType(obj1) === "object") {
|
|
169
|
+
obj1 = obj1;
|
|
170
|
+
obj2 = obj2;
|
|
171
|
+
for (var key in obj1) {
|
|
172
|
+
if (obj2.hasOwnProperty(key)) {
|
|
173
|
+
mergeObj(obj1[key], obj2[key]);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
for (var key in obj2) {
|
|
177
|
+
if (!obj1.hasOwnProperty(key)) {
|
|
178
|
+
obj1[key] = obj2[key];
|
|
179
|
+
}
|
|
180
|
+
}
|
|
181
|
+
}
|
|
182
|
+
return obj1;
|
|
183
|
+
};
|
|
184
|
+
function unflattenObject(arr) {
|
|
185
|
+
if (arr.length === 0)
|
|
186
|
+
return {};
|
|
187
|
+
if (arr.length % 2 !== 0) {
|
|
188
|
+
console.error("arr length is odd");
|
|
189
|
+
return {};
|
|
190
|
+
}
|
|
191
|
+
if (arr[0] == "") {
|
|
192
|
+
//无嵌套
|
|
193
|
+
return parseValue(arr[1]);
|
|
194
|
+
}
|
|
195
|
+
//有嵌套
|
|
196
|
+
var ans = arr[0].substring(0, 1) === "[" ? [] : {};
|
|
197
|
+
for (var i = 0; i < arr.length; i += 2) {
|
|
198
|
+
var keys = arr[i].split(">");
|
|
199
|
+
var value = arr[i + 1];
|
|
200
|
+
var tem = keys2obj(keys, value);
|
|
201
|
+
// console.log('tem', tem);
|
|
202
|
+
mergeObj(ans, tem);
|
|
203
|
+
//合并两个obj
|
|
204
|
+
}
|
|
205
|
+
return ans;
|
|
206
|
+
}
|
|
207
|
+
// let t2 = [];
|
|
208
|
+
// let t2: HMSETData = [
|
|
209
|
+
// [['a', 'b', [[[]]]]],
|
|
210
|
+
// [{}, { a: [] }],
|
|
211
|
+
// { a: 'b', b: 123, c: [1, 2, 3, true, false, null, { a: 'b' }], d: null, e: { f: { g: { s: [] } } } },
|
|
212
|
+
// ];
|
|
213
|
+
// let t3: HMSETData = JSON.parse(JSON.stringify(t2));
|
|
214
|
+
// t3![2].c[6].a = '123';
|
|
215
|
+
// console.log(JSON.stringify(HMSETHelper.DiffObjToHM(t2, t3)));
|
|
216
|
+
// let t2 = 'asd';
|
|
217
|
+
// let t2 = [];
|
|
218
|
+
// let t2 = [[[[[2]]]]];
|
|
219
|
+
// let t2 = [[[[[]]]]];
|
|
220
|
+
// let t2 = [false, null];
|
|
221
|
+
// console.log(JSON.stringify(HMSETHelper.HMToObg(HMSETHelper.ObgToHM(t2))) == JSON.stringify(t2));
|
|
222
|
+
// let t3 = JSON.parse(JSON.stringify(t));
|
|
223
|
+
// t3.b.d[2].f[2].g = '10';
|
|
224
|
+
// t3.b.d[2].f.push({ h: '11' });
|
|
225
|
+
// console.log(JSON.stringify(HMSETHelper.diff(t, t3)));
|
|
226
|
+
var runTest = function () {
|
|
227
|
+
var ok = true;
|
|
228
|
+
try {
|
|
229
|
+
for (var i = 0; i < 500; i++) {
|
|
230
|
+
if (!ok) {
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
var deep = Math.floor(Math.random() * 6);
|
|
234
|
+
var obj = generateRandomObject(deep);
|
|
235
|
+
var flat_1 = HMSETHelper.ObjToHM(obj);
|
|
236
|
+
var obj2 = HMSETHelper.HMToObj(flat_1);
|
|
237
|
+
if (JSON.stringify(obj) !== JSON.stringify(obj2)) {
|
|
238
|
+
console.error("error!! ", JSON.stringify(obj, null, 4), "----------------", JSON.stringify(obj2, null, 4));
|
|
239
|
+
ok = false;
|
|
240
|
+
break;
|
|
241
|
+
}
|
|
242
|
+
else {
|
|
243
|
+
console.log("pass " + i);
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
//循环引用测试
|
|
247
|
+
var objA = {};
|
|
248
|
+
var objB = { parent: objA };
|
|
249
|
+
objA.child = objB;
|
|
250
|
+
var flat = HMSETHelper.ObjToHM(objA);
|
|
251
|
+
var objC = HMSETHelper.HMToObj(flat);
|
|
252
|
+
console.log(JSON.stringify(flat, null, 4));
|
|
253
|
+
}
|
|
254
|
+
catch (e) {
|
|
255
|
+
console.error(e);
|
|
256
|
+
}
|
|
257
|
+
};
|
|
258
|
+
// runTest();
|
|
259
|
+
function generateRandomObject(depth) {
|
|
260
|
+
function getRDString() {
|
|
261
|
+
var characters = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%^&*()_+";
|
|
262
|
+
var l = Math.max(1, Math.floor(Math.random() * 10));
|
|
263
|
+
return Array.from({ length: l }, function () { return characters[Math.floor(Math.random() * characters.length)]; }).join("");
|
|
264
|
+
}
|
|
265
|
+
if (depth === 0) {
|
|
266
|
+
var resType = ["string", "number", "boolean", "null"][Math.floor(Math.random() * 4)];
|
|
267
|
+
var val = void 0;
|
|
268
|
+
if (resType === "string") {
|
|
269
|
+
val = getRDString();
|
|
270
|
+
}
|
|
271
|
+
else if (resType === "number") {
|
|
272
|
+
val = Math.floor(Math.random() * 100);
|
|
273
|
+
}
|
|
274
|
+
else if (resType === "boolean") {
|
|
275
|
+
val = Math.random() < 0.5;
|
|
276
|
+
}
|
|
277
|
+
else {
|
|
278
|
+
val = null;
|
|
279
|
+
}
|
|
280
|
+
return val;
|
|
281
|
+
}
|
|
282
|
+
var nestType = ["array", "object"][Math.floor(Math.random() * 2)];
|
|
283
|
+
var nestNum = Math.floor(Math.random() * 5);
|
|
284
|
+
var res = nestType === "array" ? [] : {};
|
|
285
|
+
if (nestType === "array") {
|
|
286
|
+
for (var i = 0; i < nestNum; i++) {
|
|
287
|
+
res[i] = generateRandomObject(depth - 1);
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
else {
|
|
291
|
+
for (var i = 0; i < nestNum; i++) {
|
|
292
|
+
res[getRDString()] = generateRandomObject(depth - 1);
|
|
293
|
+
}
|
|
294
|
+
}
|
|
295
|
+
return res;
|
|
296
|
+
}
|
|
297
|
+
module.exports = { HMSETHelper: HMSETHelper };
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
export class frameAnimationJs {
|
|
2
|
+
constructor({ rootId, rootClassName, totalTime, width, height, srcArr, onShow, onFinish, preloadCount, loop, showLastFrame, }: {
|
|
3
|
+
rootId?: string | undefined;
|
|
4
|
+
rootClassName?: string | undefined;
|
|
5
|
+
totalTime: any;
|
|
6
|
+
width: any;
|
|
7
|
+
height: any;
|
|
8
|
+
srcArr: any;
|
|
9
|
+
onShow: any;
|
|
10
|
+
onFinish: any;
|
|
11
|
+
preloadCount?: number | undefined;
|
|
12
|
+
loop?: boolean | undefined;
|
|
13
|
+
showLastFrame?: boolean | undefined;
|
|
14
|
+
});
|
|
15
|
+
rootId: string;
|
|
16
|
+
rootClassName: string;
|
|
17
|
+
totalTime: any;
|
|
18
|
+
width: any;
|
|
19
|
+
height: any;
|
|
20
|
+
srcArr: any;
|
|
21
|
+
onShow: any;
|
|
22
|
+
onFinish: any;
|
|
23
|
+
preloadCount: number;
|
|
24
|
+
loop: boolean;
|
|
25
|
+
showLastFrame: boolean;
|
|
26
|
+
cRefArr: HTMLImageElement[];
|
|
27
|
+
imgEleArr: HTMLImageElement[];
|
|
28
|
+
nextFrame(startTime: any, oneFrameTime: any, showedFrame: any): void;
|
|
29
|
+
rootEle: HTMLDivElement;
|
|
30
|
+
init(): void;
|
|
31
|
+
getRootEle(): HTMLDivElement;
|
|
32
|
+
}
|
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* FrameAnimation的js版本
|
|
4
|
+
* @param rootId 根节点id
|
|
5
|
+
* @param rootClassName 根节点className
|
|
6
|
+
* @param totalTime 动画总时长
|
|
7
|
+
* @param width 组件宽度
|
|
8
|
+
* @param height 组件高度
|
|
9
|
+
* @param srcArr 图片url数组
|
|
10
|
+
* @param onShow 每一帧显示时的回调
|
|
11
|
+
* @param onFinish 动画结束时的回调
|
|
12
|
+
* @param preloadCount 预加载节点的数量
|
|
13
|
+
* @param loop 是否循环播放
|
|
14
|
+
* @param showLastFrame 播放结束后是否显示最后一帧
|
|
15
|
+
*/
|
|
16
|
+
module.exports.frameAnimationJs = /** @class */ (function () {
|
|
17
|
+
function frameAnimationJs(_a) {
|
|
18
|
+
var _b = _a.rootId, rootId = _b === void 0 ? '' : _b, _c = _a.rootClassName, rootClassName = _c === void 0 ? '' : _c, totalTime = _a.totalTime, width = _a.width, height = _a.height, srcArr = _a.srcArr, onShow = _a.onShow, onFinish = _a.onFinish, _d = _a.preloadCount, preloadCount = _d === void 0 ? 5 : _d, _e = _a.loop, loop = _e === void 0 ? false : _e, _f = _a.showLastFrame, showLastFrame = _f === void 0 ? true : _f;
|
|
19
|
+
var _this = this;
|
|
20
|
+
this.rootId = rootId;
|
|
21
|
+
this.rootClassName = rootClassName;
|
|
22
|
+
this.totalTime = totalTime;
|
|
23
|
+
this.width = width;
|
|
24
|
+
this.height = height;
|
|
25
|
+
this.srcArr = srcArr;
|
|
26
|
+
this.onShow = onShow;
|
|
27
|
+
this.onFinish = onFinish;
|
|
28
|
+
this.preloadCount = preloadCount;
|
|
29
|
+
this.loop = loop;
|
|
30
|
+
this.showLastFrame = showLastFrame;
|
|
31
|
+
this.cRefArr = Array.from({ length: preloadCount }, function () { return document.createElement('img'); });
|
|
32
|
+
this.imgEleArr = Array.from({ length: preloadCount }, function (v, i) {
|
|
33
|
+
_this.cRefArr[i].src = _this.srcArr[i];
|
|
34
|
+
_this.cRefArr[i].style.position = 'absolute';
|
|
35
|
+
_this.cRefArr[i].style.visibility = 'hidden';
|
|
36
|
+
_this.cRefArr[i].style.width = _this.width;
|
|
37
|
+
_this.cRefArr[i].style.height = _this.height;
|
|
38
|
+
return _this.cRefArr[i];
|
|
39
|
+
});
|
|
40
|
+
this.nextFrame = this.nextFrame.bind(this);
|
|
41
|
+
this.rootEle = document.createElement('div');
|
|
42
|
+
console.log('ccc');
|
|
43
|
+
this.init();
|
|
44
|
+
}
|
|
45
|
+
frameAnimationJs.prototype.init = function () {
|
|
46
|
+
var _this = this;
|
|
47
|
+
this.rootEle.style.width = this.width;
|
|
48
|
+
this.rootEle.style.height = this.height;
|
|
49
|
+
this.rootEle.id = this.rootId;
|
|
50
|
+
this.rootEle.className = this.rootClassName;
|
|
51
|
+
this.imgEleArr.forEach(function (ele) {
|
|
52
|
+
_this.rootEle.appendChild(ele);
|
|
53
|
+
});
|
|
54
|
+
var startTime = Date.now();
|
|
55
|
+
var oneFrameTime = this.totalTime / this.srcArr.length;
|
|
56
|
+
//设置第一帧
|
|
57
|
+
this.cRefArr[0].style.visibility = 'visible';
|
|
58
|
+
this.nextFrame(startTime, oneFrameTime, 0);
|
|
59
|
+
};
|
|
60
|
+
//循环修改imgEleArr的src和visibility实现帧动画,,记录上一帧的序号防止跳帧
|
|
61
|
+
frameAnimationJs.prototype.nextFrame = function (startTime, oneFrameTime, showedFrame) {
|
|
62
|
+
var _this = this;
|
|
63
|
+
var nowTime = Date.now();
|
|
64
|
+
//该显示第几帧
|
|
65
|
+
var nowFrame = Math.floor((nowTime - startTime) / oneFrameTime);
|
|
66
|
+
//使用第几个img元素
|
|
67
|
+
var nowArrIndex = nowFrame % this.preloadCount;
|
|
68
|
+
var showedArrIndex = showedFrame % this.preloadCount;
|
|
69
|
+
// console.log('nextFrame', nowFrame, 'nowArrIndex', nowArrIndex, 'nowFrame', nowFrame);
|
|
70
|
+
if (!this.cRefArr[0]) {
|
|
71
|
+
//组件已经被卸载
|
|
72
|
+
this.onFinish && this.onFinish();
|
|
73
|
+
return;
|
|
74
|
+
}
|
|
75
|
+
if (nowFrame >= this.srcArr.length) {
|
|
76
|
+
this.onFinish && this.onFinish();
|
|
77
|
+
if (!this.loop) {
|
|
78
|
+
this.cRefArr.forEach(function (ref, i) {
|
|
79
|
+
ref.style.visibility = 'hidden';
|
|
80
|
+
});
|
|
81
|
+
if (this.showLastFrame) {
|
|
82
|
+
//跳帧的话,最后一帧可能不是最后一张图片,设为最后一张图片
|
|
83
|
+
this.cRefArr[0].src = this.srcArr[this.srcArr.length - 1];
|
|
84
|
+
this.cRefArr[0].style.visibility = 'visible';
|
|
85
|
+
}
|
|
86
|
+
return;
|
|
87
|
+
}
|
|
88
|
+
//重置img标签的 src 和 visibility
|
|
89
|
+
this.cRefArr.forEach(function (ref, i) {
|
|
90
|
+
ref.style.visibility = 'hidden';
|
|
91
|
+
ref.src = _this.srcArr[i];
|
|
92
|
+
});
|
|
93
|
+
startTime = Date.now();
|
|
94
|
+
this.cRefArr[0].style.visibility = 'visible';
|
|
95
|
+
requestAnimationFrame(function () {
|
|
96
|
+
_this.nextFrame(startTime, oneFrameTime, 0);
|
|
97
|
+
});
|
|
98
|
+
return;
|
|
99
|
+
}
|
|
100
|
+
if (nowFrame !== showedFrame) {
|
|
101
|
+
this.cRefArr[nowArrIndex].style.visibility = 'visible';
|
|
102
|
+
//隐藏上一帧
|
|
103
|
+
this.cRefArr[showedArrIndex].style.visibility = 'hidden';
|
|
104
|
+
//可能跳帧,在所有其他的元素上预加载
|
|
105
|
+
this.cRefArr.forEach(function (ref, i) {
|
|
106
|
+
if (i !== nowArrIndex) {
|
|
107
|
+
var offset = (i + _this.cRefArr.length - nowArrIndex) % _this.cRefArr.length;
|
|
108
|
+
var offsetFrame = (nowFrame + offset) % _this.srcArr.length;
|
|
109
|
+
if (ref.src !== _this.srcArr[offsetFrame]) {
|
|
110
|
+
ref.src = _this.srcArr[offsetFrame];
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
showedFrame = nowFrame;
|
|
115
|
+
this.onShow && this.onShow(nowFrame);
|
|
116
|
+
}
|
|
117
|
+
requestAnimationFrame(function () {
|
|
118
|
+
_this.nextFrame(startTime, oneFrameTime, showedFrame);
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
frameAnimationJs.prototype.getRootEle = function () {
|
|
122
|
+
return this.rootEle;
|
|
123
|
+
};
|
|
124
|
+
return frameAnimationJs;
|
|
125
|
+
}());
|