@versa_ai/vmml-editor 1.0.15 → 1.0.17

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/dist/index.mjs CHANGED
@@ -998,9 +998,11 @@ function usePeekControl(canvas, hideConfig) {
998
998
  return canvas;
999
999
  }
1000
1000
  var EditorCanvas = forwardRef(
1001
- ({ previewState, showCanvas, canvasSize, enterPreview, intoTextEdit, frame, vmml, dragState, initFcObjs, onVideoChange, isBatchModify, hideConfig }, ref) => {
1001
+ ({ previewState, showCanvas, canvasSize, enterPreview, intoTextEdit, frame, vmml, dragState, initFcObjs, editClips = [], onVideoChange, isBatchModify, hideConfig }, ref) => {
1002
1002
  const [fc, setFc] = useState(null);
1003
1003
  const [history, setHistory] = useState(null);
1004
+ const [canvasReady, setCanvasReady] = useState(false);
1005
+ const editRenderTime = useRef(0);
1004
1006
  const waitFcTasks = useRef([]);
1005
1007
  const vmmlConverterRef = useRef(null);
1006
1008
  const heightScaleRef = useRef(1);
@@ -1020,6 +1022,7 @@ var EditorCanvas = forwardRef(
1020
1022
  setFc(canvas);
1021
1023
  initCanvasEvent(canvas);
1022
1024
  usePeekControl(canvas, hideConfig);
1025
+ setCanvasReady(true);
1023
1026
  };
1024
1027
  const createFcObjs = (canvas) => {
1025
1028
  const ns = Math.floor(frame / 30 * 1e6);
@@ -1191,9 +1194,33 @@ var EditorCanvas = forwardRef(
1191
1194
  waitFcTasks.current.push(updateImage.bind(void 0, id, base64));
1192
1195
  }
1193
1196
  };
1197
+ const createEditObjes = async (canvas, time) => {
1198
+ const promises = editClips.map((clip) => {
1199
+ if (clip.videoClip) {
1200
+ return createImageFromClip(clip);
1201
+ }
1202
+ if (clip.textClip && !clip.audioClip) {
1203
+ return createTextFromClip(clip);
1204
+ }
1205
+ });
1206
+ const res = await Promise.allSettled(promises);
1207
+ const objects = [];
1208
+ res.forEach((item) => {
1209
+ if (item.status === "fulfilled" && item.value) {
1210
+ objects.push(item.value);
1211
+ }
1212
+ });
1213
+ if (editRenderTime.current === time) {
1214
+ canvas.add(...objects).renderAll();
1215
+ }
1216
+ };
1194
1217
  const createImageFromClip = (clip, fc2) => {
1195
- const canvas = fc || fc2;
1196
- if (canvas && canvasSize.width) {
1218
+ return new Promise((resolve) => {
1219
+ const canvas = fc || fc2;
1220
+ if (!canvas || !canvasSize.width) {
1221
+ resolve(null);
1222
+ return;
1223
+ }
1197
1224
  const url = /video/g.test(clip.videoClip.mimeType) ? "" : clip.videoClip.sourceUrl;
1198
1225
  fabric.Image.fromURL(url, (img) => {
1199
1226
  const { dimension, posParam } = clip.videoClip;
@@ -1227,16 +1254,14 @@ var EditorCanvas = forwardRef(
1227
1254
  },
1228
1255
  visible: frame >= inFrame && frame < inFrame + durationFrame
1229
1256
  });
1230
- canvas.add(img);
1231
1257
  img.on("modified", () => {
1232
1258
  const fObj = convertToJSON(img);
1233
1259
  fObj.src = "";
1234
1260
  vmmlConverterRef.current.updateClip(fObj);
1235
1261
  });
1262
+ resolve(img);
1236
1263
  });
1237
- } else {
1238
- waitFcTasks.current.push(createImageFromClip.bind(void 0, clip));
1239
- }
1264
+ });
1240
1265
  };
1241
1266
  const handleRedo = () => {
1242
1267
  history.redo();
@@ -1245,11 +1270,11 @@ var EditorCanvas = forwardRef(
1245
1270
  history.undo();
1246
1271
  };
1247
1272
  const onBatchModify = (fObj, canvas) => {
1248
- console.log("onBatchModify>>>>>>>>>>>>>>>>>>>>>>>>");
1273
+ var _a;
1249
1274
  if (!canvas) return;
1250
1275
  const textObjects = canvas.getObjects().filter((item) => {
1251
- var _a;
1252
- return ((_a = item == null ? void 0 : item.clipData) == null ? void 0 : _a.type) === "\u6587\u5B57";
1276
+ var _a2;
1277
+ return ((_a2 = item == null ? void 0 : item.clipData) == null ? void 0 : _a2.type) === "\u6587\u5B57";
1253
1278
  });
1254
1279
  const { left, top, scaleX, scaleY, angle } = fObj;
1255
1280
  textObjects.forEach((textObj) => {
@@ -1265,10 +1290,20 @@ var EditorCanvas = forwardRef(
1265
1290
  vmmlConverterRef.current.updateClip(updatedFObj);
1266
1291
  });
1267
1292
  canvas.renderAll();
1293
+ const event = new CustomEvent("editor-vmml-batch-change", {
1294
+ detail: {
1295
+ vmml: ((_a = vmmlConverterRef.current) == null ? void 0 : _a.vmml) ?? null
1296
+ }
1297
+ });
1298
+ window.dispatchEvent(event);
1268
1299
  };
1269
1300
  const createTextFromClip = async (clip, fc2) => {
1270
- const canvas = fc || fc2;
1271
- if (canvas) {
1301
+ return new Promise(async (resolve) => {
1302
+ const canvas = fc || fc2;
1303
+ if (!canvas) {
1304
+ resolve(null);
1305
+ return;
1306
+ }
1272
1307
  const { width, height } = vmml.template.dimension;
1273
1308
  const fontSize = getFontSize(width, height);
1274
1309
  const { textContent, backgroundColor, textColor, posParam, fontAssetUrl, alignType } = clip.textClip;
@@ -1306,7 +1341,7 @@ var EditorCanvas = forwardRef(
1306
1341
  originX: "center",
1307
1342
  originY: "center",
1308
1343
  clipData: {
1309
- id: v4(),
1344
+ id: clip.id,
1310
1345
  inPoint: clip.inPoint,
1311
1346
  inFrame: getFrames(clip.inPoint, 30),
1312
1347
  type: "\u6587\u5B57",
@@ -1338,11 +1373,9 @@ var EditorCanvas = forwardRef(
1338
1373
  vmmlConverterRef.current.updateClip(fObj);
1339
1374
  }
1340
1375
  });
1341
- canvas.add(imgData).renderAll();
1376
+ resolve(imgData);
1342
1377
  });
1343
- } else {
1344
- waitFcTasks.current.push(createTextFromClip.bind(void 0, clip));
1345
- }
1378
+ });
1346
1379
  };
1347
1380
  const getFontFamilyName = (url) => {
1348
1381
  const filename = url.split("/").pop() || "";
@@ -1521,6 +1554,12 @@ var EditorCanvas = forwardRef(
1521
1554
  vmmlConverterRef.current = new VmmlConverter_default({ vmml, canvasSize });
1522
1555
  }
1523
1556
  }, [canvasSize, vmml]);
