canvu-react 0.4.59 → 0.4.61

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/react.cjs CHANGED
@@ -8262,18 +8262,18 @@ var VectorViewport = react.forwardRef(
8262
8262
  (middleware) => middleware != null
8263
8263
  );
8264
8264
  if (!consumerOnItemsChange && middlewares.length === 0) return void 0;
8265
- const baseNext = (nextItems) => {
8266
- consumerOnItemsChange?.(nextItems);
8265
+ const baseNext = (nextItems, info) => {
8266
+ consumerOnItemsChange?.(nextItems, info);
8267
8267
  };
8268
8268
  return middlewares.reduceRight(
8269
- (next, middleware) => (nextItems) => {
8269
+ (next, middleware) => (nextItems, info) => {
8270
8270
  const ctx = {
8271
8271
  currentItems: items,
8272
8272
  next,
8273
8273
  consumerOnItemsChange,
8274
8274
  viewportRef: pluginViewportRef
8275
8275
  };
8276
- middleware(nextItems, ctx);
8276
+ middleware(nextItems, ctx, info);
8277
8277
  },
8278
8278
  baseNext
8279
8279
  );
@@ -8349,7 +8349,7 @@ var VectorViewport = react.forwardRef(
8349
8349
  setCanRedo(true);
8350
8350
  setCanUndo(stack.length > 0);
8351
8351
  isUndoingRef.current = true;
8352
- onItemsChangeRef.current?.(prev);
8352
+ onItemsChangeRef.current?.(prev, { motive: "undo" });
8353
8353
  }, []);
8354
8354
  const redo = react.useCallback(() => {
8355
8355
  const stack = redoStackRef.current;
@@ -8360,7 +8360,7 @@ var VectorViewport = react.forwardRef(
8360
8360
  setCanUndo(true);
8361
8361
  setCanRedo(stack.length > 0);
8362
8362
  isUndoingRef.current = true;
8363
- onItemsChangeRef.current?.(next);
8363
+ onItemsChangeRef.current?.(next, { motive: "redo" });
8364
8364
  }, []);
8365
8365
  const [contextMenu, setContextMenu] = react.useState(null);
8366
8366
  const [uncontrolledSel, setUncontrolledSel] = react.useState([]);
@@ -8404,12 +8404,12 @@ var VectorViewport = react.forwardRef(
8404
8404
  const originalOnItemsChangeRef = react.useRef(onItemsChange);
8405
8405
  originalOnItemsChangeRef.current = onItemsChange;
8406
8406
  const changeItems = react.useCallback(
8407
- (items2) => {
8407
+ (items2, info) => {
8408
8408
  if (!isUndoingRef.current) {
8409
8409
  pushUndo();
8410
8410
  }
8411
8411
  isUndoingRef.current = false;
8412
- originalOnItemsChangeRef.current?.(items2);
8412
+ originalOnItemsChangeRef.current?.(items2, info);
8413
8413
  },
8414
8414
  [pushUndo]
8415
8415
  );
@@ -8523,7 +8523,13 @@ var VectorViewport = react.forwardRef(
8523
8523
  existing.bounds
8524
8524
  )
8525
8525
  };
8526
- change(list.map((i) => i.id === item.id ? rebuilt : i));
8526
+ change(
8527
+ list.map((i) => i.id === item.id ? rebuilt : i),
8528
+ {
8529
+ motive: "asset-hydrate",
8530
+ itemIds: [item.id]
8531
+ }
8532
+ );
8527
8533
  }
8528
8534
  }
8529
8535
  }, 100);
