@meta2d/core 1.0.14 → 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
@@ -94,6 +94,7 @@ import { ViewMap } from './map';
94
94
  import * as mqtt from 'mqtt/dist/mqtt.min.js';
95
95
  import pkg from '../package.json';
96
96
  import { lockedError } from './utils/error';
97
+ import { Scroll } from './scroll';
97
98
  var Meta2d = /** @class */ (function () {
98
99
  function Meta2d(parent, opts) {
99
100
  var _this = this;
@@ -169,69 +170,100 @@ var Meta2d = /** @class */ (function () {
169
170
  }
170
171
  };
171
172
  this.doEvent = function (pen, eventName) {
172
- var _a;
173
+ var _a, _b;
173
174
  if (!pen) {
174
175
  return;
175
176
  }
176
177
  (_a = pen.events) === null || _a === void 0 ? void 0 : _a.forEach(function (event) {
177
178
  var _a;
178
- if (_this.events[event.action] && event.name === eventName) {
179
- var can = !((_a = event.where) === null || _a === void 0 ? void 0 : _a.type);
180
- if (event.where) {
181
- var _b = event.where, fn = _b.fn, fnJs = _b.fnJs, comparison = _b.comparison, key = _b.key, value = _b.value;
182
- if (fn) {
183
- 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);
184
183
  }
185
- else if (fnJs) {
186
- try {
187
- event.where.fn = new Function('pen', fnJs);
188
- }
189
- catch (err) {
190
- 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 });
191
193
  }
192
- if (event.where.fn) {
193
- 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
+ }
194
204
  }
195
- }
196
- else {
197
- switch (comparison) {
198
- case '>':
199
- can = pen[key] > +value;
200
- break;
201
- case '>=':
202
- can = pen[key] >= +value;
203
- break;
204
- case '<':
205
- can = pen[key] < +value;
206
- break;
207
- case '<=':
208
- can = pen[key] <= +value;
209
- break;
210
- case '=':
211
- case '==':
212
- can = pen[key] == value;
213
- break;
214
- case '!=':
215
- can = pen[key] != value;
216
- break;
217
- case '[)':
218
- can = valueInRange(+pen[key], value);
219
- break;
220
- case '![)':
221
- can = !valueInRange(+pen[key], value);
222
- break;
223
- case '[]':
224
- can = valueInArray(+pen[key], value);
225
- break;
226
- case '![]':
227
- can = !valueInArray(+pen[key], value);
228
- 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
+ }
229
239
  }
230
240
  }
241
+ can && _this.events[event.action](pen, event);
231
242
  }
232
- can && _this.events[event.action](pen, event);
233
243
  }
234
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
+ });
235
267
  // 事件冒泡,子执行完,父执行
236
268
  _this.doEvent(_this.store.pens[pen.parentId], eventName);
237
269
  };
