@meta2d/core 1.1.0 → 1.1.2

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meta2d/core",
3
- "version": "1.1.0",
3
+ "version": "1.1.2",
4
4
  "description": "@meta2d/core: Powerful, Beautiful, Simple, Open - Web-Based 2D At Its Best .",
5
5
  "main": "index.js",
6
6
  "types": "index.d.ts",
@@ -245,6 +245,7 @@ export declare class Canvas {
245
245
  resize(w?: number, h?: number): void;
246
246
  clearCanvas(): void;
247
247
  addPen(pen: Pen, history?: boolean, emit?: boolean, abs?: boolean): Promise<Pen>;
248
+ addPenSync(pen: Pen, history?: boolean, emit?: boolean, abs?: boolean): Pen;
248
249
  pushHistory(action: EditAction): void;
249
250
  undo(): void;
250
251
  redo(): void;
@@ -264,7 +265,8 @@ export declare class Canvas {
264
265
  */
265
266
  private firefoxLoadSvg;
266
267
  loadImage(pen: Pen): void;
267
- __loadImage(src: string): Promise<unknown>;
268
+ __loadMap: Map<any, any>;
269
+ __loadImage(src: string, retryNum?: number): any;
268
270
  private imageTimer;
269
271
  imageLoaded(): void;
270
272
  private templateImageTimer;
@@ -409,6 +411,7 @@ export declare class Canvas {
409
411
  */
410
412
  changeNodeConnectedLine(oldId: string, line: Pen, pastePens: Pen[]): void;
411
413
  delete(pens?: Pen[], canDelLocked?: boolean, history?: boolean): Promise<void>;
414
+ deleteSync(pens?: Pen[], canDelLocked?: boolean, history?: boolean): void;
412
415
  private _del;
413
416
  getDelPens(pen: Pen, delPens: Pen[]): void;
414
417
  private getLockedParent;
@@ -394,6 +394,9 @@ export class Canvas {
394
394
  data: this.dialog.data
395
395
  }), '*');
396
396
  }
397
+ if (data.name === 'closeDialog') {
398
+ this.dialog.hide();
399
+ }
397
400
  this.parent.doMessageEvent(data.name, JSON.stringify(data.data));
398
401
  }
399
402
  else {
@@ -1206,6 +1209,15 @@ export class Canvas {
1206
1209
  if (num % 2 === 0) {
1207
1210
  lastH += pen.height + 10 * this.store.data.scale;
1208
1211
  }
1212
+ delete pen.dataset;
1213
+ }
1214
+ if (pen.temOffsetX) {
1215
+ pen.x += pen.temOffsetX * this.store.data.scale;
1216
+ delete pen.temOffsetX;
1217
+ }
1218
+ if (pen.temOffsetY) {
1219
+ pen.y += pen.temOffsetY * this.store.data.scale;
1220
+ delete pen.temOffsetY;
1209
1221
  }
1210
1222
  }
1211
1223
  }
