@particle-academy/fancy-flow 0.19.0 → 0.21.0

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.
@@ -1,6 +1,6 @@
1
1
  import { jsx, jsxs, Fragment } from 'react/jsx-runtime';
2
2
  import ReactExports, { createContext, forwardRef, memo, useMemo, useState, useRef, useEffect, useCallback, useContext, useSyncExternalStore, useDebugValue, useLayoutEffect } from 'react';
3
- import 'react-dom';
3
+ import { createPortal } from 'react-dom';
4
4
 
5
5
  // node_modules/d3-dispatch/src/dispatch.js
6
6
  var noop = { value: () => {
@@ -109,9 +109,9 @@ function creator_default(name) {
109
109
  // node_modules/d3-selection/src/selector.js
110
110
  function none() {
111
111
  }
112
- function selector_default(selector) {
113
- return selector == null ? none : function() {
114
- return this.querySelector(selector);
112
+ function selector_default(selector2) {
113
+ return selector2 == null ? none : function() {
114
+ return this.querySelector(selector2);
115
115
  };
116
116
  }
117
117
 
@@ -138,9 +138,9 @@ function array(x) {
138
138
  function empty() {
139
139
  return [];
140
140
  }
141
- function selectorAll_default(selector) {
142
- return selector == null ? empty : function() {
143
- return this.querySelectorAll(selector);
141
+ function selectorAll_default(selector2) {
142
+ return selector2 == null ? empty : function() {
143
+ return this.querySelectorAll(selector2);
144
144
  };
145
145
  }
146
146
 
@@ -165,14 +165,14 @@ function selectAll_default(select) {
165
165
  }
166
166
 
167
167
  // node_modules/d3-selection/src/matcher.js
168
- function matcher_default(selector) {
168
+ function matcher_default(selector2) {
169
169
  return function() {
170
- return this.matches(selector);
170
+ return this.matches(selector2);
171
171
  };
172
172
  }
173
- function childMatcher(selector) {
173
+ function childMatcher(selector2) {
174
174
  return function(node) {
175
- return node.matches(selector);
175
+ return node.matches(selector2);
176
176
  };
177
177
  }
178
178
 
@@ -241,11 +241,11 @@ EnterNode.prototype = {
241
241
  insertBefore: function(child, next) {
242
242
  return this._parent.insertBefore(child, next);
243
243
  },
244
- querySelector: function(selector) {
245
- return this._parent.querySelector(selector);
244
+ querySelector: function(selector2) {
245
+ return this._parent.querySelector(selector2);
246
246
  },
247
- querySelectorAll: function(selector) {
248
- return this._parent.querySelectorAll(selector);
247
+ querySelectorAll: function(selector2) {
248
+ return this._parent.querySelectorAll(selector2);
249
249
  }
250
250
  };
251
251
 
@@ -856,8 +856,8 @@ Selection.prototype = selection.prototype = {
856
856
  var selection_default = selection;
857
857
 
858
858
  // node_modules/d3-selection/src/select.js
859
- function select_default2(selector) {
860
- return typeof selector === "string" ? new Selection([[document.querySelector(selector)]], [document.documentElement]) : new Selection([[selector]], root);
859
+ function select_default2(selector2) {
860
+ return typeof selector2 === "string" ? new Selection([[document.querySelector(selector2)]], [document.documentElement]) : new Selection([[selector2]], root);
861
861
  }
862
862
 
863
863
  // node_modules/d3-selection/src/sourceEvent.js
@@ -3592,6 +3592,28 @@ var addEdge = (edgeParams, edges, options = {}) => {
3592
3592
  }
3593
3593
  return edges.concat(edge);
3594
3594
  };
3595
+ var reconnectEdge = (oldEdge, newConnection, edges, options = { shouldReplaceId: true }) => {
3596
+ const { id: oldEdgeId, ...rest } = oldEdge;
3597
+ if (!newConnection.source || !newConnection.target) {
3598
+ options.onError?.("006", errorMessages["error006"]());
3599
+ return edges;
3600
+ }
3601
+ const foundEdge = edges.find((e) => e.id === oldEdge.id);
3602
+ if (!foundEdge) {
3603
+ options.onError?.("007", errorMessages["error007"](oldEdgeId));
3604
+ return edges;
3605
+ }
3606
+ const edgeIdGenerator = options.getEdgeId || getEdgeId;
3607
+ const edge = {
3608
+ ...rest,
3609
+ id: options.shouldReplaceId ? edgeIdGenerator(newConnection) : oldEdgeId,
3610
+ source: newConnection.source,
3611
+ target: newConnection.target,
3612
+ sourceHandle: newConnection.sourceHandle,
3613
+ targetHandle: newConnection.targetHandle
3614
+ };
3615
+ return edges.filter((e) => e.id !== oldEdgeId).concat(edge);
3616
+ };
3595
3617
  function getStraightPath({ sourceX, sourceY, targetX, targetY }) {
3596
3618
  const [labelX, labelY, offsetX, offsetY] = getEdgeCenter({
3597
3619
  sourceX,
@@ -3849,6 +3871,40 @@ function createMarkerIds(edges, { id: id2, defaultColor, defaultMarkerStart, def
3849
3871
  return markers;
3850
3872
  }, []).sort((a, b) => a.id.localeCompare(b.id));
3851
3873
  }
3874
+ function getNodeToolbarTransform(nodeRect, viewport, position, offset, align) {
3875
+ let alignmentOffset = 0.5;
3876
+ if (align === "start") {
3877
+ alignmentOffset = 0;
3878
+ } else if (align === "end") {
3879
+ alignmentOffset = 1;
3880
+ }
3881
+ let pos = [
3882
+ (nodeRect.x + nodeRect.width * alignmentOffset) * viewport.zoom + viewport.x,
3883
+ nodeRect.y * viewport.zoom + viewport.y - offset
3884
+ ];
3885
+ let shift = [-100 * alignmentOffset, -100];
3886
+ switch (position) {
3887
+ case Position.Right:
3888
+ pos = [
3889
+ (nodeRect.x + nodeRect.width) * viewport.zoom + viewport.x + offset,
3890
+ (nodeRect.y + nodeRect.height * alignmentOffset) * viewport.zoom + viewport.y
3891
+ ];
3892
+ shift = [0, -100 * alignmentOffset];
3893
+ break;
3894
+ case Position.Bottom:
3895
+ pos[1] = (nodeRect.y + nodeRect.height) * viewport.zoom + viewport.y + offset;
3896
+ shift[1] = 0;
3897
+ break;
3898
+ case Position.Left:
3899
+ pos = [
3900
+ nodeRect.x * viewport.zoom + viewport.x - offset,
3901
+ (nodeRect.y + nodeRect.height * alignmentOffset) * viewport.zoom + viewport.y
3902
+ ];
3903
+ shift = [-100, -100 * alignmentOffset];
3904
+ break;
3905
+ }
3906
+ return `translate(${pos[0]}px, ${pos[1]}px) translate(${shift[0]}%, ${shift[1]}%)`;
3907
+ }
3852
3908
  var SELECTED_NODE_Z = 1e3;
3853
3909
  var ROOT_PARENT_Z_INCREMENT = 10;
3854
3910
  var defaultOptions = {
@@ -4216,10 +4272,10 @@ function isParentSelected(node, nodeLookup) {
4216
4272
  }
4217
4273
  return isParentSelected(parentNode, nodeLookup);
4218
4274
  }
4219
- function hasSelector(target, selector, domNode) {
4275
+ function hasSelector(target, selector2, domNode) {
4220
4276
  let current = target;
4221
4277
  do {
4222
- if (current?.matches?.(selector))
4278
+ if (current?.matches?.(selector2))
4223
4279
  return true;
4224
4280
  if (current === domNode)
4225
4281
  return false;
@@ -5177,6 +5233,8 @@ var ResizeControlVariant;
5177
5233
  ResizeControlVariant2["Line"] = "line";
5178
5234
  ResizeControlVariant2["Handle"] = "handle";
5179
5235
  })(ResizeControlVariant || (ResizeControlVariant = {}));
5236
+ var XY_RESIZER_HANDLE_POSITIONS = ["top-left", "top-right", "bottom-left", "bottom-right"];
5237
+ var XY_RESIZER_LINE_POSITIONS = ["top", "right", "bottom", "left"];
5180
5238
  function getResizeDirection({ width, prevWidth, height, prevHeight, affectsX, affectsY }) {
5181
5239
  const deltaWidth = width - prevWidth;
5182
5240
  const deltaHeight = height - prevHeight;
@@ -5551,7 +5609,7 @@ function cc(names) {
5551
5609
  }
5552
5610
  return out;
5553
5611
  }
5554
- function useSyncExternalStoreWithSelector(subscribe, getSnapshot, getServerSnapshot, selector, isEqual) {
5612
+ function useSyncExternalStoreWithSelector(subscribe, getSnapshot, getServerSnapshot, selector2, isEqual) {
5555
5613
  const instRef = useRef(null);
5556
5614
  let inst;
5557
5615
  if (instRef.current === null) {
@@ -5568,7 +5626,7 @@ function useSyncExternalStoreWithSelector(subscribe, getSnapshot, getServerSnaps
5568
5626
  if (!hasMemo) {
5569
5627
  hasMemo = true;
5570
5628
  memoizedSnapshot = nextSnapshot;
5571
- const nextSelection2 = selector(nextSnapshot);
5629
+ const nextSelection2 = selector2(nextSnapshot);
5572
5630
  if (isEqual !== void 0 && inst.hasValue) {
5573
5631
  const currentSelection = inst.value;
5574
5632
  if (isEqual(currentSelection, nextSelection2)) {
@@ -5584,7 +5642,7 @@ function useSyncExternalStoreWithSelector(subscribe, getSnapshot, getServerSnaps
5584
5642
  if (Object.is(prevSnapshot, nextSnapshot)) {
5585
5643
  return prevSelection;
5586
5644
  }
5587
- const nextSelection = selector(nextSnapshot);
5645
+ const nextSelection = selector2(nextSnapshot);
5588
5646
  if (isEqual !== void 0 && isEqual(prevSelection, nextSelection)) {
5589
5647
  memoizedSnapshot = nextSnapshot;
5590
5648
  return prevSelection;
@@ -5597,7 +5655,7 @@ function useSyncExternalStoreWithSelector(subscribe, getSnapshot, getServerSnaps
5597
5655
  const getSnapshotWithSelector = () => memoizedSelector(getSnapshot());
5598
5656
  const getServerSnapshotWithSelector = maybeGetServerSnapshot === null ? void 0 : () => memoizedSelector(maybeGetServerSnapshot());
5599
5657
  return [getSnapshotWithSelector, getServerSnapshotWithSelector];
5600
- }, [getSnapshot, getServerSnapshot, selector, isEqual]);
5658
+ }, [getSnapshot, getServerSnapshot, selector2, isEqual]);
5601
5659
  const value = useSyncExternalStore(subscribe, getSelection, getServerSelection);
5602
5660
  useEffect(() => {
5603
5661
  inst.hasValue = true;
@@ -5644,12 +5702,12 @@ var createStore = (createState) => createState ? createStoreImpl(createState) :
5644
5702
  var { useDebugValue: useDebugValue2 } = ReactExports;
5645
5703
  var { useSyncExternalStoreWithSelector: useSyncExternalStoreWithSelector2 } = use_sync_external_store_with_selector_default;
5646
5704
  var identity3 = (arg) => arg;
5647
- function useStoreWithEqualityFn(api, selector = identity3, equalityFn) {
5705
+ function useStoreWithEqualityFn(api, selector2 = identity3, equalityFn) {
5648
5706
  const slice = useSyncExternalStoreWithSelector2(
5649
5707
  api.subscribe,
5650
5708
  api.getState,
5651
5709
  api.getServerState || api.getInitialState,
5652
- selector,
5710
+ selector2,
5653
5711
  equalityFn
5654
5712
  );
5655
5713
  useDebugValue2(slice);
@@ -5657,7 +5715,7 @@ function useStoreWithEqualityFn(api, selector = identity3, equalityFn) {
5657
5715
  }
5658
5716
  var createWithEqualityFnImpl = (createState, defaultEqualityFn) => {
5659
5717
  const api = createStore(createState);
5660
- const useBoundStoreWithEqualityFn = (selector, equalityFn = defaultEqualityFn) => useStoreWithEqualityFn(api, selector, equalityFn);
5718
+ const useBoundStoreWithEqualityFn = (selector2, equalityFn = defaultEqualityFn) => useStoreWithEqualityFn(api, selector2, equalityFn);
5661
5719
  Object.assign(useBoundStoreWithEqualityFn, api);
5662
5720
  return useBoundStoreWithEqualityFn;
5663
5721
  };
@@ -5703,12 +5761,12 @@ function shallow$1(objA, objB) {
5703
5761
  var StoreContext = createContext(null);
5704
5762
  var Provider$1 = StoreContext.Provider;
5705
5763
  var zustandErrorMessage = errorMessages["error001"]("react");
5706
- function useStore(selector, equalityFn) {
5764
+ function useStore(selector2, equalityFn) {
5707
5765
  const store = useContext(StoreContext);
5708
5766
  if (store === null) {
5709
5767
  throw new Error(zustandErrorMessage);
5710
5768
  }
5711
- return useStoreWithEqualityFn(store, selector, equalityFn);
5769
+ return useStoreWithEqualityFn(store, selector2, equalityFn);
5712
5770
  }
5713
5771
  function useStoreApi() {
5714
5772
  const store = useContext(StoreContext);
@@ -6248,6 +6306,12 @@ function addEdge2(edgeParams, edges, options = {}) {
6248
6306
  onError: options.onError ?? defaultOnError
6249
6307
  });
6250
6308
  }
6309
+ function reconnectEdge2(oldEdge, newConnection, edges, options = { shouldReplaceId: true }) {
6310
+ return reconnectEdge(oldEdge, newConnection, edges, {
6311
+ ...options,
6312
+ onError: options.onError ?? defaultOnError
6313
+ });
6314
+ }
6251
6315
  var isNode = (element) => isNodeBase(element);
6252
6316
  var isEdge = (element) => isEdgeBase(element);
6253
6317
  function fixedForwardRef(render) {
@@ -8900,8 +8964,70 @@ function ResizeControl({ nodeId, position, variant = ResizeControlVariant.Handle
8900
8964
  ...color2 && { [isHandleControl ? "backgroundColor" : "borderColor"]: color2 }
8901
8965
  }, children: children2 });
8902
8966
  }
8903
- memo(ResizeControl);
8967
+ var NodeResizeControl = memo(ResizeControl);
8968
+ function NodeResizer({ nodeId, isVisible = true, handleClassName, handleStyle, lineClassName, lineStyle, color: color2, minWidth = 10, minHeight = 10, maxWidth = Number.MAX_VALUE, maxHeight = Number.MAX_VALUE, keepAspectRatio = false, autoScale = true, shouldResize, onResizeStart, onResize, onResizeEnd }) {
8969
+ if (!isVisible) {
8970
+ return null;
8971
+ }
8972
+ return jsxs(Fragment, { children: [XY_RESIZER_LINE_POSITIONS.map((position) => jsx(NodeResizeControl, { className: lineClassName, style: lineStyle, nodeId, position, variant: ResizeControlVariant.Line, color: color2, minWidth, minHeight, maxWidth, maxHeight, onResizeStart, keepAspectRatio, autoScale, shouldResize, onResize, onResizeEnd }, position)), XY_RESIZER_HANDLE_POSITIONS.map((position) => jsx(NodeResizeControl, { className: handleClassName, style: handleStyle, nodeId, position, color: color2, minWidth, minHeight, maxWidth, maxHeight, onResizeStart, keepAspectRatio, autoScale, shouldResize, onResize, onResizeEnd }, position))] });
8973
+ }
8974
+ var selector = (state) => state.domNode?.querySelector(".react-flow__renderer");
8975
+ function NodeToolbarPortal({ children: children2 }) {
8976
+ const wrapperRef = useStore(selector);
8977
+ if (!wrapperRef) {
8978
+ return null;
8979
+ }
8980
+ return createPortal(children2, wrapperRef);
8981
+ }
8982
+ var nodeEqualityFn = (a, b) => a?.internals.positionAbsolute.x !== b?.internals.positionAbsolute.x || a?.internals.positionAbsolute.y !== b?.internals.positionAbsolute.y || a?.measured.width !== b?.measured.width || a?.measured.height !== b?.measured.height || a?.selected !== b?.selected || a?.internals.z !== b?.internals.z;
8983
+ var nodesEqualityFn = (a, b) => {
8984
+ if (a.size !== b.size) {
8985
+ return false;
8986
+ }
8987
+ for (const [key, node] of a) {
8988
+ if (nodeEqualityFn(node, b.get(key))) {
8989
+ return false;
8990
+ }
8991
+ }
8992
+ return true;
8993
+ };
8994
+ var storeSelector = (state) => ({
8995
+ x: state.transform[0],
8996
+ y: state.transform[1],
8997
+ zoom: state.transform[2],
8998
+ selectedNodesCount: state.nodes.filter((node) => node.selected).length
8999
+ });
9000
+ function NodeToolbar({ nodeId, children: children2, className, style: style2, isVisible, position = Position.Top, offset = 10, align = "center", ...rest }) {
9001
+ const contextNodeId = useNodeId();
9002
+ const nodesSelector = useCallback((state) => {
9003
+ const nodeIds = Array.isArray(nodeId) ? nodeId : [nodeId || contextNodeId || ""];
9004
+ const internalNodes = nodeIds.reduce((res, id2) => {
9005
+ const node = state.nodeLookup.get(id2);
9006
+ if (node) {
9007
+ res.set(node.id, node);
9008
+ }
9009
+ return res;
9010
+ }, /* @__PURE__ */ new Map());
9011
+ return internalNodes;
9012
+ }, [nodeId, contextNodeId]);
9013
+ const nodes = useStore(nodesSelector, nodesEqualityFn);
9014
+ const { x, y, zoom, selectedNodesCount } = useStore(storeSelector, shallow$1);
9015
+ const isActive = typeof isVisible === "boolean" ? isVisible : nodes.size === 1 && nodes.values().next().value?.selected && selectedNodesCount === 1;
9016
+ if (!isActive || !nodes.size) {
9017
+ return null;
9018
+ }
9019
+ const nodeRect = getInternalNodesBounds(nodes);
9020
+ const nodesArray = Array.from(nodes.values());
9021
+ const zIndex = Math.max(...nodesArray.map((node) => node.internals.z + 1));
9022
+ const wrapperStyle2 = {
9023
+ position: "absolute",
9024
+ transform: getNodeToolbarTransform(nodeRect, { x, y, zoom }, position, offset, align),
9025
+ zIndex,
9026
+ ...style2
9027
+ };
9028
+ return jsx(NodeToolbarPortal, { children: jsx("div", { style: wrapperStyle2, className: cc(["react-flow__node-toolbar", className]), ...rest, "data-id": nodesArray.reduce((acc, node) => `${acc}${node.id} `, "").trim(), children: children2 }) });
9029
+ }
8904
9030
 
8905
- export { Background, BackgroundVariant, Controls, Handle, MiniMap, Position, ReactFlowProvider, addEdge2 as addEdge, applyEdgeChanges, applyNodeChanges, index, useReactFlow };
8906
- //# sourceMappingURL=chunk-NF6NPY5N.js.map
8907
- //# sourceMappingURL=chunk-NF6NPY5N.js.map
9031
+ export { Background, BackgroundVariant, Controls, Handle, MiniMap, NodeResizer, NodeToolbar, Position, ReactFlowProvider, addEdge2 as addEdge, applyEdgeChanges, applyNodeChanges, index, reconnectEdge2 as reconnectEdge, useReactFlow };
9032
+ //# sourceMappingURL=chunk-ZB4HHQMR.js.map
9033
+ //# sourceMappingURL=chunk-ZB4HHQMR.js.map