@versa_ai/vmml-editor 1.0.57 → 1.0.59

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": "@versa_ai/vmml-editor",
3
- "version": "1.0.57",
3
+ "version": "1.0.59",
4
4
  "module": "dist/index.mjs",
5
5
  "main": "dist/index.mjs",
6
6
  "types": "dist/index.d.mts",
@@ -17,7 +17,7 @@
17
17
  "remotion": "4.0.166",
18
18
  "uuid": "^10.0.0",
19
19
  "zod": "^3.23.8",
20
- "@versa_ai/vmml-player": "1.1.28",
20
+ "@versa_ai/vmml-player": "1.1.29",
21
21
  "@versa_ai/vmml-utils": "1.0.16"
22
22
  },
23
23
  "devDependencies": {
@@ -18,6 +18,7 @@ const EditorCanvas = forwardRef(
18
18
  const widthScaleRef = useRef<number>(1);
19
19
  const fontCacheRef = useRef<Map<string, Promise<string | null>>>(new Map());
20
20
  const lockInBoundsRef = useRef(lockInBounds);
21
+ const lastValidScaleRef = useRef<{ scaleX: number; scaleY: number; left: number; top: number } | null>(null);
21
22
 
22
23
  const initCanvas = () => {
23
24
  const canvas = new fabric.Canvas("canvas", {
@@ -96,35 +97,68 @@ const EditorCanvas = forwardRef(
96
97
  });
97
98
 
98
99
  canvas.on('object:textEdit', (e: any) => {
99
- console.log('1111111111')
100
100
  const active = canvas.getActiveObject();
101
101
  handleIntoTextMenu(active, canvas)
102
102
  });
103
103
 
104
104
  canvas.on('object:moving', (e: any) => {
105
- if (!lockInBoundsRef.current) return;
106
-
107
- const obj = e.target;
108
- const canvasWidth = canvas.getWidth();
109
- const canvasHeight = canvas.getHeight();
110
-
111
- let left = obj.left;
112
- let top = obj.top;
113
- let w = obj.width;
114
- let h = obj.height;
115
- const paddingX = 15
116
- const paddingY = 10
117
- // 左边界
118
- if (left <= (w / 2 - paddingX)) left = w / 2 - paddingX;
119
- // 右边界
120
- if (left >= (canvasWidth - (w / 2 - paddingX - 2.5))) left = canvasWidth - (w / 2 - paddingX - 2.5);
121
- // 上边界
122
- if (top <= (h - paddingY) / 2) top = (h - paddingY) / 2;
123
- // 下边界
124
- if (top >= (canvasHeight - (h - paddingY) / 2)) top = canvasHeight - (h - paddingY) / 2;
125
-
126
- obj.set({ left, top }).setCoords();
127
- });
105
+ if (!lockInBoundsRef.current) return;
106
+
107
+ const obj = e.target;
108
+
109
+ const canvasWidth = canvas.getWidth();
110
+ const canvasHeight = canvas.getHeight();
111
+ const scaleX = obj.scaleX ?? 1;
112
+ const scaleY = obj.scaleY ?? 1;
113
+
114
+ const width = obj.width * scaleX;
115
+ const height = obj.height * scaleY;
116
+
117
+ let left = obj.left ?? 0;
118
+ let top = obj.top ?? 0;
119
+
120
+ const halfW = width / 2;
121
+ const halfH = height / 2;
122
+
123
+ // 左边界
124
+ if (left - halfW < 0) left = halfW;
125
+ // 右边界
126
+ if (left + halfW > canvasWidth) left = canvasWidth - halfW;
127
+ // 上边界
128
+ if (top - halfH < 0) top = halfH;
129
+ // 下边界
130
+ if (top + halfH > canvasHeight) top = canvasHeight - halfH;
131
+
132
+ obj.set({ left, top });
133
+ });
134
+ canvas.on('object:scaling', (e: any) => {
135
+ if (!lockInBoundsRef.current) return;
136
+
137
+ const obj = e.target;
138
+ obj.setCoords();
139
+ const bounds = obj.getBoundingRect();
140
+ const canvasWidth = canvas.getWidth();
141
+ const canvasHeight = canvas.getHeight();
142
+
143
+ const outOfBounds =
144
+ bounds.left < 0 ||
145
+ bounds.top < 0 ||
146
+ bounds.left + bounds.width > canvasWidth ||
147
+ bounds.top + bounds.height > canvasHeight;
148
+
149
+ if (outOfBounds) {
150
+ if (lastValidScaleRef.current) {
151
+ obj.set(lastValidScaleRef.current).setCoords();
152
+ }
153
+ } else {
154
+ lastValidScaleRef.current = {
155
+ scaleX: obj.scaleX,
156
+ scaleY: obj.scaleY,
157
+ left: obj.left,
158
+ top: obj.top,
159
+ };
160
+ }
161
+ })
128
162
  };
129
163
  //模拟双击
130
164
  const imitateDBclick = (active: any, canvas: any) => {
@@ -180,7 +214,6 @@ const EditorCanvas = forwardRef(
180
214
  return { ...data.toJSON(["clipData", "type"]), centerPoint };
181
215
  };
182
216
 
183
-
184
217
  // 创建图片
185
218
  const createImage = (file: any, emojiId: string) => {
186
219
  const url = file.resourcesType === "image" ? file.visibleUrl : "";
@@ -357,6 +390,11 @@ const EditorCanvas = forwardRef(
357
390
  window.dispatchEvent(event);
358
391
  }
359
392
 
393
+ const isVisibleColor = (color?: string) =>
394
+ !!color &&
395
+ color !== 'transparent' &&
396
+ !/rgba\([^)]*,\s*0\s*\)$/.test(color);
397
+
360
398
  const createTextObjs = async ({ strokeW, stColor, fontAssetUrl = null, letterSpace, bgColor, textContent, textBasicInfo, textColor }: any) => {
361
399
  const { width, height } = vmml.template.dimension;
362
400
  const fontSize = getFontSize(width, height);
@@ -443,8 +481,12 @@ const EditorCanvas = forwardRef(
443
481
  rx: round,
444
482
  ry: round,
445
483
  });
484
+
485
+ let list = []
486
+ if (bgColor && isVisibleColor(bgColor)) list = [bgRect, ...textObjs]
487
+ else list = [...textObjs]
446
488
  return {
447
- objects: [bgRect, ...textObjs],
489
+ objects: list,
448
490
  fontFamily,
449
491
  }
450
492
  }