@meta2d/core 1.0.15 → 1.0.16

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();
@@ -712,8 +859,8 @@ var Meta2d = /** @class */ (function () {
712
859
  var initJs = this.store.data.initJs;
713
860
  if (initJs && initJs.trim()) {
714
861
  try {
715
- var fn = new Function(initJs);
716
- fn();
862
+ var fn = new Function('context', initJs);
863
+ fn({ meta2d: this });
717
864
  }
718
865
  catch (e) {
719
866
  console.warn('initJs error', e);
@@ -903,7 +1050,7 @@ var Meta2d = /** @class */ (function () {
903
1050
  if (render === void 0) { render = true; }
904
1051
  this.canvas.setPenRect(pen, rect, render);
905
1052
  };
906
- Meta2d.prototype.startAnimate = function (idOrTagOrPens, index) {
1053
+ Meta2d.prototype.startAnimate = function (idOrTagOrPens, params) {
907
1054
  var _this = this;
908
1055
  this.stopAnimate(idOrTagOrPens);
909
1056
  var pens;
@@ -926,9 +1073,22 @@ var Meta2d = /** @class */ (function () {
926
1073
  pen.calculative.frameEnd += d;
927
1074
  }
928
1075
  else {
929
- if (index !== undefined &&
930
- pen.animations &&
931
- pen.animations.length > index) {
1076
+ if (params !== undefined && pen.animations) {
1077
+ var index = -1;
1078
+ if (typeof params === 'string') {
1079
+ index = pen.animations.findIndex(function (animation) { return animation.name === params; });
1080
+ if (index === -1) {
1081
+ return;
1082
+ }
1083
+ }
1084
+ else if (typeof params === 'number') {
1085
+ if (pen.animations.length > params) {
1086
+ index = params;
1087
+ }
1088
+ else {
1089
+ return;
1090
+ }
1091
+ }
932
1092
  var animate = deepClone(pen.animations[index]);
933
1093
  delete animate.name;
934
1094
  animate.currentAnimation = index;
@@ -1223,6 +1383,10 @@ var Meta2d = /** @class */ (function () {
1223
1383
  Meta2d.prototype.inactive = function () {
1224
1384
  this.canvas.inactive();
1225
1385
  };
1386
+ Meta2d.prototype.activeAll = function () {
1387
+ this.canvas.active(this.store.data.pens.filter(function (pen) { return !pen.parentId && pen.locked !== LockState.Disable; }));
1388
+ this.render();
1389
+ };
1226
1390
  /**
1227
1391
  * 删除画笔
1228
1392
  * @param pens 需要删除的画笔们
@@ -1285,7 +1449,7 @@ var Meta2d = /** @class */ (function () {
1285
1449
  var socketFn = void 0;
1286
1450
  var socketCbJs = this.store.data.socketCbJs;
1287
1451
  if (socketCbJs) {
1288
- socketFn = new Function('e', 'topic', socketCbJs);
1452
+ socketFn = new Function('e', 'topic', 'context', socketCbJs);
1289
1453
  }
1290
1454
  if (!socketFn) {
1291
1455
  return false;
@@ -1304,20 +1468,7 @@ var Meta2d = /** @class */ (function () {
1304
1468
  if (websocket) {
1305
1469
  this.store.data.websocket = websocket;
1306
1470
  }
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) {
1471
+ if (this.store.data.websocket) {
1321
1472
  this.websocket = new WebSocket(this.store.data.websocket, this.store.data.websocketProtocols);
1322
1473
  this.websocket.onmessage = function (e) {
1323
1474
  _this.socketCallback(e.data);
@@ -1334,13 +1485,6 @@ var Meta2d = /** @class */ (function () {
1334
1485
  this.websocket.close();
1335
1486
  this.websocket = undefined;
1336
1487
  }
1337
- if (this.websockets) {
1338
- this.websockets.forEach(function (websocket) {
1339
- websocket.onclose = undefined;
1340
- websocket.close();
1341
- websocket = undefined;
1342
- });
1343
- }
1344
1488
  };
1345
1489
  Meta2d.prototype.connectMqtt = function (params) {
1346
1490
  var _this = this;
@@ -1350,22 +1494,7 @@ var Meta2d = /** @class */ (function () {
1350
1494
  this.store.data.mqttTopics = params.mqttTopics;
1351
1495
  this.store.data.mqttOptions = params.mqttOptions;
1352
1496
  }
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) {
1497
+ if (this.store.data.mqtt) {
1369
1498
  if (this.store.data.mqttOptions.clientId &&
1370
1499
  !this.store.data.mqttOptions.customClientId) {
1371
1500
  this.store.data.mqttOptions.clientId = s8();
@@ -1382,10 +1511,6 @@ var Meta2d = /** @class */ (function () {
1382
1511
  Meta2d.prototype.closeMqtt = function () {
1383
1512
  var _a;
1384
1513
  (_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
1514
  };
1390
1515
  Meta2d.prototype.connectHttp = function () {
1391
1516
  var _this = this;
@@ -1500,11 +1625,93 @@ var Meta2d = /** @class */ (function () {
1500
1625
  _httpTimer = undefined;
1501
1626
  });
1502
1627
  };
1628
+ Meta2d.prototype.connectNetwork = function () {
1629
+ var _this = this;
1630
+ this.closeNetwork();
1631
+ var networks = this.store.data.networks;
1632
+ if (networks) {
1633
+ var mqttIndex_1 = 0;
1634
+ this.mqttClients = [];
1635
+ var websocketIndex_1 = 0;
1636
+ this.websockets = [];
1637
+ var https_1 = [];
1638
+ networks.forEach(function (net) {
1639
+ if (net.type === 'mqtt') {
1640
+ if (net.options.clientId && !net.options.customClientId) {
1641
+ net.options.clientId = s8();
1642
+ }
1643
+ _this.mqttClients[mqttIndex_1] = mqtt.connect(net.url, net.options);
1644
+ _this.mqttClients[mqttIndex_1].on('message', function (topic, message) {
1645
+ _this.socketCallback(message.toString(), topic);
1646
+ });
1647
+ if (net.topics) {
1648
+ _this.mqttClients[mqttIndex_1].subscribe(net.topics.split(','));
1649
+ }
1650
+ mqttIndex_1 += 1;
1651
+ }
1652
+ else if (net.type === 'websocket') {
1653
+ _this.websockets[websocketIndex_1] = new WebSocket(net.url, net.protocols);
1654
+ _this.websockets[websocketIndex_1].onmessage = function (e) {
1655
+ _this.socketCallback(e.data);
1656
+ };
1657
+ websocketIndex_1 += 1;
1658
+ }
1659
+ else {
1660
+ https_1.push(net);
1661
+ }
1662
+ });
1663
+ this.onNetworkConnect(https_1);
1664
+ }
1665
+ };
1666
+ Meta2d.prototype.onNetworkConnect = function (https) {
1667
+ var _this = this;
1668
+ this.networkTimer = setInterval(function () {
1669
+ https.forEach(function (item) { return __awaiter(_this, void 0, void 0, function () {
1670
+ var res, data;
1671
+ return __generator(this, function (_a) {
1672
+ switch (_a.label) {
1673
+ case 0:
1674
+ if (!item.url) return [3 /*break*/, 3];
1675
+ return [4 /*yield*/, fetch(item.url, {
1676
+ headers: item.headers,
1677
+ method: item.method,
1678
+ body: item.body,
1679
+ })];
1680
+ case 1:
1681
+ res = _a.sent();
1682
+ if (!res.ok) return [3 /*break*/, 3];
1683
+ return [4 /*yield*/, res.text()];
1684
+ case 2:
1685
+ data = _a.sent();
1686
+ this.socketCallback(data);
1687
+ _a.label = 3;
1688
+ case 3: return [2 /*return*/];
1689
+ }
1690
+ });
1691
+ }); });
1692
+ //模拟数据
1693
+ _this.store.data.mockData && _this.store.data.mockData();
1694
+ }, this.store.data.networkInterval || 1000);
1695
+ };
1696
+ Meta2d.prototype.closeNetwork = function () {
1697
+ this.mqttClients &&
1698
+ this.mqttClients.forEach(function (mqttClient) {
1699
+ mqttClient.end();
1700
+ });
1701
+ this.websockets &&
1702
+ this.websockets.forEach(function (websocket) {
1703
+ websocket.close();
1704
+ });
1705
+ this.mqttClients = undefined;
1706
+ this.websockets = undefined;
1707
+ clearInterval(this.networkTimer);
1708
+ this.networkTimer = undefined;
1709
+ };
1503
1710
  Meta2d.prototype.socketCallback = function (message, topic) {
1504
1711
  var _this = this;
1505
1712
  if (topic === void 0) { topic = ''; }
1506
1713
  this.store.emitter.emit('socket', { message: message, topic: topic });
1507
- if (this.socketFn && !this.socketFn(message, topic)) {
1714
+ if (this.socketFn && !this.socketFn(message, topic, { meta2d: this })) {
1508
1715
  return;
1509
1716
  }
1510
1717
  var data;
@@ -1688,6 +1895,68 @@ var Meta2d = /** @class */ (function () {
1688
1895
  Meta2d.prototype.clearDropdownList = function () {
1689
1896
  this.canvas.clearDropdownList();
1690
1897
  };
1898
+ Meta2d.prototype.judgeCondition = function (pen, key, condition) {
1899
+ var type = condition.type, target = condition.target, fnJs = condition.fnJs, fn = condition.fn, operator = condition.operator, valueType = condition.valueType;
1900
+ var can = false;
1901
+ if (type === 'fn') {
1902
+ //方法
1903
+ if (fn) {
1904
+ can = fn(pen, { meta2d: this });
1905
+ }
1906
+ else if (fnJs) {
1907
+ try {
1908
+ condition.fn = new Function('pen', 'context', fnJs);
1909
+ }
1910
+ catch (err) {
1911
+ console.error('Error: make function:', err);
1912
+ }
1913
+ if (condition.fn) {
1914
+ can = condition.fn(pen, { meta2d: this });
1915
+ }
1916
+ }
1917
+ }
1918
+ else {
1919
+ //TODO boolean类型 数字类型
1920
+ var value = condition.value;
1921
+ if (valueType === 'prop') {
1922
+ value = this.store.pens[target][condition.value];
1923
+ }
1924
+ switch (operator) {
1925
+ case '>':
1926
+ can = pen[key] > +value;
1927
+ break;
1928
+ case '>=':
1929
+ can = pen[key] >= +value;
1930
+ break;
1931
+ case '<':
1932
+ can = pen[key] < +value;
1933
+ break;
1934
+ case '<=':
1935
+ can = pen[key] <= +value;
1936
+ break;
1937
+ case '=':
1938
+ case '==':
1939
+ can = pen[key] == value;
1940
+ break;
1941
+ case '!=':
1942
+ can = pen[key] != value;
1943
+ break;
1944
+ case '[)':
1945
+ can = valueInRange(+pen[key], value);
1946
+ break;
1947
+ case '![)':
1948
+ can = !valueInRange(+pen[key], value);
1949
+ break;
1950
+ case '[]':
1951
+ can = valueInArray(+pen[key], value);
1952
+ break;
1953
+ case '![]':
1954
+ can = !valueInArray(+pen[key], value);
1955
+ break;
1956
+ }
1957
+ }
1958
+ return can;
1959
+ };
1691
1960
  Meta2d.prototype.pushChildren = function (parent, children) {
1692
1961
  var _this = this;
1693
1962
  var initUpdatePens = [deepClone(parent, true)];
@@ -1949,7 +2218,7 @@ var Meta2d = /** @class */ (function () {
1949
2218
  Meta2d.prototype.beSameByFirst = function (pens, attribute) {
1950
2219
  if (pens === void 0) { pens = this.store.data.pens; }
1951
2220
  var initPens = deepClone(pens); // 原 pens ,深拷贝一下
1952
- // 1. 得到第一个画笔的 宽高 字体大小
2221
+ // 1. 得到第一个画笔的 宽高
1953
2222
  var firstPen = pens[0];
1954
2223
  var _a = this.getPenRect(firstPen), width = _a.width, height = _a.height;
1955
2224
  for (var i = 1; i < pens.length; i++) {
@@ -1971,6 +2240,35 @@ var Meta2d = /** @class */ (function () {
1971
2240
  pens: pens,
1972
2241
  });
1973
2242
  };
2243
+ /**
2244
+ * 大小相同
2245
+ * @param pens 画笔们
2246
+ */
2247
+ Meta2d.prototype.beSameByLast = function (pens, attribute) {
2248
+ if (pens === void 0) { pens = this.store.data.pens; }
2249
+ var initPens = deepClone(pens); // 原 pens ,深拷贝一下
2250
+ // 1. 得到最后一个画笔的 宽高
2251
+ var lastPen = pens[pens.length - 1];
2252
+ var _a = this.getPenRect(lastPen), width = _a.width, height = _a.height;
2253
+ for (var i = 0; i < pens.length - 1; i++) {
2254
+ var pen = pens[i];
2255
+ if (attribute === 'width') {
2256
+ this.setValue({ id: pen.id, width: width }, { render: false, doEvent: false });
2257
+ }
2258
+ else if (attribute === 'height') {
2259
+ this.setValue({ id: pen.id, height: height }, { render: false, doEvent: false });
2260
+ }
2261
+ else {
2262
+ this.setValue({ id: pen.id, width: width, height: height }, { render: false, doEvent: false });
2263
+ }
2264
+ }
2265
+ this.render();
2266
+ this.pushHistory({
2267
+ type: EditType.Update,
2268
+ initPens: initPens,
2269
+ pens: pens,
2270
+ });
2271
+ };
1974
2272
  /**
1975
2273
  * 格式刷(样式相同,大小无需一致。)
1976
2274
  * @param pens 画笔们
@@ -2093,6 +2391,27 @@ var Meta2d = /** @class */ (function () {
2093
2391
  pens: pens,
2094
2392
  });
2095
2393
  };
2394
+ /**
2395
+ * 对齐画笔,基于最后选中的画笔
2396
+ * @param align 左对齐,右对齐,上对齐,下对齐,居中对齐
2397
+ * @param pens
2398
+ */
2399
+ Meta2d.prototype.alignNodesByLast = function (align, pens) {
2400
+ if (pens === void 0) { pens = this.store.data.pens; }
2401
+ var initPens = deepClone(pens); // 原 pens ,深拷贝一下
2402
+ var lastPen = pens[pens.length - 1];
2403
+ var rect = this.getPenRect(lastPen);
2404
+ for (var i = 0; i < pens.length - 1; i++) {
2405
+ var pen = pens[i];
2406
+ this.alignPen(align, pen, rect);
2407
+ }
2408
+ this.render();
2409
+ this.pushHistory({
2410
+ type: EditType.Update,
2411
+ initPens: initPens,
2412
+ pens: pens,
2413
+ });
2414
+ };
2096
2415
  /**
2097
2416
  * 将画笔参照 rect 进行 align 对齐
2098
2417
  * @param align 左对齐,右对齐,上对齐,下对齐,居中对齐
@@ -2285,11 +2604,10 @@ var Meta2d = /** @class */ (function () {
2285
2604
  /**
2286
2605
  * 将该画笔置顶,即放到数组最后,最后绘制即在顶部
2287
2606
  * @param pen pen 置顶的画笔
2288
- * @param pens 画笔们,注意 pen 必须在该数组内才有效
2289
2607
  */
2290
- Meta2d.prototype.top = function (pen, pens) {
2608
+ Meta2d.prototype.top = function (pen) {
2291
2609
  var _this = this;
2292
- if (pens === void 0) { pens = this.store.data.pens; }
2610
+ var pens = this.store.data.pens;
2293
2611
  // 获取它包含它的子节点
2294
2612
  var allIds = __spreadArray(__spreadArray([], __read(getAllChildren(pen, this.store)), false), [pen], false).map(function (p) { return p.id; });
2295
2613
  var allPens = pens.filter(function (p) { return allIds.includes(p.id); });
@@ -2314,8 +2632,8 @@ var Meta2d = /** @class */ (function () {
2314
2632
  * 该画笔置底,即放到数组最前,最后绘制即在底部
2315
2633
  * @param pens 画笔们,注意 pen 必须在该数组内才有效
2316
2634
  */
2317
- Meta2d.prototype.bottom = function (pen, pens) {
2318
- if (pens === void 0) { pens = this.store.data.pens; }
2635
+ Meta2d.prototype.bottom = function (pen) {
2636
+ var pens = this.store.data.pens;
2319
2637
  var allIds = __spreadArray(__spreadArray([], __read(getAllChildren(pen, this.store)), false), [pen], false).map(function (p) { return p.id; });
2320
2638
  var allPens = pens.filter(function (p) { return allIds.includes(p.id); });
2321
2639
  var _loop_3 = function (i) {
@@ -2400,28 +2718,86 @@ var Meta2d = /** @class */ (function () {
2400
2718
  };
2401
2719
  /**
2402
2720
  * 该画笔上移,即把该画笔在数组中的位置向后移动一个
2403
- * @param pens 画笔们,注意 pen 必须在该数组内才有效
2721
+ * @param pen 画笔
2404
2722
  */
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]);
2723
+ Meta2d.prototype.up = function (pen) {
2724
+ var pens = this.store.data.pens;
2725
+ if (pen.children) {
2726
+ //组合图元
2727
+ var preMovePens = __spreadArray(__spreadArray([], __read(getAllChildren(pen, this.store)), false), [pen], false);
2728
+ //先保证组合图元的顺序正确。
2729
+ var orderPens = [];
2730
+ var _loop_5 = function (index) {
2731
+ var _pen = pens[index];
2732
+ if (preMovePens.findIndex(function (p) { return p.id === _pen.id; }) !== -1) {
2733
+ _pen.temIndex = index;
2734
+ orderPens.push(_pen);
2735
+ }
2736
+ };
2737
+ for (var index = 0; index < pens.length; index++) {
2738
+ _loop_5(index);
2739
+ }
2740
+ var lastIndex_1 = -1;
2741
+ var offset_1 = 0;
2742
+ orderPens.forEach(function (_pen) {
2743
+ _pen.temIndex -= offset_1;
2744
+ pens.splice(_pen.temIndex, 1);
2745
+ offset_1 += 1;
2746
+ lastIndex_1 = _pen.temIndex;
2747
+ delete _pen.temIndex;
2748
+ });
2749
+ pens.splice.apply(pens, __spreadArray([lastIndex_1 + 1, 0], __read(preMovePens), false));
2750
+ }
2751
+ else {
2752
+ var index = pens.findIndex(function (p) { return p.id === pen.id; });
2753
+ if (index > -1 && index !== pens.length - 1) {
2754
+ pens.splice(index + 2, 0, pens[index]);
2755
+ pens.splice(index, 1);
2756
+ this.initImageCanvas([pen]);
2757
+ }
2412
2758
  }
2413
2759
  };
2414
2760
  /**
2415
2761
  * 该画笔下移,即把该画笔在该数组中的位置前移一个
2416
- * @param pens 画笔们,注意 pen 必须在该数组内才有效
2762
+ * @param pen 画笔
2417
2763
  */
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]);
2764
+ Meta2d.prototype.down = function (pen) {
2765
+ var pens = this.store.data.pens;
2766
+ if (pen.children) {
2767
+ //组合图元
2768
+ var preMovePens = __spreadArray(__spreadArray([], __read(getAllChildren(pen, this.store)), false), [pen], false);
2769
+ //先保证组合图元的顺序正确。
2770
+ var orderPens = [];
2771
+ var _loop_6 = function (index) {
2772
+ var _pen = pens[index];
2773
+ if (preMovePens.findIndex(function (p) { return p.id === _pen.id; }) !== -1) {
2774
+ _pen.temIndex = index;
2775
+ orderPens.push(_pen);
2776
+ }
2777
+ };
2778
+ for (var index = 0; index < pens.length; index++) {
2779
+ _loop_6(index);
2780
+ }
2781
+ var firstIndex_1 = -1;
2782
+ var offset_2 = 0;
2783
+ orderPens.forEach(function (_pen, index) {
2784
+ _pen.temIndex -= offset_2;
2785
+ pens.splice(_pen.temIndex, 1);
2786
+ offset_2 += 1;
2787
+ if (index === 0) {
2788
+ firstIndex_1 = _pen.temIndex;
2789
+ }
2790
+ delete _pen.temIndex;
2791
+ });
2792
+ pens.splice.apply(pens, __spreadArray([firstIndex_1 - 1, 0], __read(preMovePens), false));
2793
+ }
2794
+ else {
2795
+ var index = pens.findIndex(function (p) { return p.id === pen.id; });
2796
+ if (index > -1 && index !== 0) {
2797
+ pens.splice(index - 1, 0, pens[index]);
2798
+ pens.splice(index + 1, 1);
2799
+ this.initImageCanvas([pen]);
2800
+ }
2425
2801
  }
2426
2802
  };
2427
2803
  Meta2d.prototype.setLayer = function (pen, toIndex, pens) {
@@ -2498,7 +2874,7 @@ var Meta2d = /** @class */ (function () {
2498
2874
  lines.forEach(function (line) {
2499
2875
  var e_7, _a;
2500
2876
  var lineNextNode = _this.nextNode(line);
2501
- var _loop_5 = function (node) {
2877
+ var _loop_7 = function (node) {
2502
2878
  var have = nextNodes_1.find(function (next) { return next.id === node.id; });
2503
2879
  // 3. 不重复的才加进去
2504
2880
  !have && nextNodes_1.push(node);
@@ -2506,7 +2882,7 @@ var Meta2d = /** @class */ (function () {
2506
2882
  try {
2507
2883
  for (var lineNextNode_1 = __values(lineNextNode), lineNextNode_1_1 = lineNextNode_1.next(); !lineNextNode_1_1.done; lineNextNode_1_1 = lineNextNode_1.next()) {
2508
2884
  var node = lineNextNode_1_1.value;
2509
- _loop_5(node);
2885
+ _loop_7(node);
2510
2886
  }
2511
2887
  }
2512
2888
  catch (e_7_1) { e_7 = { error: e_7_1 }; }
@@ -2539,7 +2915,7 @@ var Meta2d = /** @class */ (function () {
2539
2915
  lines.forEach(function (line) {
2540
2916
  var e_8, _a;
2541
2917
  var linePreNode = _this.previousNode(line);
2542
- var _loop_6 = function (node) {
2918
+ var _loop_8 = function (node) {
2543
2919
  var have = preNodes_1.find(function (pre) { return pre.id === node.id; });
2544
2920
  // 3. 不重复的才加进去
2545
2921
  !have && preNodes_1.push(node);
@@ -2547,7 +2923,7 @@ var Meta2d = /** @class */ (function () {
2547
2923
  try {
2548
2924
  for (var linePreNode_1 = __values(linePreNode), linePreNode_1_1 = linePreNode_1.next(); !linePreNode_1_1.done; linePreNode_1_1 = linePreNode_1.next()) {
2549
2925
  var node = linePreNode_1_1.value;
2550
- _loop_6(node);
2926
+ _loop_8(node);
2551
2927
  }
2552
2928
  }
2553
2929
  catch (e_8_1) { e_8 = { error: e_8_1 }; }