@@ -8635,7 +8641,8 @@ var VectorViewport = react.forwardRef(
8635
8641
  if (item) {
8636
8642
  const exists = itemsRef.current.some((it) => it.id === id);
8637
8643
  change(
8638
- exists ? replaceItem(itemsRef.current, id, item) : [...itemsRef.current, item]
8644
+ exists ? replaceItem(itemsRef.current, id, item) : [...itemsRef.current, item],
8645
+ { motive: "draw", itemIds: [id], toolId: args.tool }
8639
8646
  );
8640
8647
  patchCurrentStrokeStyle({
8641
8648
  stroke: item.stroke ?? DEFAULT_STROKE_STYLE.stroke,
@@ -9040,7 +9047,10 @@ var VectorViewport = react.forwardRef(
9040
9047
  const moved = clones.map(
9041
9048
  (it) => moveItemByDelta(it, PASTE_OFFSET_WORLD, PASTE_OFFSET_WORLD)
9042
9049
  );
9043
- change([...itemsRef.current, ...moved]);
9050
+ change([...itemsRef.current, ...moved], {
9051
+ motive: "paste",
9052
+ itemIds: moved.map((i) => i.id)
9053
+ });
9044
9054
  setEffectiveSelectedIdsRef.current(moved.map((i) => i.id));
9045
9055
  }, []);
9046
9056
  const duplicateIds = react.useCallback((ids) => {
@@ -9052,20 +9062,32 @@ var VectorViewport = react.forwardRef(
9052
9062
  const moved = clones.map(
9053
9063
  (it) => moveItemByDelta(it, PASTE_OFFSET_WORLD, PASTE_OFFSET_WORLD)
9054
9064
  );
9055
- change([...list, ...moved]);
9065
+ change([...list, ...moved], {
9066
+ motive: "duplicate",
9067
+ itemIds: moved.map((i) => i.id)
9068
+ });
9056
9069
  setEffectiveSelectedIdsRef.current(moved.map((i) => i.id));
9057
9070
  }, []);
9058
- const deleteIds = react.useCallback((ids) => {
9059
- const change = onItemsChangeRef.current;
9060
- if (!change || ids.length === 0) return;
9061
- const idSet = new Set(ids);
9062
- change(itemsRef.current.filter((i) => !idSet.has(i.id)));
9063
- setEffectiveSelectedIdsRef.current([]);
9064
- }, []);
9071
+ const deleteIds = react.useCallback(
9072
+ (ids, motive = "delete") => {
9073
+ const change = onItemsChangeRef.current;
9074
+ if (!change || ids.length === 0) return;
9075
+ const idSet = new Set(ids);
9076
+ change(
9077
+ itemsRef.current.filter((i) => !idSet.has(i.id)),
9078
+ {
9079
+ motive,
9080
+ itemIds: [...ids]
9081
+ }
9082
+ );
9083
+ setEffectiveSelectedIdsRef.current([]);
9084
+ },
9085
+ []
9086
+ );
9065
9087
  const cutIds = react.useCallback(
9066
9088
  (ids) => {
9067
9089
  copyIdsToInternalClipboard(ids);
9068
- deleteIds(ids);
9090
+ deleteIds(ids, "cut");
9069
9091
  },
9070
9092
  [copyIdsToInternalClipboard, deleteIds]
9071
9093
  );
@@ -9074,7 +9096,8 @@ var VectorViewport = react.forwardRef(
9074
9096
  if (!change || ids.length === 0) return;
9075
9097
  const idSet = new Set(ids);
9076
9098
  change(
9077
- itemsRef.current.map((it) => idSet.has(it.id) ? { ...it, locked } : it)
9099
+ itemsRef.current.map((it) => idSet.has(it.id) ? { ...it, locked } : it),
9100
+ { motive: locked ? "lock" : "unlock", itemIds: [...ids] }
9078
9101
  );
9079
9102
  }, []);
9080
9103
  const reorderIds = react.useCallback(
@@ -9082,7 +9105,7 @@ var VectorViewport = react.forwardRef(
9082
9105
  const change = onItemsChangeRef.current;
9083
9106
  if (!change || ids.length === 0) return;
9084
9107
  const next = reorderItemsByIds(itemsRef.current, ids, direction);
9085
- change(next);
9108
+ change(next, { motive: "reorder", itemIds: [...ids] });
9086
9109
  setEffectiveSelectedIdsRef.current([...ids]);
9087
9110
  },
9088
9111
  []
@@ -9502,7 +9525,7 @@ var VectorViewport = react.forwardRef(
9502
9525
  const out = editingTextIdRef.current === id && next.toolKind === "text" ? applyTextDraftWhileEditing(next, draftTextRef.current) : next;
9503
9526
  nextList = replaceItem(nextList, id, out);
9504
9527
  }
9505
- change(nextList);
9528
+ change(nextList, { motive: "style", itemIds: [...ids] });
9506
9529
  patchCurrentStrokeStyle(patch);
9507
9530
  },
9508
9531
  [patchCurrentStrokeStyle]
@@ -9527,7 +9550,10 @@ var VectorViewport = react.forwardRef(
9527
9550
  return;
9528
9551
  }
9529
9552
  const next = rebuildItemSvg({ ...it, text });
9530
- change(replaceItem(list, id, next));
9553
+ change(replaceItem(list, id, next), {
9554
+ motive: "text-edit",
9555
+ itemIds: [id]
9556
+ });
9531
9557
  editingTextSnapshotRef.current = null;
9532
9558
  setEditingTextId(null);
9533
9559
  }, []);
@@ -9539,7 +9565,10 @@ var VectorViewport = react.forwardRef(
9539
9565
  const list = itemsRef.current;
9540
9566
  const it = list.find((i) => i.id === editId);
9541
9567
  if (!it || it.toolKind !== "text") return;
9542
- change(replaceItem(list, editId, applyTextDraftWhileEditing(it, v)));
9568
+ change(replaceItem(list, editId, applyTextDraftWhileEditing(it, v)), {
9569
+ motive: "text-edit",
9570
+ itemIds: [editId]
9571
+ });
9543
9572
  }, []);