1557
+ useEffect(() => {
1558
+ if (editClips.length && canvasReady && fc) {
1559
+ editRenderTime.current = Date.now();
1560
+ createEditObjes(fc, editRenderTime.current);
1561
+ }
1562
+ }, [editClips, canvasReady]);
1524
1563
  useEffect(() => {
1525
1564
  if (fc) {
1526
1565
  const historyClass2 = new HistoryClass(fc);
@@ -1531,6 +1570,9 @@ var EditorCanvas = forwardRef(
1531
1570
  }
1532
1571
  }
1533
1572
  }, [fc]);
1573
+ const getCanvasCtx = () => {
1574
+ return fc;
1575
+ };
1534
1576
  useImperativeHandle(ref, () => ({
1535
1577
  createImage,
1536
1578
  createText,
@@ -1544,8 +1586,9 @@ var EditorCanvas = forwardRef(
1544
1586
  checkObjectInPoint,
1545
1587
  createImageFromClip,
1546
1588
  createTextFromClip,
1547
- changeObjectVisible
1548
- }));
1589
+ changeObjectVisible,
1590
+ getCanvasCtx
1591
+ }), [fc]);
1549
1592
  const getActions = () => {
1550
1593
  if (history) {
1551
1594
  return history.getActionType();
@@ -2311,7 +2354,7 @@ var VideoMenu = ({ createImage, videoMenus, menuState }) => {
2311
2354
  var VideoMenu_default = VideoMenu;
2312
2355
  var historyClass = new HistoryClass();
2313
2356
  var EditorFn = ({
2314
- vmml,
2357
+ vmml: propVmml,
2315
2358
  pauseFrame = 0,
2316
2359
  maxText = 10,
2317
2360
  maxVideo = 5,
@@ -2328,7 +2371,7 @@ var EditorFn = ({
2328
2371
  // { hideDelete: false, hideEdit: false, hideMute: false, hideVolume: false }
2329
2372
  }, ref) => {
2330
2373
  var _a;
2331
- vmml = convertVmmlTextScaleByForbidden(vmml);
2374
+ const [vmmlState, setVmmlState] = useState(() => convertVmmlTextScaleByForbidden(propVmml));
2332
2375
  const textMenuRef = useRef(null);
2333
2376
  const vmmlPlayerRef = useRef();
2334
2377
  const canvasRef = useRef();
@@ -2340,7 +2383,7 @@ var EditorFn = ({
2340
2383
  const [isPlaying, setIsPlaying] = useState(false);
2341
2384
  const [showCanvas, setShowCanvas] = useState(false);
2342
2385
  const [filterIds, setFilterIds] = useState([]);
2343
- const [durationInFrames, setDurationInFrames] = useState(getFrames(((_a = vmml == null ? void 0 : vmml.template) == null ? void 0 : _a.duration) || 1, fps));
2386
+ const [durationInFrames, setDurationInFrames] = useState(getFrames(((_a = vmmlState == null ? void 0 : vmmlState.template) == null ? void 0 : _a.duration) || 1, fps));
2344
2387
  const [previewState, setPreviewState] = useState(true);
2345
2388
  const [menuState, setMenuState] = useState("");
2346
2389
  const [canvasSize, setCanvasSize] = useState({ width: 0, height: 0, top: 0 });
@@ -2353,15 +2396,17 @@ var EditorFn = ({
2353
2396
  const [dragState, setDragState] = useState(0);
2354
2397
  const vmmlConverterRef = useRef(null);
2355
2398
  const [initFcObjs, setInitFcObjs] = useState([]);
2399
+ const [editClips, setEditClips] = useState([]);
2400
+ const [refreshEdit, setRefreshEdit] = useState(0);
2356
2401
  const vmmlFlag = useRef(false);
2357
2402
  const needPlay = useRef(true);
2358
2403
  const setVmml = () => {
2359
2404
  const { current } = vmmlPlayerRef;
2360
2405
  if (!current) return;
2361
2406
  if (!once.current) {
2362
- current.setVmml(vmml, pauseFrame);
2407
+ current.setVmml(vmmlState, pauseFrame);
2363
2408
  } else {
2364
- current.setVmml(vmml, frame);
2409
+ current.setVmml(vmmlState, frame);
2365
2410
  }
2366
2411
  };
2367
2412
  const onPlayerReady = () => {
@@ -2535,18 +2580,9 @@ var EditorFn = ({
2535
2580
  setBuffering(false);
2536
2581
  };
2537
2582
  const initCanEditClips = (tracks = []) => {
2538
- if (editableArray.length) {
2583
+ if (editableArray.length && tracks.length) {
2539
2584
  const list = findEditClips(tracks);
2540
- if (list.length) {
2541
- const { current } = canvasRef;
2542
- list.forEach((clip) => {
2543
- if (clip.videoClip) {
2544
- current.createImageFromClip(clip);
2545
- } else {
2546
- if (!clip.audioClip) current.createTextFromClip(clip);
2547
- }
2548
- });
2549
- }
2585
+ setEditClips(list);
2550
2586
  }
2551
2587
  };
2552
2588
  const findEditClips = (tracks = []) => {
@@ -2574,7 +2610,7 @@ var EditorFn = ({
2574
2610
  };
2575
2611
  const getVmml = () => {
2576
2612
  try {
2577
- const tracks = vmml.template.tracks.filter((item) => item.editorType === "\u6587\u5B57");
2613
+ const tracks = vmmlState.template.tracks.filter((item) => item.editorType === "\u6587\u5B57");
2578
2614
  tracks.forEach((track) => {
2579
2615
  track.clips.forEach((clip) => {
2580
2616
  clip.fObj.src = "";
@@ -2583,7 +2619,7 @@ var EditorFn = ({
2583
2619
  } catch {
2584
2620
  console.log("\u51FA\u9519\u4E86");
2585
2621
  }
2586
- return vmml;
2622
+ return vmmlState;
2587
2623
  };
2588
2624
  const getPlayer = () => {
2589
2625
  return player;
@@ -2600,18 +2636,33 @@ var EditorFn = ({
2600
2636
  }
2601
2637
  }, [previewState]);
2602
2638
  useEffect(() => {
2603
- if (canvasSize.width && canvasSize.height && !vmmlConverterRef.current && vmml) {
2604
- vmmlConverterRef.current = new VmmlConverter_default({ vmml, canvasSize });
2639
+ if (canvasSize.width && canvasSize.height && vmmlState) {
2640
+ if (!vmmlConverterRef.current) {
2641
+ vmmlConverterRef.current = new VmmlConverter_default({ vmml: vmmlState, canvasSize });
2642
+ } else {
2643
+ vmmlConverterRef.current.vmml = vmmlState;
2644
+ }
2605
2645
  }
2606
- if (canvasSize.width) {
2607
- initCanEditClips(vmml.template.tracks);
2646
+ }, [canvasSize, vmmlState]);
2647
+ useEffect(() => {
2648
+ if (editableArray.length && (vmmlState == null ? void 0 : vmmlState.template)) {
2649
+ initCanEditClips(vmmlState.template.tracks);
2608
2650
  }
2609
- }, [canvasSize, vmml]);
2651
+ }, [editableArray, refreshEdit]);
2652
+ useEffect(() => {
2653
+ var _a2;
2654
+ if (propVmml) {
2655
+ const convertedVmml = convertVmmlTextScaleByForbidden(propVmml);
2656
+ setVmmlState(convertedVmml);
2657
+ setDurationInFrames(getFrames(((_a2 = propVmml == null ? void 0 : propVmml.template) == null ? void 0 : _a2.duration) || 1, fps));
2658
+ setRefreshEdit(Date.now());
2659
+ }
2660
+ }, [propVmml]);
2610
2661
  useEffect(() => {
2611
- if (vmml) {
2612
- initFcObjects(vmml.template.tracks);
2662
+ if (vmmlState) {
2663
+ initFcObjects(vmmlState.template.tracks);
2613
2664
  }
2614
- }, [vmml]);
2665
+ }, [vmmlState]);
2615
2666
  useEffect(() => {
2616
2667
  if (player) {
2617
2668
  const parent = player.getContainerNode();
@@ -2646,7 +2697,7 @@ var EditorFn = ({
2646
2697
  if (dragState === 2) {
2647
2698
  needPlay.current = false;
2648
2699
  const { current } = vmmlPlayerRef;
2649
- current.setVmml(vmml, frame, false);
2700
+ current.setVmml(vmmlState, frame, false);
2650
2701
  setShowCanvas(false);
2651
2702
  }
2652
2703
  if (dragState === 3 || dragState === 4) {
@@ -2654,6 +2705,21 @@ var EditorFn = ({
2654
2705
  setPreviewState(false);
2655
2706
  }
2656
2707
  }, [dragState]);
2708
+ const updateVmml = (v) => {
2709
+ var _a2, _b, _c;
2710
+ const { current: playerCurrent } = vmmlPlayerRef;
2711
+ const { current: canvasCurrent } = canvasRef;
2712
+ if (!playerCurrent) return;
2713
+ (_b = (_a2 = canvasCurrent == null ? void 0 : canvasCurrent.getCanvasCtx()) == null ? void 0 : _a2.clear) == null ? void 0 : _b.call(_a2);
2714
+ const convertedVmml = convertVmmlTextScaleByForbidden(v);
2715
+ setVmmlState(convertedVmml);
2716
+ setDurationInFrames(getFrames(((_c = v == null ? void 0 : v.template) == null ? void 0 : _c.duration) || 1, fps));
2717
+ playerCurrent.setVmml(convertedVmml, pauseFrame);
2718
+ setRefreshEdit(Date.now());
2719
+ if (canvasCurrent) {
2720
+ canvasCurrent.checkObjectInPoint(pauseFrame);
2721
+ }
2722
+ };
2657
2723
  useImperativeHandle(
2658
2724
  ref,
2659
2725
  () => ({
@@ -2662,9 +2728,10 @@ var EditorFn = ({
2662
2728
  getVmml,
2663
2729
  getPlayer,
2664
2730
  texteditClose,
2665
- textFinish
2731
+ textFinish,
2732
+ updateVmml
2666
2733
  }),
2667
- [vmml, player]
2734
+ [vmmlState, player]
2668
2735
  );
2669
2736
  const texteditClose = () => {
2670
2737
  if (textMenuRef) {
@@ -2694,7 +2761,7 @@ var EditorFn = ({
2694
2761
  VmmlPlayer,
2695
2762
  {
2696
2763
  ref: vmmlPlayerRef,
2697
- vmml,
2764
+ vmml: vmmlState,
2698
2765
  existenceBorderRadio: true,
2699
2766
  moveToBeginningWhenEnded: true,
2700
2767
  muted: true,
@@ -2715,9 +2782,10 @@ var EditorFn = ({
2715
2782
  enterPreview,
2716
2783
  intoTextEdit,
2717
2784
  frame,
2718
- vmml,
2785
+ vmml: vmmlState,
2719
2786
  dragState,
2720
2787
  initFcObjs,
2788
+ editClips,
2721
2789
  onVideoChange,
2722
2790
  isBatchModify,
2723
2791
  hideConfig