@meta2d/core 1.0.53 → 1.0.54

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.d.ts CHANGED
@@ -185,8 +185,10 @@ export declare class Meta2d {
185
185
  undo(): void;
186
186
  redo(): void;
187
187
  listenSocket(): boolean;
188
+ websocketTimes: number;
188
189
  connectWebsocket(websocket?: string): void;
189
190
  closeWebsocket(): void;
191
+ mqttTimes: number;
190
192
  connectMqtt(params?: {
191
193
  mqtt: string;
192
194
  mqttTopics: string;
@@ -207,6 +209,7 @@ export declare class Meta2d {
207
209
  updateTimer: any;
208
210
  updateTimerList: any[];
209
211
  connectNetwork(): void;
212
+ connectNetWebSocket(net: Network): void;
210
213
  randomString(e: number): string;
211
214
  mockValue(data: any): any;
212
215
  dataMock(): void;
package/src/core.js CHANGED
@@ -83,7 +83,7 @@ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
83
83
  };
84
84
  import { commonAnchors, commonPens, cube } from './diagrams';
85
85
  import { Canvas } from './canvas';
86
- import { calcInView, calcTextDrawRect, calcTextLines, calcTextRect, facePen, formatAttrs, getAllChildren, getFromAnchor, getParent, getToAnchor, getWords, LockState, PenType, renderPenRaw, setElemPosition, connectLine, nearestAnchor, setChildValue, isAncestor, isShowChild, CanvasLayer, validationPlugin, setLifeCycleFunc, getAllFollowers } from './pen';
86
+ import { calcInView, calcTextDrawRect, calcTextLines, calcTextRect, facePen, formatAttrs, getAllChildren, getFromAnchor, getParent, getToAnchor, getWords, LockState, PenType, renderPenRaw, setElemPosition, connectLine, nearestAnchor, setChildValue, isAncestor, isShowChild, CanvasLayer, validationPlugin, setLifeCycleFunc, getAllFollowers, isInteraction } from './pen';
87
87
  import { rotatePoint } from './point';
88
88
  import { clearStore, EditType, globalStore, register, registerAnchors, registerCanvasDraw, useStore, } from './store';
89
89
  import { formatPadding, loadCss, s8, valueInArray, valueInRange, } from './utils';
@@ -112,6 +112,8 @@ var Meta2d = /** @class */ (function () {
112
112
  this.register = register;
113
113
  this.registerCanvasDraw = registerCanvasDraw;
114
114
  this.registerAnchors = registerAnchors;
115
+ this.websocketTimes = 0;
116
+ this.mqttTimes = 0;
115
117
  this.httpTimerList = [];
116
118
  this.updateTimerList = [];
117
119
  this.onEvent = function (eventName, e) {
@@ -184,6 +186,12 @@ var Meta2d = /** @class */ (function () {
184
186
  }
185
187
  _this.navigatorTo(e.params);
186
188
  break;
189
+ case 'input':
190
+ _this.store.data.locked && e && (!e.disabled) && _this.doEvent(e, eventName);
191
+ break;
192
+ case 'change':
193
+ _this.store.data.locked && e && (!e.disabled) && _this.doEvent(e, eventName);
194
+ break;
187
195
  }
188
196
  if (_this.store.messageEvents[eventName]) {
189
197
  _this.store.messageEvents[eventName].forEach(function (item) {
@@ -425,7 +433,7 @@ var Meta2d = /** @class */ (function () {
425
433
  return _this.judgeCondition(pen, condition.key, condition);
426
434
  });
427
435
  }
428
- else if (trigger.conditionType === 'or') {
436
+ else if (state.conditionType === 'or') {
429
437
  flag = state.conditions.some(function (condition) {
430
438
  return _this.judgeCondition(pen, condition.key, condition);
431
439
  });
@@ -1406,8 +1414,10 @@ var Meta2d = /** @class */ (function () {
1406
1414
  this.stopPencil();
1407
1415
  //恢复可选状态
1408
1416
  this.store.data.pens.forEach(function (pen) {
1417
+ var _a;
1409
1418
  if (pen.externElement === true) {
1410
- pen.onMove && pen.onMove(pen);
1419
+ // pen.onMove && pen.onMove(pen);
1420
+ ((_a = pen.calculative.singleton) === null || _a === void 0 ? void 0 : _a.div) && setElemPosition(pen, pen.calculative.singleton.div);
1411
1421
  }
1412
1422
  });
1413
1423
  if (lock > 0) {
@@ -1832,6 +1842,7 @@ var Meta2d = /** @class */ (function () {
1832
1842
  var childRect = calcRelativeRect(pen.calculative.worldRect, rect);
1833
1843
  Object.assign(pen, childRect);
1834
1844
  pen.locked = (_a = pen.lockedOnCombine) !== null && _a !== void 0 ? _a : LockState.DisableMove;
1845
+ pen.locked = (pen.interaction || isInteraction.includes(pen.name)) ? 0 : pen.locked;
1835
1846
  });
1836
1847
  //将组合后的父节点置底
1837
1848
  this.store.data.pens.splice(minIndex, 0, parent);
@@ -2057,7 +2068,18 @@ var Meta2d = /** @class */ (function () {
2057
2068
  url: _this.store.data.websocket,
2058
2069
  });
2059
2070
  };
2071
+ this.websocket.onerror = function (error) {
2072
+ _this.store.emitter.emit('error', { type: 'websocket', error: error });
2073
+ };
2060
2074
  this.websocket.onclose = function () {
2075
+ if (_this.store.options.reconnetTimes) {
2076
+ _this.websocketTimes++;
2077
+ if (_this.websocketTimes >= _this.store.options.reconnetTimes) {
2078
+ _this.websocketTimes = 0;
2079
+ _this.closeWebsocket();
2080
+ return;
2081
+ }
2082
+ }
2061
2083
  console.info('Canvas websocket closed and reconneting...');
2062
2084
  _this.connectWebsocket();
2063
2085
  };
@@ -2102,6 +2124,18 @@ var Meta2d = /** @class */ (function () {
2102
2124
  url: _this.store.data.mqtt,
2103
2125
  });
2104
2126
  });
2127
+ this.mqttClient.on('error', function (error) {
2128
+ _this.store.emitter.emit('error', { type: 'mqtt', error: error });
2129
+ });
2130
+ this.mqttClient.on('close', function () {
2131
+ if (_this.store.options.reconnetTimes) {
2132
+ _this.mqttTimes++;
2133
+ if (_this.mqttTimes >= _this.store.options.reconnetTimes) {
2134
+ _this.mqttTimes = 0;
2135
+ _this.closeMqtt();
2136
+ }
2137
+ }
2138
+ });
2105
2139
  if (this.store.data.mqttTopics) {
2106
2140
  this.mqttClient.subscribe(this.store.data.mqttTopics.split(','));
2107
2141
  }
@@ -2130,10 +2164,19 @@ var Meta2d = /** @class */ (function () {
2130
2164
  }
2131
2165
  https.forEach(function (item, index) {
2132
2166
  if (item.http) {
2167
+ item.times = 0;
2133
2168
  _this.httpTimerList[index] = setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
2134
2169
  return __generator(this, function (_a) {
2135
2170
  // 默认每一秒请求一次
2136
2171
  this.oldRequestHttp(item);
2172
+ if (this.store.options.reconnetTimes) {
2173
+ item.times++;
2174
+ if (item.times >= this.store.options.reconnetTimes) {
2175
+ item.times = 0;
2176
+ clearInterval(this.httpTimerList[index]);
2177
+ this.httpTimerList[index] = undefined;
2178
+ }
2179
+ }
2137
2180
  return [2 /*return*/];
2138
2181
  });
2139
2182
  }); }, item.httpTimeInterval || 1000);
@@ -2172,7 +2215,7 @@ var Meta2d = /** @class */ (function () {
2172
2215
  switch (_a.label) {
2173
2216
  case 0:
2174
2217
  req = deepClone(_req);
2175
- if (!req.http) return [3 /*break*/, 3];
2218
+ if (!req.http) return [3 /*break*/, 4];
2176
2219
  return [4 /*yield*/, fetch(req.http, {
2177
2220
  headers: req.httpHeaders,
2178
2221
  method: req.method || 'GET',
@@ -2185,8 +2228,11 @@ var Meta2d = /** @class */ (function () {
2185
2228
  case 2:
2186
2229
  data = _a.sent();
2187
2230
  this.socketCallback(data, { type: 'http', url: req.http });
2188
- _a.label = 3;
2189
- case 3: return [2 /*return*/];
2231
+ return [3 /*break*/, 4];
2232
+ case 3:
2233
+ this.store.emitter.emit('error', { type: 'http', error: res });
2234
+ _a.label = 4;
2235
+ case 4: return [2 /*return*/];
2190
2236
  }
2191
2237
  });
2192
2238
  });
@@ -2263,9 +2309,11 @@ var Meta2d = /** @class */ (function () {
2263
2309
  networks.forEach(function (net) {
2264
2310
  if (net.type === 'subscribe') {
2265
2311
  if (net.protocol === 'mqtt') {
2312
+ net.index = mqttIndex_1;
2266
2313
  if (net.options.clientId && !net.options.customClientId) {
2267
2314
  net.options.clientId = s8();
2268
2315
  }
2316
+ net.times = 0;
2269
2317
  _this.mqttClients[mqttIndex_1] = mqtt.connect(net.url, net.options);
2270
2318
  _this.mqttClients[mqttIndex_1].on('message', function (topic, message) {
2271
2319
  _this.socketCallback(message.toString(), {
@@ -2274,16 +2322,48 @@ var Meta2d = /** @class */ (function () {
2274
2322
  url: net.url,
2275
2323
  });
2276
2324
  });
2325
+ _this.mqttClients[mqttIndex_1].on('error', function (error) {
2326
+ _this.store.emitter.emit('error', { type: 'mqtt', error: error });
2327
+ });
2328
+ _this.mqttClients[mqttIndex_1].on('close', function () {
2329
+ var _a;
2330
+ if (_this.store.options.reconnetTimes) {
2331
+ net.times++;
2332
+ if (net.times >= _this.store.options.reconnetTimes) {
2333
+ net.times = 0;
2334
+ _this.mqttClients && ((_a = _this.mqttClients[net.index]) === null || _a === void 0 ? void 0 : _a.end());
2335
+ }
2336
+ }
2337
+ });
2277
2338
  if (net.topics) {
2278
2339
  _this.mqttClients[mqttIndex_1].subscribe(net.topics.split(','));
2279
2340
  }
2280
2341
  mqttIndex_1 += 1;
2281
2342
  }
2282
2343
  else if (net.protocol === 'websocket') {
2283
- _this.websockets[websocketIndex_1] = new WebSocket(net.url, net.protocols || undefined);
2284
- _this.websockets[websocketIndex_1].onmessage = function (e) {
2285
- _this.socketCallback(e.data, { type: 'websocket', url: net.url });
2286
- };
2344
+ net.index = websocketIndex_1;
2345
+ _this.connectNetWebSocket(net);
2346
+ // this.websockets[websocketIndex] = new WebSocket(
2347
+ // net.url,
2348
+ // net.protocols || undefined
2349
+ // );
2350
+ // this.websockets[websocketIndex].onmessage = (e) => {
2351
+ // this.socketCallback(e.data, { type: 'websocket', url: net.url });
2352
+ // };
2353
+ // this.websockets[websocketIndex].onerror = (error) => {
2354
+ // this.store.emitter.emit('error', { type: 'websocket', error });
2355
+ // };
2356
+ // this.websockets[websocketIndex].onclose = () => {
2357
+ // if (this.store.options.reconnetTimes) {
2358
+ // net.times++;
2359
+ // if (net.times >= this.store.options.reconnetTimes) {
2360
+ // net.times = 0;
2361
+ // this.websockets[net.index]?.close();
2362
+ // return;
2363
+ // }
2364
+ // }
2365
+ // console.info('Canvas websocket closed and reconneting...');
2366
+ // };
2287
2367
  websocketIndex_1 += 1;
2288
2368
  }
2289
2369
  else if (net.protocol === 'http') {
@@ -2300,6 +2380,39 @@ var Meta2d = /** @class */ (function () {
2300
2380
  }
2301
2381
  this.onNetworkConnect(https);
2302
2382
  };
2383
+ Meta2d.prototype.connectNetWebSocket = function (net) {
2384
+ var _this = this;
2385
+ var _a;
2386
+ if (this.websockets[net.index]) {
2387
+ this.websockets[net.index].onclose = undefined;
2388
+ (_a = this.websockets[net.index]) === null || _a === void 0 ? void 0 : _a.close();
2389
+ this.websockets[net.index] = undefined;
2390
+ }
2391
+ this.websockets[net.index] = new WebSocket(net.url, net.protocols || undefined);
2392
+ this.websockets[net.index].onmessage = function (e) {
2393
+ _this.socketCallback(e.data, { type: 'websocket', url: net.url });
2394
+ };
2395
+ this.websockets[net.index].onerror = function (error) {
2396
+ _this.store.emitter.emit('error', { type: 'websocket', error: error });
2397
+ };
2398
+ this.websockets[net.index].onclose = function () {
2399
+ var _a;
2400
+ if (_this.store.options.reconnetTimes) {
2401
+ net.times++;
2402
+ if (net.times >= _this.store.options.reconnetTimes) {
2403
+ net.times = 0;
2404
+ _this.websockets[net.index].onclose = undefined;
2405
+ (_a = _this.websockets[net.index]) === null || _a === void 0 ? void 0 : _a.close();
2406
+ _this.websockets[net.index] = undefined;
2407
+ return;
2408
+ }
2409
+ }
2410
+ setTimeout(function () {
2411
+ console.info('Canvas websocket closed and reconneting...');
2412
+ _this.connectNetWebSocket(net);
2413
+ }, 2000);
2414
+ };
2415
+ };
2303
2416
  Meta2d.prototype.randomString = function (e) {
2304
2417
  e = e || 32;
2305
2418
  var t = 'ABCDEFGHJKMNPQRSTWXYZabcdefhijkmnprstwxyz2345678', a = t.length, n = '';
@@ -2479,8 +2592,9 @@ var Meta2d = /** @class */ (function () {
2479
2592
  if (Object.keys(_d_1).length) {
2480
2593
  var data = pen.onBeforeValue ? pen.onBeforeValue(pen, _d_1) : _d_1;
2481
2594
  this.canvas.updateValue(pen, data);
2482
- this.store.emitter.emit('valueUpdate', pen);
2595
+ // this.store.emitter.emit('valueUpdate', pen);
2483
2596
  (_a = pen.onValue) === null || _a === void 0 ? void 0 : _a.call(pen, pen);
2597
+ this.store.emitter.emit('valueUpdate', pen);
2484
2598
  }
2485
2599
  }
2486
2600
  };
@@ -2551,9 +2665,18 @@ var Meta2d = /** @class */ (function () {
2551
2665
  // }, this.store.data.networkInterval || 1000);
2552
2666
  // }
2553
2667
  https.forEach(function (_item, index) {
2668
+ _item.times = 0;
2554
2669
  _this.updateTimerList[index] = setInterval(function () { return __awaiter(_this, void 0, void 0, function () {
2555
2670
  return __generator(this, function (_a) {
2556
2671
  this.requestHttp(_item);
2672
+ if (this.store.options.reconnetTimes) {
2673
+ _item.times++;
2674
+ if (_item.times >= this.store.options.reconnetTimes) {
2675
+ _item.times = 0;
2676
+ clearInterval(this.updateTimerList[index]);
2677
+ this.updateTimerList[index] = undefined;
2678
+ }
2679
+ }
2557
2680
  return [2 /*return*/];
2558
2681
  });
2559
2682
  }); }, _item.interval || 1000);
@@ -2566,7 +2689,7 @@ var Meta2d = /** @class */ (function () {
2566
2689
  switch (_a.label) {
2567
2690
  case 0:
2568
2691
  req = deepClone(_req);
2569
- if (!req.url) return [3 /*break*/, 3];
2692
+ if (!req.url) return [3 /*break*/, 4];
2570
2693
  if (typeof req.headers === 'object') {
2571
2694
  for (i in req.headers) {
2572
2695
  if (typeof req.headers[i] === 'string') {
@@ -2599,8 +2722,11 @@ var Meta2d = /** @class */ (function () {
2599
2722
  case 2:
2600
2723
  data = _a.sent();
2601
2724
  this.socketCallback(data, { type: 'http', url: req.url });
2602
- _a.label = 3;
2603
- case 3: return [2 /*return*/];
2725
+ return [3 /*break*/, 4];
2726
+ case 3:
2727
+ this.store.emitter.emit('error', { type: 'http', error: res });
2728
+ _a.label = 4;
2729
+ case 4: return [2 /*return*/];
2604
2730
  }
2605
2731
  });
2606
2732
  });
@@ -2612,7 +2738,11 @@ var Meta2d = /** @class */ (function () {
2612
2738
  });
2613
2739
  this.websockets &&
2614
2740
  this.websockets.forEach(function (websocket) {
2615
- websocket.close();
2741
+ if (websocket) {
2742
+ websocket.onclose = undefined;
2743
+ websocket.close();
2744
+ websocket = undefined;
2745
+ }
2616
2746
  });
2617
2747
  this.mqttClients = undefined;
2618
2748
  this.websockets = undefined;