@meta2d/core 1.0.15 → 1.0.17

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/src/core.js CHANGED
@@ -170,69 +170,100 @@ var Meta2d = /** @class */ (function () {
170
170
  }
171
171
  };
172
172
  this.doEvent = function (pen, eventName) {
173
- var _a;
173
+ var _a, _b;
174
174
  if (!pen) {
175
175
  return;
176
176
  }
177
177
  (_a = pen.events) === null || _a === void 0 ? void 0 : _a.forEach(function (event) {
178
178
  var _a;
179
- if (_this.events[event.action] && event.name === eventName) {
180
- var can = !((_a = event.where) === null || _a === void 0 ? void 0 : _a.type);
181
- if (event.where) {
182
- var _b = event.where, fn = _b.fn, fnJs = _b.fnJs, comparison = _b.comparison, key = _b.key, value = _b.value;
183
- if (fn) {
184
- can = fn(pen);
179
+ if (event.actions && event.actions.length) {
180
+ event.actions.forEach(function (action) {
181
+ if (_this.events[action.action] && event.name === eventName) {
182
+ _this.events[action.action](pen, action);
185
183
  }
186
- else if (fnJs) {
187
- try {
188
- event.where.fn = new Function('pen', fnJs);
189
- }
190
- catch (err) {
191
- console.error('Error: make function:', err);
184
+ });
185
+ }
186
+ else {
187
+ if (_this.events[event.action] && event.name === eventName) {
188
+ var can = !((_a = event.where) === null || _a === void 0 ? void 0 : _a.type);
189
+ if (event.where) {
190
+ var _b = event.where, fn = _b.fn, fnJs = _b.fnJs, comparison = _b.comparison, key = _b.key, value = _b.value;
191
+ if (fn) {
192
+ can = fn(pen, { meta2d: _this });
192
193
  }
193
- if (event.where.fn) {
194
- can = event.where.fn(pen);
194
+ else if (fnJs) {
195
+ try {
196
+ event.where.fn = new Function('pen', 'context', fnJs);
197
+ }
198
+ catch (err) {
199
+ console.error('Error: make function:', err);
200
+ }
201
+ if (event.where.fn) {
202
+ can = event.where.fn(pen, { meta2d: _this });
203
+ }
195
204
  }
196
- }
197
- else {
198
- switch (comparison) {
199
- case '>':
200
- can = pen[key] > +value;
201
- break;
202
- case '>=':
203
- can = pen[key] >= +value;
204
- break;
205
- case '<':
206
- can = pen[key] < +value;
207
- break;
208
- case '<=':
209
- can = pen[key] <= +value;
210
- break;
211
- case '=':
212
- case '==':
213
- can = pen[key] == value;
214
- break;
215
- case '!=':
216
- can = pen[key] != value;
217
- break;
218
- case '[)':
219
- can = valueInRange(+pen[key], value);
220
- break;
221
- case '![)':
222
- can = !valueInRange(+pen[key], value);
223
- break;
224
- case '[]':
225
- can = valueInArray(+pen[key], value);
226
- break;
227
- case '![]':
228
- can = !valueInArray(+pen[key], value);
229
- break;
205
+ else {
206
+ switch (comparison) {
207
+ case '>':
208
+ can = pen[key] > +value;
209
+ break;
210
+ case '>=':
211
+ can = pen[key] >= +value;
212
+ break;
213
+ case '<':
214
+ can = pen[key] < +value;
215
+ break;
216
+ case '<=':
217
+ can = pen[key] <= +value;
218
+ break;
219
+ case '=':
220
+ case '==':
221
+ can = pen[key] == value;
222
+ break;
223
+ case '!=':
224
+ can = pen[key] != value;
225
+ break;
226
+ case '[)':
227
+ can = valueInRange(+pen[key], value);
228
+ break;
229
+ case '![)':
230
+ can = !valueInRange(+pen[key], value);
231
+ break;
232
+ case '[]':
233
+ can = valueInArray(+pen[key], value);
234
+ break;
235
+ case '![]':
236
+ can = !valueInArray(+pen[key], value);
237
+ break;
238
+ }
230
239
  }
231
240
  }
241
+ can && _this.events[event.action](pen, event);
232
242
  }
233
- can && _this.events[event.action](pen, event);
234
243
  }
235
244
  });
245
+ (_b = pen.realTimes) === null || _b === void 0 ? void 0 : _b.forEach(function (realTime) {
246
+ var _a;
247
+ (_a = realTime.triggers) === null || _a === void 0 ? void 0 : _a.forEach(function (trigger) {
248
+ var _a;
249
+ var flag = false;
250
+ if (trigger.conditionType === 'and') {
251
+ flag = trigger.conditions.every(function (condition) {
252
+ return _this.judgeCondition(pen, realTime.key, condition);
253
+ });
254
+ }
255
+ else if (trigger.conditionType === 'or') {
256
+ flag = trigger.conditions.some(function (condition) {
257
+ return _this.judgeCondition(pen, realTime.key, condition);
258
+ });
259
+ }
260
+ if (flag) {
261
+ (_a = trigger.actions) === null || _a === void 0 ? void 0 : _a.forEach(function (event) {
262
+ _this.events[event.action](pen, event);
263
+ });
264
+ }
265
+ });
266
+ });
236
267
  // 事件冒泡,子执行完,父执行
237
268
  _this.doEvent(_this.store.pens[pen.parentId], eventName);
238
269
  };
@@ -376,6 +407,10 @@ var Meta2d = /** @class */ (function () {
376
407
  console.warn('[meta2d] SetProps value is not an object');
377
408
  };
378
409
  this.events[EventAction.StartAnimate] = function (pen, e) {
410
+ if (e.targetType && e.params) {
411
+ _this.startAnimate(e.value || [pen], e.params);
412
+ return;
413
+ }
379
414
  if (!e.value || typeof e.value === 'string') {
380
415
  _this.startAnimate(e.value || [pen]);
381
416
  return;
@@ -425,13 +460,13 @@ var Meta2d = /** @class */ (function () {
425
460
  throw new Error('[meta2d] Function value must be string');
426
461
  }
427
462
  var fnJs = e.value;
428
- e.fn = new Function('pen', 'params', fnJs);
463
+ e.fn = new Function('pen', 'params', 'context', fnJs);
429
464
  }
430
465
  catch (err) {
431
466
  console.error('[meta2d]: Error on make a function:', err);
432
467
  }
433
468
  }
434
- (_a = e.fn) === null || _a === void 0 ? void 0 : _a.call(e, pen, e.params);
469
+ (_a = e.fn) === null || _a === void 0 ? void 0 : _a.call(e, pen, e.params, { meta2d: _this, eventName: e.name });
435
470
  };
436
471
  this.events[EventAction.GlobalFn] = function (pen, e) {
437
472
  if (typeof e.value !== 'string') {
@@ -450,6 +485,7 @@ var Meta2d = /** @class */ (function () {
450
485
  _this.store.emitter.emit(e.value, {
451
486
  pen: pen,
452
487
  params: e.params,
488
+ eventName: e.name,
453
489
  });
454
490
  };
455
491
  this.events[EventAction.SendPropData] = function (pen, e) {
@@ -496,6 +532,51 @@ var Meta2d = /** @class */ (function () {
496
532
  }
497
533
  console.warn('[meta2d] SendVarData value is not an object');
498
534
  };
535
+ this.events[EventAction.Navigator] = function (pen, e) {
536
+ if (e.value && typeof e.value === 'string') {
537
+ _this.navigatorTo(e.value);
538
+ }
539
+ };
540
+ this.events[EventAction.Dialog] = function (pen, e) {
541
+ if (e.params &&
542
+ typeof e.params === 'string' &&
543
+ e.value &&
544
+ typeof e.value === 'string') {
545
+ _this.canvas.dialog.show(e.value, e.params);
546
+ }
547
+ };
548
+ this.events[EventAction.SendData] = function (pen, e) {
549
+ var value = deepClone(e.value);
550
+ if (value && typeof value === 'object') {
551
+ if (e.targetType === 'action') {
552
+ var _pen = e.params ? _this.findOne(e.params) : pen;
553
+ for (var key in value) {
554
+ if (!value[key]) {
555
+ value[key] = _pen[key];
556
+ }
557
+ }
558
+ value.id = _pen.id;
559
+ _this.sendDataToNetWork(value, e.network);
560
+ return;
561
+ }
562
+ }
563
+ };
564
+ };
565
+ Meta2d.prototype.navigatorTo = function (id) {
566
+ if (!id) {
567
+ return;
568
+ }
569
+ var href = window.location.href;
570
+ var arr = href.split('id=');
571
+ if (arr.length > 1) {
572
+ var idx = arr[1].indexOf('&');
573
+ if (idx === -1) {
574
+ window.location.href = arr[0] + 'id=' + id;
575
+ }
576
+ else {
577
+ window.location.href = arr[0] + 'id=' + id + arr[1].slice(idx + 1);
578
+ }
579
+ }
499
580
  };
500
581
  Meta2d.prototype.doSendDataEvent = function (value, topics) {
501
582
  var _this = this;
@@ -521,6 +602,72 @@ var Meta2d = /** @class */ (function () {
521
602
  }
522
603
  this.store.emitter.emit('sendData', data);
523
604
  };
605
+ Meta2d.prototype.sendDataToNetWork = function (value, network) {
606
+ return __awaiter(this, void 0, void 0, function () {
607
+ var res, clients_1, mqttClient_1, websockets, websocket_1;
608
+ return __generator(this, function (_a) {
609
+ switch (_a.label) {
610
+ case 0:
611
+ if (!network.url) {
612
+ return [2 /*return*/];
613
+ }
614
+ if (!(network.type === 'http')) return [3 /*break*/, 2];
615
+ return [4 /*yield*/, fetch(network.url, {
616
+ headers: network.headers,
617
+ method: network.method,
618
+ body: value,
619
+ })];
620
+ case 1:
621
+ res = _a.sent();
622
+ if (res.ok) {
623
+ console.info('http消息发送成功');
624
+ }
625
+ return [3 /*break*/, 3];
626
+ case 2:
627
+ if (network.type === 'mqtt') {
628
+ clients_1 = this.mqttClients.filter(function (client) { return client.options.href === network.url; });
629
+ if (clients_1 && clients_1.length) {
630
+ if (clients_1[0].connected) {
631
+ network.topics.split(',').forEach(function (topic) {
632
+ clients_1[0].publish(topic, value);
633
+ });
634
+ }
635
+ }
636
+ else {
637
+ mqttClient_1 = mqtt.connect(network.url, network.options);
638
+ mqttClient_1.on('connect', function () {
639
+ console.info('mqtt连接成功');
640
+ network.topics.split(',').forEach(function (topic) {
641
+ mqttClient_1.publish(topic, value);
642
+ mqttClient_1 === null || mqttClient_1 === void 0 ? void 0 : mqttClient_1.end();
643
+ });
644
+ });
645
+ }
646
+ }
647
+ else if (network.type === 'websocket') {
648
+ websockets = this.websockets.filter(function (socket) { return socket.url === network.url; });
649
+ if (websockets && websockets.length) {
650
+ if (websockets[0].readyState === 1) {
651
+ websockets[0].send(value);
652
+ }
653
+ }
654
+ else {
655
+ websocket_1 = new WebSocket(network.url, network.protocols);
656
+ websocket_1.onopen = function () {
657
+ console.info('websocket连接成功');
658
+ websocket_1.send(value);
659
+ setTimeout(function () {
660
+ websocket_1.close();
661
+ }, 100);
662
+ };
663
+ }
664
+ }
665
+ _a.label = 3;
666
+ case 3: return [2 /*return*/];
667
+ }
668
+ });
669
+ });
670
+ };
524
671
  Meta2d.prototype.resize = function (width, height) {
525
672
  this.canvas.resize(width, height);
526
673
  this.render();
@@ -609,7 +756,7 @@ var Meta2d = /** @class */ (function () {
609
756
  this.store.patchFlagsBackground = true;
610
757
  };
611
758
  Meta2d.prototype.setGrid = function (_a) {
612
- var _b = _a === void 0 ? {} : _a, _c = _b.grid, grid = _c === void 0 ? this.store.data.grid : _c, _d = _b.gridColor, gridColor = _d === void 0 ? this.store.data.gridColor : _d, _e = _b.gridSize, gridSize = _e === void 0 ? this.store.data.gridSize : _e, _f = _b.gridRotate, gridRotate = _f === void 0 ? this.store.data.gridRotate : _f;
759
+ var _b = _a === void 0 ? {} : _a, _c = _b.grid, grid = _c === void 0 ? this.store.data.grid : _c, _e = _b.gridColor, gridColor = _e === void 0 ? this.store.data.gridColor : _e, _f = _b.gridSize, gridSize = _f === void 0 ? this.store.data.gridSize : _f, _g = _b.gridRotate, gridRotate = _g === void 0 ? this.store.data.gridRotate : _g;
613
760
  this.store.data.grid = grid;
614
761
  this.store.data.gridColor = gridColor;
615
762
  this.store.data.gridSize = gridSize;
@@ -617,13 +764,14 @@ var Meta2d = /** @class */ (function () {
617
764
  this.store.patchFlagsBackground = true;
618
765
  };
619
766
  Meta2d.prototype.setRule = function (_a) {
620
- var _b = _a === void 0 ? {} : _a, _c = _b.rule, rule = _c === void 0 ? this.store.data.rule : _c, _d = _b.ruleColor, ruleColor = _d === void 0 ? this.store.data.ruleColor : _d;
767
+ var _b = _a === void 0 ? {} : _a, _c = _b.rule, rule = _c === void 0 ? this.store.data.rule : _c, _e = _b.ruleColor, ruleColor = _e === void 0 ? this.store.data.ruleColor : _e;
621
768
  this.store.data.rule = rule;
622
769
  this.store.data.ruleColor = ruleColor;
623
770
  this.store.patchFlagsTop = true;
624
771
  };
625
- Meta2d.prototype.open = function (data) {
772
+ Meta2d.prototype.open = function (data, render) {
626
773
  var e_1, _a, e_2, _b;
774
+ if (render === void 0) { render = true; }
627
775
  this.clear(false);
628
776
  if (data) {
629
777
  this.setBackgroundImage(data.bkImage);
@@ -631,8 +779,8 @@ var Meta2d = /** @class */ (function () {
631
779
  this.store.data.pens = [];
632
780
  try {
633
781
  // 第一遍赋初值
634
- for (var _c = __values(data.pens), _d = _c.next(); !_d.done; _d = _c.next()) {
635
- var pen = _d.value;
782
+ for (var _c = __values(data.pens), _e = _c.next(); !_e.done; _e = _c.next()) {
783
+ var pen = _e.value;
636
784
  if (!pen.id) {
637
785
  pen.id = s8();
638
786
  }
@@ -643,24 +791,27 @@ var Meta2d = /** @class */ (function () {
643
791
  catch (e_1_1) { e_1 = { error: e_1_1 }; }
644
792
  finally {
645
793
  try {
646
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
794
+ if (_e && !_e.done && (_a = _c.return)) _a.call(_c);
647
795
  }
648
796
  finally { if (e_1) throw e_1.error; }
649
797
  }
650
798
  try {
651
- for (var _e = __values(data.pens), _f = _e.next(); !_f.done; _f = _e.next()) {
652
- var pen = _f.value;
799
+ for (var _f = __values(data.pens), _g = _f.next(); !_g.done; _g = _f.next()) {
800
+ var pen = _g.value;
653
801
  this.canvas.makePen(pen);
654
802
  }
655
803
  }
656
804
  catch (e_2_1) { e_2 = { error: e_2_1 }; }
657
805
  finally {
658
806
  try {
659
- if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
807
+ if (_g && !_g.done && (_b = _f.return)) _b.call(_f);
660
808
  }
661
809
  finally { if (e_2) throw e_2.error; }
662
810
  }
663
811
  }
812
+ if (!render) {
813
+ this.canvas.opening = true;
814
+ }
664
815
  this.initBindDatas();
665
816
  this.render();
666
817
  this.listenSocket();
@@ -712,8 +863,8 @@ var Meta2d = /** @class */ (function () {
712
863
  var initJs = this.store.data.initJs;
713
864
  if (initJs && initJs.trim()) {
714
865
  try {
715
- var fn = new Function(initJs);
716
- fn();
866
+ var fn = new Function('context', initJs);
867
+ fn({ meta2d: this });
717
868
  }
718
869
  catch (e) {
719
870
  console.warn('initJs error', e);
@@ -835,15 +986,15 @@ var Meta2d = /** @class */ (function () {
835
986
  var _b;
836
987
  if (render === void 0) { render = true; }
837
988
  try {
838
- for (var _c = __values(this.store.data.pens), _d = _c.next(); !_d.done; _d = _c.next()) {
839
- var pen = _d.value;
989
+ for (var _c = __values(this.store.data.pens), _e = _c.next(); !_e.done; _e = _c.next()) {
990
+ var pen = _e.value;
840
991
  (_b = pen.onDestroy) === null || _b === void 0 ? void 0 : _b.call(pen, pen);
841
992
  }
842
993
  }
843
994
  catch (e_3_1) { e_3 = { error: e_3_1 }; }
844
995
  finally {
845
996
  try {
846
- if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
997
+ if (_e && !_e.done && (_a = _c.return)) _a.call(_c);
847
998
  }
848
999
  finally { if (e_3) throw e_3.error; }
849
1000
  }
@@ -903,7 +1054,7 @@ var Meta2d = /** @class */ (function () {
903
1054
  if (render === void 0) { render = true; }
904
1055
  this.canvas.setPenRect(pen, rect, render);
905
1056
  };
906
- Meta2d.prototype.startAnimate = function (idOrTagOrPens, index) {
1057
+ Meta2d.prototype.startAnimate = function (idOrTagOrPens, params) {
907
1058
  var _this = this;
908
1059
  this.stopAnimate(idOrTagOrPens);
909
1060
  var pens;
@@ -926,9 +1077,22 @@ var Meta2d = /** @class */ (function () {
926
1077
  pen.calculative.frameEnd += d;
927
1078
  }
928
1079
  else {
929
- if (index !== undefined &&
930
- pen.animations &&
931
- pen.animations.length > index) {
1080
+ if (params !== undefined && pen.animations) {
1081
+ var index = -1;
1082
+ if (typeof params === 'string') {
1083
+ index = pen.animations.findIndex(function (animation) { return animation.name === params; });
1084
+ if (index === -1) {
1085
+ return;
1086
+ }
1087
+ }
1088
+ else if (typeof params === 'number') {
1089
+ if (pen.animations.length > params) {
1090
+ index = params;
1091
+ }
1092
+ else {
1093
+ return;
1094
+ }
1095
+ }
932
1096
  var animate = deepClone(pen.animations[index]);
933
1097
  delete animate.name;
934
1098
  animate.currentAnimation = index;
@@ -1223,6 +1387,10 @@ var Meta2d = /** @class */ (function () {
1223
1387
  Meta2d.prototype.inactive = function () {
1224
1388
  this.canvas.inactive();
1225
1389
  };
1390
+ Meta2d.prototype.activeAll = function () {
1391
+ this.canvas.active(this.store.data.pens.filter(function (pen) { return !pen.parentId && pen.locked !== LockState.Disable; }));
1392
+ this.render();
1393
+ };
1226
1394
  /**
1227
1395
  * 删除画笔
1228
1396
  * @param pens 需要删除的画笔们
@@ -1285,7 +1453,7 @@ var Meta2d = /** @class */ (function () {
1285
1453
  var socketFn = void 0;
1286
1454
  var socketCbJs = this.store.data.socketCbJs;
1287
1455
  if (socketCbJs) {
1288
- socketFn = new Function('e', 'topic', socketCbJs);
1456
+ socketFn = new Function('e', 'context', socketCbJs);
1289
1457
  }
1290
1458
  if (!socketFn) {
1291
1459
  return false;
@@ -1304,23 +1472,13 @@ var Meta2d = /** @class */ (function () {
1304
1472
  if (websocket) {
1305
1473
  this.store.data.websocket = websocket;
1306
1474
  }
1307
- if (this.store.data.websockets) {
1308
- this.websockets = [];
1309
- this.store.data.websockets.forEach(function (websocket, index) {
1310
- _this.websockets[index] = new WebSocket(websocket.url, websocket.protocols);
1311
- _this.websockets[index].onmessage = function (e) {
1312
- _this.socketCallback(e.data);
1313
- };
1314
- _this.websockets[index].onclose = function () {
1315
- console.info('Canvas websocket closed and reconneting...');
1316
- _this.connectWebsocket();
1317
- };
1318
- });
1319
- }
1320
- else if (this.store.data.websocket) {
1475
+ if (this.store.data.websocket) {
1321
1476
  this.websocket = new WebSocket(this.store.data.websocket, this.store.data.websocketProtocols);
1322
1477
  this.websocket.onmessage = function (e) {
1323
- _this.socketCallback(e.data);
1478
+ _this.socketCallback(e.data, {
1479
+ type: 'websocket',
1480
+ url: _this.store.data.websocket,
1481
+ });
1324
1482
  };
1325
1483
  this.websocket.onclose = function () {
1326
1484
  console.info('Canvas websocket closed and reconneting...');
@@ -1334,13 +1492,6 @@ var Meta2d = /** @class */ (function () {
1334
1492
  this.websocket.close();
1335
1493
  this.websocket = undefined;
1336
1494
  }
1337
- if (this.websockets) {
1338
- this.websockets.forEach(function (websocket) {
1339
- websocket.onclose = undefined;
1340
- websocket.close();
1341
- websocket = undefined;
1342
- });
1343
- }
1344
1495
  };
1345
1496
  Meta2d.prototype.connectMqtt = function (params) {
1346
1497
  var _this = this;
@@ -1350,29 +1501,18 @@ var Meta2d = /** @class */ (function () {
1350
1501
  this.store.data.mqttTopics = params.mqttTopics;
1351
1502
  this.store.data.mqttOptions = params.mqttOptions;
1352
1503
  }
1353
- if (this.store.data.mqtts) {
1354
- this.mqttClients = [];
1355
- this.store.data.mqtts.forEach(function (_mqtt, index) {
1356
- if (_mqtt.options.clientId && !_mqtt.options.customClientId) {
1357
- _mqtt.options.clientId = s8();
1358
- }
1359
- _this.mqttClients[index] = mqtt.connect(_mqtt.url, _mqtt.options);
1360
- _this.mqttClients[index].on('message', function (topic, message) {
1361
- _this.socketCallback(message.toString(), topic);
1362
- });
1363
- if (_mqtt.topics) {
1364
- _this.mqttClients[index].subscribe(_mqtt.topics.split(','));
1365
- }
1366
- });
1367
- }
1368
- else if (this.store.data.mqtt) {
1504
+ if (this.store.data.mqtt) {
1369
1505
  if (this.store.data.mqttOptions.clientId &&
1370
1506
  !this.store.data.mqttOptions.customClientId) {
1371
1507
  this.store.data.mqttOptions.clientId = s8();
1372
1508
  }
1373
1509
  this.mqttClient = mqtt.connect(this.store.data.mqtt, this.store.data.mqttOptions);
1374
1510
  this.mqttClient.on('message', function (topic, message) {
1375
- _this.socketCallback(message.toString(), topic);
1511
+ _this.socketCallback(message.toString(), {
1512
+ topic: topic,
1513
+ type: 'mqtt',
1514
+ url: _this.store.data.mqtt,
1515
+ });
1376
1516
  });
1377
1517
  if (this.store.data.mqttTopics) {
1378
1518
  this.mqttClient.subscribe(this.store.data.mqttTopics.split(','));
@@ -1382,10 +1522,6 @@ var Meta2d = /** @class */ (function () {
1382
1522
  Meta2d.prototype.closeMqtt = function () {
1383
1523
  var _a;
1384
1524
  (_a = this.mqttClient) === null || _a === void 0 ? void 0 : _a.end();
1385
- this.mqttClients &&
1386
- this.mqttClients.forEach(function (mqttClient) {
1387
- mqttClient === null || mqttClient === void 0 ? void 0 : mqttClient.end();
1388
- });
1389
1525
  };
1390
1526
  Meta2d.prototype.connectHttp = function () {
1391
1527
  var _this = this;
@@ -1407,7 +1543,7 @@ var Meta2d = /** @class */ (function () {
1407
1543
  return [4 /*yield*/, res.text()];
1408
1544
  case 2:
1409
1545
  data = _a.sent();
1410
- this.socketCallback(data);
1546
+ this.socketCallback(data, { type: 'http', url: item.http });
1411
1547
  _a.label = 3;
1412
1548
  case 3: return [2 /*return*/];
1413
1549
  }
@@ -1432,7 +1568,7 @@ var Meta2d = /** @class */ (function () {
1432
1568
  return [4 /*yield*/, res.text()];
1433
1569
  case 2:
1434
1570
  data = _a.sent();
1435
- this.socketCallback(data);
1571
+ this.socketCallback(data, { type: 'http', url: http_1 });
1436
1572
  _a.label = 3;
1437
1573
  case 3: return [2 /*return*/];
1438
1574
  }
@@ -1500,11 +1636,182 @@ var Meta2d = /** @class */ (function () {
1500
1636
  _httpTimer = undefined;
1501
1637
  });
1502
1638
  };
1503
- Meta2d.prototype.socketCallback = function (message, topic) {
1639
+ Meta2d.prototype.connectNetwork = function () {
1640
+ var _this = this;
1641
+ this.closeNetwork();
1642
+ var networks = this.store.data.networks;
1643
+ if (networks) {
1644
+ var mqttIndex_1 = 0;
1645
+ this.mqttClients = [];
1646
+ var websocketIndex_1 = 0;
1647
+ this.websockets = [];
1648
+ var https_1 = [];
1649
+ networks.forEach(function (net) {
1650
+ if (net.type === 'mqtt') {
1651
+ if (net.options.clientId && !net.options.customClientId) {
1652
+ net.options.clientId = s8();
1653
+ }
1654
+ _this.mqttClients[mqttIndex_1] = mqtt.connect(net.url, net.options);
1655
+ _this.mqttClients[mqttIndex_1].on('message', function (topic, message) {
1656
+ _this.socketCallback(message.toString(), {
1657
+ topic: topic,
1658
+ type: 'mqtt',
1659
+ url: net.url,
1660
+ });
1661
+ });
1662
+ if (net.topics) {
1663
+ _this.mqttClients[mqttIndex_1].subscribe(net.topics.split(','));
1664
+ }
1665
+ mqttIndex_1 += 1;
1666
+ }
1667
+ else if (net.type === 'websocket') {
1668
+ _this.websockets[websocketIndex_1] = new WebSocket(net.url, net.protocols);
1669
+ _this.websockets[websocketIndex_1].onmessage = function (e) {
1670
+ _this.socketCallback(e.data, { type: 'websocket', url: net.url });
1671
+ };
1672
+ websocketIndex_1 += 1;
1673
+ }
1674
+ else {
1675
+ https_1.push(net);
1676
+ }
1677
+ });
1678
+ this.onNetworkConnect(https_1);
1679
+ }
1680
+ };
1681
+ Meta2d.prototype.randomString = function (e) {
1682
+ e = e || 32;
1683
+ var t = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678', a = t.length, n = '';
1684
+ for (var i = 0; i < e; i++) {
1685
+ n += t.charAt(Math.floor(Math.random() * a));
1686
+ }
1687
+ return n;
1688
+ };
1689
+ Meta2d.prototype.penMock = function (pen) {
1690
+ var _this = this;
1691
+ var _a;
1692
+ if (pen.realTimes) {
1693
+ var _d_1 = {};
1694
+ pen.realTimes.forEach(function (realTime) {
1695
+ if (!realTime.binds || !realTime.binds.length) {
1696
+ if (realTime.type === 'number') {
1697
+ if (realTime.value && realTime.value.indexOf(',') !== -1) {
1698
+ var arr = realTime.value.split(',');
1699
+ var rai = Math.floor(Math.random() * arr.length);
1700
+ _d_1[realTime.key] = parseFloat(arr[rai]);
1701
+ }
1702
+ else if (realTime.value && realTime.value.indexOf('-') !== -1) {
1703
+ var arr = realTime.value.split('-');
1704
+ var max = parseFloat(arr[1]);
1705
+ var min = parseFloat(arr[0]);
1706
+ _d_1[realTime.key] = Math.random() * (max - min) + min;
1707
+ }
1708
+ else {
1709
+ _d_1[realTime.key] = parseFloat(realTime.value);
1710
+ }
1711
+ }
1712
+ else if (realTime.type === 'bool') {
1713
+ if (typeof realTime.value === 'boolean') {
1714
+ _d_1[realTime.key] = realTime.value;
1715
+ }
1716
+ else if ('true' === realTime.value) {
1717
+ _d_1[realTime.key] = true;
1718
+ }
1719
+ else if ('false' === realTime.value) {
1720
+ _d_1[realTime.key] = false;
1721
+ }
1722
+ else {
1723
+ _d_1[realTime.key] = Math.random() < 0.5;
1724
+ }
1725
+ }
1726
+ else if (realTime.type === 'object' || realTime.type === 'array') {
1727
+ _d_1[realTime.key] = realTime.value;
1728
+ }
1729
+ else {
1730
+ //if (realTime.type === 'string')
1731
+ if (realTime.value &&
1732
+ realTime.value.startsWith('{') &&
1733
+ realTime.value.endsWith('}')) {
1734
+ var str = realTime.value.substring(1, realTime.value.length - 1);
1735
+ var arr = str.split(',');
1736
+ var rai = Math.floor(Math.random() * arr.length);
1737
+ _d_1[realTime.key] = arr[rai];
1738
+ }
1739
+ else if (realTime.value &&
1740
+ realTime.value.startsWith('[') &&
1741
+ realTime.value.endsWith(']')) {
1742
+ var len = parseInt(realTime.value.substring(1, realTime.value.length - 1));
1743
+ _d_1[realTime.key] = _this.randomString(len);
1744
+ }
1745
+ else {
1746
+ _d_1[realTime.key] = realTime.value;
1747
+ }
1748
+ }
1749
+ }
1750
+ });
1751
+ if (Object.keys(_d_1).length) {
1752
+ this.canvas.updateValue(pen, _d_1);
1753
+ this.store.emitter.emit('valueUpdate', pen);
1754
+ (_a = pen.onValue) === null || _a === void 0 ? void 0 : _a.call(pen, pen);
1755
+ }
1756
+ }
1757
+ };
1758
+ Meta2d.prototype.onNetworkConnect = function (https) {
1504
1759
  var _this = this;
1505
- if (topic === void 0) { topic = ''; }
1506
- this.store.emitter.emit('socket', { message: message, topic: topic });
1507
- if (this.socketFn && !this.socketFn(message, topic)) {
1760
+ this.updateTimer = setInterval(function () {
1761
+ //模拟数据
1762
+ _this.store.data.pens.forEach(function (pen) {
1763
+ _this.penMock(pen);
1764
+ });
1765
+ https.forEach(function (item) { return __awaiter(_this, void 0, void 0, function () {
1766
+ var res, data;
1767
+ return __generator(this, function (_a) {
1768
+ switch (_a.label) {
1769
+ case 0:
1770
+ if (!item.url) return [3 /*break*/, 3];
1771
+ return [4 /*yield*/, fetch(item.url, {
1772
+ headers: item.headers,
1773
+ method: item.method,
1774
+ body: item.body,
1775
+ })];
1776
+ case 1:
1777
+ res = _a.sent();
1778
+ if (!res.ok) return [3 /*break*/, 3];
1779
+ return [4 /*yield*/, res.text()];
1780
+ case 2:
1781
+ data = _a.sent();
1782
+ this.socketCallback(data, { type: 'http', url: item.url });
1783
+ _a.label = 3;
1784
+ case 3: return [2 /*return*/];
1785
+ }
1786
+ });
1787
+ }); });
1788
+ _this.render();
1789
+ }, this.store.data.networkInterval || 1000);
1790
+ };
1791
+ Meta2d.prototype.closeNetwork = function () {
1792
+ this.mqttClients &&
1793
+ this.mqttClients.forEach(function (mqttClient) {
1794
+ mqttClient.end();
1795
+ });
1796
+ this.websockets &&
1797
+ this.websockets.forEach(function (websocket) {
1798
+ websocket.close();
1799
+ });
1800
+ this.mqttClients = undefined;
1801
+ this.websockets = undefined;
1802
+ clearInterval(this.updateTimer);
1803
+ this.updateTimer = undefined;
1804
+ };
1805
+ Meta2d.prototype.socketCallback = function (message, context) {
1806
+ var _this = this;
1807
+ this.store.emitter.emit('socket', { message: message, context: context });
1808
+ if (this.socketFn &&
1809
+ !this.socketFn(message, {
1810
+ meta2d: this,
1811
+ type: context.type,
1812
+ topic: context.topic,
1813
+ url: context.url,
1814
+ })) {
1508
1815
  return;
1509
1816
  }
1510
1817
  var data;
@@ -1540,7 +1847,7 @@ var Meta2d = /** @class */ (function () {
1540
1847
  // 绑定变量方式更新组件数据
1541
1848
  Meta2d.prototype.setDatas = function (datas, _a) {
1542
1849
  var _this = this;
1543
- var _b = _a === void 0 ? {} : _a, _c = _b.render, render = _c === void 0 ? true : _c, _d = _b.doEvent, doEvent = _d === void 0 ? true : _d, history = _b.history;
1850
+ var _b = _a === void 0 ? {} : _a, _c = _b.render, render = _c === void 0 ? true : _c, _e = _b.doEvent, doEvent = _e === void 0 ? true : _e, history = _b.history;
1544
1851
  // 把{dataId: string; value: any}转成setValue格式数据
1545
1852
  var penValues = new Map();
1546
1853
  datas.forEach(function (v) {
@@ -1596,7 +1903,7 @@ var Meta2d = /** @class */ (function () {
1596
1903
  };
1597
1904
  Meta2d.prototype.setValue = function (data, _a) {
1598
1905
  var _this = this;
1599
- var _b = _a === void 0 ? {} : _a, _c = _b.render, render = _c === void 0 ? true : _c, _d = _b.doEvent, doEvent = _d === void 0 ? true : _d, history = _b.history;
1906
+ var _b = _a === void 0 ? {} : _a, _c = _b.render, render = _c === void 0 ? true : _c, _e = _b.doEvent, doEvent = _e === void 0 ? true : _e, history = _b.history;
1600
1907
  var pens = [];
1601
1908
  if (data.id) {
1602
1909
  if (data.id === this.store.data.id) {
@@ -1688,6 +1995,68 @@ var Meta2d = /** @class */ (function () {
1688
1995
  Meta2d.prototype.clearDropdownList = function () {
1689
1996
  this.canvas.clearDropdownList();
1690
1997
  };
1998
+ Meta2d.prototype.judgeCondition = function (pen, key, condition) {
1999
+ var type = condition.type, target = condition.target, fnJs = condition.fnJs, fn = condition.fn, operator = condition.operator, valueType = condition.valueType;
2000
+ var can = false;
2001
+ if (type === 'fn') {
2002
+ //方法
2003
+ if (fn) {
2004
+ can = fn(pen, { meta2d: this });
2005
+ }
2006
+ else if (fnJs) {
2007
+ try {
2008
+ condition.fn = new Function('pen', 'context', fnJs);
2009
+ }
2010
+ catch (err) {
2011
+ console.error('Error: make function:', err);
2012
+ }
2013
+ if (condition.fn) {
2014
+ can = condition.fn(pen, { meta2d: this });
2015
+ }
2016
+ }
2017
+ }
2018
+ else {
2019
+ //TODO boolean类型 数字类型
2020
+ var value = condition.value;
2021
+ if (valueType === 'prop') {
2022
+ value = this.store.pens[target][condition.value];
2023
+ }
2024
+ switch (operator) {
2025
+ case '>':
2026
+ can = pen[key] > +value;
2027
+ break;
2028
+ case '>=':
2029
+ can = pen[key] >= +value;
2030
+ break;
2031
+ case '<':
2032
+ can = pen[key] < +value;
2033
+ break;
2034
+ case '<=':
2035
+ can = pen[key] <= +value;
2036
+ break;
2037
+ case '=':
2038
+ case '==':
2039
+ can = pen[key] == value;
2040
+ break;
2041
+ case '!=':
2042
+ can = pen[key] != value;
2043
+ break;
2044
+ case '[)':
2045
+ can = valueInRange(+pen[key], value);
2046
+ break;
2047
+ case '![)':
2048
+ can = !valueInRange(+pen[key], value);
2049
+ break;
2050
+ case '[]':
2051
+ can = valueInArray(+pen[key], value);
2052
+ break;
2053
+ case '![]':
2054
+ can = !valueInArray(+pen[key], value);
2055
+ break;
2056
+ }
2057
+ }
2058
+ return can;
2059
+ };
1691
2060
  Meta2d.prototype.pushChildren = function (parent, children) {
1692
2061
  var _this = this;
1693
2062
  var initUpdatePens = [deepClone(parent, true)];
@@ -1751,12 +2120,33 @@ var Meta2d = /** @class */ (function () {
1751
2120
  * @param padding 上右下左的内边距
1752
2121
  */
1753
2122
  Meta2d.prototype.downloadPng = function (name, padding) {
1754
- var a = document.createElement('a');
1755
- a.setAttribute('download', name || 'le5le.meta2d.png');
1756
- a.setAttribute('href', this.toPng(padding, undefined, true));
1757
- var evt = document.createEvent('MouseEvents');
1758
- evt.initEvent('click', true, true);
1759
- a.dispatchEvent(evt);
2123
+ var e_4, _a;
2124
+ var _this = this;
2125
+ var _b;
2126
+ try {
2127
+ for (var _c = __values(this.store.data.pens), _e = _c.next(); !_e.done; _e = _c.next()) {
2128
+ var pen = _e.value;
2129
+ if (pen.calculative.img) {
2130
+ //重新生成绘制图片
2131
+ (_b = pen.onRenderPenRaw) === null || _b === void 0 ? void 0 : _b.call(pen, pen);
2132
+ }
2133
+ }
2134
+ }
2135
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
2136
+ finally {
2137
+ try {
2138
+ if (_e && !_e.done && (_a = _c.return)) _a.call(_c);
2139
+ }
2140
+ finally { if (e_4) throw e_4.error; }
2141
+ }
2142
+ setTimeout(function () {
2143
+ var a = document.createElement('a');
2144
+ a.setAttribute('download', name || 'le5le.meta2d.png');
2145
+ a.setAttribute('href', _this.toPng(padding, undefined, true));
2146
+ var evt = document.createEvent('MouseEvents');
2147
+ evt.initEvent('click', true, true);
2148
+ a.dispatchEvent(evt);
2149
+ });
1760
2150
  };
1761
2151
  Meta2d.prototype.getRect = function (pens) {
1762
2152
  if (pens === void 0) { pens = this.store.data.pens; }
@@ -1949,7 +2339,7 @@ var Meta2d = /** @class */ (function () {
1949
2339
  Meta2d.prototype.beSameByFirst = function (pens, attribute) {
1950
2340
  if (pens === void 0) { pens = this.store.data.pens; }
1951
2341
  var initPens = deepClone(pens); // 原 pens ,深拷贝一下
1952
- // 1. 得到第一个画笔的 宽高 字体大小
2342
+ // 1. 得到第一个画笔的 宽高
1953
2343
  var firstPen = pens[0];
1954
2344
  var _a = this.getPenRect(firstPen), width = _a.width, height = _a.height;
1955
2345
  for (var i = 1; i < pens.length; i++) {
@@ -1971,6 +2361,35 @@ var Meta2d = /** @class */ (function () {
1971
2361
  pens: pens,
1972
2362
  });
1973
2363
  };
2364
+ /**
2365
+ * 大小相同
2366
+ * @param pens 画笔们
2367
+ */
2368
+ Meta2d.prototype.beSameByLast = function (pens, attribute) {
2369
+ if (pens === void 0) { pens = this.store.data.pens; }
2370
+ var initPens = deepClone(pens); // 原 pens ,深拷贝一下
2371
+ // 1. 得到最后一个画笔的 宽高
2372
+ var lastPen = pens[pens.length - 1];
2373
+ var _a = this.getPenRect(lastPen), width = _a.width, height = _a.height;
2374
+ for (var i = 0; i < pens.length - 1; i++) {
2375
+ var pen = pens[i];
2376
+ if (attribute === 'width') {
2377
+ this.setValue({ id: pen.id, width: width }, { render: false, doEvent: false });
2378
+ }
2379
+ else if (attribute === 'height') {
2380
+ this.setValue({ id: pen.id, height: height }, { render: false, doEvent: false });
2381
+ }
2382
+ else {
2383
+ this.setValue({ id: pen.id, width: width, height: height }, { render: false, doEvent: false });
2384
+ }
2385
+ }
2386
+ this.render();
2387
+ this.pushHistory({
2388
+ type: EditType.Update,
2389
+ initPens: initPens,
2390
+ pens: pens,
2391
+ });
2392
+ };
1974
2393
  /**
1975
2394
  * 格式刷(样式相同,大小无需一致。)
1976
2395
  * @param pens 画笔们
@@ -2048,7 +2467,7 @@ var Meta2d = /** @class */ (function () {
2048
2467
  this.formatPainter();
2049
2468
  };
2050
2469
  Meta2d.prototype.alignNodes = function (align, pens, rect) {
2051
- var e_4, _a;
2470
+ var e_5, _a;
2052
2471
  if (pens === void 0) { pens = this.store.data.pens; }
2053
2472
  !rect && (rect = this.getPenRect(this.getRect(pens)));
2054
2473
  var initPens = deepClone(pens); // 原 pens ,深拷贝一下
@@ -2058,12 +2477,12 @@ var Meta2d = /** @class */ (function () {
2058
2477
  this.alignPen(align, item, rect);
2059
2478
  }
2060
2479
  }
2061
- catch (e_4_1) { e_4 = { error: e_4_1 }; }
2480
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
2062
2481
  finally {
2063
2482
  try {
2064
2483
  if (pens_1_1 && !pens_1_1.done && (_a = pens_1.return)) _a.call(pens_1);
2065
2484
  }
2066
- finally { if (e_4) throw e_4.error; }
2485
+ finally { if (e_5) throw e_5.error; }
2067
2486
  }
2068
2487
  this.render();
2069
2488
  this.pushHistory({
@@ -2093,6 +2512,27 @@ var Meta2d = /** @class */ (function () {
2093
2512
  pens: pens,
2094
2513
  });
2095
2514
  };
2515
+ /**
2516
+ * 对齐画笔,基于最后选中的画笔
2517
+ * @param align 左对齐,右对齐,上对齐,下对齐,居中对齐
2518
+ * @param pens
2519
+ */
2520
+ Meta2d.prototype.alignNodesByLast = function (align, pens) {
2521
+ if (pens === void 0) { pens = this.store.data.pens; }
2522
+ var initPens = deepClone(pens); // 原 pens ,深拷贝一下
2523
+ var lastPen = pens[pens.length - 1];
2524
+ var rect = this.getPenRect(lastPen);
2525
+ for (var i = 0; i < pens.length - 1; i++) {
2526
+ var pen = pens[i];
2527
+ this.alignPen(align, pen, rect);
2528
+ }
2529
+ this.render();
2530
+ this.pushHistory({
2531
+ type: EditType.Update,
2532
+ initPens: initPens,
2533
+ pens: pens,
2534
+ });
2535
+ };
2096
2536
  /**
2097
2537
  * 将画笔参照 rect 进行 align 对齐
2098
2538
  * @param align 左对齐,右对齐,上对齐,下对齐,居中对齐
@@ -2131,7 +2571,7 @@ var Meta2d = /** @class */ (function () {
2131
2571
  * @param distance 总的宽 or 高
2132
2572
  */
2133
2573
  Meta2d.prototype.spaceBetweenByDirection = function (direction, pens, distance) {
2134
- var e_5, _a;
2574
+ var e_6, _a;
2135
2575
  var _this = this;
2136
2576
  if (pens === void 0) { pens = this.store.data.pens; }
2137
2577
  !distance && (distance = this.getPenRect(this.getRect(pens))[direction]);
@@ -2165,12 +2605,12 @@ var Meta2d = /** @class */ (function () {
2165
2605
  this.setValue(__assign({ id: pen.id }, penRect), { render: false, doEvent: false });
2166
2606
  }
2167
2607
  }
2168
- catch (e_5_1) { e_5 = { error: e_5_1 }; }
2608
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
2169
2609
  finally {
2170
2610
  try {
2171
2611
  if (pens_2_1 && !pens_2_1.done && (_a = pens_2.return)) _a.call(pens_2);
2172
2612
  }
2173
- finally { if (e_5) throw e_5.error; }
2613
+ finally { if (e_6) throw e_6.error; }
2174
2614
  }
2175
2615
  this.render();
2176
2616
  this.pushHistory({
@@ -2285,11 +2725,10 @@ var Meta2d = /** @class */ (function () {
2285
2725
  /**
2286
2726
  * 将该画笔置顶,即放到数组最后,最后绘制即在顶部
2287
2727
  * @param pen pen 置顶的画笔
2288
- * @param pens 画笔们,注意 pen 必须在该数组内才有效
2289
2728
  */
2290
- Meta2d.prototype.top = function (pen, pens) {
2729
+ Meta2d.prototype.top = function (pen) {
2291
2730
  var _this = this;
2292
- if (pens === void 0) { pens = this.store.data.pens; }
2731
+ var pens = this.store.data.pens;
2293
2732
  // 获取它包含它的子节点
2294
2733
  var allIds = __spreadArray(__spreadArray([], __read(getAllChildren(pen, this.store)), false), [pen], false).map(function (p) { return p.id; });
2295
2734
  var allPens = pens.filter(function (p) { return allIds.includes(p.id); });
@@ -2314,8 +2753,8 @@ var Meta2d = /** @class */ (function () {
2314
2753
  * 该画笔置底,即放到数组最前,最后绘制即在底部
2315
2754
  * @param pens 画笔们,注意 pen 必须在该数组内才有效
2316
2755
  */
2317
- Meta2d.prototype.bottom = function (pen, pens) {
2318
- if (pens === void 0) { pens = this.store.data.pens; }
2756
+ Meta2d.prototype.bottom = function (pen) {
2757
+ var pens = this.store.data.pens;
2319
2758
  var allIds = __spreadArray(__spreadArray([], __read(getAllChildren(pen, this.store)), false), [pen], false).map(function (p) { return p.id; });
2320
2759
  var allPens = pens.filter(function (p) { return allIds.includes(p.id); });
2321
2760
  var _loop_3 = function (i) {
@@ -2339,7 +2778,7 @@ var Meta2d = /** @class */ (function () {
2339
2778
  * @param pen 画笔
2340
2779
  */
2341
2780
  Meta2d.prototype.upByArea = function (pen) {
2342
- var _a, e_6, _b;
2781
+ var _a, e_7, _b;
2343
2782
  var _this = this;
2344
2783
  var index = this.store.data.pens.findIndex(function (p) { return p.id === pen.id; });
2345
2784
  if (index === -1) {
@@ -2389,39 +2828,97 @@ var Meta2d = /** @class */ (function () {
2389
2828
  _loop_4(pen_2);
2390
2829
  }
2391
2830
  }
2392
- catch (e_6_1) { e_6 = { error: e_6_1 }; }
2831
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
2393
2832
  finally {
2394
2833
  try {
2395
2834
  if (allPens_1_1 && !allPens_1_1.done && (_b = allPens_1.return)) _b.call(allPens_1);
2396
2835
  }
2397
- finally { if (e_6) throw e_6.error; }
2836
+ finally { if (e_7) throw e_7.error; }
2398
2837
  }
2399
2838
  this.initImageCanvas([pen]);
2400
2839
  };
2401
2840
  /**
2402
2841
  * 该画笔上移,即把该画笔在数组中的位置向后移动一个
2403
- * @param pens 画笔们,注意 pen 必须在该数组内才有效
2842
+ * @param pen 画笔
2404
2843
  */
2405
- Meta2d.prototype.up = function (pen, pens) {
2406
- if (pens === void 0) { pens = this.store.data.pens; }
2407
- var index = pens.findIndex(function (p) { return p.id === pen.id; });
2408
- if (index > -1 && index !== pens.length - 1) {
2409
- pens.splice(index + 2, 0, pens[index]);
2410
- pens.splice(index, 1);
2411
- this.initImageCanvas([pen]);
2844
+ Meta2d.prototype.up = function (pen) {
2845
+ var pens = this.store.data.pens;
2846
+ if (pen.children) {
2847
+ //组合图元
2848
+ var preMovePens = __spreadArray(__spreadArray([], __read(getAllChildren(pen, this.store)), false), [pen], false);
2849
+ //先保证组合图元的顺序正确。
2850
+ var orderPens = [];
2851
+ var _loop_5 = function (index) {
2852
+ var _pen = pens[index];
2853
+ if (preMovePens.findIndex(function (p) { return p.id === _pen.id; }) !== -1) {
2854
+ _pen.temIndex = index;
2855
+ orderPens.push(_pen);
2856
+ }
2857
+ };
2858
+ for (var index = 0; index < pens.length; index++) {
2859
+ _loop_5(index);
2860
+ }
2861
+ var lastIndex_1 = -1;
2862
+ var offset_1 = 0;
2863
+ orderPens.forEach(function (_pen) {
2864
+ _pen.temIndex -= offset_1;
2865
+ pens.splice(_pen.temIndex, 1);
2866
+ offset_1 += 1;
2867
+ lastIndex_1 = _pen.temIndex;
2868
+ delete _pen.temIndex;
2869
+ });
2870
+ pens.splice.apply(pens, __spreadArray([lastIndex_1 + 1, 0], __read(preMovePens), false));
2871
+ }
2872
+ else {
2873
+ var index = pens.findIndex(function (p) { return p.id === pen.id; });
2874
+ if (index > -1 && index !== pens.length - 1) {
2875
+ pens.splice(index + 2, 0, pens[index]);
2876
+ pens.splice(index, 1);
2877
+ this.initImageCanvas([pen]);
2878
+ }
2412
2879
  }
2413
2880
  };
2414
2881
  /**
2415
2882
  * 该画笔下移,即把该画笔在该数组中的位置前移一个
2416
- * @param pens 画笔们,注意 pen 必须在该数组内才有效
2883
+ * @param pen 画笔
2417
2884
  */
2418
- Meta2d.prototype.down = function (pen, pens) {
2419
- if (pens === void 0) { pens = this.store.data.pens; }
2420
- var index = pens.findIndex(function (p) { return p.id === pen.id; });
2421
- if (index > -1 && index !== 0) {
2422
- pens.splice(index - 1, 0, pens[index]);
2423
- pens.splice(index + 1, 1);
2424
- this.initImageCanvas([pen]);
2885
+ Meta2d.prototype.down = function (pen) {
2886
+ var pens = this.store.data.pens;
2887
+ if (pen.children) {
2888
+ //组合图元
2889
+ var preMovePens = __spreadArray(__spreadArray([], __read(getAllChildren(pen, this.store)), false), [pen], false);
2890
+ //先保证组合图元的顺序正确。
2891
+ var orderPens = [];
2892
+ var _loop_6 = function (index) {
2893
+ var _pen = pens[index];
2894
+ if (preMovePens.findIndex(function (p) { return p.id === _pen.id; }) !== -1) {
2895
+ _pen.temIndex = index;
2896
+ orderPens.push(_pen);
2897
+ }
2898
+ };
2899
+ for (var index = 0; index < pens.length; index++) {
2900
+ _loop_6(index);
2901
+ }
2902
+ var firstIndex_1 = -1;
2903
+ var offset_2 = 0;
2904
+ orderPens.forEach(function (_pen, index) {
2905
+ _pen.temIndex -= offset_2;
2906
+ pens.splice(_pen.temIndex, 1);
2907
+ offset_2 += 1;
2908
+ if (index === 0) {
2909
+ firstIndex_1 = _pen.temIndex;
2910
+ }
2911
+ delete _pen.temIndex;
2912
+ });
2913
+ pens.splice.apply(pens, __spreadArray([firstIndex_1 - 1, 0], __read(preMovePens), false));
2914
+ }
2915
+ else {
2916
+ var index = pens.findIndex(function (p) { return p.id === pen.id; });
2917
+ if (index > -1 && index !== 0) {
2918
+ pens.splice(index - 1, 0, pens[index]);
2919
+ pens.splice(index + 1, 1);
2920
+ this.initImageCanvas([pen]);
2921
+ }
2425
2922
  }
2426
2923
  };
2427
2924
  Meta2d.prototype.setLayer = function (pen, toIndex, pens) {
@@ -2496,9 +2993,9 @@ var Meta2d = /** @class */ (function () {
2496
2993
  var nextNodes_1 = [];
2497
2994
  // 2. 遍历出线的 nextNode
2498
2995
  lines.forEach(function (line) {
2499
- var e_7, _a;
2996
+ var e_8, _a;
2500
2997
  var lineNextNode = _this.nextNode(line);
2501
- var _loop_5 = function (node) {
2998
+ var _loop_7 = function (node) {
2502
2999
  var have = nextNodes_1.find(function (next) { return next.id === node.id; });
2503
3000
  // 3. 不重复的才加进去
2504
3001
  !have && nextNodes_1.push(node);
@@ -2506,15 +3003,15 @@ var Meta2d = /** @class */ (function () {
2506
3003
  try {
2507
3004
  for (var lineNextNode_1 = __values(lineNextNode), lineNextNode_1_1 = lineNextNode_1.next(); !lineNextNode_1_1.done; lineNextNode_1_1 = lineNextNode_1.next()) {
2508
3005
  var node = lineNextNode_1_1.value;
2509
- _loop_5(node);
3006
+ _loop_7(node);
2510
3007
  }
2511
3008
  }
2512
- catch (e_7_1) { e_7 = { error: e_7_1 }; }
3009
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
2513
3010
  finally {
2514
3011
  try {
2515
3012
  if (lineNextNode_1_1 && !lineNextNode_1_1.done && (_a = lineNextNode_1.return)) _a.call(lineNextNode_1);
2516
3013
  }
2517
- finally { if (e_7) throw e_7.error; }
3014
+ finally { if (e_8) throw e_8.error; }
2518
3015
  }
2519
3016
  });
2520
3017
  return nextNodes_1;
@@ -2537,9 +3034,9 @@ var Meta2d = /** @class */ (function () {
2537
3034
  var preNodes_1 = [];
2538
3035
  // 2. 遍历入线的 preNode
2539
3036
  lines.forEach(function (line) {
2540
- var e_8, _a;
3037
+ var e_9, _a;
2541
3038
  var linePreNode = _this.previousNode(line);
2542
- var _loop_6 = function (node) {
3039
+ var _loop_8 = function (node) {
2543
3040
  var have = preNodes_1.find(function (pre) { return pre.id === node.id; });
2544
3041
  // 3. 不重复的才加进去
2545
3042
  !have && preNodes_1.push(node);
@@ -2547,15 +3044,15 @@ var Meta2d = /** @class */ (function () {
2547
3044
  try {
2548
3045
  for (var linePreNode_1 = __values(linePreNode), linePreNode_1_1 = linePreNode_1.next(); !linePreNode_1_1.done; linePreNode_1_1 = linePreNode_1.next()) {
2549
3046
  var node = linePreNode_1_1.value;
2550
- _loop_6(node);
3047
+ _loop_8(node);
2551
3048
  }
2552
3049
  }
2553
- catch (e_8_1) { e_8 = { error: e_8_1 }; }
3050
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
2554
3051
  finally {
2555
3052
  try {
2556
3053
  if (linePreNode_1_1 && !linePreNode_1_1.done && (_a = linePreNode_1.return)) _a.call(linePreNode_1);
2557
3054
  }
2558
- finally { if (e_8) throw e_8.error; }
3055
+ finally { if (e_9) throw e_9.error; }
2559
3056
  }
2560
3057
  });
2561
3058
  return preNodes_1;
@@ -2831,7 +3328,7 @@ var Meta2d = /** @class */ (function () {
2831
3328
  : deepClone(__spreadArray([parent], __read(components), false));
2832
3329
  };
2833
3330
  Meta2d.prototype.setVisible = function (pen, visible, render) {
2834
- var e_9, _a;
3331
+ var e_10, _a;
2835
3332
  if (render === void 0) { render = true; }
2836
3333
  this.onSizeUpdate();
2837
3334
  this.setValue({ id: pen.id, visible: visible }, { render: false, doEvent: false });
@@ -2843,12 +3340,12 @@ var Meta2d = /** @class */ (function () {
2843
3340
  child && this.setVisible(child, visible, false);
2844
3341
  }
2845
3342
  }
2846
- catch (e_9_1) { e_9 = { error: e_9_1 }; }
3343
+ catch (e_10_1) { e_10 = { error: e_10_1 }; }
2847
3344
  finally {
2848
3345
  try {
2849
3346
  if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
2850
3347
  }
2851
- finally { if (e_9) throw e_9.error; }
3348
+ finally { if (e_10) throw e_10.error; }
2852
3349
  }
2853
3350
  }
2854
3351
  render && this.render();