@@ -3324,6 +3336,7 @@ export class Canvas {
3324
3336
  clearCanvas() {
3325
3337
  this.activeRect = undefined;
3326
3338
  this.sizeCPs = undefined;
3339
+ this.__loadMap = new Map();
3327
3340
  this.canvas
3328
3341
  .getContext('2d')
3329
3342
  .clearRect(0, 0, this.canvas.width, this.canvas.height);
@@ -3358,6 +3371,25 @@ export class Canvas {
3358
3371
  }
3359
3372
  return pen;
3360
3373
  }
3374
+ addPenSync(pen, history, emit, abs) {
3375
+ if (this.beforeAddPen && this.beforeAddPen(pen) != true) {
3376
+ return;
3377
+ }
3378
+ if (abs) {
3379
+ pen.x = pen.x * this.store.data.scale + this.store.data.origin.x;
3380
+ pen.y = pen.y * this.store.data.scale + this.store.data.origin.y;
3381
+ pen.width = pen.width * this.store.data.scale;
3382
+ pen.height = pen.height * this.store.data.scale;
3383
+ }
3384
+ this.makePen(pen);
3385
+ this.active([pen]);
3386
+ this.render();
3387
+ emit && this.store.emitter.emit('add', [pen]);
3388
+ if (history) {
3389
+ this.pushHistory({ type: EditType.Add, pens: [pen] });
3390
+ }
3391
+ return pen;
3392
+ }
3361
3393
  pushHistory(action) {
3362
3394
  if (this.store.data.locked) {
3363
3395
  return;
@@ -4031,28 +4063,50 @@ export class Canvas {
4031
4063
  pen.calculative.strokeImage = pen.strokeImage;
4032
4064
  }
4033
4065
  }
4066
+ __loadMap = new Map();
4034
4067
  // 加载图片到全局缓存
4035
- __loadImage(src) {
4036
- return new Promise(resolve => {
4068
+ __loadImage(src, retryNum = 5) {
4069
+ if (this.__loadMap.has(src))
4070
+ return this.__loadMap.get(src); // promise
4071
+ const promise = new Promise((resolve, reject) => {
4037
4072
  if (!globalStore.htmlElements[src]) {
4038
4073
  const img = new Image();
4039
4074
  img.crossOrigin = 'anonymous';
4040
- img.src = src;
4075
+ // 处理 CDN 路径
4041
4076
  if (this.store.options.cdn &&
4042
4077
  !(src.startsWith('http') ||
4043
4078
  src.startsWith('//') ||
4044
4079
  src.startsWith('data:image'))) {
4045
4080
  img.src = this.store.options.cdn + src;
4046
4081
  }
4082
+ else {
4083
+ img.src = src;
4084
+ }
4047
4085
  img.onload = () => {
4048
4086
  globalStore.htmlElements[src] = img;
4087
+ this.__loadMap.delete(src); // 成功后清理
4049
4088
  resolve(img);
4050
4089
  };
4090
+ img.onerror = () => {
4091
+ if (retryNum > 0) {
4092
+ console.warn(`Image ${src} load failed, retrying... (${retryNum})`);
4093
+ setTimeout(() => {
4094
+ this.__loadMap.delete(src);
4095
+ this.__loadImage(src, retryNum - 1).then(resolve).catch(reject);
4096
+ }, 1000);
4097
+ }
4098
+ else {
4099
+ this.__loadMap.set(src, 0);
4100
+ reject(new Error(`Failed to load image: ${src}`));
4101
+ }
4102
+ };
4051
4103
  }
4052
4104
  else {
4053
4105
  resolve(globalStore.htmlElements[src]);
4054
4106
  }
4055
4107
  });
4108
+ this.__loadMap.set(src, promise);
4109
+ return promise;
4056
4110
  }
4057
4111
  imageTimer;
4058
4112
  // 避免初始化图片加载重复调用 render,此处防抖
@@ -4952,7 +5006,7 @@ export class Canvas {
4952
5006
  calcRightBottom(pen.calculative.worldRect);
4953
5007
  calcCenter(pen.calculative.worldRect);
4954
5008
  this.updatePenRect(pen, { worldRectIsReady: true });
4955
- this.execPenResize(pen);
5009
+ this.execPenResize(pen, true);
4956
5010
  this.updateLines(pen);
4957
5011
  });
4958
5012
  this.getSizeCPs();
@@ -6298,6 +6352,31 @@ export class Canvas {
6298
6352
  }
6299
6353
  this.store.emitter.emit('delete', pens);
6300
6354
  }
6355
+ deleteSync(pens = this.store.active, canDelLocked = false, history = true) {
6356
+ if (!pens || !pens.length) {
6357
+ return;
6358
+ }
6359
+ if (!canDelLocked) {
6360
+ pens = pens.filter((pen) => !pen.locked);
6361
+ }
6362
+ if (!pens || !pens.length) {
6363
+ return;
6364
+ }
6365
+ const deletePens = [];
6366
+ this._del(pens, deletePens, canDelLocked);
6367
+ this.initImageCanvas(deletePens);
6368
+ this.initTemplateCanvas(deletePens);
6369
+ this.inactive();
6370
+ this.clearHover();
6371
+ this.render();
6372
+ // TODO: 连线的删除 ,连接的 node 的 connectLines 会变化(删除 node ,line 的 anchors 类似),未记历史记录
6373
+ if (history) {
6374
+ if (deletePens.length === 0)
6375
+ return;
6376
+ this.pushHistory({ type: EditType.Delete, pens: deletePens });
6377
+ }
6378
+ this.store.emitter.emit('delete', pens);
6379
+ }
6301
6380
  _del(pens, delPens, canDelLocked) {
6302
6381
  if (!pens) {
6303
6382
  return;
@@ -6558,7 +6637,7 @@ export class Canvas {
6558
6637
  else {
6559
6638
  // this.inputRight.style.display = 'none';
6560
6639
  }
6561
- this.inputDiv.contentEditable = 'true';
6640
+ this.inputDiv.contentEditable = pen.readonly ? 'false' : 'true';
6562
6641
  this.inputDiv.focus();
6563
6642
  const range = window.getSelection(); //创建range
6564
6643
  range.selectAllChildren(this.inputDiv); //range 选择obj下所有子内容
@@ -7278,11 +7357,11 @@ export class Canvas {
7278
7357
  /**
7279
7358
  * 执行 pen ,以及 pen 的子孙节点的 onResize 生命周期函数
7280
7359
  */
7281
- execPenResize(pen) {
7282
- pen.onResize?.(pen);
7360
+ execPenResize(pen, raw) {
7361
+ pen.onResize?.(pen, raw);
7283
7362
  pen.children?.forEach((chlidId) => {
7284
7363
  const child = this.store.pens[chlidId];
7285
- child && this.execPenResize(child);
7364
+ child && this.execPenResize(child, raw);
7286
7365
  });
7287
7366
  }
7288
7367
  setPenRect(pen, rect, render = true) {
@@ -7298,7 +7377,7 @@ export class Canvas {
7298
7377
  pen.height = rect.height * scale;
7299
7378
  }
7300
7379
  this.updatePenRect(pen);
7301
- this.execPenResize(pen);
7380
+ this.execPenResize(pen, true);
7302
7381
  render && this.render();
7303
7382
  }
7304
7383
  getPenRect(pen, origin = this.store.data.origin, scale = this.store.data.scale) {
@@ -7775,45 +7854,49 @@ export class Canvas {
7775
7854
  if (rect.width > 0.5) {
7776
7855
  rect.left = true;
7777
7856
  rect.right = true;
7778
- rect.leftValue = (rect.x - 0) * scale * width;
7779
- rect.rightValue = (1 - (rect.x + rect.width)) * scale * width;
7857
+ rect.leftValue = rect.x;
7858
+ rect.rightValue = 1 - (rect.x + rect.width);
7780
7859
  }
7781
7860
  else {
7782
7861
  if (rect.x < 0.5) {
7783
7862
  rect.left = true;
7784
- rect.leftValue = (rect.x - 0) * scale * width;
7863
+ rect.leftValue = rect.x;
7864
+ if (rect.x > 0.2 && (rect.x + rect.width) > 0.5) {
7865
+ rect.right = true;
7866
+ rect.rightValue = 1 - (rect.x + rect.width);
7867
+ }
7785
7868
  }
7786
7869
  else {
7787
7870
  rect.right = true;
7788
- rect.rightValue = (1 - (rect.x + rect.width)) * scale * width;
7871
+ rect.rightValue = 1 - (rect.x + rect.width);
7789
7872
  }
7790
7873
  }
7791
- if (rect.leftValue < 1) {
7874
+ if (rect.leftValue < 0.05) {
7792
7875
  rect.leftValue = 0;
7793
7876
  }
7794
- if (rect.rightValue < 1) {
7877
+ if (rect.rightValue < 0.05) {
7795
7878
  rect.rightValue = 0;
7796
7879
  }
7797
7880
  if (rect.height > 0.5) {
7798
7881
  rect.top = true;
7799
7882
  rect.bottom = true;
7800
- rect.topValue = (rect.y - 0) * scale * height;
7801
- rect.bottomValue = (1 - (rect.y + rect.height)) * scale * height;
7883
+ rect.topValue = rect.y;
7884
+ rect.bottomValue = 1 - (rect.y + rect.height);
7802
7885
  }
7803
7886
  else {
7804
7887
  if (rect.y < 0.5) {
7805
7888
  rect.top = true;
7806
- rect.topValue = (rect.y - 0) * scale * height;
7889
+ rect.topValue = rect.y;
7807
7890
  }
7808
7891
  else {
7809
7892
  rect.bottom = true;
7810
- rect.bottomValue = (1 - (rect.y + rect.height)) * scale * height;
7893
+ rect.bottomValue = 1 - (rect.y + rect.height);
7811
7894
  }
7812
7895
  }
7813
- if (rect.topValue < 1) {
7896
+ if (rect.topValue < 0.05) {
7814
7897
  rect.topValue = 0;
7815
7898
  }
7816
- if (rect.bottomValue < 1) {
7899
+ if (rect.bottomValue < 0.05) {
7817
7900
  rect.bottomValue = 0;
7818
7901
  }
7819
7902
  if (!this.store.data.fits) {
@@ -7907,45 +7990,49 @@ export class Canvas {
7907
7990
  if (fit.width > 0.5) {
7908
7991
  fit.left = true;
7909
7992
  fit.right = true;
7910
- fit.leftValue = (fit.x - 0) * scale * width;
7911
- fit.rightValue = ((1 - (fit.x + fit.width)) * scale * width);
7993
+ fit.leftValue = fit.x;
7994
+ fit.rightValue = 1 - (fit.x + fit.width);
7912
7995
  }
7913
7996
  else {
7914
7997
  if (fit.x < 0.5) {
7915
7998
  fit.left = true;
7916
- fit.leftValue = (fit.x - 0) * scale * width;
7999
+ fit.leftValue = fit.x;
8000
+ if (fit.x > 0.2 && (fit.x + fit.width) > 0.5) {
8001
+ fit.right = true;
8002
+ fit.rightValue = 1 - (fit.x + fit.width);
8003
+ }
7917
8004
  }
7918
8005
  else {
7919
8006
  fit.right = true;
7920
- fit.rightValue = ((1 - (fit.x + fit.width)) * scale * width);
8007
+ fit.rightValue = 1 - (fit.x + fit.width);
7921
8008
  }
7922
8009
  }
7923
- if (Math.abs(fit.leftValue) < 1) {
8010
+ if (Math.abs(fit.leftValue) < 0.05) {
7924
8011
  fit.leftValue = 0;
7925
8012
  }
7926
- if (Math.abs(fit.rightValue) < 1) {
8013
+ if (Math.abs(fit.rightValue) < 0.05) {
7927
8014
  fit.rightValue = 0;
7928
8015
  }
7929
8016
  if (fit.height > 0.5) {
7930
8017
  fit.top = true;
7931
8018
  fit.bottom = true;
7932
- fit.topValue = (fit.y - 0) * scale * height;
7933
- fit.bottomValue = (1 - (fit.y + fit.height)) * scale * height;
8019
+ fit.topValue = fit.y;
8020
+ fit.bottomValue = 1 - (fit.y + fit.height);
7934
8021
  }
7935
8022
  else {
7936
8023
  if (fit.y < 0.5) {
7937
8024
  fit.top = true;
7938
- fit.topValue = (fit.y - 0) * scale * height;
8025
+ fit.topValue = fit.y - 0;
7939
8026
  }
7940
8027
  else {
7941
8028
  fit.bottom = true;
7942
- fit.bottomValue = (1 - (fit.y + fit.height)) * scale * height;
8029
+ fit.bottomValue = 1 - (fit.y + fit.height);
7943
8030
  }
7944
8031
  }
7945
- if (Math.abs(fit.topValue) < 1) {
8032
+ if (Math.abs(fit.topValue) < 0.05) {
7946
8033
  fit.topValue = 0;
7947
8034
  }
7948
- if (Math.abs(fit.bottomValue) < 1) {
8035
+ if (Math.abs(fit.bottomValue) < 0.05) {
7949
8036
  fit.bottomValue = 0;
7950
8037
  }
7951
8038
  fit.children = pens.map(pen => pen.id);