@meta2d/core 1.0.79 → 1.0.81

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
@@ -103,6 +103,7 @@ export declare class Meta2d {
103
103
  open(data?: Meta2dData, render?: boolean): void;
104
104
  cacheData(id: string): void;
105
105
  loadCacheData(id: string): void;
106
+ loadLineAnimateDraws(): void;
106
107
  statistics(): {
107
108
  图元总数量: number;
108
109
  图片图元数量: number;
@@ -148,6 +149,8 @@ export declare class Meta2d {
148
149
  register: typeof register;
149
150
  registerCanvasDraw: typeof registerCanvasDraw;
150
151
  registerAnchors: typeof registerAnchors;
152
+ registerLineAnimateDraws: (name: any, drawFunc: any) => void;
153
+ updateLineAnimateDraws(name: any, option: any): void;
151
154
  registerMoveDock(dock: (store: Meta2dStore, rect: Rect, pens: Pen[], offset: Point) => {
152
155
  xDock: Point;
153
156
  yDock: Point;
@@ -278,6 +281,7 @@ export declare class Meta2d {
278
281
  topic?: string;
279
282
  url?: string;
280
283
  method?: string;
284
+ name?: string;
281
285
  }): void;
282
286
  setDatas(datas: {
283
287
  dataId?: string;
package/src/core.js CHANGED
@@ -229,7 +229,16 @@ export class Meta2d {
229
229
  initEventFns() {
230
230
  this.events[EventAction.Link] = (pen, e) => {
231
231
  if (window && e.value && typeof e.value === 'string') {
232
- window.open(e.value, e.params ?? '_blank');
232
+ let url = e.value;
233
+ if (url.includes('${')) {
234
+ let keys = url.match(/\$\{([^}]+)\}/g)?.map(m => m.slice(2, -1));
235
+ if (keys) {
236
+ keys?.forEach((key) => {
237
+ url = url.replace(`\${${key}}`, pen[key]);
238
+ });
239
+ }
240
+ }
241
+ window.open(url, e.params ?? '_blank');
233
242
  return;
234
243
  }
235
244
  console.warn('[meta2d] Link param is not a string');
@@ -239,13 +248,34 @@ export class Meta2d {
239
248
  const value = e.value;
240
249
  if (value && typeof value === 'object') {
241
250
  const pens = e.params ? this.find(e.params) : this.find(pen.id);
251
+ const _value = {};
252
+ for (let key in value) {
253
+ if (value[key]?.id) {
254
+ _value[key] = this.store.pens[value[key].id]?.[value[key].key];
255
+ }
256
+ else {
257
+ if (typeof value[key] === 'string' && value[key].includes('${')) {
258
+ let __value = value[key];
259
+ let keys = __value.match(/\$\{([^}]+)\}/g)?.map(m => m.slice(2, -1));
260
+ if (keys) {
261
+ keys.forEach((key) => {
262
+ __value = __value.replace(`\${${key}}`, pen[key] || this.getDynamicParam(key));
263
+ });
264
+ }
265
+ _value[key] = __value;
266
+ }
267
+ else {
268
+ _value[key] = value[key];
269
+ }
270
+ }
271
+ }
242
272
  pens.forEach((pen) => {
243
- if (value.hasOwnProperty('visible')) {
244
- if (pen.visible !== value.visible) {
245
- this.setVisible(pen, value.visible);
273
+ if (_value.hasOwnProperty('visible')) {
274
+ if (pen.visible !== _value.visible) {
275
+ this.setVisible(pen, _value.visible);
246
276
  }
247
277
  }
248
- this.setValue({ id: pen.id, ...value }, { render: false, doEvent: false });
278
+ this.setValue({ id: pen.id, ..._value }, { render: false, doEvent: false });
249
279
  });
250
280
  this.render();
251
281
  return;
@@ -526,7 +556,19 @@ export class Meta2d {
526
556
  value[item.prop] = pen[item.key];
527
557
  }
528
558
  else {
529
- value[item.prop] = this.convertType(item.value, item.type);
559
+ if (typeof item.value === 'string' && item.value.includes('${')) {
560
+ let _value = item.value;
561
+ let keys = _value.match(/\$\{([^}]+)\}/g)?.map(m => m.slice(2, -1));
562
+ if (keys) {
563
+ keys.forEach((key) => {
564
+ _value = _value.replace(`\${${key}}`, this.getDynamicParam(key));
565
+ });
566
+ }
567
+ value[item.prop] = _value;
568
+ }
569
+ else {
570
+ value[item.prop] = this.convertType(item.value, item.type);
571
+ }
530
572
  }
531
573
  }
532
574
  });
@@ -876,6 +918,7 @@ export class Meta2d {
876
918
  this.initBindDatas();
877
919
  this.initBinds();
878
920
  this.doInitFn();
921
+ this.loadLineAnimateDraws();
879
922
  this.initMessageEvents();
880
923
  this.initGlobalTriggers();
881
924
  this.startAnimate();
@@ -954,6 +997,12 @@ export class Meta2d {
954
997
  });
955
998
  this.render();
956
999
  }
1000
+ loadLineAnimateDraws() {
1001
+ globalStore.lineAnimateDraws = {};
1002
+ Object.entries(this.store.data.lineAnimateDraws).forEach(([key, drawFunc]) => {
1003
+ globalStore.lineAnimateDraws[key] = eval(drawFunc);
1004
+ });
1005
+ }
957
1006
  statistics() {
958
1007
  const num = this.store.data.pens.length;
959
1008
  const imgNum = this.store.data.pens.filter((pen) => pen.image).length;
@@ -1320,6 +1369,22 @@ export class Meta2d {
1320
1369
  register = register;
1321
1370
  registerCanvasDraw = registerCanvasDraw;
1322
1371
  registerAnchors = registerAnchors;
1372
+ registerLineAnimateDraws = (name, drawFunc) => {
1373
+ drawFunc = typeof drawFunc === 'string' ? drawFunc : drawFunc.toString();
1374
+ this.store.data.lineAnimateDraws[name] = drawFunc;
1375
+ // 同步到store
1376
+ globalStore.lineAnimateDraws[name] = eval(drawFunc);
1377
+ };
1378
+ updateLineAnimateDraws(name, option) {
1379
+ if (!option)
1380
+ return;
1381
+ delete this.store.data.lineAnimateDraws[name];
1382
+ delete globalStore.lineAnimateDraws[name];
1383
+ if (option === -1) { // -1 表示删除
1384
+ return;
1385
+ }
1386
+ this.registerLineAnimateDraws(option.name || name, option.code);
1387
+ }
1323
1388
  // customeDock = (store, rect, pens, offset) => {xDock, yDock}
1324
1389
  // customDock return:
1325
1390
  // {
@@ -1469,6 +1534,7 @@ export class Meta2d {
1469
1534
  pen.currentAnimation = undefined;
1470
1535
  pen.calculative.pause = undefined;
1471
1536
  pen.calculative.start = undefined;
1537
+ pen.calculative.cycleStart = undefined;
1472
1538
  pen.calculative.duration = undefined;
1473
1539
  pen.calculative.animatePos = 0;
1474
1540
  this.store.animates.delete(pen);
@@ -1885,6 +1951,9 @@ export class Meta2d {
1885
1951
  ...Object.keys(this.store.bind),
1886
1952
  ...Object.keys(this.store.bindDatas),
1887
1953
  ];
1954
+ Object.entries(globalStore.lineAnimateDraws).forEach(([key, drawFunc]) => {
1955
+ data.lineAnimateDraws[key] = drawFunc.toString();
1956
+ });
1888
1957
  return data;
1889
1958
  }
1890
1959
  copy(pens) {
@@ -2291,7 +2360,7 @@ export class Meta2d {
2291
2360
  connectSSE(net) {
2292
2361
  this.eventSources[net.index] = new EventSource(net.url, { withCredentials: net.withCredentials });
2293
2362
  this.eventSources[net.index].onmessage = (e) => {
2294
- this.socketCallback(e.data, { type: 'SSE', url: net.url });
2363
+ this.socketCallback(e.data, { type: 'SSE', url: net.url, name: net.name });
2295
2364
  };
2296
2365
  this.eventSources[net.index].onerror = (error) => {
2297
2366
  this.store.emitter.emit('error', { type: 'SSE', error });
@@ -2320,12 +2389,30 @@ export class Meta2d {
2320
2389
  }
2321
2390
  }
2322
2391
  net.times = 0;
2323
- this.mqttClients[net.index] = mqtt.connect(url, net.options);
2392
+ let options = deepClone(net.options);
2393
+ if (options?.username && options.username.includes('${')) {
2394
+ let keys = options.username.match(/\$\{([^}]+)\}/g)?.map(m => m.slice(2, -1));
2395
+ if (keys) {
2396
+ keys.forEach((key) => {
2397
+ options.username = options.username.replace(`\${${key}}`, this.getDynamicParam(key));
2398
+ });
2399
+ }
2400
+ }
2401
+ if (options?.password && options.password.includes('${')) {
2402
+ let keys = options.password.match(/\$\{([^}]+)\}/g)?.map(m => m.slice(2, -1));
2403
+ if (keys) {
2404
+ keys.forEach((key) => {
2405
+ options.password = options.password.replace(`\${${key}}`, this.getDynamicParam(key));
2406
+ });
2407
+ }
2408
+ }
2409
+ this.mqttClients[net.index] = mqtt.connect(url, options);
2324
2410
  this.mqttClients[net.index].on('message', (topic, message) => {
2325
2411
  this.socketCallback(message.toString(), {
2326
2412
  topic,
2327
2413
  type: 'mqtt',
2328
2414
  url: net.url,
2415
+ name: net.name
2329
2416
  });
2330
2417
  });
2331
2418
  this.mqttClients[net.index].on('error', (error) => {
@@ -2341,7 +2428,16 @@ export class Meta2d {
2341
2428
  }
2342
2429
  });
2343
2430
  if (net.topics) {
2344
- this.mqttClients[net.index].subscribe(net.topics.split(','));
2431
+ let topics = net.topics;
2432
+ if (topics.indexOf('${') > -1) {
2433
+ let keys = topics.match(/\$\{([^}]+)\}/g)?.map(m => m.slice(2, -1));
2434
+ if (keys) {
2435
+ keys.forEach((key) => {
2436
+ topics = topics.replace(`\${${key}}`, this.getDynamicParam(key));
2437
+ });
2438
+ }
2439
+ }
2440
+ this.mqttClients[net.index].subscribe(topics.split(','));
2345
2441
  }
2346
2442
  }
2347
2443
  connectNetWebSocket(net) {
@@ -2361,7 +2457,7 @@ export class Meta2d {
2361
2457
  }
2362
2458
  this.websockets[net.index] = new WebSocket(url, net.protocols || undefined);
2363
2459
  this.websockets[net.index].onmessage = (e) => {
2364
- this.socketCallback(e.data, { type: 'websocket', url: net.url });
2460
+ this.socketCallback(e.data, { type: 'websocket', url: net.url, name: net.name });
2365
2461
  };
2366
2462
  this.websockets[net.index].onerror = (error) => {
2367
2463
  this.store.emitter.emit('error', { type: 'websocket', error });
@@ -2735,7 +2831,7 @@ export class Meta2d {
2735
2831
  });
2736
2832
  if (res.ok) {
2737
2833
  const data = await res.text();
2738
- this.socketCallback(data, { type: 'http', url: req.url });
2834
+ this.socketCallback(data, { type: 'http', url: req.url, name: req.name });
2739
2835
  }
2740
2836
  else {
2741
2837
  _req.times++;
@@ -3158,13 +3254,19 @@ export class Meta2d {
3158
3254
  break;
3159
3255
  case 'change':
3160
3256
  e.pen && updateFormData(e.pen);
3161
- this.store.data.locked &&
3162
- e &&
3163
- !e.disabled &&
3164
- this.doEvent(e, eventName);
3257
+ if (e.pen) {
3258
+ this.store.data.locked && !e.pen.disabled &&
3259
+ this.doEvent(e.pen, eventName);
3260
+ }
3261
+ else {
3262
+ this.store.data.locked &&
3263
+ e &&
3264
+ !e.disabled &&
3265
+ this.doEvent(e, eventName);
3266
+ }
3165
3267
  break;
3166
3268
  }
3167
- this.doMessageEvent(eventName);
3269
+ this.doMessageEvent(eventName, e);
3168
3270
  };
3169
3271
  doEvent = (pen, eventName) => {
3170
3272
  if (!pen) {
@@ -3936,7 +4038,7 @@ export class Meta2d {
3936
4038
  else {
3937
4039
  right = 0;
3938
4040
  }
3939
- let ratio = (this.canvas.width - left - right) / (rect.width);
4041
+ let ratio = (this.canvas.width - left - right) / (rect.width - left - right);
3940
4042
  pens.forEach((pen) => {
3941
4043
  if (pen.image && pen.imageRatio) {
3942
4044
  if (pen.calculative.worldRect.width / this.canvas.width > 0.1) {
@@ -3959,7 +4061,7 @@ export class Meta2d {
3959
4061
  if (pen.externElement) {
3960
4062
  pen.onResize?.(pen);
3961
4063
  }
3962
- if (pen.children.length) {
4064
+ if (pen.children?.length) {
3963
4065
  const cPens = getAllChildren(pen, this.store);
3964
4066
  cPens.forEach((cPen) => {
3965
4067
  if (cPen.externElement) {