@@ -307,6 +339,15 @@ var Meta2d = /** @class */ (function () {
307
339
  Meta2d.prototype.setOptions = function (opts) {
308
340
  if (opts === void 0) { opts = {}; }
309
341
  this.store.options = Object.assign(this.store.options, opts);
342
+ if (this.canvas && opts.scroll !== undefined) {
343
+ if (opts.scroll) {
344
+ !this.canvas.scroll && (this.canvas.scroll = new Scroll(this.canvas));
345
+ this.canvas.scroll.show();
346
+ }
347
+ else {
348
+ this.canvas.scroll.hide();
349
+ }
350
+ }
310
351
  };
311
352
  Meta2d.prototype.getOptions = function () {
312
353
  return this.store.options;
@@ -366,6 +407,10 @@ var Meta2d = /** @class */ (function () {
366
407
  console.warn('[meta2d] SetProps value is not an object');
367
408
  };
368
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
+ }
369
414
  if (!e.value || typeof e.value === 'string') {
370
415
  _this.startAnimate(e.value || [pen]);
371
416
  return;
@@ -415,13 +460,13 @@ var Meta2d = /** @class */ (function () {
415
460
  throw new Error('[meta2d] Function value must be string');
416
461
  }
417
462
  var fnJs = e.value;
418
- e.fn = new Function('pen', 'params', fnJs);
463
+ e.fn = new Function('pen', 'params', 'context', fnJs);
419
464
  }
420
465
  catch (err) {
421
466
  console.error('[meta2d]: Error on make a function:', err);
422
467
  }
423
468
  }
424
- (_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 });
425
470
  };
426
471
  this.events[EventAction.GlobalFn] = function (pen, e) {
427
472
  if (typeof e.value !== 'string') {
@@ -440,6 +485,7 @@ var Meta2d = /** @class */ (function () {
440
485
  _this.store.emitter.emit(e.value, {
441
486
  pen: pen,
442
487
  params: e.params,
488
+ eventName: e.name,
443
489
  });
444
490
  };
445
491
  this.events[EventAction.SendPropData] = function (pen, e) {
@@ -486,6 +532,51 @@ var Meta2d = /** @class */ (function () {
486
532
  }
487
533
  console.warn('[meta2d] SendVarData value is not an object');
488
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
+ }
489
580
  };
490
581
  Meta2d.prototype.doSendDataEvent = function (value, topics) {
491
582
  var _this = this;
@@ -511,6 +602,72 @@ var Meta2d = /** @class */ (function () {
511
602
  }
512
603
  this.store.emitter.emit('sendData', data);
513
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
+ };
514
671
  Meta2d.prototype.resize = function (width, height) {
515
672
  this.canvas.resize(width, height);
516
673
  this.render();
@@ -702,8 +859,8 @@ var Meta2d = /** @class */ (function () {
702
859
  var initJs = this.store.data.initJs;
703
860
  if (initJs && initJs.trim()) {
704
861
  try {
705
- var fn = new Function(initJs);
706
- fn();
862
+ var fn = new Function('context', initJs);
863
+ fn({ meta2d: this });
707
864
  }
708
865
  catch (e) {
709
866
  console.warn('initJs error', e);
@@ -893,7 +1050,7 @@ var Meta2d = /** @class */ (function () {
893
1050
  if (render === void 0) { render = true; }
894
1051
  this.canvas.setPenRect(pen, rect, render);
895
1052
  };
896
- Meta2d.prototype.startAnimate = function (idOrTagOrPens, index) {
1053
+ Meta2d.prototype.startAnimate = function (idOrTagOrPens, params) {
897
1054
  var _this = this;
898
1055
  this.stopAnimate(idOrTagOrPens);
899
1056
  var pens;
@@ -916,9 +1073,22 @@ var Meta2d = /** @class */ (function () {
916
1073
  pen.calculative.frameEnd += d;
917
1074
  }
918
1075
  else {
919
- if (index !== undefined &&
920
- pen.animations &&
921
- 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
+ }
922
1092
  var animate = deepClone(pen.animations[index]);
923
1093
  delete animate.name;
924
1094
  animate.currentAnimation = index;
@@ -1213,6 +1383,10 @@ var Meta2d = /** @class */ (function () {
1213
1383
  Meta2d.prototype.inactive = function () {
1214
1384
  this.canvas.inactive();
1215
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
+ };
1216
1390
  /**
1217
1391
  * 删除画笔
1218
1392
  * @param pens 需要删除的画笔们
@@ -1275,7 +1449,7 @@ var Meta2d = /** @class */ (function () {
1275
1449
  var socketFn = void 0;
1276
1450
  var socketCbJs = this.store.data.socketCbJs;
1277
1451
  if (socketCbJs) {
1278
- socketFn = new Function('e', 'topic', socketCbJs);
1452
+ socketFn = new Function('e', 'topic', 'context', socketCbJs);
1279
1453
  }
1280
1454
  if (!socketFn) {
1281
1455
  return false;
@@ -1295,7 +1469,7 @@ var Meta2d = /** @class */ (function () {
1295
1469
  this.store.data.websocket = websocket;
1296
1470
  }
1297
1471
  if (this.store.data.websocket) {
1298
- this.websocket = new WebSocket(this.store.data.websocket);
1472
+ this.websocket = new WebSocket(this.store.data.websocket, this.store.data.websocketProtocols);
1299
1473
  this.websocket.onmessage = function (e) {
1300
1474
  _this.socketCallback(e.data);
1301
1475
  };
@@ -1451,11 +1625,93 @@ var Meta2d = /** @class */ (function () {
1451
1625
  _httpTimer = undefined;
1452
1626
  });
1453
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
+ };
1454
1710
  Meta2d.prototype.socketCallback = function (message, topic) {
1455
1711
  var _this = this;
1456
1712
  if (topic === void 0) { topic = ''; }
1457
1713
  this.store.emitter.emit('socket', { message: message, topic: topic });
1458
- if (this.socketFn && !this.socketFn(message, topic)) {
1714
+ if (this.socketFn && !this.socketFn(message, topic, { meta2d: this })) {
1459
1715
  return;
1460
1716
  }
1461
1717
  var data;
@@ -1639,6 +1895,68 @@ var Meta2d = /** @class */ (function () {
1639
1895
  Meta2d.prototype.clearDropdownList = function () {
1640
1896
  this.canvas.clearDropdownList();
1641
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
+ };
1642
1960
  Meta2d.prototype.pushChildren = function (parent, children) {
1643
1961
  var _this = this;
1644
1962
  var initUpdatePens = [deepClone(parent, true)];
@@ -1805,8 +2123,9 @@ var Meta2d = /** @class */ (function () {
1805
2123
  * 宽度放大到屏幕尺寸,并滚动到最顶部
1806
2124
  *
1807
2125
  */
1808
- Meta2d.prototype.scrollView = function (viewPadding) {
2126
+ Meta2d.prototype.scrollView = function (viewPadding, pageMode) {
1809
2127
  if (viewPadding === void 0) { viewPadding = 10; }
2128
+ if (pageMode === void 0) { pageMode = false; }
1810
2129
  if (!this.hasView())
1811
2130
  return;
1812
2131
  //滚动状态下
@@ -1821,6 +2140,28 @@ var Meta2d = /** @class */ (function () {
1821
2140
  var ratio = (width - padding[1] - padding[3]) / rect.width;
1822
2141
  this.scale(ratio * this.store.data.scale);
1823
2142
  this.topView(padding[0]);
2143
+ if (pageMode) {
2144
+ this.canvas.scroll.changeMode();
2145
+ }
2146
+ };
2147
+ Meta2d.prototype.screenView = function (viewPadding, WorH) {
2148
+ if (viewPadding === void 0) { viewPadding = 10; }
2149
+ if (WorH === void 0) { WorH = true; }
2150
+ if (!this.hasView())
2151
+ return;
2152
+ var canvas = this.canvas.canvas;
2153
+ var width = canvas.offsetWidth, height = canvas.offsetHeight;
2154
+ this.resize(width, height);
2155
+ var padding = formatPadding(viewPadding);
2156
+ var rect = this.getRect();
2157
+ //默认宽度充满
2158
+ var ratio = (width - padding[1] - padding[3]) / rect.width;
2159
+ if (!WorH) {
2160
+ ratio = (height - padding[0] - padding[2]) / rect.height;
2161
+ }
2162
+ this.scale(ratio * this.store.data.scale);
2163
+ //height充满时是居中
2164
+ this.topView(padding[0]);
1824
2165
  };
1825
2166
  Meta2d.prototype.topView = function (paddingTop) {
1826
2167
  if (paddingTop === void 0) { paddingTop = 10; }
@@ -1877,7 +2218,7 @@ var Meta2d = /** @class */ (function () {
1877
2218
  Meta2d.prototype.beSameByFirst = function (pens, attribute) {
1878
2219
  if (pens === void 0) { pens = this.store.data.pens; }
1879
2220
  var initPens = deepClone(pens); // 原 pens ,深拷贝一下
1880
- // 1. 得到第一个画笔的 宽高 字体大小
2221
+ // 1. 得到第一个画笔的 宽高
1881
2222
  var firstPen = pens[0];
1882
2223
  var _a = this.getPenRect(firstPen), width = _a.width, height = _a.height;
1883
2224
  for (var i = 1; i < pens.length; i++) {
@@ -1899,6 +2240,35 @@ var Meta2d = /** @class */ (function () {
1899
2240
  pens: pens,
1900
2241
  });
1901
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
+ };
1902
2272
  /**
1903
2273
  * 格式刷(样式相同,大小无需一致。)
1904
2274
  * @param pens 画笔们
@@ -2021,6 +2391,27 @@ var Meta2d = /** @class */ (function () {
2021
2391
  pens: pens,
2022
2392
  });
2023
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
+ };
2024
2415
  /**
2025
2416
  * 将画笔参照 rect 进行 align 对齐
2026
2417
  * @param align 左对齐,右对齐,上对齐,下对齐,居中对齐
@@ -2213,11 +2604,10 @@ var Meta2d = /** @class */ (function () {
2213
2604
  /**
2214
2605
  * 将该画笔置顶,即放到数组最后,最后绘制即在顶部
2215
2606
  * @param pen pen 置顶的画笔
2216
- * @param pens 画笔们,注意 pen 必须在该数组内才有效
2217
2607
  */
2218
- Meta2d.prototype.top = function (pen, pens) {
2608
+ Meta2d.prototype.top = function (pen) {
2219
2609
  var _this = this;
2220
- if (pens === void 0) { pens = this.store.data.pens; }
2610
+ var pens = this.store.data.pens;
2221
2611
  // 获取它包含它的子节点
2222
2612
  var allIds = __spreadArray(__spreadArray([], __read(getAllChildren(pen, this.store)), false), [pen], false).map(function (p) { return p.id; });
2223
2613
  var allPens = pens.filter(function (p) { return allIds.includes(p.id); });
@@ -2242,8 +2632,8 @@ var Meta2d = /** @class */ (function () {
2242
2632
  * 该画笔置底,即放到数组最前,最后绘制即在底部
2243
2633
  * @param pens 画笔们,注意 pen 必须在该数组内才有效
2244
2634
  */
2245
- Meta2d.prototype.bottom = function (pen, pens) {
2246
- if (pens === void 0) { pens = this.store.data.pens; }
2635
+ Meta2d.prototype.bottom = function (pen) {
2636
+ var pens = this.store.data.pens;
2247
2637
  var allIds = __spreadArray(__spreadArray([], __read(getAllChildren(pen, this.store)), false), [pen], false).map(function (p) { return p.id; });
2248
2638
  var allPens = pens.filter(function (p) { return allIds.includes(p.id); });
2249
2639
  var _loop_3 = function (i) {
@@ -2328,28 +2718,86 @@ var Meta2d = /** @class */ (function () {
2328
2718
  };
2329
2719
  /**
2330
2720
  * 该画笔上移,即把该画笔在数组中的位置向后移动一个
2331
- * @param pens 画笔们,注意 pen 必须在该数组内才有效
2721
+ * @param pen 画笔
2332
2722
  */
2333
- Meta2d.prototype.up = function (pen, pens) {
2334
- if (pens === void 0) { pens = this.store.data.pens; }
2335
- var index = pens.findIndex(function (p) { return p.id === pen.id; });
2336
- if (index > -1 && index !== pens.length - 1) {
2337
- pens.splice(index + 2, 0, pens[index]);
2338
- pens.splice(index, 1);
2339
- 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
+ }
2340
2758
  }
2341
2759
  };
2342
2760
  /**
2343
2761
  * 该画笔下移,即把该画笔在该数组中的位置前移一个
2344
- * @param pens 画笔们,注意 pen 必须在该数组内才有效
2762
+ * @param pen 画笔
2345
2763
  */
2346
- Meta2d.prototype.down = function (pen, pens) {
2347
- if (pens === void 0) { pens = this.store.data.pens; }
2348
- var index = pens.findIndex(function (p) { return p.id === pen.id; });
2349
- if (index > -1 && index !== 0) {
2350
- pens.splice(index - 1, 0, pens[index]);
2351
- pens.splice(index + 1, 1);
2352
- 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
+ }
2353
2801
  }
2354
2802
  };
2355
2803
  Meta2d.prototype.setLayer = function (pen, toIndex, pens) {
@@ -2426,7 +2874,7 @@ var Meta2d = /** @class */ (function () {
2426
2874
  lines.forEach(function (line) {
2427
2875
  var e_7, _a;
2428
2876
  var lineNextNode = _this.nextNode(line);
2429
- var _loop_5 = function (node) {
2877
+ var _loop_7 = function (node) {
2430
2878
  var have = nextNodes_1.find(function (next) { return next.id === node.id; });
2431
2879
  // 3. 不重复的才加进去
2432
2880
  !have && nextNodes_1.push(node);
@@ -2434,7 +2882,7 @@ var Meta2d = /** @class */ (function () {
2434
2882
  try {
2435
2883
  for (var lineNextNode_1 = __values(lineNextNode), lineNextNode_1_1 = lineNextNode_1.next(); !lineNextNode_1_1.done; lineNextNode_1_1 = lineNextNode_1.next()) {
2436
2884
  var node = lineNextNode_1_1.value;
2437
- _loop_5(node);
2885
+ _loop_7(node);
2438
2886
  }
2439
2887
  }
2440
2888
  catch (e_7_1) { e_7 = { error: e_7_1 }; }
@@ -2467,7 +2915,7 @@ var Meta2d = /** @class */ (function () {
2467
2915
  lines.forEach(function (line) {
2468
2916
  var e_8, _a;
2469
2917
  var linePreNode = _this.previousNode(line);
2470
- var _loop_6 = function (node) {
2918
+ var _loop_8 = function (node) {
2471
2919
  var have = preNodes_1.find(function (pre) { return pre.id === node.id; });
2472
2920
  // 3. 不重复的才加进去
2473
2921
  !have && preNodes_1.push(node);
@@ -2475,7 +2923,7 @@ var Meta2d = /** @class */ (function () {
2475
2923
  try {
2476
2924
  for (var linePreNode_1 = __values(linePreNode), linePreNode_1_1 = linePreNode_1.next(); !linePreNode_1_1.done; linePreNode_1_1 = linePreNode_1.next()) {
2477
2925
  var node = linePreNode_1_1.value;
2478
- _loop_6(node);
2926
+ _loop_8(node);
2479
2927
  }
2480
2928
  }
2481
2929
  catch (e_8_1) { e_8 = { error: e_8_1 }; }