9544
9573
  react.useEffect(() => {
9545
9574
  if (!editingTextId) return;
@@ -9551,7 +9580,10 @@ var VectorViewport = react.forwardRef(
9551
9580
  const id = editingTextIdRef.current;
9552
9581
  const snap = editingTextSnapshotRef.current;
9553
9582
  if (change && id && snap) {
9554
- change(replaceItem(itemsRef.current, id, snap));
9583
+ change(replaceItem(itemsRef.current, id, snap), {
9584
+ motive: "text-edit",
9585
+ itemIds: [id]
9586
+ });
9555
9587
  }
9556
9588
  editingTextSnapshotRef.current = null;
9557
9589
  setEditingTextId(null);
@@ -9598,7 +9630,10 @@ var VectorViewport = react.forwardRef(
9598
9630
  onItemsReady: (nextItems) => {
9599
9631
  if (nextItems.length === 0) return;
9600
9632
  setLoadingSkeletons([]);
9601
- change([...itemsRef.current, ...nextItems]);
9633
+ change([...itemsRef.current, ...nextItems], {
9634
+ motive: "asset-add",
9635
+ itemIds: nextItems.map((item) => item.id)
9636
+ });
9602
9637
  setEffectiveSelectedIdsRef.current(nextItems.map((item) => item.id));
9603
9638
  }
9604
9639
  });
@@ -9762,7 +9797,13 @@ var VectorViewport = react.forwardRef(
9762
9797
  setDraftText(draft);
9763
9798
  setEditingTextId(hit.id);
9764
9799
  if (change && it && it.toolKind === "text") {
9765
- change(replaceItem(list, hit.id, applyTextDraftWhileEditing(it, draft)));
9800
+ change(
9801
+ replaceItem(list, hit.id, applyTextDraftWhileEditing(it, draft)),
9802
+ {
9803
+ motive: "text-edit",
9804
+ itemIds: [hit.id]
9805
+ }
9806
+ );
9766
9807
  }
9767
9808
  }
9768
9809
  },
@@ -10532,7 +10573,7 @@ var VectorViewport = react.forwardRef(
10532
10573
  const next = moveItemByDelta(snap, dx, dy);
10533
10574
  nextList = replaceItem(nextList, id, next);
10534
10575
  }
10535
- change(nextList);
10576
+ change(nextList, { motive: "move", itemIds: [...st.ids] });
10536
10577
  return;
10537
10578
  }
10538
10579
  if (st.kind === "rotate") {
@@ -10544,7 +10585,10 @@ var VectorViewport = react.forwardRef(
10544
10585
  st.startPointerAngleRad,
10545
10586
  angle
10546
10587
  );
10547
- change(replaceItem(list, st.id, next));
10588
+ change(replaceItem(list, st.id, next), {
10589
+ motive: "rotate",
10590
+ itemIds: [st.id]
10591
+ });
10548
10592
  return;
10549
10593
  }
10550
10594
  if (st.kind === "resize") {
@@ -10552,7 +10596,10 @@ var VectorViewport = react.forwardRef(
10552
10596
  x: worldX,
10553
10597
  y: worldY
10554
10598
  });
10555
- change(replaceItem(list, st.id, next));
10599
+ change(replaceItem(list, st.id, next), {
10600
+ motive: "resize",
10601
+ itemIds: [st.id]
10602
+ });
10556
10603
  return;
10557
10604
  }
