pixuireactcomponents 1.5.10 → 1.5.12

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pixuireactcomponents",
3
- "version": "1.5.10",
3
+ "version": "1.5.12",
4
4
  "description": "pixui react components",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -68,29 +68,25 @@ export function Carousel(props) {
68
68
  : 0.3 : _p;
69
69
  // console.log('children', children.length);
70
70
  var safeNext = function () {
71
- // console.log('safeNext1', canClickSwitch.current, isMoving.current);
72
- if (!canClickSwitch.current || isMoving.current)
71
+ if (!canClickSwitch.current)
73
72
  return;
74
- // console.log('safeNext2');
75
73
  canClickSwitch.current = false;
76
74
  if (safeNextHandle.current)
77
75
  clearTimeout(safeNextHandle.current);
78
76
  safeNextHandle.current = setTimeout(function () {
79
77
  canClickSwitch.current = true;
80
- }, 1000);
78
+ }, 1100);
81
79
  handleNext();
82
80
  };
83
81
  var safePrev = function () {
84
- // console.log('safePrev1', canClickSwitch.current, isMoving.current);
85
- if (!canClickSwitch.current || isMoving.current)
82
+ if (!canClickSwitch.current)
86
83
  return;
87
- // console.log('safePrev2');
88
84
  canClickSwitch.current = false;
89
85
  if (safePrevHandle.current)
90
86
  clearTimeout(safePrevHandle.current);
91
87
  safePrevHandle.current = setTimeout(function () {
92
88
  canClickSwitch.current = true;
93
- }, 1000);
89
+ }, 1100);
94
90
  handlePrev();
95
91
  };
96
92
  useImperativeHandle(props.cRef, function () { return ({
@@ -98,7 +94,7 @@ export function Carousel(props) {
98
94
  handlePrev: safePrev,
99
95
  setShowIndex: setShowIndex,
100
96
  }); });
101
- var duration = switchDuration < 1000 ? 1000 : switchDuration;
97
+ var duration = switchDuration < 1100 ? 1100 : switchDuration;
102
98
  var _q = useState(defaultIndex), showIndex = _q[0], setShowIndex = _q[1];
103
99
  //轮播元素正在滚动
104
100
  var isMoving = useRef(false);
@@ -106,8 +102,6 @@ export function Carousel(props) {
106
102
  var isMouseDown = useRef(false);
107
103
  var mouseDownX = useRef(0);
108
104
  var mouseDownY = useRef(0);
109
- var lastAniFinishTime = useRef(0);
110
- var isSideAniPlaying = useRef(false);
111
105
  //pixui在 onTransitionEnd 之后马上触发 transform 会丢onTransitionEnd
112
106
  var canClickSwitch = useRef(true);
113
107
  var handleNextRef = useRef(function () { });
@@ -173,7 +167,7 @@ export function Carousel(props) {
173
167
  };
174
168
  }, []);
175
169
  var handlePrev = function () {
176
- if (isMoving.current || isMouseDown.current)
170
+ if (isMoving.current)
177
171
  return;
178
172
  isMoving.current = true;
179
173
  if (showIndex === 0) {
@@ -186,40 +180,20 @@ export function Carousel(props) {
186
180
  var handleNext = function () {
187
181
  if (!loop && showIndex == carouselItems.length - 2)
188
182
  return;
189
- if (isMoving.current || isMouseDown.current)
183
+ if (isMoving.current)
190
184
  return;
191
185
  isMoving.current = true;
192
186
  setShowIndex(showIndex + 1);
193
187
  };
194
188
  var handleTransitionEnd = function () {
195
- var now = Date.now();
196
- // console.log(
197
- // 'handleTransitionEnd ? isSideAniPlaying',
198
- // isSideAniPlaying.current,
199
- // 'time',
200
- // now - lastAniFinishTime.current,
201
- // 'showTransition',
202
- // showTransition
203
- // );
204
- if (!isSideAniPlaying.current && Math.abs(now - lastAniFinishTime.current) < 800) {
205
- //pixui bug 重复触发handleTransitionEnd
206
- return;
207
- }
208
- lastAniFinishTime.current = now;
209
189
  //判断前后补位元素准备跳转
210
190
  //在动画结束以后关掉,否则用鼠标拖动的时候不能补充剩下的offset
211
191
  if (isLastCarouseItem() || isFirstCarouseLastItem()) {
212
192
  setShowTransition(false);
213
- isSideAniPlaying.current = true;
214
- // console.log('handleTransitionEnd 1');
215
- }
216
- else {
217
- // console.log('handleTransitionEnd 3');
218
- isMoving.current = false;
219
- isSideAniPlaying.current = false;
220
193
  }
194
+ isMoving.current = false;
221
195
  //触发onSlideChange
222
- if (!isLastCarouseItem() && !isFirstCarouseLastItem() && onSlideChange && !isSideAniPlaying.current)
196
+ if (!isLastCarouseItem() && !isFirstCarouseLastItem() && onSlideChange && showTransition)
223
197
  onSlideChange(showIndex - 1);
224
198
  // 处理首尾特殊case
225
199
  if (isLastCarouseItem()) {
@@ -318,7 +292,7 @@ export function Carousel(props) {
318
292
  display: 'flex',
319
293
  width: isVertical ? compWidth : compWidth * carouselItems.length + 'px',
320
294
  height: isVertical ? compHeight * carouselItems.length : compHeight + 'px',
321
- transition: "transform ".concat(showTransition ? '0.5s' : '0s', " ease 0s"),
295
+ transition: "transform ".concat(showTransition ? '0.4s' : '0s', " ease 0s"),
322
296
  transform: isVertical
323
297
  ? "translate(0px, ".concat(offset + gestureoffset[1], "px)")
324
298
  : "translate(".concat(offset + gestureoffset[0], "px, 0px)"),
@@ -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
+ 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 default DragList;
@@ -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 default 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
- setSBlockY(
140
- // 滑块顶部距离sframe上边的差值 = 鼠标Y位置 - 鼠标Y距离元素顶部位置 - 滚动框绝对top
141
- event.clientY - clientYtoBlockTop - sFrame.getBoundingClientRect().top);
142
- }
143
- {
144
- //移动 content
145
- var sblock_height = sBlock.getBoundingClientRect().height;
146
- var sframe_height = sFrame.getBoundingClientRect().height;
147
- var swindow_height = sWindow.getBoundingClientRect().height;
148
- var content_height = sWindow.scrollHeight;
149
- var slideMarginTop = sBlock.getBoundingClientRect().top - sFrame.getBoundingClientRect().top;
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
+ }());