@meta2d/core 1.1.0 → 1.1.1

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.1",
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",
@@ -264,7 +264,8 @@ export declare class Canvas {
264
264
  */
265
265
  private firefoxLoadSvg;
266
266
  loadImage(pen: Pen): void;
267
- __loadImage(src: string): Promise<unknown>;
267
+ __loadMap: Map<any, any>;
268
+ __loadImage(src: string, retryNum?: number): any;
268
269
  private imageTimer;
269
270
  imageLoaded(): void;
270
271
  private templateImageTimer;
@@ -3324,6 +3324,7 @@ export class Canvas {
3324
3324
  clearCanvas() {
3325
3325
  this.activeRect = undefined;
3326
3326
  this.sizeCPs = undefined;
3327
+ this.__loadMap = new Map();
3327
3328
  this.canvas
3328
3329
  .getContext('2d')
3329
3330
  .clearRect(0, 0, this.canvas.width, this.canvas.height);
@@ -4031,28 +4032,50 @@ export class Canvas {
4031
4032
  pen.calculative.strokeImage = pen.strokeImage;
4032
4033
  }
4033
4034
  }
4035
+ __loadMap = new Map();
4034
4036
  // 加载图片到全局缓存
4035
- __loadImage(src) {
4036
- return new Promise(resolve => {
4037
+ __loadImage(src, retryNum = 5) {
4038
+ if (this.__loadMap.has(src))
4039
+ return this.__loadMap.get(src); // promise
4040
+ const promise = new Promise((resolve, reject) => {
4037
4041
  if (!globalStore.htmlElements[src]) {
4038
4042
  const img = new Image();
4039
4043
  img.crossOrigin = 'anonymous';
4040
- img.src = src;
4044
+ // 处理 CDN 路径
4041
4045
  if (this.store.options.cdn &&
4042
4046
  !(src.startsWith('http') ||
4043
4047
  src.startsWith('//') ||
4044
4048
  src.startsWith('data:image'))) {
4045
4049
  img.src = this.store.options.cdn + src;
4046
4050
  }
4051
+ else {
4052
+ img.src = src;
4053
+ }
4047
4054
  img.onload = () => {
4048
4055
  globalStore.htmlElements[src] = img;
4056
+ this.__loadMap.delete(src); // 成功后清理
4049
4057
  resolve(img);
4050
4058
  };
4059
+ img.onerror = () => {
4060
+ if (retryNum > 0) {
4061
+ console.warn(`Image ${src} load failed, retrying... (${retryNum})`);
4062
+ setTimeout(() => {
4063
+ this.__loadMap.delete(src);
4064
+ this.__loadImage(src, retryNum - 1).then(resolve).catch(reject);
4065
+ }, 1000);
4066
+ }
4067
+ else {
4068
+ this.__loadMap.set(src, 0);
4069
+ reject(new Error(`Failed to load image: ${src}`));
4070
+ }
4071
+ };
4051
4072
  }
4052
4073
  else {
4053
4074
  resolve(globalStore.htmlElements[src]);
4054
4075
  }
4055
4076
  });
4077
+ this.__loadMap.set(src, promise);
4078
+ return promise;
4056
4079
  }
4057
4080
  imageTimer;
4058
4081
  // 避免初始化图片加载重复调用 render,此处防抖
@@ -6558,7 +6581,7 @@ export class Canvas {
6558
6581
  else {
6559
6582
  // this.inputRight.style.display = 'none';
6560
6583
  }
6561
- this.inputDiv.contentEditable = 'true';
6584
+ this.inputDiv.contentEditable = pen.readonly ? 'false' : 'true';
6562
6585
  this.inputDiv.focus();
6563
6586
  const range = window.getSelection(); //创建range
6564
6587
  range.selectAllChildren(this.inputDiv); //range 选择obj下所有子内容