10558
10605
  if (st.kind === "place") {
@@ -10670,7 +10717,8 @@ var VectorViewport = react.forwardRef(
10670
10717
  items: itemsRef.current,
10671
10718
  updateItem: (next) => {
10672
10719
  onItemsChangeRef.current?.(
10673
- replaceItem(itemsRef.current, item.id, next)
10720
+ replaceItem(itemsRef.current, item.id, next),
10721
+ { motive: "custom", itemIds: [item.id] }
10674
10722
  );
10675
10723
  },
10676
10724
  setSelectedIds: (ids) => setEffectiveSelectedIdsRef.current(ids)
@@ -10744,7 +10792,13 @@ var VectorViewport = react.forwardRef(
10744
10792
  const change = onItemsChangeRef.current;
10745
10793
  if (change && eraserPreviewIdsRef.current.size > 0) {
10746
10794
  const idSet = new Set(eraserPreviewIdsRef.current);
10747
- change(itemsRef.current.filter((i) => !idSet.has(i.id)));
10795
+ change(
10796
+ itemsRef.current.filter((i) => !idSet.has(i.id)),
10797
+ {
10798
+ motive: "erase",
10799
+ itemIds: [...idSet]
10800
+ }
10801
+ );
10748
10802
  }
10749
10803
  eraserPreviewIdsRef.current.clear();
10750
10804
  setEraserPreviewIds([]);
@@ -10787,7 +10841,11 @@ var VectorViewport = react.forwardRef(
10787
10841
  bounds: { ...newItem.bounds }
10788
10842
  };
10789
10843
  const hidden = applyTextDraftWhileEditing(newItem, "");
10790
- change([...itemsRef.current, hidden]);
10844
+ change([...itemsRef.current, hidden], {
10845
+ motive: "text-create",
10846
+ itemIds: [id],
10847
+ toolId: st.tool
10848
+ });
10791
10849
  setEffectiveSelectedIdsRef.current([id]);
10792
10850
  setEditingTextId(id);
10793
10851
  setDraftText("");
@@ -10842,10 +10900,17 @@ var VectorViewport = react.forwardRef(
10842
10900
  ...snapB ? { end: snapB.binding } : {}
10843
10901
  };
10844
10902
  }
10845
- change([
10846
- ...itemsRef.current,
10847
- createLineItem(id2, rawArrow, line, "arrow", pen2, arrowBind)
10848
- ]);
10903
+ change(
10904
+ [
10905
+ ...itemsRef.current,
10906
+ createLineItem(id2, rawArrow, line, "arrow", pen2, arrowBind)
10907
+ ],
10908
+ {
10909
+ motive: "place",
10910
+ itemIds: [id2],
10911
+ toolId: st.tool
10912
+ }
10913
+ );
10849
10914
  setEffectiveSelectedIdsRef.current([id2]);
10850
10915
  return;
10851
10916
  }
@@ -10876,36 +10941,59 @@ var VectorViewport = react.forwardRef(
10876
10941
  cpUp.createItem({ id, bounds: br }),
10877
10942
  cpUp.toolId
10878
10943
  );
10879
- change(itemsRef.current.concat(item));
10944
+ change(itemsRef.current.concat(item), {
10945
+ motive: "place",
10946
+ itemIds: [id],
10947
+ toolId: st.tool
10948
+ });
10880
10949
  if (cpUp.selectAfterCreate !== false) {
10881
10950
  setEffectiveSelectedIdsRef.current([id]);
10882
10951
  }
10883
10952
  return;
10884
10953
  }
10885
10954
  if (st.tool === "rect") {
10886
- change([...itemsRef.current, createRectangleItem(id, raw, pen)]);
10955
+ change([...itemsRef.current, createRectangleItem(id, raw, pen)], {
10956
+ motive: "place",
10957
+ itemIds: [id],
10958
+ toolId: st.tool
10959
+ });
10887
10960
  setEffectiveSelectedIdsRef.current([id]);
10888
10961
  } else if (st.tool === "ellipse") {
10889
- change([...itemsRef.current, createEllipseItem(id, raw, pen)]);
10962
+ change([...itemsRef.current, createEllipseItem(id, raw, pen)], {
10963
+ motive: "place",
10964
+ itemIds: [id],
10965
+ toolId: st.tool
10966
+ });
10890
10967
  setEffectiveSelectedIdsRef.current([id]);
10891
10968
  } else if (st.tool === "architectural-cloud") {
10892
- change([
10893
- ...itemsRef.current,
10894
- createArchitecturalCloudItem(id, raw, pen)
10895
- ]);
10969
+ change(
10970
+ [...itemsRef.current, createArchitecturalCloudItem(id, raw, pen)],
10971
+ {
10972
+ motive: "place",
10973
+ itemIds: [id],
10974
+ toolId: st.tool
10975
+ }
10976
+ );
10896
10977
  setEffectiveSelectedIdsRef.current([id]);
10897
10978
  } else if (st.tool === "line" || st.tool === "arrow") {
10898
10979
  const line = lineEndpointsToLocal(raw, lineA, lineB);
10899
- change([
10900
- ...itemsRef.current,
10901
- createLineItem(
10902
- id,
10903
- raw,
10904
- line,
10905
- st.tool === "arrow" ? "arrow" : "line",
10906
- pen
10907
- )
10908
- ]);
10980
+ change(
10981
+ [
10982
+ ...itemsRef.current,
10983
+ createLineItem(
10984
+ id,
10985
+ raw,
10986
+ line,
10987
+ st.tool === "arrow" ? "arrow" : "line",
10988
+ pen
10989
+ )
10990
+ ],
10991
+ {
10992
+ motive: "place",
10993
+ itemIds: [id],
10994
+ toolId: st.tool
10995
+ }
10996
+ );
10909
10997
  setEffectiveSelectedIdsRef.current([id]);
10910
10998
  }
10911
10999
  requestAutoResetTool(st.tool);