@statelyai/flow 0.1.0 → 0.4.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.
- package/README.md +20 -3
- package/dist/esm/index.d.ts +1294 -544
- package/dist/esm/index.js +2526 -321
- package/package.json +17 -15
package/dist/esm/index.js
CHANGED
|
@@ -1,6 +1,18 @@
|
|
|
1
1
|
import { createStore } from "@xstate/store";
|
|
2
2
|
import { undoRedo } from "@xstate/store/undo";
|
|
3
|
-
import { enableMapSet, produce } from "immer";
|
|
3
|
+
import { current, enableMapSet, isDraft, produce, setAutoFreeze } from "immer";
|
|
4
|
+
//#region src/profile.ts
|
|
5
|
+
function defineFlowProfile(profile) {
|
|
6
|
+
return profile;
|
|
7
|
+
}
|
|
8
|
+
const defaultFlowProfile = defineFlowProfile({
|
|
9
|
+
defaultMode: "interactive",
|
|
10
|
+
modes: {
|
|
11
|
+
interactive: {},
|
|
12
|
+
readonly: {}
|
|
13
|
+
}
|
|
14
|
+
});
|
|
15
|
+
//#endregion
|
|
4
16
|
//#region src/viewport/index.ts
|
|
5
17
|
/** Create a default viewport (origin, no zoom) */
|
|
6
18
|
function createViewport(x = 0, y = 0, zoom = 1) {
|
|
@@ -97,6 +109,14 @@ function zoomTo(viewport, zoom, center) {
|
|
|
97
109
|
function clampZoom(zoom, minZoom, maxZoom) {
|
|
98
110
|
return Math.min(Math.max(zoom, minZoom), maxZoom);
|
|
99
111
|
}
|
|
112
|
+
/** Viewport that places the given canvas point at the center of the screen. */
|
|
113
|
+
function viewportCenteredOn(center, size, zoom) {
|
|
114
|
+
return {
|
|
115
|
+
x: size.width / 2 - center.x * zoom,
|
|
116
|
+
y: size.height / 2 - center.y * zoom,
|
|
117
|
+
zoom
|
|
118
|
+
};
|
|
119
|
+
}
|
|
100
120
|
//#endregion
|
|
101
121
|
//#region src/spatial.ts
|
|
102
122
|
/** Resolved visual position: layout position + drag offset */
|
|
@@ -108,12 +128,25 @@ function resolvedPosition(node) {
|
|
|
108
128
|
}
|
|
109
129
|
/** Resolved visual bounds: layout position + drag offset + dimensions.
|
|
110
130
|
* Uses style.customWidth/customHeight when set (from resize), otherwise inherent width/height. */
|
|
131
|
+
/** Size the geometry should use, in priority order:
|
|
132
|
+
* 1. `style.customWidth/customHeight` (an explicit resize override),
|
|
133
|
+
* 2. for `autoSize` nodes, the DOM-`measured` size (the renderer owns it),
|
|
134
|
+
* 3. the authored `width/height`.
|
|
135
|
+
* Authored `width/height` are never mutated — measured size lives on
|
|
136
|
+
* `node.measured`, so this is the single place that reconciles the two. */
|
|
137
|
+
function resolvedSize(node) {
|
|
138
|
+
return {
|
|
139
|
+
width: node.style?.customWidth ?? (node.autoSize ? node.measured?.width ?? node.width : node.width),
|
|
140
|
+
height: node.style?.customHeight ?? (node.autoSize ? node.measured?.height ?? node.height : node.height)
|
|
141
|
+
};
|
|
142
|
+
}
|
|
111
143
|
function resolvedBounds(node) {
|
|
144
|
+
const size = resolvedSize(node);
|
|
112
145
|
return {
|
|
113
146
|
x: node.x + (node.dx ?? 0),
|
|
114
147
|
y: node.y + (node.dy ?? 0),
|
|
115
|
-
width:
|
|
116
|
-
height:
|
|
148
|
+
width: size.width,
|
|
149
|
+
height: size.height
|
|
117
150
|
};
|
|
118
151
|
}
|
|
119
152
|
/** Resolved bounds with visual snap — for rendering during drag.
|
|
@@ -129,11 +162,12 @@ function snappedBounds(node, grid, snapOffset) {
|
|
|
129
162
|
x = snapToGrid(x, grid[0]);
|
|
130
163
|
y = snapToGrid(y, grid[1]);
|
|
131
164
|
}
|
|
165
|
+
const size = resolvedSize(node);
|
|
132
166
|
return {
|
|
133
167
|
x,
|
|
134
168
|
y,
|
|
135
|
-
width:
|
|
136
|
-
height:
|
|
169
|
+
width: size.width,
|
|
170
|
+
height: size.height
|
|
137
171
|
};
|
|
138
172
|
}
|
|
139
173
|
/** Compute bounding rect of all nodes + edge labels (resolved positions) */
|
|
@@ -172,6 +206,17 @@ function getGraphBounds(nodes, edges) {
|
|
|
172
206
|
function rectsIntersect(a, b) {
|
|
173
207
|
return a.x < b.x + b.width && a.x + a.width > b.x && a.y < b.y + b.height && a.y + a.height > b.y;
|
|
174
208
|
}
|
|
209
|
+
/** Smallest axis-aligned rect containing both `a` and `b`. */
|
|
210
|
+
function unionRect(a, b) {
|
|
211
|
+
const x = Math.min(a.x, b.x);
|
|
212
|
+
const y = Math.min(a.y, b.y);
|
|
213
|
+
return {
|
|
214
|
+
x,
|
|
215
|
+
y,
|
|
216
|
+
width: Math.max(a.x + a.width, b.x + b.width) - x,
|
|
217
|
+
height: Math.max(a.y + a.height, b.y + b.height) - y
|
|
218
|
+
};
|
|
219
|
+
}
|
|
175
220
|
/** Snap a value to the nearest grid increment */
|
|
176
221
|
function snapToGrid(value, gridSize) {
|
|
177
222
|
return Math.round(value / gridSize) * gridSize;
|
|
@@ -690,6 +735,29 @@ function isPointNearPath(point, path, tolerance) {
|
|
|
690
735
|
}
|
|
691
736
|
return false;
|
|
692
737
|
}
|
|
738
|
+
/**
|
|
739
|
+
* Axis-aligned bounding box of a path's control/anchor points. A cubic bezier
|
|
740
|
+
* lies within the convex hull of its control points, so this is a conservative
|
|
741
|
+
* superset of the rendered curve — exact enough for viewport culling (no false
|
|
742
|
+
* negatives) and ~O(points) instead of sampling the curve. Returns null for an
|
|
743
|
+
* empty path.
|
|
744
|
+
*/
|
|
745
|
+
function pathBounds(path) {
|
|
746
|
+
let minX = Infinity, minY = Infinity, maxX = -Infinity, maxY = -Infinity;
|
|
747
|
+
for (const point of path.points) {
|
|
748
|
+
if (point.x < minX) minX = point.x;
|
|
749
|
+
if (point.y < minY) minY = point.y;
|
|
750
|
+
if (point.x > maxX) maxX = point.x;
|
|
751
|
+
if (point.y > maxY) maxY = point.y;
|
|
752
|
+
}
|
|
753
|
+
if (!isFinite(minX)) return null;
|
|
754
|
+
return {
|
|
755
|
+
x: minX,
|
|
756
|
+
y: minY,
|
|
757
|
+
width: maxX - minX,
|
|
758
|
+
height: maxY - minY
|
|
759
|
+
};
|
|
760
|
+
}
|
|
693
761
|
function rectIntersectsPath(rect, path) {
|
|
694
762
|
const samplePoints = samplePathPoints(path);
|
|
695
763
|
for (const point of samplePoints) if (pointInRect(point, rect)) return true;
|
|
@@ -798,8 +866,246 @@ function onSegment(a, b, c) {
|
|
|
798
866
|
return Math.min(a.x, b.x) <= c.x && c.x <= Math.max(a.x, b.x) && Math.min(a.y, b.y) <= c.y && c.y <= Math.max(a.y, b.y);
|
|
799
867
|
}
|
|
800
868
|
//#endregion
|
|
869
|
+
//#region src/graph-index.ts
|
|
870
|
+
const nodeIndexCache = /* @__PURE__ */ new WeakMap();
|
|
871
|
+
const edgeIndexCache = /* @__PURE__ */ new WeakMap();
|
|
872
|
+
function indexNodes(nodes) {
|
|
873
|
+
let index = nodeIndexCache.get(nodes);
|
|
874
|
+
if (!index) {
|
|
875
|
+
index = /* @__PURE__ */ new Map();
|
|
876
|
+
for (const node of nodes) if (!index.has(node.id)) index.set(node.id, node);
|
|
877
|
+
nodeIndexCache.set(nodes, index);
|
|
878
|
+
}
|
|
879
|
+
return index;
|
|
880
|
+
}
|
|
881
|
+
function indexEdges(edges) {
|
|
882
|
+
let index = edgeIndexCache.get(edges);
|
|
883
|
+
if (!index) {
|
|
884
|
+
index = /* @__PURE__ */ new Map();
|
|
885
|
+
for (const edge of edges) if (!index.has(edge.id)) index.set(edge.id, edge);
|
|
886
|
+
edgeIndexCache.set(edges, index);
|
|
887
|
+
}
|
|
888
|
+
return index;
|
|
889
|
+
}
|
|
890
|
+
const childrenIndexCache = /* @__PURE__ */ new WeakMap();
|
|
891
|
+
/** parentId → child nodes, cached on array identity. Self-parents ignored. */
|
|
892
|
+
function indexChildren(nodes) {
|
|
893
|
+
let index = childrenIndexCache.get(nodes);
|
|
894
|
+
if (!index) {
|
|
895
|
+
index = /* @__PURE__ */ new Map();
|
|
896
|
+
for (const node of nodes) {
|
|
897
|
+
const parentId = node.parentId ?? null;
|
|
898
|
+
if (parentId == null || parentId === node.id) continue;
|
|
899
|
+
const children = index.get(parentId);
|
|
900
|
+
if (children) children.push(node);
|
|
901
|
+
else index.set(parentId, [node]);
|
|
902
|
+
}
|
|
903
|
+
childrenIndexCache.set(nodes, index);
|
|
904
|
+
}
|
|
905
|
+
return index;
|
|
906
|
+
}
|
|
907
|
+
const depthCache = /* @__PURE__ */ new WeakMap();
|
|
908
|
+
/**
|
|
909
|
+
* Hierarchy depth per node id (roots = 0), cached on array identity.
|
|
910
|
+
* Cycle-safe; missing parents count as roots. Renderers can sort by this so
|
|
911
|
+
* parents paint beneath children regardless of array order.
|
|
912
|
+
*/
|
|
913
|
+
function nodeDepths(nodes) {
|
|
914
|
+
let depths = depthCache.get(nodes);
|
|
915
|
+
if (!depths) {
|
|
916
|
+
const index = indexNodes(nodes);
|
|
917
|
+
const resolved = /* @__PURE__ */ new Map();
|
|
918
|
+
const depthOf = (node, seen) => {
|
|
919
|
+
const existing = resolved.get(node.id);
|
|
920
|
+
if (existing !== void 0) return existing;
|
|
921
|
+
const parentId = node.parentId ?? null;
|
|
922
|
+
if (parentId == null || parentId === node.id || seen.has(node.id)) {
|
|
923
|
+
resolved.set(node.id, 0);
|
|
924
|
+
return 0;
|
|
925
|
+
}
|
|
926
|
+
seen.add(node.id);
|
|
927
|
+
const parent = index.get(parentId);
|
|
928
|
+
const depth = parent ? depthOf(parent, seen) + 1 : 0;
|
|
929
|
+
resolved.set(node.id, depth);
|
|
930
|
+
return depth;
|
|
931
|
+
};
|
|
932
|
+
for (const node of nodes) depthOf(node, /* @__PURE__ */ new Set());
|
|
933
|
+
depths = resolved;
|
|
934
|
+
depthCache.set(nodes, depths);
|
|
935
|
+
}
|
|
936
|
+
return depths;
|
|
937
|
+
}
|
|
938
|
+
//#endregion
|
|
939
|
+
//#region src/hierarchy.ts
|
|
940
|
+
function resolveGroupPadding(padding) {
|
|
941
|
+
if (typeof padding === "number") return {
|
|
942
|
+
top: padding,
|
|
943
|
+
right: padding,
|
|
944
|
+
bottom: padding,
|
|
945
|
+
left: padding
|
|
946
|
+
};
|
|
947
|
+
return {
|
|
948
|
+
top: padding?.top ?? 16,
|
|
949
|
+
right: padding?.right ?? 16,
|
|
950
|
+
bottom: padding?.bottom ?? 16,
|
|
951
|
+
left: padding?.left ?? 16
|
|
952
|
+
};
|
|
953
|
+
}
|
|
954
|
+
function isGroup(node) {
|
|
955
|
+
return node.group !== void 0;
|
|
956
|
+
}
|
|
957
|
+
function isDerivedGroup(node) {
|
|
958
|
+
return node.group?.type === "derived";
|
|
959
|
+
}
|
|
960
|
+
/**
|
|
961
|
+
* Derive a `derived` group's geometry from its content size and the union of
|
|
962
|
+
* its children's bounds (pass null when the group has no children — the
|
|
963
|
+
* anchor point then places the group's top-left).
|
|
964
|
+
*/
|
|
965
|
+
function deriveGroupGeometry(contentSize, childrenBounds, layout, anchor = {
|
|
966
|
+
x: 0,
|
|
967
|
+
y: 0
|
|
968
|
+
}) {
|
|
969
|
+
const side = layout.content ?? "top";
|
|
970
|
+
const padding = resolveGroupPadding(layout.padding);
|
|
971
|
+
const minArea = layout.minChildArea ?? {
|
|
972
|
+
width: 0,
|
|
973
|
+
height: 0
|
|
974
|
+
};
|
|
975
|
+
let childArea;
|
|
976
|
+
if (childrenBounds) childArea = {
|
|
977
|
+
x: childrenBounds.x - padding.left,
|
|
978
|
+
y: childrenBounds.y - padding.top,
|
|
979
|
+
width: Math.max(childrenBounds.width + padding.left + padding.right, minArea.width),
|
|
980
|
+
height: Math.max(childrenBounds.height + padding.top + padding.bottom, minArea.height)
|
|
981
|
+
};
|
|
982
|
+
else {
|
|
983
|
+
const horizontal = side === "top" || side === "bottom";
|
|
984
|
+
childArea = {
|
|
985
|
+
x: anchor.x,
|
|
986
|
+
y: anchor.y,
|
|
987
|
+
width: Math.max(minArea.width, padding.left + padding.right, horizontal ? contentSize.width : 0),
|
|
988
|
+
height: Math.max(minArea.height, padding.top + padding.bottom, horizontal ? 0 : contentSize.height)
|
|
989
|
+
};
|
|
990
|
+
if (side === "top") childArea.y += contentSize.height;
|
|
991
|
+
if (side === "left") childArea.x += contentSize.width;
|
|
992
|
+
}
|
|
993
|
+
return composeGeometry(contentSize, childArea, side);
|
|
994
|
+
}
|
|
995
|
+
/**
|
|
996
|
+
* Geometry for a `fixed` group: stored bounds are authoritative; the content
|
|
997
|
+
* block is carved from the configured side and the child area is the
|
|
998
|
+
* remainder inset by padding.
|
|
999
|
+
*/
|
|
1000
|
+
function fixedGroupGeometry(bounds, contentSize, layout) {
|
|
1001
|
+
const side = layout.content ?? "top";
|
|
1002
|
+
const padding = resolveGroupPadding(layout.padding);
|
|
1003
|
+
let contentBounds;
|
|
1004
|
+
let childArea;
|
|
1005
|
+
if (side === "top" || side === "bottom") {
|
|
1006
|
+
const contentHeight = Math.min(contentSize.height, bounds.height);
|
|
1007
|
+
contentBounds = {
|
|
1008
|
+
x: bounds.x,
|
|
1009
|
+
y: side === "top" ? bounds.y : bounds.y + bounds.height - contentHeight,
|
|
1010
|
+
width: bounds.width,
|
|
1011
|
+
height: contentHeight
|
|
1012
|
+
};
|
|
1013
|
+
childArea = {
|
|
1014
|
+
x: bounds.x + padding.left,
|
|
1015
|
+
y: (side === "top" ? bounds.y + contentHeight : bounds.y) + padding.top,
|
|
1016
|
+
width: Math.max(bounds.width - padding.left - padding.right, 0),
|
|
1017
|
+
height: Math.max(bounds.height - contentHeight - padding.top - padding.bottom, 0)
|
|
1018
|
+
};
|
|
1019
|
+
} else {
|
|
1020
|
+
const contentWidth = Math.min(contentSize.width, bounds.width);
|
|
1021
|
+
contentBounds = {
|
|
1022
|
+
x: side === "left" ? bounds.x : bounds.x + bounds.width - contentWidth,
|
|
1023
|
+
y: bounds.y,
|
|
1024
|
+
width: contentWidth,
|
|
1025
|
+
height: bounds.height
|
|
1026
|
+
};
|
|
1027
|
+
childArea = {
|
|
1028
|
+
x: (side === "left" ? bounds.x + contentWidth : bounds.x) + padding.left,
|
|
1029
|
+
y: bounds.y + padding.top,
|
|
1030
|
+
width: Math.max(bounds.width - contentWidth - padding.left - padding.right, 0),
|
|
1031
|
+
height: Math.max(bounds.height - padding.top - padding.bottom, 0)
|
|
1032
|
+
};
|
|
1033
|
+
}
|
|
1034
|
+
return {
|
|
1035
|
+
bounds,
|
|
1036
|
+
contentBounds,
|
|
1037
|
+
childArea
|
|
1038
|
+
};
|
|
1039
|
+
}
|
|
1040
|
+
function composeGeometry(contentSize, childArea, side) {
|
|
1041
|
+
if (side === "top" || side === "bottom") {
|
|
1042
|
+
const width = Math.max(childArea.width, contentSize.width);
|
|
1043
|
+
const x = childArea.x - (width - childArea.width) / 2;
|
|
1044
|
+
const contentY = side === "top" ? childArea.y - contentSize.height : childArea.y + childArea.height;
|
|
1045
|
+
return {
|
|
1046
|
+
bounds: {
|
|
1047
|
+
x,
|
|
1048
|
+
y: side === "top" ? contentY : childArea.y,
|
|
1049
|
+
width,
|
|
1050
|
+
height: contentSize.height + childArea.height
|
|
1051
|
+
},
|
|
1052
|
+
contentBounds: {
|
|
1053
|
+
x,
|
|
1054
|
+
y: contentY,
|
|
1055
|
+
width,
|
|
1056
|
+
height: contentSize.height
|
|
1057
|
+
},
|
|
1058
|
+
childArea
|
|
1059
|
+
};
|
|
1060
|
+
}
|
|
1061
|
+
const height = Math.max(childArea.height, contentSize.height);
|
|
1062
|
+
const y = childArea.y - (height - childArea.height) / 2;
|
|
1063
|
+
const contentX = side === "left" ? childArea.x - contentSize.width : childArea.x + childArea.width;
|
|
1064
|
+
return {
|
|
1065
|
+
bounds: {
|
|
1066
|
+
x: side === "left" ? contentX : childArea.x,
|
|
1067
|
+
y,
|
|
1068
|
+
width: contentSize.width + childArea.width,
|
|
1069
|
+
height
|
|
1070
|
+
},
|
|
1071
|
+
contentBounds: {
|
|
1072
|
+
x: contentX,
|
|
1073
|
+
y,
|
|
1074
|
+
width: contentSize.width,
|
|
1075
|
+
height
|
|
1076
|
+
},
|
|
1077
|
+
childArea
|
|
1078
|
+
};
|
|
1079
|
+
}
|
|
1080
|
+
/**
|
|
1081
|
+
* The id of the node that contains this edge, or null for the root.
|
|
1082
|
+
* Override per engine via `FlowEngineOptions.edgeOwner`.
|
|
1083
|
+
*/
|
|
1084
|
+
function getEdgeOwner(edge, nodes) {
|
|
1085
|
+
const index = indexNodes(nodes);
|
|
1086
|
+
const source = index.get(edge.sourceId);
|
|
1087
|
+
const target = index.get(edge.targetId);
|
|
1088
|
+
if (!source) return null;
|
|
1089
|
+
if (!target || source.id === target.id) return source.parentId ?? null;
|
|
1090
|
+
const sourceChain = new Set([source.id]);
|
|
1091
|
+
let marker = source.parentId ?? null;
|
|
1092
|
+
while (marker && !sourceChain.has(marker)) {
|
|
1093
|
+
sourceChain.add(marker);
|
|
1094
|
+
marker = index.get(marker)?.parentId ?? null;
|
|
1095
|
+
}
|
|
1096
|
+
let candidate = target.id;
|
|
1097
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1098
|
+
while (candidate && !seen.has(candidate)) {
|
|
1099
|
+
if (sourceChain.has(candidate)) return candidate;
|
|
1100
|
+
seen.add(candidate);
|
|
1101
|
+
candidate = index.get(candidate)?.parentId ?? null;
|
|
1102
|
+
}
|
|
1103
|
+
return null;
|
|
1104
|
+
}
|
|
1105
|
+
//#endregion
|
|
801
1106
|
//#region src/store/index.ts
|
|
802
1107
|
enableMapSet();
|
|
1108
|
+
if (process.env.NODE_ENV === "production") setAutoFreeze(false);
|
|
803
1109
|
const UNDOABLE_EVENT_TYPES = [
|
|
804
1110
|
"createNode",
|
|
805
1111
|
"deleteNode",
|
|
@@ -809,9 +1115,10 @@ const UNDOABLE_EVENT_TYPES = [
|
|
|
809
1115
|
"updateEdgeData",
|
|
810
1116
|
"deleteSelected",
|
|
811
1117
|
"reparentNode",
|
|
812
|
-
"
|
|
813
|
-
"
|
|
1118
|
+
"dragStart",
|
|
1119
|
+
"resizeStart",
|
|
814
1120
|
"reconnectEdge",
|
|
1121
|
+
"moveNodes",
|
|
815
1122
|
"applyLayoutResult",
|
|
816
1123
|
"bakeLabelPositions"
|
|
817
1124
|
];
|
|
@@ -838,20 +1145,229 @@ function applyBeforeDelete(options, draft, reason, nodes, edges) {
|
|
|
838
1145
|
}
|
|
839
1146
|
return false;
|
|
840
1147
|
}
|
|
1148
|
+
/**
|
|
1149
|
+
* Validate an edge about to be inserted. Returns an actionable error message,
|
|
1150
|
+
* or null when the edge is valid. Used by `createEdge` and undo tracking.
|
|
1151
|
+
*/
|
|
1152
|
+
function validateNewEdge(ctx, edge) {
|
|
1153
|
+
if (ctx.graph.edges.some((e) => e.id === edge.id)) return `createEdge rejected: an edge with id "${edge.id}" already exists. Edge ids must be unique — use reconnectEdge or updateEdgeData to modify the existing edge, or create the edge with a different id.`;
|
|
1154
|
+
const missing = [];
|
|
1155
|
+
if (!ctx.graph.nodes.some((n) => n.id === edge.sourceId)) missing.push(`source node "${edge.sourceId}"`);
|
|
1156
|
+
if (!ctx.graph.nodes.some((n) => n.id === edge.targetId)) missing.push(`target node "${edge.targetId}"`);
|
|
1157
|
+
if (missing.length > 0) return `createEdge rejected: edge "${edge.id}" references ${missing.join(" and ")}, which ${missing.length === 1 ? "does" : "do"} not exist in the graph. Create the node(s) first, then the edge.`;
|
|
1158
|
+
return null;
|
|
1159
|
+
}
|
|
1160
|
+
/**
|
|
1161
|
+
* Validate a reparent operation. Returns an error message or null when valid.
|
|
1162
|
+
* Rejects self-parenting, non-existent parents, and cycles (parentId is a
|
|
1163
|
+
* descendant of nodeId).
|
|
1164
|
+
*/
|
|
1165
|
+
function reparentProblem(graph, nodeId, parentId) {
|
|
1166
|
+
if (parentId === null) return null;
|
|
1167
|
+
if (parentId === nodeId) return `reparentNode rejected: node "${nodeId}" cannot be its own parent.`;
|
|
1168
|
+
if (!graph.nodes.some((n) => n.id === parentId)) return `reparentNode rejected: parent node "${parentId}" does not exist in the graph.`;
|
|
1169
|
+
const nodeIndex = new Map(graph.nodes.map((n) => [n.id, n]));
|
|
1170
|
+
let candidate = parentId;
|
|
1171
|
+
const seen = /* @__PURE__ */ new Set();
|
|
1172
|
+
while (candidate && !seen.has(candidate)) {
|
|
1173
|
+
if (candidate === nodeId) return `reparentNode rejected: node "${parentId}" is a descendant of "${nodeId}", which would create a cycle.`;
|
|
1174
|
+
seen.add(candidate);
|
|
1175
|
+
candidate = nodeIndex.get(candidate)?.parentId ?? null;
|
|
1176
|
+
}
|
|
1177
|
+
return null;
|
|
1178
|
+
}
|
|
1179
|
+
/** Drop hover/highlight references to entities that no longer exist. */
|
|
1180
|
+
function pruneVisualState(draft, deletedIds) {
|
|
1181
|
+
for (const id of deletedIds) draft.hoveredEntityIds.delete(id);
|
|
1182
|
+
for (const [kind, bucket] of draft.highlights) {
|
|
1183
|
+
for (const id of deletedIds) bucket.delete(id);
|
|
1184
|
+
if (bucket.size === 0) draft.highlights.delete(kind);
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
841
1187
|
function isUndoableEventType(type) {
|
|
842
1188
|
return UNDOABLE_EVENT_TYPES.includes(type);
|
|
843
1189
|
}
|
|
1190
|
+
function planNodeMovement(ctx, dx, dy, options = {}) {
|
|
1191
|
+
const nodes = ctx.graph.nodes;
|
|
1192
|
+
const edges = ctx.graph.edges;
|
|
1193
|
+
const nodeIndex = indexNodes(nodes);
|
|
1194
|
+
const edgeIndex = indexEdges(edges);
|
|
1195
|
+
const movingNodeIds = /* @__PURE__ */ new Set();
|
|
1196
|
+
const labelEdgeIds = /* @__PURE__ */ new Set();
|
|
1197
|
+
for (const id of ctx.selection) {
|
|
1198
|
+
const node = nodeIndex.get(id);
|
|
1199
|
+
if (node) {
|
|
1200
|
+
if (node.draggable !== false) movingNodeIds.add(id);
|
|
1201
|
+
continue;
|
|
1202
|
+
}
|
|
1203
|
+
if (options.includeLabels) {
|
|
1204
|
+
const edge = edgeIndex.get(id);
|
|
1205
|
+
if (edge && edge.draggable !== false && edge.label) labelEdgeIds.add(id);
|
|
1206
|
+
}
|
|
1207
|
+
}
|
|
1208
|
+
if (movingNodeIds.size === 0 && labelEdgeIds.size === 0) return null;
|
|
1209
|
+
let childrenByParent = null;
|
|
1210
|
+
for (const node of nodes) {
|
|
1211
|
+
const parentId = node.parentId ?? null;
|
|
1212
|
+
if (parentId == null || parentId === node.id) continue;
|
|
1213
|
+
if (!childrenByParent) childrenByParent = /* @__PURE__ */ new Map();
|
|
1214
|
+
const children = childrenByParent.get(parentId);
|
|
1215
|
+
if (children) children.push(node.id);
|
|
1216
|
+
else childrenByParent.set(parentId, [node.id]);
|
|
1217
|
+
}
|
|
1218
|
+
const deltasById = /* @__PURE__ */ new Map();
|
|
1219
|
+
for (const id of movingNodeIds) {
|
|
1220
|
+
const node = nodeIndex.get(id);
|
|
1221
|
+
const oldDx = node.dx ?? 0;
|
|
1222
|
+
const oldDy = node.dy ?? 0;
|
|
1223
|
+
let newDx = oldDx + dx;
|
|
1224
|
+
let newDy = oldDy + dy;
|
|
1225
|
+
if (ctx.nodeExtent) {
|
|
1226
|
+
const nodeSize = resolvedSize(node);
|
|
1227
|
+
const clampedX = Math.max(ctx.nodeExtent[0].x, Math.min(node.x + newDx, ctx.nodeExtent[1].x - nodeSize.width));
|
|
1228
|
+
const clampedY = Math.max(ctx.nodeExtent[0].y, Math.min(node.y + newDy, ctx.nodeExtent[1].y - nodeSize.height));
|
|
1229
|
+
newDx = clampedX - node.x;
|
|
1230
|
+
newDy = clampedY - node.y;
|
|
1231
|
+
}
|
|
1232
|
+
const parent = node.parentId ? nodeIndex.get(node.parentId) : void 0;
|
|
1233
|
+
if (parent?.group?.type === "fixed") {
|
|
1234
|
+
const axes = parent.group.clampChildren ?? "none";
|
|
1235
|
+
if (axes !== "none") {
|
|
1236
|
+
const area = fixedGroupGeometry({
|
|
1237
|
+
x: parent.x + (parent.dx ?? 0),
|
|
1238
|
+
y: parent.y + (parent.dy ?? 0),
|
|
1239
|
+
width: parent.style?.customWidth ?? parent.width,
|
|
1240
|
+
height: parent.style?.customHeight ?? parent.height
|
|
1241
|
+
}, parent.group.contentSize ?? {
|
|
1242
|
+
width: 0,
|
|
1243
|
+
height: 0
|
|
1244
|
+
}, parent.group).childArea;
|
|
1245
|
+
const { width: nodeWidth, height: nodeHeight } = resolvedSize(node);
|
|
1246
|
+
if (axes === "x" || axes === "both") newDx = Math.max(area.x, Math.min(node.x + newDx, area.x + area.width - nodeWidth)) - node.x;
|
|
1247
|
+
if (axes === "y" || axes === "both") newDy = Math.max(area.y, Math.min(node.y + newDy, area.y + area.height - nodeHeight)) - node.y;
|
|
1248
|
+
}
|
|
1249
|
+
}
|
|
1250
|
+
if (ctx.constrainNodeMovement) {
|
|
1251
|
+
const adjusted = ctx.constrainNodeMovement(node, {
|
|
1252
|
+
x: node.x + newDx,
|
|
1253
|
+
y: node.y + newDy
|
|
1254
|
+
}, ctx);
|
|
1255
|
+
if (adjusted) {
|
|
1256
|
+
newDx = adjusted.x - node.x;
|
|
1257
|
+
newDy = adjusted.y - node.y;
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
const applied = {
|
|
1261
|
+
x: newDx - oldDx,
|
|
1262
|
+
y: newDy - oldDy
|
|
1263
|
+
};
|
|
1264
|
+
if (applied.x === 0 && applied.y === 0) continue;
|
|
1265
|
+
deltasById.set(id, applied);
|
|
1266
|
+
if (childrenByParent) collectDescendantDeltas(childrenByParent, id, applied, movingNodeIds, deltasById);
|
|
1267
|
+
}
|
|
1268
|
+
const nodeDeltasByIndex = /* @__PURE__ */ new Map();
|
|
1269
|
+
if (deltasById.size > 0) for (let i = 0; i < nodes.length; i++) {
|
|
1270
|
+
const delta = deltasById.get(nodes[i].id);
|
|
1271
|
+
if (delta) nodeDeltasByIndex.set(i, delta);
|
|
1272
|
+
}
|
|
1273
|
+
const labelEdgeIndexes = [];
|
|
1274
|
+
if (labelEdgeIds.size > 0) {
|
|
1275
|
+
for (let i = 0; i < edges.length; i++) if (labelEdgeIds.has(edges[i].id)) labelEdgeIndexes.push(i);
|
|
1276
|
+
}
|
|
1277
|
+
const labelDeltasByIndex = /* @__PURE__ */ new Map();
|
|
1278
|
+
if (deltasById.size > 0) for (let i = 0; i < edges.length; i++) {
|
|
1279
|
+
const edge = edges[i];
|
|
1280
|
+
if (!edge.label || edge.draggable === false) continue;
|
|
1281
|
+
if (labelEdgeIds.has(edge.id)) continue;
|
|
1282
|
+
const source = deltasById.get(edge.sourceId);
|
|
1283
|
+
if (!source) continue;
|
|
1284
|
+
const target = edge.targetId === edge.sourceId ? source : deltasById.get(edge.targetId);
|
|
1285
|
+
if (!target || target.x !== source.x || target.y !== source.y) continue;
|
|
1286
|
+
labelDeltasByIndex.set(i, source);
|
|
1287
|
+
}
|
|
1288
|
+
if (nodeDeltasByIndex.size === 0 && labelEdgeIndexes.length === 0 && labelDeltasByIndex.size === 0) return null;
|
|
1289
|
+
return {
|
|
1290
|
+
nodeDeltasByIndex,
|
|
1291
|
+
labelEdgeIndexes,
|
|
1292
|
+
labelDeltasByIndex
|
|
1293
|
+
};
|
|
1294
|
+
}
|
|
1295
|
+
function collectDescendantDeltas(childrenByParent, parentId, delta, movingIds, out, visited = new Set([parentId])) {
|
|
1296
|
+
const children = childrenByParent.get(parentId);
|
|
1297
|
+
if (!children) return;
|
|
1298
|
+
for (const childId of children) {
|
|
1299
|
+
if (visited.has(childId) || movingIds.has(childId)) continue;
|
|
1300
|
+
visited.add(childId);
|
|
1301
|
+
if (!out.has(childId)) out.set(childId, delta);
|
|
1302
|
+
collectDescendantDeltas(childrenByParent, childId, delta, movingIds, out, visited);
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
function applyMovementPlan(draft, plan, labelDx, labelDy) {
|
|
1306
|
+
for (const [index, delta] of plan.nodeDeltasByIndex) {
|
|
1307
|
+
const node = draft.graph.nodes[index];
|
|
1308
|
+
if (!node) continue;
|
|
1309
|
+
node.dx = (node.dx ?? 0) + delta.x;
|
|
1310
|
+
node.dy = (node.dy ?? 0) + delta.y;
|
|
1311
|
+
}
|
|
1312
|
+
for (const index of plan.labelEdgeIndexes) {
|
|
1313
|
+
const edge = draft.graph.edges[index];
|
|
1314
|
+
if (!edge?.label) continue;
|
|
1315
|
+
edge.label.dx = (edge.label.dx ?? 0) + labelDx;
|
|
1316
|
+
edge.label.dy = (edge.label.dy ?? 0) + labelDy;
|
|
1317
|
+
}
|
|
1318
|
+
for (const [index, delta] of plan.labelDeltasByIndex) {
|
|
1319
|
+
const edge = draft.graph.edges[index];
|
|
1320
|
+
if (!edge?.label) continue;
|
|
1321
|
+
edge.label.dx = (edge.label.dx ?? 0) + delta.x;
|
|
1322
|
+
edge.label.dy = (edge.label.dy ?? 0) + delta.y;
|
|
1323
|
+
}
|
|
1324
|
+
}
|
|
1325
|
+
/**
|
|
1326
|
+
* Remove nodes from the draft graph and resolve connected edges according to
|
|
1327
|
+
* the configured `onDeleteNodes` strategy. Shared by `deleteNode` and
|
|
1328
|
+
* `deleteSelected` so both events always agree on edge handling.
|
|
1329
|
+
*
|
|
1330
|
+
* `edgesToDelete` are edge ids that must be removed regardless of strategy
|
|
1331
|
+
* (explicitly selected/targeted edges).
|
|
1332
|
+
*/
|
|
1333
|
+
function applyNodeDeletionStrategy(draft, nodesToDelete, edgesToDelete, strategy) {
|
|
1334
|
+
if (typeof strategy === "function") {
|
|
1335
|
+
const result = strategy({
|
|
1336
|
+
...draft.graph,
|
|
1337
|
+
nodes: [...draft.graph.nodes],
|
|
1338
|
+
edges: [...draft.graph.edges]
|
|
1339
|
+
}, nodesToDelete);
|
|
1340
|
+
draft.graph.nodes = result.nodes;
|
|
1341
|
+
draft.graph.edges = result.edges;
|
|
1342
|
+
return;
|
|
1343
|
+
}
|
|
1344
|
+
draft.graph.nodes = draft.graph.nodes.filter((n) => !nodesToDelete.has(n.id));
|
|
1345
|
+
if (strategy === "retarget") {
|
|
1346
|
+
const kept = [];
|
|
1347
|
+
for (const edge of draft.graph.edges) {
|
|
1348
|
+
if (edgesToDelete.has(edge.id)) continue;
|
|
1349
|
+
const srcDel = nodesToDelete.has(edge.sourceId);
|
|
1350
|
+
const tgtDel = nodesToDelete.has(edge.targetId);
|
|
1351
|
+
if (srcDel && tgtDel) continue;
|
|
1352
|
+
if (srcDel) edge.sourceId = edge.targetId;
|
|
1353
|
+
else if (tgtDel) edge.targetId = edge.sourceId;
|
|
1354
|
+
kept.push(edge);
|
|
1355
|
+
}
|
|
1356
|
+
draft.graph.edges = kept;
|
|
1357
|
+
return;
|
|
1358
|
+
}
|
|
1359
|
+
draft.graph.edges = draft.graph.edges.filter((e) => !edgesToDelete.has(e.id) && !nodesToDelete.has(e.sourceId) && !nodesToDelete.has(e.targetId));
|
|
1360
|
+
}
|
|
844
1361
|
function hasSelectedEntitiesToDelete(ctx) {
|
|
845
1362
|
for (const id of ctx.selection) if (ctx.graph.nodes.some((node) => node.id === id) || ctx.graph.edges.some((edge) => edge.id === id)) return true;
|
|
846
1363
|
return false;
|
|
847
1364
|
}
|
|
848
|
-
function hasPendingDragDelta(ctx) {
|
|
849
|
-
return ctx.graph.nodes.some((node) => (node.dx ?? 0) !== 0 || (node.dy ?? 0) !== 0) || ctx.graph.edges.some((edge) => !!edge.label && ((edge.label.dx ?? 0) !== 0 || (edge.label.dy ?? 0) !== 0));
|
|
850
|
-
}
|
|
851
1365
|
function shouldTrackUndoEvent(event, snapshot) {
|
|
852
1366
|
if (!isUndoableEventType(event.type)) return false;
|
|
853
1367
|
const ctx = snapshot.context;
|
|
854
1368
|
switch (event.type) {
|
|
1369
|
+
case "createNode": return !ctx.graph.nodes.some((node) => node.id === event.node.id);
|
|
1370
|
+
case "createEdge": return validateNewEdge(ctx, event.edge) === null;
|
|
855
1371
|
case "deleteNode": return ctx.graph.nodes.some((node) => node.id === event.id);
|
|
856
1372
|
case "deleteEdge": return ctx.graph.edges.some((edge) => edge.id === event.id);
|
|
857
1373
|
case "updateNodeData": {
|
|
@@ -867,8 +1383,9 @@ function shouldTrackUndoEvent(event, snapshot) {
|
|
|
867
1383
|
const node = ctx.graph.nodes.find((candidate) => candidate.id === event.id);
|
|
868
1384
|
return !!node && (node.parentId ?? null) !== event.parentId;
|
|
869
1385
|
}
|
|
870
|
-
case "
|
|
871
|
-
case "
|
|
1386
|
+
case "dragStart": return !!event.transactionId && (ctx.graph.nodes.some((node) => node.id === event.id) || ctx.graph.edges.some((edge) => edge.id === event.id));
|
|
1387
|
+
case "moveNodes": return (event.direction.x !== 0 || event.direction.y !== 0) && [...ctx.selection].some((id) => isExplicitlyDraggable(id, ctx.graph) && ctx.graph.nodes.some((node) => node.id === id));
|
|
1388
|
+
case "resizeStart": return !!event.transactionId && ctx.graph.nodes.some((node) => node.id === event.id);
|
|
872
1389
|
case "reconnectEdge": {
|
|
873
1390
|
const edge = ctx.graph.edges.find((candidate) => candidate.id === event.edgeId);
|
|
874
1391
|
if (!edge) return false;
|
|
@@ -879,11 +1396,65 @@ function shouldTrackUndoEvent(event, snapshot) {
|
|
|
879
1396
|
default: return true;
|
|
880
1397
|
}
|
|
881
1398
|
}
|
|
1399
|
+
/**
|
|
1400
|
+
* Snapshot a (possibly drafted) graph into plain data for emission — immer
|
|
1401
|
+
* drafts are revoked before listeners run. Plain values (e.g. right after
|
|
1402
|
+
* `draft.graph = event.graph`) pass through untouched.
|
|
1403
|
+
*/
|
|
1404
|
+
function snapshotGraph(graph) {
|
|
1405
|
+
return isDraft(graph) ? current(graph) : graph;
|
|
1406
|
+
}
|
|
1407
|
+
/** Copy a (possibly drafted) viewport into plain data for emission. */
|
|
1408
|
+
function plainViewport(viewport) {
|
|
1409
|
+
return {
|
|
1410
|
+
x: viewport.x,
|
|
1411
|
+
y: viewport.y,
|
|
1412
|
+
zoom: viewport.zoom
|
|
1413
|
+
};
|
|
1414
|
+
}
|
|
1415
|
+
function createEventObject(type, event) {
|
|
1416
|
+
return {
|
|
1417
|
+
type,
|
|
1418
|
+
...event ?? {}
|
|
1419
|
+
};
|
|
1420
|
+
}
|
|
1421
|
+
function isExplicitlyVisibleNode(node) {
|
|
1422
|
+
return !node.hidden;
|
|
1423
|
+
}
|
|
1424
|
+
function isExplicitlyVisibleEdge(edge, nodes) {
|
|
1425
|
+
if (edge.hidden) return false;
|
|
1426
|
+
const source = nodes.find((node) => node.id === edge.sourceId);
|
|
1427
|
+
const target = nodes.find((node) => node.id === edge.targetId);
|
|
1428
|
+
return !!source && !!target && isExplicitlyVisibleNode(source) && isExplicitlyVisibleNode(target);
|
|
1429
|
+
}
|
|
1430
|
+
function isExplicitlyDraggable(id, graph) {
|
|
1431
|
+
const node = graph.nodes.find((candidate) => candidate.id === id);
|
|
1432
|
+
if (node) return node.draggable !== false;
|
|
1433
|
+
const edge = graph.edges.find((candidate) => candidate.id === id);
|
|
1434
|
+
if (edge) return edge.draggable !== false;
|
|
1435
|
+
return false;
|
|
1436
|
+
}
|
|
1437
|
+
function getMode(profile, mode) {
|
|
1438
|
+
return profile.modes[mode] ? mode : profile.defaultMode;
|
|
1439
|
+
}
|
|
1440
|
+
function setModeTransition(profile) {
|
|
1441
|
+
return (ctx, event) => produce(ctx, (draft) => {
|
|
1442
|
+
draft.mode = getMode(profile, event.mode);
|
|
1443
|
+
});
|
|
1444
|
+
}
|
|
1445
|
+
function dispatchProfile(profile, ctx, event, enqueue, fallback) {
|
|
1446
|
+
const mode = getMode(profile, ctx.mode);
|
|
1447
|
+
const transition = profile.modes[mode]?.on?.[event.type] ?? profile.on?.[event.type] ?? fallback;
|
|
1448
|
+
if (!transition) return ctx;
|
|
1449
|
+
return transition(ctx, event, enqueue);
|
|
1450
|
+
}
|
|
882
1451
|
function createFlowStore(initialGraph = {
|
|
883
1452
|
nodes: [],
|
|
884
1453
|
edges: [],
|
|
885
1454
|
direction: "down"
|
|
886
1455
|
}, options = {}) {
|
|
1456
|
+
const profile = options.profile ?? defaultFlowProfile;
|
|
1457
|
+
const initialMode = getMode(profile, options.mode ?? profile.defaultMode);
|
|
887
1458
|
const initialContext = {
|
|
888
1459
|
graph: initialGraph,
|
|
889
1460
|
parentNodeId: null,
|
|
@@ -900,11 +1471,14 @@ function createFlowStore(initialGraph = {
|
|
|
900
1471
|
nodeExtent: options.nodeExtent ?? null,
|
|
901
1472
|
selection: /* @__PURE__ */ new Set(),
|
|
902
1473
|
selectionBox: null,
|
|
903
|
-
|
|
1474
|
+
hoveredEntityIds: /* @__PURE__ */ new Set(),
|
|
1475
|
+
highlights: /* @__PURE__ */ new Map(),
|
|
904
1476
|
focusedEntityId: null,
|
|
1477
|
+
measurements: /* @__PURE__ */ new Map(),
|
|
905
1478
|
canvasState: "idle",
|
|
906
1479
|
draggedEntityId: null,
|
|
907
1480
|
dragThreshold: options.dragThreshold ?? 3,
|
|
1481
|
+
constrainNodeMovement: options.constrainNodeMovement ?? null,
|
|
908
1482
|
autoPanOnDrag: options.autoPanOnDrag ?? true,
|
|
909
1483
|
autoPanThreshold: options.autoPanThreshold ?? 40,
|
|
910
1484
|
autoPanSpeed: options.autoPanSpeed ?? 15,
|
|
@@ -916,7 +1490,7 @@ function createFlowStore(initialGraph = {
|
|
|
916
1490
|
resizingEntityId: null,
|
|
917
1491
|
resizeHandle: null,
|
|
918
1492
|
resizeCorners: null,
|
|
919
|
-
|
|
1493
|
+
mode: initialMode,
|
|
920
1494
|
selectionMode: options.selectionMode ?? "partial",
|
|
921
1495
|
snapToGrid: options.snapToGrid ?? false,
|
|
922
1496
|
snapGrid: options.snapGrid ?? [10, 10],
|
|
@@ -926,45 +1500,110 @@ function createFlowStore(initialGraph = {
|
|
|
926
1500
|
autoLayout: {
|
|
927
1501
|
status: "idle",
|
|
928
1502
|
algorithm: null
|
|
929
|
-
}
|
|
1503
|
+
},
|
|
1504
|
+
colorMode: options.colorMode ?? "system"
|
|
930
1505
|
};
|
|
931
1506
|
return createStore({
|
|
932
1507
|
context: initialContext,
|
|
933
|
-
emits: {
|
|
934
|
-
layoutRequested: (_event) => {},
|
|
935
|
-
connectionEnded: (_event) => {}
|
|
936
|
-
},
|
|
937
1508
|
on: {
|
|
938
|
-
createNode: (ctx, event) => produce(ctx, (draft) => {
|
|
1509
|
+
createNode: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1510
|
+
if (draft.graph.nodes.some((n) => n.id === event.node.id)) {
|
|
1511
|
+
enqueue.emit.error({
|
|
1512
|
+
error: /* @__PURE__ */ new Error(`createNode rejected: a node with id "${event.node.id}" already exists. Node ids must be unique — use updateNodeData to modify the existing node, or create the node with a different id.`),
|
|
1513
|
+
event: createEventObject("createNode", event)
|
|
1514
|
+
});
|
|
1515
|
+
return;
|
|
1516
|
+
}
|
|
939
1517
|
draft.graph.nodes.push(event.node);
|
|
1518
|
+
enqueue.emit.graphChanged({
|
|
1519
|
+
graph: snapshotGraph(draft.graph),
|
|
1520
|
+
event: createEventObject("createNode", event)
|
|
1521
|
+
});
|
|
940
1522
|
}),
|
|
941
|
-
deleteNode: (ctx, event) => produce(ctx, (draft) => {
|
|
1523
|
+
deleteNode: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
942
1524
|
const node = draft.graph.nodes.find((n) => n.id === event.id);
|
|
943
1525
|
const edges = draft.graph.edges.filter((e) => e.sourceId === event.id || e.targetId === event.id);
|
|
944
1526
|
if (node && applyBeforeDelete(options, draft, "deleteNode", [node], edges)) return;
|
|
945
|
-
|
|
946
|
-
draft
|
|
1527
|
+
const edgeIdsBefore = draft.graph.edges.map((e) => e.id);
|
|
1528
|
+
applyNodeDeletionStrategy(draft, new Set([event.id]), /* @__PURE__ */ new Set(), options.onDeleteNodes ?? "cascade");
|
|
1529
|
+
const remainingEdgeIds = new Set(draft.graph.edges.map((e) => e.id));
|
|
1530
|
+
const deletedEdgeIds = edgeIdsBefore.filter((id) => !remainingEdgeIds.has(id));
|
|
947
1531
|
draft.selection.delete(event.id);
|
|
1532
|
+
draft.measurements.delete(event.id);
|
|
1533
|
+
for (const edgeId of deletedEdgeIds) draft.measurements.delete(edgeId);
|
|
948
1534
|
if (draft.focusedEntityId === event.id) draft.focusedEntityId = null;
|
|
1535
|
+
pruneVisualState(draft, [event.id, ...deletedEdgeIds]);
|
|
1536
|
+
enqueue.emit.entitiesDeleted({
|
|
1537
|
+
ids: [event.id, ...deletedEdgeIds],
|
|
1538
|
+
event: createEventObject("deleteNode", event)
|
|
1539
|
+
});
|
|
1540
|
+
enqueue.emit.graphChanged({
|
|
1541
|
+
graph: snapshotGraph(draft.graph),
|
|
1542
|
+
event: createEventObject("deleteNode", event)
|
|
1543
|
+
});
|
|
1544
|
+
enqueue.emit.selectionChanged({
|
|
1545
|
+
selection: new Set(draft.selection),
|
|
1546
|
+
event: createEventObject("deleteNode", event)
|
|
1547
|
+
});
|
|
949
1548
|
}),
|
|
950
|
-
updateNodeData: (ctx, event) => produce(ctx, (draft) => {
|
|
1549
|
+
updateNodeData: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
951
1550
|
const node = draft.graph.nodes.find((n) => n.id === event.id);
|
|
952
|
-
if (node)
|
|
1551
|
+
if (node) {
|
|
1552
|
+
if (!node.data) node.data = {};
|
|
1553
|
+
Object.assign(node.data, event.data);
|
|
1554
|
+
enqueue.emit.graphChanged({
|
|
1555
|
+
graph: snapshotGraph(draft.graph),
|
|
1556
|
+
event: createEventObject("updateNodeData", event)
|
|
1557
|
+
});
|
|
1558
|
+
}
|
|
953
1559
|
}),
|
|
954
|
-
createEdge: (ctx, event) => produce(ctx, (draft) => {
|
|
1560
|
+
createEdge: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1561
|
+
const problem = validateNewEdge(draft, event.edge);
|
|
1562
|
+
if (problem) {
|
|
1563
|
+
enqueue.emit.error({
|
|
1564
|
+
error: new Error(problem),
|
|
1565
|
+
event: createEventObject("createEdge", event)
|
|
1566
|
+
});
|
|
1567
|
+
return;
|
|
1568
|
+
}
|
|
955
1569
|
draft.graph.edges.push(event.edge);
|
|
1570
|
+
enqueue.emit.graphChanged({
|
|
1571
|
+
graph: snapshotGraph(draft.graph),
|
|
1572
|
+
event: createEventObject("createEdge", event)
|
|
1573
|
+
});
|
|
956
1574
|
}),
|
|
957
|
-
deleteEdge: (ctx, event) => produce(ctx, (draft) => {
|
|
1575
|
+
deleteEdge: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
958
1576
|
const edge = draft.graph.edges.find((e) => e.id === event.id);
|
|
959
1577
|
if (edge && applyBeforeDelete(options, draft, "deleteEdge", [], [edge])) return;
|
|
960
1578
|
draft.graph.edges = draft.graph.edges.filter((e) => e.id !== event.id);
|
|
961
1579
|
draft.selection.delete(event.id);
|
|
1580
|
+
draft.measurements.delete(event.id);
|
|
1581
|
+
pruneVisualState(draft, [event.id]);
|
|
1582
|
+
enqueue.emit.entitiesDeleted({
|
|
1583
|
+
ids: [event.id],
|
|
1584
|
+
event: createEventObject("deleteEdge", event)
|
|
1585
|
+
});
|
|
1586
|
+
enqueue.emit.graphChanged({
|
|
1587
|
+
graph: snapshotGraph(draft.graph),
|
|
1588
|
+
event: createEventObject("deleteEdge", event)
|
|
1589
|
+
});
|
|
1590
|
+
enqueue.emit.selectionChanged({
|
|
1591
|
+
selection: new Set(draft.selection),
|
|
1592
|
+
event: createEventObject("deleteEdge", event)
|
|
1593
|
+
});
|
|
962
1594
|
}),
|
|
963
|
-
updateEdgeData: (ctx, event) => produce(ctx, (draft) => {
|
|
1595
|
+
updateEdgeData: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
964
1596
|
const edge = draft.graph.edges.find((e) => e.id === event.id);
|
|
965
|
-
if (edge)
|
|
1597
|
+
if (edge) {
|
|
1598
|
+
if (!edge.data) edge.data = {};
|
|
1599
|
+
Object.assign(edge.data, event.data);
|
|
1600
|
+
enqueue.emit.graphChanged({
|
|
1601
|
+
graph: snapshotGraph(draft.graph),
|
|
1602
|
+
event: createEventObject("updateEdgeData", event)
|
|
1603
|
+
});
|
|
1604
|
+
}
|
|
966
1605
|
}),
|
|
967
|
-
deleteSelected: (ctx) => produce(ctx, (draft) => {
|
|
1606
|
+
deleteSelected: (ctx, _event, enqueue) => produce(ctx, (draft) => {
|
|
968
1607
|
const sel = draft.selection;
|
|
969
1608
|
const nodesToDelete = /* @__PURE__ */ new Set();
|
|
970
1609
|
const edgesToDelete = /* @__PURE__ */ new Set();
|
|
@@ -972,90 +1611,100 @@ function createFlowStore(initialGraph = {
|
|
|
972
1611
|
if (draft.graph.nodes.some((n) => n.id === id)) nodesToDelete.add(id);
|
|
973
1612
|
if (draft.graph.edges.some((e) => e.id === id)) edgesToDelete.add(id);
|
|
974
1613
|
}
|
|
975
|
-
|
|
976
|
-
const
|
|
977
|
-
if (
|
|
978
|
-
|
|
979
|
-
...draft.graph,
|
|
980
|
-
nodes: [...draft.graph.nodes],
|
|
981
|
-
edges: [...draft.graph.edges]
|
|
982
|
-
}, nodesToDelete);
|
|
983
|
-
draft.graph.nodes = result.nodes;
|
|
984
|
-
draft.graph.edges = result.edges;
|
|
985
|
-
} else if (strategy === "retarget") {
|
|
986
|
-
draft.graph.nodes = draft.graph.nodes.filter((n) => !nodesToDelete.has(n.id));
|
|
987
|
-
draft.graph.edges = draft.graph.edges.filter((e) => !sel.has(e.id));
|
|
988
|
-
for (const edge of draft.graph.edges) {
|
|
989
|
-
const srcDel = nodesToDelete.has(edge.sourceId);
|
|
990
|
-
const tgtDel = nodesToDelete.has(edge.targetId);
|
|
991
|
-
if (srcDel && tgtDel) draft.graph.edges = draft.graph.edges.filter((e) => e.id !== edge.id);
|
|
992
|
-
else if (srcDel) edge.sourceId = edge.targetId;
|
|
993
|
-
else if (tgtDel) edge.targetId = edge.sourceId;
|
|
994
|
-
}
|
|
995
|
-
} else {
|
|
996
|
-
draft.graph.nodes = draft.graph.nodes.filter((n) => !nodesToDelete.has(n.id));
|
|
997
|
-
draft.graph.edges = draft.graph.edges.filter((e) => !sel.has(e.id) && !nodesToDelete.has(e.sourceId) && !nodesToDelete.has(e.targetId));
|
|
998
|
-
}
|
|
1614
|
+
const selectedNodes = draft.graph.nodes.filter((node) => nodesToDelete.has(node.id));
|
|
1615
|
+
const selectedEdges = draft.graph.edges.filter((edge) => edgesToDelete.has(edge.id) || nodesToDelete.has(edge.sourceId) || nodesToDelete.has(edge.targetId));
|
|
1616
|
+
if (applyBeforeDelete(options, draft, "deleteSelected", selectedNodes, selectedEdges)) return;
|
|
1617
|
+
applyNodeDeletionStrategy(draft, nodesToDelete, edgesToDelete, options.onDeleteNodes ?? "cascade");
|
|
999
1618
|
draft.selection.clear();
|
|
1000
1619
|
draft.focusedEntityId = null;
|
|
1620
|
+
for (const node of selectedNodes) draft.measurements.delete(node.id);
|
|
1621
|
+
for (const edge of selectedEdges) draft.measurements.delete(edge.id);
|
|
1622
|
+
pruneVisualState(draft, [...selectedNodes.map((node) => node.id), ...selectedEdges.map((edge) => edge.id)]);
|
|
1623
|
+
enqueue.emit.entitiesDeleted({
|
|
1624
|
+
ids: [...selectedNodes.map((node) => node.id), ...selectedEdges.map((edge) => edge.id)],
|
|
1625
|
+
event: createEventObject("deleteSelected", _event)
|
|
1626
|
+
});
|
|
1627
|
+
enqueue.emit.graphChanged({
|
|
1628
|
+
graph: snapshotGraph(draft.graph),
|
|
1629
|
+
event: createEventObject("deleteSelected", _event)
|
|
1630
|
+
});
|
|
1631
|
+
enqueue.emit.selectionChanged({
|
|
1632
|
+
selection: new Set(draft.selection),
|
|
1633
|
+
event: createEventObject("deleteSelected", _event)
|
|
1634
|
+
});
|
|
1001
1635
|
}),
|
|
1002
|
-
setGraph: (ctx, event) => produce(ctx, (draft) => {
|
|
1636
|
+
setGraph: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1003
1637
|
draft.graph = event.graph;
|
|
1004
1638
|
draft.selection.clear();
|
|
1639
|
+
draft.hoveredEntityIds = /* @__PURE__ */ new Set();
|
|
1640
|
+
draft.highlights = /* @__PURE__ */ new Map();
|
|
1005
1641
|
draft.focusedEntityId = null;
|
|
1006
1642
|
draft.canvasState = "idle";
|
|
1007
1643
|
draft.draggedEntityId = null;
|
|
1008
1644
|
draft.connection = null;
|
|
1009
1645
|
draft.selectionBox = null;
|
|
1646
|
+
draft.measurements.clear();
|
|
1647
|
+
enqueue.emit.graphChanged({
|
|
1648
|
+
graph: snapshotGraph(draft.graph),
|
|
1649
|
+
event: createEventObject("setGraph", event)
|
|
1650
|
+
});
|
|
1651
|
+
enqueue.emit.selectionChanged({
|
|
1652
|
+
selection: new Set(draft.selection),
|
|
1653
|
+
event: createEventObject("setGraph", event)
|
|
1654
|
+
});
|
|
1010
1655
|
}),
|
|
1011
|
-
reset: (ctx) => produce(ctx, (draft) => {
|
|
1656
|
+
reset: (ctx, _event, enqueue) => produce(ctx, (draft) => {
|
|
1012
1657
|
Object.assign(draft, {
|
|
1013
1658
|
...initialContext,
|
|
1014
1659
|
viewportSize: draft.viewportSize
|
|
1015
1660
|
});
|
|
1661
|
+
enqueue.emit.graphChanged({
|
|
1662
|
+
graph: snapshotGraph(draft.graph),
|
|
1663
|
+
event: createEventObject("reset", _event)
|
|
1664
|
+
});
|
|
1665
|
+
enqueue.emit.selectionChanged({
|
|
1666
|
+
selection: new Set(draft.selection),
|
|
1667
|
+
event: createEventObject("reset", _event)
|
|
1668
|
+
});
|
|
1016
1669
|
}),
|
|
1017
|
-
reparentNode: (ctx, event) => produce(ctx, (draft) => {
|
|
1670
|
+
reparentNode: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1018
1671
|
const node = draft.graph.nodes.find((n) => n.id === event.id);
|
|
1019
|
-
if (node)
|
|
1672
|
+
if (!node) return;
|
|
1673
|
+
const problem = reparentProblem(draft.graph, event.id, event.parentId);
|
|
1674
|
+
if (problem) {
|
|
1675
|
+
enqueue.emit.error({
|
|
1676
|
+
error: new Error(problem),
|
|
1677
|
+
event: createEventObject("reparentNode", event)
|
|
1678
|
+
});
|
|
1679
|
+
return;
|
|
1680
|
+
}
|
|
1681
|
+
node.parentId = event.parentId;
|
|
1682
|
+
enqueue.emit.graphChanged({
|
|
1683
|
+
graph: snapshotGraph(draft.graph),
|
|
1684
|
+
event: createEventObject("reparentNode", event)
|
|
1685
|
+
});
|
|
1020
1686
|
}),
|
|
1021
1687
|
dragStart: (ctx, event) => produce(ctx, (draft) => {
|
|
1688
|
+
if (!isExplicitlyDraggable(event.id, draft.graph)) return;
|
|
1022
1689
|
draft.canvasState = "dragging";
|
|
1023
1690
|
draft.draggedEntityId = event.id;
|
|
1024
1691
|
draft.selection.add(event.id);
|
|
1025
1692
|
}),
|
|
1026
|
-
dragMove: (ctx, event) =>
|
|
1027
|
-
if (
|
|
1693
|
+
dragMove: (ctx, event) => {
|
|
1694
|
+
if (ctx.canvasState !== "dragging") return ctx;
|
|
1028
1695
|
const { dx, dy } = event;
|
|
1029
|
-
if (dx === 0 && dy === 0) return;
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
const newX = node.x + newDx;
|
|
1037
|
-
const newY = node.y + newDy;
|
|
1038
|
-
const clampedX = Math.max(draft.nodeExtent[0].x, Math.min(newX, draft.nodeExtent[1].x - node.width));
|
|
1039
|
-
const clampedY = Math.max(draft.nodeExtent[0].y, Math.min(newY, draft.nodeExtent[1].y - node.height));
|
|
1040
|
-
newDx = clampedX - node.x;
|
|
1041
|
-
newDy = clampedY - node.y;
|
|
1042
|
-
}
|
|
1043
|
-
node.dx = newDx;
|
|
1044
|
-
node.dy = newDy;
|
|
1045
|
-
continue;
|
|
1046
|
-
}
|
|
1047
|
-
const edge = draft.graph.edges.find((e) => e.id === id);
|
|
1048
|
-
if (edge?.label) {
|
|
1049
|
-
edge.label.dx = (edge.label.dx ?? 0) + dx;
|
|
1050
|
-
edge.label.dy = (edge.label.dy ?? 0) + dy;
|
|
1051
|
-
}
|
|
1052
|
-
}
|
|
1053
|
-
}),
|
|
1696
|
+
if (dx === 0 && dy === 0) return ctx;
|
|
1697
|
+
const plan = planNodeMovement(ctx, dx, dy, { includeLabels: true });
|
|
1698
|
+
if (!plan) return ctx;
|
|
1699
|
+
return produce(ctx, (draft) => {
|
|
1700
|
+
applyMovementPlan(draft, plan, dx, dy);
|
|
1701
|
+
});
|
|
1702
|
+
},
|
|
1054
1703
|
dragEnd: (ctx) => produce(ctx, (draft) => {
|
|
1055
1704
|
draft.canvasState = "idle";
|
|
1056
1705
|
draft.draggedEntityId = null;
|
|
1057
1706
|
}),
|
|
1058
|
-
commitDrag: (ctx) => produce(ctx, (draft) => {
|
|
1707
|
+
commitDrag: (ctx, _event, enqueue) => produce(ctx, (draft) => {
|
|
1059
1708
|
let snapOffX = 0;
|
|
1060
1709
|
let snapOffY = 0;
|
|
1061
1710
|
if (draft.enableSnapLines && draft.selection.size > 0) {
|
|
@@ -1111,23 +1760,49 @@ function createFlowStore(initialGraph = {
|
|
|
1111
1760
|
edge.label.dy = 0;
|
|
1112
1761
|
}
|
|
1113
1762
|
}
|
|
1763
|
+
enqueue.emit.graphChanged({
|
|
1764
|
+
graph: snapshotGraph(draft.graph),
|
|
1765
|
+
event: createEventObject("commitDrag", _event)
|
|
1766
|
+
});
|
|
1114
1767
|
}),
|
|
1115
|
-
select: (ctx, event) => produce(ctx, (draft) => {
|
|
1768
|
+
select: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1116
1769
|
const mode = event.mode ?? "single";
|
|
1117
1770
|
if (mode === "single") draft.selection.clear();
|
|
1118
1771
|
if (mode === "multi" && event.ids.every((id) => draft.selection.has(id))) for (const id of event.ids) draft.selection.delete(id);
|
|
1119
1772
|
else for (const id of event.ids) draft.selection.add(id);
|
|
1773
|
+
enqueue.emit.selectionChanged({
|
|
1774
|
+
selection: new Set(draft.selection),
|
|
1775
|
+
event: createEventObject("select", event)
|
|
1776
|
+
});
|
|
1120
1777
|
}),
|
|
1121
|
-
selectAll: (ctx) => produce(ctx, (draft) => {
|
|
1122
|
-
for (const node of draft.graph.nodes)
|
|
1123
|
-
|
|
1778
|
+
selectAll: (ctx, _event, enqueue) => produce(ctx, (draft) => {
|
|
1779
|
+
for (const node of draft.graph.nodes) {
|
|
1780
|
+
if (!isExplicitlyVisibleNode(node)) continue;
|
|
1781
|
+
draft.selection.add(node.id);
|
|
1782
|
+
}
|
|
1783
|
+
for (const edge of draft.graph.edges) {
|
|
1784
|
+
if (!isExplicitlyVisibleEdge(edge, draft.graph.nodes)) continue;
|
|
1785
|
+
draft.selection.add(edge.id);
|
|
1786
|
+
}
|
|
1787
|
+
enqueue.emit.selectionChanged({
|
|
1788
|
+
selection: new Set(draft.selection),
|
|
1789
|
+
event: createEventObject("selectAll", _event)
|
|
1790
|
+
});
|
|
1124
1791
|
}),
|
|
1125
|
-
clearSelection: (ctx) => produce(ctx, (draft) => {
|
|
1792
|
+
clearSelection: (ctx, _event, enqueue) => produce(ctx, (draft) => {
|
|
1126
1793
|
draft.selection.clear();
|
|
1794
|
+
enqueue.emit.selectionChanged({
|
|
1795
|
+
selection: new Set(draft.selection),
|
|
1796
|
+
event: createEventObject("clearSelection", _event)
|
|
1797
|
+
});
|
|
1127
1798
|
}),
|
|
1128
|
-
selectionBoxStart: (ctx, event) => produce(ctx, (draft) => {
|
|
1799
|
+
selectionBoxStart: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1129
1800
|
draft.canvasState = "selecting";
|
|
1130
1801
|
if ((event.mode ?? "single") === "single") draft.selection.clear();
|
|
1802
|
+
enqueue.emit.selectionChanged({
|
|
1803
|
+
selection: new Set(draft.selection),
|
|
1804
|
+
event: createEventObject("selectionBoxStart", event)
|
|
1805
|
+
});
|
|
1131
1806
|
draft.selectionBox = [{
|
|
1132
1807
|
x: event.point.x,
|
|
1133
1808
|
y: event.point.y
|
|
@@ -1143,12 +1818,16 @@ function createFlowStore(initialGraph = {
|
|
|
1143
1818
|
y: event.point.y
|
|
1144
1819
|
};
|
|
1145
1820
|
}),
|
|
1146
|
-
selectionBoxEnd: (ctx) => produce(ctx, (draft) => {
|
|
1821
|
+
selectionBoxEnd: (ctx, _event, enqueue) => produce(ctx, (draft) => {
|
|
1147
1822
|
if (!draft.selectionBox) return;
|
|
1148
1823
|
const rect = boxToRect(draft.selectionBox[0], draft.selectionBox[1]);
|
|
1149
1824
|
const contains = draft.selectionMode === "full" ? rectContains : rectsIntersect;
|
|
1150
|
-
for (const node of draft.graph.nodes)
|
|
1825
|
+
for (const node of draft.graph.nodes) {
|
|
1826
|
+
if (!isExplicitlyVisibleNode(node)) continue;
|
|
1827
|
+
if (contains(rect, resolvedBounds(node))) draft.selection.add(node.id);
|
|
1828
|
+
}
|
|
1151
1829
|
for (const edge of draft.graph.edges) {
|
|
1830
|
+
if (!isExplicitlyVisibleEdge(edge, draft.graph.nodes)) continue;
|
|
1152
1831
|
if (edge.label) {
|
|
1153
1832
|
if (contains(rect, {
|
|
1154
1833
|
x: edge.label.x + (edge.label.dx ?? 0),
|
|
@@ -1169,6 +1848,10 @@ function createFlowStore(initialGraph = {
|
|
|
1169
1848
|
}
|
|
1170
1849
|
draft.selectionBox = null;
|
|
1171
1850
|
draft.canvasState = "idle";
|
|
1851
|
+
enqueue.emit.selectionChanged({
|
|
1852
|
+
selection: new Set(draft.selection),
|
|
1853
|
+
event: createEventObject("selectionBoxEnd", _event)
|
|
1854
|
+
});
|
|
1172
1855
|
}),
|
|
1173
1856
|
focus: (ctx, event) => produce(ctx, (draft) => {
|
|
1174
1857
|
draft.focusedEntityId = event.entityId;
|
|
@@ -1176,24 +1859,68 @@ function createFlowStore(initialGraph = {
|
|
|
1176
1859
|
clearFocus: (ctx) => produce(ctx, (draft) => {
|
|
1177
1860
|
draft.focusedEntityId = null;
|
|
1178
1861
|
}),
|
|
1862
|
+
setHover: (ctx, event) => {
|
|
1863
|
+
if (ctx.hoveredEntityIds.size === event.ids.length && event.ids.every((id) => ctx.hoveredEntityIds.has(id))) return ctx;
|
|
1864
|
+
return produce(ctx, (draft) => {
|
|
1865
|
+
draft.hoveredEntityIds = new Set(event.ids);
|
|
1866
|
+
});
|
|
1867
|
+
},
|
|
1868
|
+
clearHover: (ctx) => {
|
|
1869
|
+
if (ctx.hoveredEntityIds.size === 0) return ctx;
|
|
1870
|
+
return produce(ctx, (draft) => {
|
|
1871
|
+
draft.hoveredEntityIds = /* @__PURE__ */ new Set();
|
|
1872
|
+
});
|
|
1873
|
+
},
|
|
1874
|
+
setHighlights: (ctx, event) => produce(ctx, (draft) => {
|
|
1875
|
+
const bucket = /* @__PURE__ */ new Map();
|
|
1876
|
+
for (const entry of event.entries) bucket.set(entry.id, entry.meta);
|
|
1877
|
+
if (bucket.size === 0) draft.highlights.delete(event.kind);
|
|
1878
|
+
else draft.highlights.set(event.kind, bucket);
|
|
1879
|
+
}),
|
|
1880
|
+
addHighlights: (ctx, event) => produce(ctx, (draft) => {
|
|
1881
|
+
let bucket = draft.highlights.get(event.kind);
|
|
1882
|
+
if (!bucket) {
|
|
1883
|
+
bucket = /* @__PURE__ */ new Map();
|
|
1884
|
+
draft.highlights.set(event.kind, bucket);
|
|
1885
|
+
}
|
|
1886
|
+
for (const entry of event.entries) bucket.set(entry.id, entry.meta);
|
|
1887
|
+
}),
|
|
1888
|
+
removeHighlights: (ctx, event) => produce(ctx, (draft) => {
|
|
1889
|
+
const bucket = draft.highlights.get(event.kind);
|
|
1890
|
+
if (!bucket) return;
|
|
1891
|
+
for (const id of event.ids) bucket.delete(id);
|
|
1892
|
+
if (bucket.size === 0) draft.highlights.delete(event.kind);
|
|
1893
|
+
}),
|
|
1894
|
+
clearHighlights: (ctx, event) => produce(ctx, (draft) => {
|
|
1895
|
+
if (event?.kind !== void 0) draft.highlights.delete(event.kind);
|
|
1896
|
+
else draft.highlights.clear();
|
|
1897
|
+
}),
|
|
1179
1898
|
updateParentNodeId: (ctx, event) => produce(ctx, (draft) => {
|
|
1180
1899
|
draft.parentNodeId = event.parentNodeId;
|
|
1181
1900
|
draft.selection.clear();
|
|
1182
1901
|
draft.focusedEntityId = null;
|
|
1183
1902
|
}),
|
|
1184
|
-
setViewport: (ctx, event) => produce(ctx, (draft) => {
|
|
1903
|
+
setViewport: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1185
1904
|
draft.viewport = {
|
|
1186
1905
|
x: event.x,
|
|
1187
1906
|
y: event.y,
|
|
1188
1907
|
zoom: event.zoom
|
|
1189
1908
|
};
|
|
1909
|
+
enqueue.emit.viewportChanged({
|
|
1910
|
+
viewport: plainViewport(draft.viewport),
|
|
1911
|
+
event: createEventObject("setViewport", event)
|
|
1912
|
+
});
|
|
1190
1913
|
}),
|
|
1191
|
-
panBy: (ctx, event) => produce(ctx, (draft) => {
|
|
1914
|
+
panBy: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1192
1915
|
draft.viewport.x += event.x;
|
|
1193
1916
|
draft.viewport.y += event.y;
|
|
1194
1917
|
if (draft.translateExtent) draft.viewport = constrainViewport(draft.viewport, draft.viewportSize, draft.translateExtent);
|
|
1918
|
+
enqueue.emit.viewportChanged({
|
|
1919
|
+
viewport: plainViewport(draft.viewport),
|
|
1920
|
+
event: createEventObject("panBy", event)
|
|
1921
|
+
});
|
|
1195
1922
|
}),
|
|
1196
|
-
zoomTo: (ctx, event) => produce(ctx, (draft) => {
|
|
1923
|
+
zoomTo: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1197
1924
|
const clamped = Math.min(Math.max(event.zoom, draft.minZoom), draft.maxZoom);
|
|
1198
1925
|
const ratio = clamped / draft.viewport.zoom;
|
|
1199
1926
|
draft.viewport = {
|
|
@@ -1202,6 +1929,28 @@ function createFlowStore(initialGraph = {
|
|
|
1202
1929
|
zoom: clamped
|
|
1203
1930
|
};
|
|
1204
1931
|
if (draft.translateExtent) draft.viewport = constrainViewport(draft.viewport, draft.viewportSize, draft.translateExtent);
|
|
1932
|
+
enqueue.emit.viewportChanged({
|
|
1933
|
+
viewport: plainViewport(draft.viewport),
|
|
1934
|
+
event: createEventObject("zoomTo", event)
|
|
1935
|
+
});
|
|
1936
|
+
}),
|
|
1937
|
+
zoomBy: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1938
|
+
const center = event.center ?? {
|
|
1939
|
+
x: draft.viewportSize.width / 2,
|
|
1940
|
+
y: draft.viewportSize.height / 2
|
|
1941
|
+
};
|
|
1942
|
+
const clamped = Math.min(Math.max(draft.viewport.zoom * event.factor, draft.minZoom), draft.maxZoom);
|
|
1943
|
+
const ratio = clamped / draft.viewport.zoom;
|
|
1944
|
+
draft.viewport = {
|
|
1945
|
+
x: center.x - (center.x - draft.viewport.x) * ratio,
|
|
1946
|
+
y: center.y - (center.y - draft.viewport.y) * ratio,
|
|
1947
|
+
zoom: clamped
|
|
1948
|
+
};
|
|
1949
|
+
if (draft.translateExtent) draft.viewport = constrainViewport(draft.viewport, draft.viewportSize, draft.translateExtent);
|
|
1950
|
+
enqueue.emit.viewportChanged({
|
|
1951
|
+
viewport: plainViewport(draft.viewport),
|
|
1952
|
+
event: createEventObject("zoomBy", event)
|
|
1953
|
+
});
|
|
1205
1954
|
}),
|
|
1206
1955
|
setMinZoom: (ctx, event) => produce(ctx, (draft) => {
|
|
1207
1956
|
draft.minZoom = event.minZoom;
|
|
@@ -1231,20 +1980,32 @@ function createFlowStore(initialGraph = {
|
|
|
1231
1980
|
height: event.height
|
|
1232
1981
|
};
|
|
1233
1982
|
}),
|
|
1234
|
-
fitView: (ctx, event) => produce(ctx, (draft) => {
|
|
1983
|
+
fitView: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1235
1984
|
if (draft.viewportSize.width === 0 || draft.viewportSize.height === 0) return;
|
|
1236
|
-
const
|
|
1237
|
-
const
|
|
1985
|
+
const ids = event?.nodes?.map((node) => typeof node === "string" ? node : node.id);
|
|
1986
|
+
const hasIds = ids && ids.length > 0;
|
|
1987
|
+
const idSet = hasIds ? new Set(ids) : null;
|
|
1988
|
+
const bounds = getGraphBounds(hasIds ? draft.graph.nodes.filter((node) => idSet.has(node.id) && isExplicitlyVisibleNode(node)) : draft.graph.nodes.filter((node) => isExplicitlyVisibleNode(node)), hasIds ? draft.graph.edges.filter((edge) => idSet.has(edge.id) && isExplicitlyVisibleEdge(edge, draft.graph.nodes)) : draft.graph.edges.filter((edge) => isExplicitlyVisibleEdge(edge, draft.graph.nodes)));
|
|
1238
1989
|
if (bounds.width === 0 && bounds.height === 0) return;
|
|
1239
1990
|
const { nodes: _nodes, ...options } = event ?? {};
|
|
1240
1991
|
draft.viewport = viewportForBounds(bounds, draft.viewportSize, options);
|
|
1992
|
+
enqueue.emit.viewportChanged({
|
|
1993
|
+
viewport: plainViewport(draft.viewport),
|
|
1994
|
+
event: createEventObject("fitView", event)
|
|
1995
|
+
});
|
|
1241
1996
|
}),
|
|
1242
1997
|
pushViewport: (ctx) => produce(ctx, (draft) => {
|
|
1243
1998
|
draft.viewportStack.push({ ...draft.viewport });
|
|
1244
1999
|
}),
|
|
1245
|
-
popViewport: (ctx) => produce(ctx, (draft) => {
|
|
2000
|
+
popViewport: (ctx, _event, enqueue) => produce(ctx, (draft) => {
|
|
1246
2001
|
const prev = draft.viewportStack.pop();
|
|
1247
|
-
if (prev)
|
|
2002
|
+
if (prev) {
|
|
2003
|
+
draft.viewport = prev;
|
|
2004
|
+
enqueue.emit.viewportChanged({
|
|
2005
|
+
viewport: plainViewport(draft.viewport),
|
|
2006
|
+
event: createEventObject("popViewport", _event)
|
|
2007
|
+
});
|
|
2008
|
+
}
|
|
1248
2009
|
}),
|
|
1249
2010
|
panStart: (ctx, event) => produce(ctx, (draft) => {
|
|
1250
2011
|
draft.canvasState = "panning";
|
|
@@ -1254,7 +2015,7 @@ function createFlowStore(initialGraph = {
|
|
|
1254
2015
|
};
|
|
1255
2016
|
draft.panViewportOrigin = { ...draft.viewport };
|
|
1256
2017
|
}),
|
|
1257
|
-
pan: (ctx, event) => produce(ctx, (draft) => {
|
|
2018
|
+
pan: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1258
2019
|
if (!draft.panOrigin || !draft.panViewportOrigin) return;
|
|
1259
2020
|
let newX = draft.panViewportOrigin.x + (event.x - draft.panOrigin.x);
|
|
1260
2021
|
let newY = draft.panViewportOrigin.y + (event.y - draft.panOrigin.y);
|
|
@@ -1268,6 +2029,10 @@ function createFlowStore(initialGraph = {
|
|
|
1268
2029
|
y: newY,
|
|
1269
2030
|
zoom: draft.panViewportOrigin.zoom
|
|
1270
2031
|
};
|
|
2032
|
+
enqueue.emit.viewportChanged({
|
|
2033
|
+
viewport: plainViewport(draft.viewport),
|
|
2034
|
+
event: createEventObject("pan", event)
|
|
2035
|
+
});
|
|
1271
2036
|
}),
|
|
1272
2037
|
panEnd: (ctx) => produce(ctx, (draft) => {
|
|
1273
2038
|
draft.canvasState = "idle";
|
|
@@ -1302,13 +2067,14 @@ function createFlowStore(initialGraph = {
|
|
|
1302
2067
|
if (!draft.connection.active && Math.hypot(dx, dy) >= threshold) draft.connection.active = true;
|
|
1303
2068
|
}),
|
|
1304
2069
|
connectionEnd: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1305
|
-
|
|
2070
|
+
const connection = ctx.connection;
|
|
2071
|
+
if (connection) enqueue.emit.connectionEnded({
|
|
1306
2072
|
source: {
|
|
1307
|
-
nodeId:
|
|
1308
|
-
port:
|
|
2073
|
+
nodeId: connection.sourceId,
|
|
2074
|
+
port: connection.sourcePort
|
|
1309
2075
|
},
|
|
1310
2076
|
target: event?.target ?? { kind: "none" },
|
|
1311
|
-
connection
|
|
2077
|
+
connection
|
|
1312
2078
|
});
|
|
1313
2079
|
draft.canvasState = "idle";
|
|
1314
2080
|
draft.connection = null;
|
|
@@ -1329,8 +2095,7 @@ function createFlowStore(initialGraph = {
|
|
|
1329
2095
|
draft.resizeHandle = event.handle;
|
|
1330
2096
|
const x = node.x + (node.dx ?? 0);
|
|
1331
2097
|
const y = node.y + (node.dy ?? 0);
|
|
1332
|
-
const w
|
|
1333
|
-
const h = node.style?.customHeight ?? node.height;
|
|
2098
|
+
const { width: w, height: h } = resolvedSize(node);
|
|
1334
2099
|
const tl = {
|
|
1335
2100
|
x,
|
|
1336
2101
|
y
|
|
@@ -1389,7 +2154,7 @@ function createFlowStore(initialGraph = {
|
|
|
1389
2154
|
y: event.y
|
|
1390
2155
|
};
|
|
1391
2156
|
}),
|
|
1392
|
-
resizeEnd: (ctx) => produce(ctx, (draft) => {
|
|
2157
|
+
resizeEnd: (ctx, _event, enqueue) => produce(ctx, (draft) => {
|
|
1393
2158
|
if (draft.resizingEntityId && draft.resizeCorners) {
|
|
1394
2159
|
const node = draft.graph.nodes.find((n) => n.id === draft.resizingEntityId);
|
|
1395
2160
|
if (node) {
|
|
@@ -1411,6 +2176,10 @@ function createFlowStore(initialGraph = {
|
|
|
1411
2176
|
draft.resizingEntityId = null;
|
|
1412
2177
|
draft.resizeHandle = null;
|
|
1413
2178
|
draft.resizeCorners = null;
|
|
2179
|
+
enqueue.emit.graphChanged({
|
|
2180
|
+
graph: snapshotGraph(draft.graph),
|
|
2181
|
+
event: createEventObject("resizeEnd", _event)
|
|
2182
|
+
});
|
|
1414
2183
|
}),
|
|
1415
2184
|
resizeCancel: (ctx) => produce(ctx, (draft) => {
|
|
1416
2185
|
draft.canvasState = "idle";
|
|
@@ -1418,30 +2187,31 @@ function createFlowStore(initialGraph = {
|
|
|
1418
2187
|
draft.resizeHandle = null;
|
|
1419
2188
|
draft.resizeCorners = null;
|
|
1420
2189
|
}),
|
|
1421
|
-
reconnectEdge: (ctx, event) => produce(ctx, (draft) => {
|
|
2190
|
+
reconnectEdge: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1422
2191
|
const edge = draft.graph.edges.find((e) => e.id === event.edgeId);
|
|
1423
2192
|
if (!edge) return;
|
|
2193
|
+
const resultingSourceId = event.newSourceId ?? edge.sourceId;
|
|
2194
|
+
const resultingTargetId = event.newTargetId ?? edge.targetId;
|
|
2195
|
+
const missing = [];
|
|
2196
|
+
if (!draft.graph.nodes.some((n) => n.id === resultingSourceId)) missing.push(`source node "${resultingSourceId}"`);
|
|
2197
|
+
if (!draft.graph.nodes.some((n) => n.id === resultingTargetId)) missing.push(`target node "${resultingTargetId}"`);
|
|
2198
|
+
if (missing.length > 0) {
|
|
2199
|
+
enqueue.emit.error({
|
|
2200
|
+
error: /* @__PURE__ */ new Error(`reconnectEdge rejected: edge "${event.edgeId}" references ${missing.join(" and ")}, which ${missing.length === 1 ? "does" : "do"} not exist in the graph.`),
|
|
2201
|
+
event: createEventObject("reconnectEdge", event)
|
|
2202
|
+
});
|
|
2203
|
+
return;
|
|
2204
|
+
}
|
|
1424
2205
|
if (event.newSourceId !== void 0) edge.sourceId = event.newSourceId;
|
|
1425
2206
|
if (event.newTargetId !== void 0) edge.targetId = event.newTargetId;
|
|
1426
2207
|
if (event.newSourcePort !== void 0) edge.sourcePort = event.newSourcePort;
|
|
1427
2208
|
if (event.newTargetPort !== void 0) edge.targetPort = event.newTargetPort;
|
|
2209
|
+
enqueue.emit.graphChanged({
|
|
2210
|
+
graph: snapshotGraph(draft.graph),
|
|
2211
|
+
event: createEventObject("reconnectEdge", event)
|
|
2212
|
+
});
|
|
1428
2213
|
}),
|
|
1429
|
-
|
|
1430
|
-
for (const m of event.measurements) {
|
|
1431
|
-
const node = draft.graph.nodes.find((n) => n.id === m.nodeId);
|
|
1432
|
-
if (!node?.ports) continue;
|
|
1433
|
-
const port = node.ports.find((p) => p.name === m.portName);
|
|
1434
|
-
if (port) {
|
|
1435
|
-
port.x = m.x;
|
|
1436
|
-
port.y = m.y;
|
|
1437
|
-
port.width = m.width;
|
|
1438
|
-
port.height = m.height;
|
|
1439
|
-
}
|
|
1440
|
-
}
|
|
1441
|
-
}),
|
|
1442
|
-
setInteractionMode: (ctx, event) => produce(ctx, (draft) => {
|
|
1443
|
-
draft.interactionMode = event.mode;
|
|
1444
|
-
}),
|
|
2214
|
+
setMode: (ctx, event, enqueue) => dispatchProfile(profile, ctx, createEventObject("setMode", event), enqueue, setModeTransition(profile)),
|
|
1445
2215
|
setSnapToGrid: (ctx, event) => produce(ctx, (draft) => {
|
|
1446
2216
|
draft.snapToGrid = event.enabled;
|
|
1447
2217
|
if (event.grid) draft.snapGrid = event.grid;
|
|
@@ -1451,22 +2221,24 @@ function createFlowStore(initialGraph = {
|
|
|
1451
2221
|
if (event.threshold !== void 0) draft.snapThreshold = event.threshold;
|
|
1452
2222
|
if (event.spacing !== void 0) draft.enableSpacingGuides = event.spacing;
|
|
1453
2223
|
}),
|
|
1454
|
-
|
|
2224
|
+
setColorMode: (ctx, event) => produce(ctx, (draft) => {
|
|
2225
|
+
draft.colorMode = event.colorMode;
|
|
2226
|
+
}),
|
|
2227
|
+
moveNodes: (ctx, event) => {
|
|
1455
2228
|
const f = event.factor ?? 1;
|
|
1456
2229
|
let dx = event.direction.x * f;
|
|
1457
2230
|
let dy = event.direction.y * f;
|
|
1458
|
-
if (
|
|
1459
|
-
dx = dx !== 0 ? Math.sign(dx) *
|
|
1460
|
-
dy = dy !== 0 ? Math.sign(dy) *
|
|
1461
|
-
}
|
|
1462
|
-
for (const id of draft.selection) {
|
|
1463
|
-
const node = draft.graph.nodes.find((n) => n.id === id);
|
|
1464
|
-
if (node) {
|
|
1465
|
-
node.dx = (node.dx ?? 0) + dx;
|
|
1466
|
-
node.dy = (node.dy ?? 0) + dy;
|
|
1467
|
-
}
|
|
2231
|
+
if (ctx.snapToGrid) {
|
|
2232
|
+
dx = dx !== 0 ? Math.sign(dx) * ctx.snapGrid[0] : 0;
|
|
2233
|
+
dy = dy !== 0 ? Math.sign(dy) * ctx.snapGrid[1] : 0;
|
|
1468
2234
|
}
|
|
1469
|
-
|
|
2235
|
+
if (dx === 0 && dy === 0) return ctx;
|
|
2236
|
+
const plan = planNodeMovement(ctx, dx, dy, { includeLabels: true });
|
|
2237
|
+
if (!plan) return ctx;
|
|
2238
|
+
return produce(ctx, (draft) => {
|
|
2239
|
+
applyMovementPlan(draft, plan, dx, dy);
|
|
2240
|
+
});
|
|
2241
|
+
},
|
|
1470
2242
|
requestLayout: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1471
2243
|
if (draft.canvasState === "dragging") return;
|
|
1472
2244
|
const algorithm = event.algorithm ?? draft.autoLayout.algorithm;
|
|
@@ -1480,7 +2252,7 @@ function createFlowStore(initialGraph = {
|
|
|
1480
2252
|
graph: ctx.graph
|
|
1481
2253
|
});
|
|
1482
2254
|
}),
|
|
1483
|
-
applyLayoutResult: (ctx, event) => produce(ctx, (draft) => {
|
|
2255
|
+
applyLayoutResult: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1484
2256
|
for (const [id, pos] of event.positions) {
|
|
1485
2257
|
const node = draft.graph.nodes.find((n) => n.id === id);
|
|
1486
2258
|
if (node) {
|
|
@@ -1500,23 +2272,44 @@ function createFlowStore(initialGraph = {
|
|
|
1500
2272
|
}
|
|
1501
2273
|
}
|
|
1502
2274
|
draft.autoLayout.status = "idle";
|
|
2275
|
+
enqueue.emit.graphChanged({
|
|
2276
|
+
graph: snapshotGraph(draft.graph),
|
|
2277
|
+
event: createEventObject("applyLayoutResult", event)
|
|
2278
|
+
});
|
|
1503
2279
|
}),
|
|
1504
|
-
|
|
1505
|
-
for (const
|
|
1506
|
-
|
|
2280
|
+
updateMeasurements: (ctx, event) => produce(ctx, (draft) => {
|
|
2281
|
+
for (const measurement of event.measurements) {
|
|
2282
|
+
draft.measurements.set(measurement.id, measurement.rect);
|
|
2283
|
+
const node = draft.graph.nodes.find((n) => n.id === measurement.id);
|
|
1507
2284
|
if (node) {
|
|
1508
|
-
node.
|
|
1509
|
-
|
|
2285
|
+
if (node.autoSize) node.measured = {
|
|
2286
|
+
width: measurement.rect.width,
|
|
2287
|
+
height: measurement.rect.height
|
|
2288
|
+
};
|
|
1510
2289
|
continue;
|
|
1511
2290
|
}
|
|
1512
|
-
const edge = draft.graph.edges.find((e) => e.id === id);
|
|
2291
|
+
const edge = draft.graph.edges.find((e) => e.id === measurement.id);
|
|
1513
2292
|
if (edge?.label) {
|
|
1514
|
-
edge.label.width =
|
|
1515
|
-
edge.label.height =
|
|
2293
|
+
edge.label.width = measurement.rect.width;
|
|
2294
|
+
edge.label.height = measurement.rect.height;
|
|
2295
|
+
continue;
|
|
2296
|
+
}
|
|
2297
|
+
for (const candidate of draft.graph.nodes) {
|
|
2298
|
+
const port = candidate.ports?.find((p) => p.id === measurement.id || `${candidate.id}:${p.name}` === measurement.id);
|
|
2299
|
+
if (port) {
|
|
2300
|
+
port.x = measurement.rect.x - (candidate.x + (candidate.dx ?? 0));
|
|
2301
|
+
port.y = measurement.rect.y - (candidate.y + (candidate.dy ?? 0));
|
|
2302
|
+
port.width = measurement.rect.width;
|
|
2303
|
+
port.height = measurement.rect.height;
|
|
2304
|
+
break;
|
|
2305
|
+
}
|
|
1516
2306
|
}
|
|
1517
2307
|
}
|
|
1518
2308
|
}),
|
|
1519
|
-
|
|
2309
|
+
removeMeasurements: (ctx, event) => produce(ctx, (draft) => {
|
|
2310
|
+
for (const id of event.ids) draft.measurements.delete(id);
|
|
2311
|
+
}),
|
|
2312
|
+
bakeLabelPositions: (ctx, event, enqueue) => produce(ctx, (draft) => {
|
|
1520
2313
|
for (const [edgeId, center] of event.positions) {
|
|
1521
2314
|
const edge = draft.graph.edges.find((e) => e.id === edgeId);
|
|
1522
2315
|
if (edge?.label) {
|
|
@@ -1526,6 +2319,10 @@ function createFlowStore(initialGraph = {
|
|
|
1526
2319
|
edge.label.dy = 0;
|
|
1527
2320
|
}
|
|
1528
2321
|
}
|
|
2322
|
+
enqueue.emit.graphChanged({
|
|
2323
|
+
graph: snapshotGraph(draft.graph),
|
|
2324
|
+
event: createEventObject("bakeLabelPositions", event)
|
|
2325
|
+
});
|
|
1529
2326
|
})
|
|
1530
2327
|
}
|
|
1531
2328
|
}).with(undoRedo({
|
|
@@ -1817,16 +2614,150 @@ function createFlowEngine(store, options = {}) {
|
|
|
1817
2614
|
function ctx() {
|
|
1818
2615
|
return store.getSnapshot().context;
|
|
1819
2616
|
}
|
|
1820
|
-
|
|
1821
|
-
|
|
2617
|
+
const edgePathsCache = /* @__PURE__ */ new WeakMap();
|
|
2618
|
+
const snapOffsetCache = /* @__PURE__ */ new WeakMap();
|
|
2619
|
+
const renderableCache = /* @__PURE__ */ new WeakMap();
|
|
2620
|
+
const groupGeometryCache = /* @__PURE__ */ new WeakMap();
|
|
2621
|
+
const edgeOwnerCache = /* @__PURE__ */ new WeakMap();
|
|
2622
|
+
function ownedEdges(c) {
|
|
2623
|
+
let index = edgeOwnerCache.get(c);
|
|
2624
|
+
if (!index) {
|
|
2625
|
+
index = /* @__PURE__ */ new Map();
|
|
2626
|
+
for (const edge of c.graph.edges) {
|
|
2627
|
+
const custom = options.edgeOwner?.(edge, c);
|
|
2628
|
+
const owner = options.edgeOwner && custom !== void 0 ? custom : getEdgeOwner(edge, c.graph.nodes);
|
|
2629
|
+
if (owner == null) continue;
|
|
2630
|
+
let bucket = index.get(owner);
|
|
2631
|
+
if (!bucket) index.set(owner, bucket = []);
|
|
2632
|
+
bucket.push(edge);
|
|
2633
|
+
}
|
|
2634
|
+
edgeOwnerCache.set(c, index);
|
|
2635
|
+
}
|
|
2636
|
+
return index;
|
|
1822
2637
|
}
|
|
1823
|
-
|
|
1824
|
-
|
|
2638
|
+
const highlightIndexCache = /* @__PURE__ */ new WeakMap();
|
|
2639
|
+
const NO_HIGHLIGHTS = [];
|
|
2640
|
+
/**
|
|
2641
|
+
* Group geometry per snapshot, derived bottom-up. `derived` groups are a
|
|
2642
|
+
* pure function of their children's (visual) bounds — recomputed per
|
|
2643
|
+
* transition, so containment can't drift and sizing is reversible.
|
|
2644
|
+
*/
|
|
2645
|
+
function groupGeometryFor(c, node, visiting) {
|
|
2646
|
+
if (!node.group) return null;
|
|
2647
|
+
let byId = groupGeometryCache.get(c);
|
|
2648
|
+
const cached = byId?.get(node.id);
|
|
2649
|
+
if (cached !== void 0) return cached;
|
|
2650
|
+
const geometry = computeGroupGeometryRaw(c, node, visiting, void 0);
|
|
2651
|
+
if (!byId) {
|
|
2652
|
+
byId = /* @__PURE__ */ new Map();
|
|
2653
|
+
groupGeometryCache.set(c, byId);
|
|
2654
|
+
}
|
|
2655
|
+
byId.set(node.id, geometry);
|
|
2656
|
+
return geometry;
|
|
1825
2657
|
}
|
|
1826
|
-
function
|
|
1827
|
-
|
|
2658
|
+
function computeGroupGeometryRaw(c, node, visiting, excludeChildren) {
|
|
2659
|
+
const contentSize = resolvedSize(node);
|
|
2660
|
+
if (node.group.type === "fixed") return fixedGroupGeometry(resolvedBounds(node), node.group.contentSize ?? {
|
|
2661
|
+
width: 0,
|
|
2662
|
+
height: 0
|
|
2663
|
+
}, node.group);
|
|
2664
|
+
const seen = visiting ?? /* @__PURE__ */ new Set();
|
|
2665
|
+
seen.add(node.id);
|
|
2666
|
+
let union = null;
|
|
2667
|
+
const children = indexChildren(c.graph.nodes).get(node.id);
|
|
2668
|
+
for (const child of children ?? []) {
|
|
2669
|
+
if (child.hidden || seen.has(child.id)) continue;
|
|
2670
|
+
if (excludeChildren?.has(child.id)) continue;
|
|
2671
|
+
const b = isDerivedGroup(child) ? excludeChildren ? computeGroupGeometryRaw(c, child, seen, excludeChildren).bounds : groupGeometryFor(c, child, seen)?.bounds ?? resolvedBounds(child) : hierarchyBounds(child, c);
|
|
2672
|
+
union = uniteRects(union, b);
|
|
2673
|
+
}
|
|
2674
|
+
for (const edge of ownedEdges(c).get(node.id) ?? []) {
|
|
2675
|
+
if (edge.hidden || !edge.label) continue;
|
|
2676
|
+
if (edge.sourceId === node.id && edge.targetId === node.id) continue;
|
|
2677
|
+
const label = edge.label;
|
|
2678
|
+
if (!label.width && !label.height) continue;
|
|
2679
|
+
union = uniteRects(union, {
|
|
2680
|
+
x: label.x + (label.dx ?? 0),
|
|
2681
|
+
y: label.y + (label.dy ?? 0),
|
|
2682
|
+
width: label.width,
|
|
2683
|
+
height: label.height
|
|
2684
|
+
});
|
|
2685
|
+
}
|
|
2686
|
+
return deriveGroupGeometry(contentSize, union, node.group, resolvedPosition(node));
|
|
1828
2687
|
}
|
|
1829
|
-
function
|
|
2688
|
+
function uniteRects(union, b) {
|
|
2689
|
+
return union ? unionRect(union, b) : { ...b };
|
|
2690
|
+
}
|
|
2691
|
+
/** Bounds with group hierarchy applied (derived groups wrap their children). */
|
|
2692
|
+
function hierarchyBounds(node, c) {
|
|
2693
|
+
if (isDerivedGroup(node)) {
|
|
2694
|
+
const geometry = groupGeometryFor(c, node);
|
|
2695
|
+
if (geometry) return geometry.bounds;
|
|
2696
|
+
}
|
|
2697
|
+
return resolvedBounds(node);
|
|
2698
|
+
}
|
|
2699
|
+
const edgePathMemo = /* @__PURE__ */ new Map();
|
|
2700
|
+
let edgePathMemoInputs = null;
|
|
2701
|
+
function getNode(id) {
|
|
2702
|
+
return indexNodes(ctx().graph.nodes).get(id);
|
|
2703
|
+
}
|
|
2704
|
+
function getEdge(id) {
|
|
2705
|
+
return indexEdges(ctx().graph.edges).get(id);
|
|
2706
|
+
}
|
|
2707
|
+
function getEntity(id) {
|
|
2708
|
+
return getNode(id) ?? getEdge(id);
|
|
2709
|
+
}
|
|
2710
|
+
function isNodeVisible(node, c, viewport = c.viewport) {
|
|
2711
|
+
if (node.hidden) return false;
|
|
2712
|
+
if (globalConfig.visible && !resolveBehavior(globalConfig.visible, node, c)) return false;
|
|
2713
|
+
if (isVisibleHook && !isVisibleHook(node, viewport)) return false;
|
|
2714
|
+
return true;
|
|
2715
|
+
}
|
|
2716
|
+
function isEdgeVisible(edge, graph, c, viewport = c.viewport) {
|
|
2717
|
+
if (edge.hidden) return false;
|
|
2718
|
+
const nodeIndex = indexNodes(graph.nodes);
|
|
2719
|
+
const source = nodeIndex.get(edge.sourceId);
|
|
2720
|
+
const target = nodeIndex.get(edge.targetId);
|
|
2721
|
+
if (!source || !target) return false;
|
|
2722
|
+
if (!isNodeVisible(source, c, viewport) || !isNodeVisible(target, c, viewport)) return false;
|
|
2723
|
+
if (globalConfig.visible && !resolveBehavior(globalConfig.visible, edge, c)) return false;
|
|
2724
|
+
if (isVisibleHook && !isVisibleHook(edge, viewport)) return false;
|
|
2725
|
+
return true;
|
|
2726
|
+
}
|
|
2727
|
+
function getAddressable(id) {
|
|
2728
|
+
const node = getNode(id);
|
|
2729
|
+
if (node) return {
|
|
2730
|
+
kind: "node",
|
|
2731
|
+
id,
|
|
2732
|
+
node
|
|
2733
|
+
};
|
|
2734
|
+
const edge = getEdge(id);
|
|
2735
|
+
if (edge) return {
|
|
2736
|
+
kind: "edge",
|
|
2737
|
+
id,
|
|
2738
|
+
edge
|
|
2739
|
+
};
|
|
2740
|
+
for (const candidate of ctx().graph.nodes) {
|
|
2741
|
+
const port = candidate.ports?.find((p) => p.id === id || `${candidate.id}:${p.name}` === id);
|
|
2742
|
+
if (port) return {
|
|
2743
|
+
kind: "port",
|
|
2744
|
+
id,
|
|
2745
|
+
nodeId: candidate.id,
|
|
2746
|
+
portName: port.name,
|
|
2747
|
+
port
|
|
2748
|
+
};
|
|
2749
|
+
}
|
|
2750
|
+
return null;
|
|
2751
|
+
}
|
|
2752
|
+
function getOwnerEntity(id) {
|
|
2753
|
+
const addressable = getAddressable(id);
|
|
2754
|
+
if (!addressable) return null;
|
|
2755
|
+
if (addressable.kind === "node") return addressable.node;
|
|
2756
|
+
if (addressable.kind === "edge") return addressable.edge;
|
|
2757
|
+
if (addressable.kind === "port") return getNode(addressable.nodeId) ?? null;
|
|
2758
|
+
return getEntity(addressable.ownerId) ?? null;
|
|
2759
|
+
}
|
|
2760
|
+
function screenToCanvas$1(screenPoint) {
|
|
1830
2761
|
return screenToCanvas(screenPoint, ctx().viewport);
|
|
1831
2762
|
}
|
|
1832
2763
|
function canvasToScreen$1(canvasPoint) {
|
|
@@ -1834,10 +2765,11 @@ function createFlowEngine(store, options = {}) {
|
|
|
1834
2765
|
}
|
|
1835
2766
|
function entityAtPoint(canvasPoint) {
|
|
1836
2767
|
const { graph } = ctx();
|
|
2768
|
+
const c = ctx();
|
|
1837
2769
|
for (let i = graph.nodes.length - 1; i >= 0; i--) {
|
|
1838
2770
|
const node = graph.nodes[i];
|
|
1839
2771
|
if (!node) continue;
|
|
1840
|
-
const bounds =
|
|
2772
|
+
const bounds = hierarchyBounds(node, c);
|
|
1841
2773
|
if (getShapeOrDefault(node.shape ?? "rect").hitTest(canvasPoint, bounds)) return node;
|
|
1842
2774
|
}
|
|
1843
2775
|
const nodeMap = /* @__PURE__ */ new Map();
|
|
@@ -1863,10 +2795,11 @@ function createFlowEngine(store, options = {}) {
|
|
|
1863
2795
|
}
|
|
1864
2796
|
function getEdgePath(edgeId) {
|
|
1865
2797
|
const { graph } = ctx();
|
|
1866
|
-
const edge = graph.edges.
|
|
2798
|
+
const edge = indexEdges(graph.edges).get(edgeId);
|
|
1867
2799
|
if (!edge) return null;
|
|
1868
|
-
const
|
|
1869
|
-
const
|
|
2800
|
+
const nodeIndex = indexNodes(graph.nodes);
|
|
2801
|
+
const sourceNode = nodeIndex.get(edge.sourceId);
|
|
2802
|
+
const targetNode = nodeIndex.get(edge.targetId);
|
|
1870
2803
|
if (!sourceNode || !targetNode) return null;
|
|
1871
2804
|
const srcBounds = resolvedBounds(sourceNode);
|
|
1872
2805
|
const tgtBounds = resolvedBounds(targetNode);
|
|
@@ -1882,6 +2815,40 @@ function createFlowEngine(store, options = {}) {
|
|
|
1882
2815
|
obstacles: []
|
|
1883
2816
|
});
|
|
1884
2817
|
}
|
|
2818
|
+
function getEdgePathBounds(edgeId) {
|
|
2819
|
+
const path = getAllEdgePaths().get(edgeId);
|
|
2820
|
+
if (!path) return null;
|
|
2821
|
+
return boundsForPoints(path.segments.flatMap((segment) => segment.points));
|
|
2822
|
+
}
|
|
2823
|
+
function getEdgeLabelBounds(edgeId) {
|
|
2824
|
+
const measured = ctx().measurements.get(edgeId);
|
|
2825
|
+
if (measured) return measured;
|
|
2826
|
+
const edge = getEdge(edgeId);
|
|
2827
|
+
if (!edge?.label) return null;
|
|
2828
|
+
const nodeMap = /* @__PURE__ */ new Map();
|
|
2829
|
+
for (const n of ctx().graph.nodes) nodeMap.set(n.id, n);
|
|
2830
|
+
return resolveEdgeLabelBounds(edge, nodeMap);
|
|
2831
|
+
}
|
|
2832
|
+
function getBounds(id) {
|
|
2833
|
+
const measured = ctx().measurements.get(id);
|
|
2834
|
+
if (measured) return measured;
|
|
2835
|
+
const addressable = getAddressable(id);
|
|
2836
|
+
if (!addressable) return null;
|
|
2837
|
+
if (addressable.kind === "node") return resolvedBounds(addressable.node);
|
|
2838
|
+
if (addressable.kind === "edge") return getEdgeLabelBounds(id) ?? getEdgePathBounds(id);
|
|
2839
|
+
if (addressable.kind === "port") {
|
|
2840
|
+
const owner = getNode(addressable.nodeId);
|
|
2841
|
+
if (!owner) return null;
|
|
2842
|
+
const ownerPosition = resolvedPosition(owner);
|
|
2843
|
+
return {
|
|
2844
|
+
x: ownerPosition.x + addressable.port.x,
|
|
2845
|
+
y: ownerPosition.y + addressable.port.y,
|
|
2846
|
+
width: addressable.port.width,
|
|
2847
|
+
height: addressable.port.height
|
|
2848
|
+
};
|
|
2849
|
+
}
|
|
2850
|
+
return null;
|
|
2851
|
+
}
|
|
1885
2852
|
/** Resolve edge label bounds using committed (static) node positions */
|
|
1886
2853
|
function resolveEdgeLabelBounds(edge, nodeMap) {
|
|
1887
2854
|
if (!edge.label) return null;
|
|
@@ -1965,16 +2932,20 @@ function createFlowEngine(store, options = {}) {
|
|
|
1965
2932
|
}
|
|
1966
2933
|
return rects;
|
|
1967
2934
|
}
|
|
1968
|
-
/** Check if rect `a` fully contains rect `b` */
|
|
1969
|
-
function rectContains(a, b) {
|
|
1970
|
-
return b.x >= a.x && b.y >= a.y && b.x + b.width <= a.x + a.width && b.y + b.height <= a.y + a.height;
|
|
1971
|
-
}
|
|
1972
2935
|
/**
|
|
1973
2936
|
* Compute visual snap offset for edge routing.
|
|
1974
2937
|
* Returns the offset to apply to selected nodes so edges follow the snapped visual position.
|
|
2938
|
+
* Memoized per store snapshot — callers invoke this once per node per frame.
|
|
1975
2939
|
*/
|
|
1976
2940
|
function getVisualSnapOffset() {
|
|
1977
2941
|
const c = ctx();
|
|
2942
|
+
const cached = snapOffsetCache.get(c);
|
|
2943
|
+
if (cached) return cached;
|
|
2944
|
+
const result = computeVisualSnapOffset(c);
|
|
2945
|
+
snapOffsetCache.set(c, result);
|
|
2946
|
+
return result;
|
|
2947
|
+
}
|
|
2948
|
+
function computeVisualSnapOffset(c) {
|
|
1978
2949
|
const selection = c.selection;
|
|
1979
2950
|
const grid = c.snapToGrid ? c.snapGrid : null;
|
|
1980
2951
|
let offset = {
|
|
@@ -2004,9 +2975,37 @@ function createFlowEngine(store, options = {}) {
|
|
|
2004
2975
|
}
|
|
2005
2976
|
/** Get visual bounds for a node — applies snap offset for selected nodes during drag */
|
|
2006
2977
|
function visualBounds(node, snap) {
|
|
2978
|
+
const c = ctx();
|
|
2979
|
+
if (c.canvasState === "resizing" && c.resizingEntityId === node.id) {
|
|
2980
|
+
const resizeBounds = getResizeBoundsFromContext(c);
|
|
2981
|
+
if (resizeBounds) return resizeBounds;
|
|
2982
|
+
}
|
|
2983
|
+
if (isDerivedGroup(node)) {
|
|
2984
|
+
const geometry = groupGeometryFor(c, node);
|
|
2985
|
+
if (geometry) return geometry.bounds;
|
|
2986
|
+
}
|
|
2007
2987
|
const entityOffset = snap.selection.has(node.id) ? snap.offset : null;
|
|
2008
2988
|
return snappedBounds(node, snap.grid, entityOffset);
|
|
2009
2989
|
}
|
|
2990
|
+
function visualLabelBounds(edge, basePos, snap) {
|
|
2991
|
+
const isSelected = snap.selection.has(edge.id);
|
|
2992
|
+
let x = basePos.x + (edge.label?.dx ?? 0);
|
|
2993
|
+
let y = basePos.y + (edge.label?.dy ?? 0);
|
|
2994
|
+
if (isSelected) {
|
|
2995
|
+
x += snap.offset.x;
|
|
2996
|
+
y += snap.offset.y;
|
|
2997
|
+
if (snap.grid) {
|
|
2998
|
+
x = snapToGrid(x, snap.grid[0]);
|
|
2999
|
+
y = snapToGrid(y, snap.grid[1]);
|
|
3000
|
+
}
|
|
3001
|
+
}
|
|
3002
|
+
return {
|
|
3003
|
+
x,
|
|
3004
|
+
y,
|
|
3005
|
+
width: edge.label?.width ?? 0,
|
|
3006
|
+
height: edge.label?.height ?? 0
|
|
3007
|
+
};
|
|
3008
|
+
}
|
|
2010
3009
|
/** Get visual position for a node — applies snap offset for selected nodes during drag */
|
|
2011
3010
|
function visualPosition(node, snap) {
|
|
2012
3011
|
const b = visualBounds(node, snap);
|
|
@@ -2032,6 +3031,10 @@ function createFlowEngine(store, options = {}) {
|
|
|
2032
3031
|
if (snap) return visualBounds(node, snap);
|
|
2033
3032
|
return resolvedBounds(node);
|
|
2034
3033
|
}
|
|
3034
|
+
function portSideOf(node, portName) {
|
|
3035
|
+
if (!portName || portName === "*" || !node.ports) return null;
|
|
3036
|
+
return node.ports.find((p) => p.name === portName)?.side ?? null;
|
|
3037
|
+
}
|
|
2035
3038
|
function getShapeAnchor(node, bounds, direction) {
|
|
2036
3039
|
return getShapeOrDefault(node.shape ?? "rect").edgeAnchor(bounds, direction);
|
|
2037
3040
|
}
|
|
@@ -2052,70 +3055,121 @@ function createFlowEngine(store, options = {}) {
|
|
|
2052
3055
|
}
|
|
2053
3056
|
return getShapeAnchor(node, nodeBounds, fallbackDirection);
|
|
2054
3057
|
}
|
|
2055
|
-
|
|
2056
|
-
|
|
3058
|
+
/**
|
|
3059
|
+
* Resolve the geometry shared by edge rendering and endpoint queries: the
|
|
3060
|
+
* endpoint bounds, the facing direction of each end, and the anchor points.
|
|
3061
|
+
* Single source of truth so rendered paths and `getEdgeEndpoints` can never
|
|
3062
|
+
* disagree on which side of a node an edge attaches to.
|
|
3063
|
+
*/
|
|
3064
|
+
function resolveEdgeGeometry(edge, source, target, snap) {
|
|
3065
|
+
const sourceBounds = getEndpointBounds(source, edge.sourcePort, snap);
|
|
3066
|
+
const targetBounds = getEndpointBounds(target, edge.targetPort, snap);
|
|
3067
|
+
const sourceDirection = portSideOf(source, edge.sourcePort) ?? bestDirection(sourceBounds, targetBounds);
|
|
3068
|
+
const targetDirection = portSideOf(target, edge.targetPort) ?? bestDirection(targetBounds, sourceBounds);
|
|
3069
|
+
return {
|
|
3070
|
+
sourceBounds,
|
|
3071
|
+
targetBounds,
|
|
3072
|
+
sourceDirection,
|
|
3073
|
+
targetDirection,
|
|
3074
|
+
sourcePoint: resolveEndpointPoint(source, sourceBounds, edge.sourcePort, targetBounds, sourceDirection, snap),
|
|
3075
|
+
targetPoint: resolveEndpointPoint(target, targetBounds, edge.targetPort, sourceBounds, targetDirection, snap)
|
|
3076
|
+
};
|
|
3077
|
+
}
|
|
3078
|
+
/**
|
|
3079
|
+
* Route all edges. Memoized per (store snapshot, graph) pair — repeated
|
|
3080
|
+
* calls within one transition (edge layer, label layer, hit testing) reuse
|
|
3081
|
+
* the same Map. Callers must treat the result as read-only.
|
|
3082
|
+
*/
|
|
3083
|
+
function getAllEdgePaths(options = {}) {
|
|
3084
|
+
const c = ctx();
|
|
3085
|
+
const graph = options.graph ?? c.graph;
|
|
3086
|
+
let byGraph = edgePathsCache.get(c);
|
|
3087
|
+
const cached = byGraph?.get(graph);
|
|
3088
|
+
if (cached) return cached;
|
|
3089
|
+
const paths = computeAllEdgePaths(c, graph);
|
|
3090
|
+
if (!byGraph) {
|
|
3091
|
+
byGraph = /* @__PURE__ */ new WeakMap();
|
|
3092
|
+
edgePathsCache.set(c, byGraph);
|
|
3093
|
+
}
|
|
3094
|
+
byGraph.set(graph, paths);
|
|
3095
|
+
return paths;
|
|
3096
|
+
}
|
|
3097
|
+
function computeAllEdgePaths(c, graph) {
|
|
2057
3098
|
const paths = /* @__PURE__ */ new Map();
|
|
2058
3099
|
const snap = getVisualSnapOffset();
|
|
2059
|
-
const nodeMap =
|
|
2060
|
-
|
|
3100
|
+
const nodeMap = indexNodes(graph.nodes);
|
|
3101
|
+
const memoUsable = !isVisibleHook && !globalConfig.visible;
|
|
3102
|
+
if (memoUsable) {
|
|
3103
|
+
const inputs = edgePathMemoInputs;
|
|
3104
|
+
if (!inputs || inputs.canvasState !== c.canvasState || inputs.selection !== c.selection || inputs.offsetX !== snap.offset.x || inputs.offsetY !== snap.offset.y || inputs.grid !== snap.grid || inputs.resizingEntityId !== c.resizingEntityId || inputs.resizeCorners !== c.resizeCorners) {
|
|
3105
|
+
edgePathMemo.clear();
|
|
3106
|
+
edgePathMemoInputs = {
|
|
3107
|
+
canvasState: c.canvasState,
|
|
3108
|
+
selection: c.selection,
|
|
3109
|
+
offsetX: snap.offset.x,
|
|
3110
|
+
offsetY: snap.offset.y,
|
|
3111
|
+
grid: snap.grid,
|
|
3112
|
+
resizingEntityId: c.resizingEntityId,
|
|
3113
|
+
resizeCorners: c.resizeCorners
|
|
3114
|
+
};
|
|
3115
|
+
}
|
|
3116
|
+
}
|
|
2061
3117
|
for (const edge of graph.edges) {
|
|
2062
3118
|
const source = nodeMap.get(edge.sourceId);
|
|
2063
3119
|
const target = nodeMap.get(edge.targetId);
|
|
2064
3120
|
if (!source || !target) continue;
|
|
2065
|
-
|
|
2066
|
-
const
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
3121
|
+
if (!isEdgeVisible(edge, graph, c)) continue;
|
|
3122
|
+
const memoizableEdge = memoUsable && !isDerivedGroup(source) && !isDerivedGroup(target);
|
|
3123
|
+
if (memoizableEdge) {
|
|
3124
|
+
const entry = edgePathMemo.get(edge.id);
|
|
3125
|
+
if (entry && entry.edge === edge && entry.source === source && entry.target === target) {
|
|
3126
|
+
paths.set(edge.id, entry.path);
|
|
3127
|
+
continue;
|
|
3128
|
+
}
|
|
3129
|
+
}
|
|
3130
|
+
const { sourceBounds, targetBounds, sourcePoint, targetPoint } = resolveEdgeGeometry(edge, source, target, snap);
|
|
3131
|
+
let pathData;
|
|
2071
3132
|
if (edge.label) {
|
|
2072
|
-
const
|
|
2073
|
-
|
|
2074
|
-
|
|
2075
|
-
|
|
2076
|
-
|
|
2077
|
-
|
|
2078
|
-
|
|
2079
|
-
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2083
|
-
|
|
2084
|
-
|
|
2085
|
-
|
|
2086
|
-
|
|
2087
|
-
|
|
2088
|
-
|
|
2089
|
-
obstacles: []
|
|
2090
|
-
});
|
|
2091
|
-
const seg2 = router(edge, {
|
|
2092
|
-
sourceBounds: labelBounds,
|
|
2093
|
-
targetBounds,
|
|
2094
|
-
sourceDirection: bestDirection(labelBounds, targetBounds),
|
|
2095
|
-
targetDirection: bestDirection(targetBounds, labelBounds),
|
|
2096
|
-
targetPoint,
|
|
2097
|
-
obstacles: []
|
|
2098
|
-
});
|
|
2099
|
-
paths.set(edge.id, {
|
|
2100
|
-
segments: [seg1, seg2],
|
|
3133
|
+
const labelBounds = visualLabelBounds(edge, resolveLabelAnchor(edge.labelAnchor ?? defaultLabelAnchor, edge.label, sourceBounds, targetBounds), snap);
|
|
3134
|
+
pathData = {
|
|
3135
|
+
segments: [router(edge, {
|
|
3136
|
+
sourceBounds,
|
|
3137
|
+
targetBounds: labelBounds,
|
|
3138
|
+
sourceDirection: portSideOf(source, edge.sourcePort) ?? bestDirection(sourceBounds, labelBounds),
|
|
3139
|
+
targetDirection: bestDirection(labelBounds, sourceBounds),
|
|
3140
|
+
sourcePoint,
|
|
3141
|
+
obstacles: []
|
|
3142
|
+
}), router(edge, {
|
|
3143
|
+
sourceBounds: labelBounds,
|
|
3144
|
+
targetBounds,
|
|
3145
|
+
sourceDirection: bestDirection(labelBounds, targetBounds),
|
|
3146
|
+
targetDirection: portSideOf(target, edge.targetPort) ?? bestDirection(targetBounds, labelBounds),
|
|
3147
|
+
targetPoint,
|
|
3148
|
+
obstacles: []
|
|
3149
|
+
})],
|
|
2101
3150
|
labelBounds
|
|
2102
|
-
}
|
|
2103
|
-
} else {
|
|
2104
|
-
|
|
3151
|
+
};
|
|
3152
|
+
} else pathData = {
|
|
3153
|
+
segments: [router(edge, {
|
|
2105
3154
|
sourceBounds,
|
|
2106
3155
|
targetBounds,
|
|
2107
|
-
sourceDirection: bestDirection(sourceBounds, targetBounds),
|
|
2108
|
-
targetDirection: bestDirection(targetBounds, sourceBounds),
|
|
3156
|
+
sourceDirection: portSideOf(source, edge.sourcePort) ?? bestDirection(sourceBounds, targetBounds),
|
|
3157
|
+
targetDirection: portSideOf(target, edge.targetPort) ?? bestDirection(targetBounds, sourceBounds),
|
|
2109
3158
|
sourcePoint,
|
|
2110
3159
|
targetPoint,
|
|
2111
3160
|
obstacles: []
|
|
2112
|
-
})
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
|
|
2116
|
-
|
|
2117
|
-
|
|
3161
|
+
})],
|
|
3162
|
+
labelBounds: null
|
|
3163
|
+
};
|
|
3164
|
+
paths.set(edge.id, pathData);
|
|
3165
|
+
if (memoizableEdge) edgePathMemo.set(edge.id, {
|
|
3166
|
+
edge,
|
|
3167
|
+
source,
|
|
3168
|
+
target,
|
|
3169
|
+
path: pathData
|
|
3170
|
+
});
|
|
2118
3171
|
}
|
|
3172
|
+
if (memoUsable && edgePathMemo.size > graph.edges.length * 2 + 16) edgePathMemo.clear();
|
|
2119
3173
|
return paths;
|
|
2120
3174
|
}
|
|
2121
3175
|
function subscribe(listener) {
|
|
@@ -2124,6 +3178,46 @@ function createFlowEngine(store, options = {}) {
|
|
|
2124
3178
|
});
|
|
2125
3179
|
return () => subscription.unsubscribe();
|
|
2126
3180
|
}
|
|
3181
|
+
function computeRenderableEntities(c, options) {
|
|
3182
|
+
const vp = c.viewport;
|
|
3183
|
+
const overscan = options.overscan ?? 0;
|
|
3184
|
+
const visibleRect = getVisibleBounds(vp, c.viewportSize);
|
|
3185
|
+
const cullRect = {
|
|
3186
|
+
x: visibleRect.x - overscan,
|
|
3187
|
+
y: visibleRect.y - overscan,
|
|
3188
|
+
width: visibleRect.width + overscan * 2,
|
|
3189
|
+
height: visibleRect.height + overscan * 2
|
|
3190
|
+
};
|
|
3191
|
+
const cullMode = options.cull === "viewport" && rectContains(cullRect, getGraphBounds(c.graph.nodes, c.graph.edges)) ? "none" : options.cull;
|
|
3192
|
+
const nodes = c.graph.nodes.filter((n) => {
|
|
3193
|
+
if (!isNodeVisible(n, c, vp)) return false;
|
|
3194
|
+
if (cullMode === "viewport" && !rectsIntersect(hierarchyBounds(n, c), cullRect)) return false;
|
|
3195
|
+
return true;
|
|
3196
|
+
});
|
|
3197
|
+
const edgePaths = getAllEdgePaths();
|
|
3198
|
+
return {
|
|
3199
|
+
nodes,
|
|
3200
|
+
edges: c.graph.edges.filter((e) => {
|
|
3201
|
+
if (!isEdgeVisible(e, c.graph, c, vp)) return false;
|
|
3202
|
+
if (cullMode !== "viewport") return true;
|
|
3203
|
+
if (e.label) {
|
|
3204
|
+
if (rectsIntersect({
|
|
3205
|
+
x: e.label.x + (e.label.dx ?? 0),
|
|
3206
|
+
y: e.label.y + (e.label.dy ?? 0),
|
|
3207
|
+
width: e.label.width,
|
|
3208
|
+
height: e.label.height
|
|
3209
|
+
}, cullRect)) return true;
|
|
3210
|
+
}
|
|
3211
|
+
if (edgePaths.get(e.id)?.segments.some((segment) => {
|
|
3212
|
+
const bounds = pathBounds(segment);
|
|
3213
|
+
return bounds !== null && rectsIntersect(bounds, cullRect);
|
|
3214
|
+
})) return true;
|
|
3215
|
+
const srcNode = getNode(e.sourceId);
|
|
3216
|
+
const tgtNode = getNode(e.targetId);
|
|
3217
|
+
return !!srcNode && !!tgtNode && (rectsIntersect(resolvedBounds(srcNode), cullRect) || rectsIntersect(resolvedBounds(tgtNode), cullRect));
|
|
3218
|
+
})
|
|
3219
|
+
};
|
|
3220
|
+
}
|
|
2127
3221
|
return {
|
|
2128
3222
|
store,
|
|
2129
3223
|
trigger: store.trigger,
|
|
@@ -2131,11 +3225,16 @@ function createFlowEngine(store, options = {}) {
|
|
|
2131
3225
|
getNode,
|
|
2132
3226
|
getEdge,
|
|
2133
3227
|
getEntity,
|
|
3228
|
+
getAddressable,
|
|
3229
|
+
getOwnerEntity,
|
|
3230
|
+
getBounds,
|
|
2134
3231
|
screenToCanvas: screenToCanvas$1,
|
|
2135
3232
|
canvasToScreen: canvasToScreen$1,
|
|
2136
3233
|
entityAtPoint,
|
|
2137
3234
|
entityIdAtPoint,
|
|
2138
3235
|
getEdgePath,
|
|
3236
|
+
getEdgePathBounds,
|
|
3237
|
+
getEdgeLabelBounds,
|
|
2139
3238
|
getAllEdgePaths,
|
|
2140
3239
|
resolvedPosition: (nodeOrId) => {
|
|
2141
3240
|
const node = typeof nodeOrId === "string" ? getNode(nodeOrId) : nodeOrId;
|
|
@@ -2153,18 +3252,154 @@ function createFlowEngine(store, options = {}) {
|
|
|
2153
3252
|
width: 0,
|
|
2154
3253
|
height: 0
|
|
2155
3254
|
};
|
|
3255
|
+
const c = ctx();
|
|
3256
|
+
if (isDerivedGroup(node)) return hierarchyBounds(node, c);
|
|
3257
|
+
if (c.canvasState === "dragging" && c.selection.has(node.id) && ((node.dx ?? 0) !== 0 || (node.dy ?? 0) !== 0)) return visualBounds(node, getVisualSnapOffset());
|
|
2156
3258
|
return resolvedBounds(node);
|
|
2157
3259
|
},
|
|
3260
|
+
/**
|
|
3261
|
+
* All highlights on an entity as `{ kind, meta }` entries. Returns a
|
|
3262
|
+
* referentially stable array (the same empty array when none), so
|
|
3263
|
+
* subscribers can use identity equality.
|
|
3264
|
+
*/
|
|
3265
|
+
getHighlights: (entityId) => {
|
|
3266
|
+
const highlights = ctx().highlights;
|
|
3267
|
+
if (highlights.size === 0) return NO_HIGHLIGHTS;
|
|
3268
|
+
let index = highlightIndexCache.get(highlights);
|
|
3269
|
+
if (!index) {
|
|
3270
|
+
index = /* @__PURE__ */ new Map();
|
|
3271
|
+
for (const [kind, bucket] of highlights) for (const [id, meta] of bucket) {
|
|
3272
|
+
let entries = index.get(id);
|
|
3273
|
+
if (!entries) index.set(id, entries = []);
|
|
3274
|
+
entries.push({
|
|
3275
|
+
kind,
|
|
3276
|
+
meta
|
|
3277
|
+
});
|
|
3278
|
+
}
|
|
3279
|
+
highlightIndexCache.set(highlights, index);
|
|
3280
|
+
}
|
|
3281
|
+
return index.get(entityId) ?? NO_HIGHLIGHTS;
|
|
3282
|
+
},
|
|
3283
|
+
/** Whether the entity is currently hovered. */
|
|
3284
|
+
isHovered: (entityId) => ctx().hoveredEntityIds.has(entityId),
|
|
3285
|
+
/**
|
|
3286
|
+
* Deepest group whose child area intersects `bounds`, with the overlap
|
|
3287
|
+
* ratio (intersection / bounds area). Groups in `exclude` (and groups
|
|
3288
|
+
* inside excluded subtrees) are skipped, and excluded nodes do not
|
|
3289
|
+
* contribute to derived child areas — i.e. each group is evaluated as it
|
|
3290
|
+
* would be without the moving nodes. Drives reparent-on-drop.
|
|
3291
|
+
*/
|
|
3292
|
+
getGroupDropTarget: (bounds, options = {}) => {
|
|
3293
|
+
const c = ctx();
|
|
3294
|
+
const excluded = new Set(options.exclude ?? []);
|
|
3295
|
+
const nodeIndex = indexNodes(c.graph.nodes);
|
|
3296
|
+
const depths = nodeDepths(c.graph.nodes);
|
|
3297
|
+
const area = Math.max(bounds.width * bounds.height, 1);
|
|
3298
|
+
let best = null;
|
|
3299
|
+
for (const node of c.graph.nodes) {
|
|
3300
|
+
if (!node.group || node.hidden || excluded.has(node.id)) continue;
|
|
3301
|
+
let ancestorId = node.parentId ?? null;
|
|
3302
|
+
let insideExcluded = false;
|
|
3303
|
+
const seen = /* @__PURE__ */ new Set();
|
|
3304
|
+
while (ancestorId && !seen.has(ancestorId)) {
|
|
3305
|
+
if (excluded.has(ancestorId)) {
|
|
3306
|
+
insideExcluded = true;
|
|
3307
|
+
break;
|
|
3308
|
+
}
|
|
3309
|
+
seen.add(ancestorId);
|
|
3310
|
+
ancestorId = nodeIndex.get(ancestorId)?.parentId ?? null;
|
|
3311
|
+
}
|
|
3312
|
+
if (insideExcluded) continue;
|
|
3313
|
+
const childArea = computeGroupGeometryRaw(c, node, void 0, excluded).childArea;
|
|
3314
|
+
const overlap = Math.max(0, Math.min(bounds.x + bounds.width, childArea.x + childArea.width) - Math.max(bounds.x, childArea.x)) * Math.max(0, Math.min(bounds.y + bounds.height, childArea.y + childArea.height) - Math.max(bounds.y, childArea.y)) / area;
|
|
3315
|
+
if (overlap <= 0) continue;
|
|
3316
|
+
const depth = depths.get(node.id) ?? 0;
|
|
3317
|
+
if (!best || depth > best.depth || depth === best.depth && overlap > best.overlap) best = {
|
|
3318
|
+
id: node.id,
|
|
3319
|
+
overlap,
|
|
3320
|
+
childArea,
|
|
3321
|
+
depth
|
|
3322
|
+
};
|
|
3323
|
+
}
|
|
3324
|
+
return best ? {
|
|
3325
|
+
id: best.id,
|
|
3326
|
+
overlap: best.overlap,
|
|
3327
|
+
childArea: best.childArea
|
|
3328
|
+
} : null;
|
|
3329
|
+
},
|
|
3330
|
+
/**
|
|
3331
|
+
* Canvas-space anchor rect for toolbars: a node's (hierarchy-aware)
|
|
3332
|
+
* bounds, an edge's label bounds — or, with no label, an assumed 1×1
|
|
3333
|
+
* label at the path midpoint — and, given several ids (e.g. the
|
|
3334
|
+
* selection), the union of their anchors. Null when nothing resolves.
|
|
3335
|
+
*/
|
|
3336
|
+
getToolbarAnchor: (idOrIds) => {
|
|
3337
|
+
const ids = typeof idOrIds === "string" ? [idOrIds] : [...idOrIds];
|
|
3338
|
+
const c = ctx();
|
|
3339
|
+
let union = null;
|
|
3340
|
+
for (const id of ids) {
|
|
3341
|
+
let rect = null;
|
|
3342
|
+
const node = getNode(id);
|
|
3343
|
+
if (node) rect = hierarchyBounds(node, c);
|
|
3344
|
+
else {
|
|
3345
|
+
const edge = getEdge(id);
|
|
3346
|
+
if (edge?.label && (edge.label.width || edge.label.height)) rect = {
|
|
3347
|
+
x: edge.label.x + (edge.label.dx ?? 0),
|
|
3348
|
+
y: edge.label.y + (edge.label.dy ?? 0),
|
|
3349
|
+
width: edge.label.width,
|
|
3350
|
+
height: edge.label.height
|
|
3351
|
+
};
|
|
3352
|
+
else if (edge) {
|
|
3353
|
+
const points = getAllEdgePaths().get(id)?.segments[0]?.points ?? [];
|
|
3354
|
+
if (points.length > 0) {
|
|
3355
|
+
const mid = points.length % 2 === 1 ? points[(points.length - 1) / 2] : {
|
|
3356
|
+
x: (points[points.length / 2 - 1].x + points[points.length / 2].x) / 2,
|
|
3357
|
+
y: (points[points.length / 2 - 1].y + points[points.length / 2].y) / 2
|
|
3358
|
+
};
|
|
3359
|
+
rect = {
|
|
3360
|
+
x: mid.x,
|
|
3361
|
+
y: mid.y,
|
|
3362
|
+
width: 1,
|
|
3363
|
+
height: 1
|
|
3364
|
+
};
|
|
3365
|
+
}
|
|
3366
|
+
}
|
|
3367
|
+
}
|
|
3368
|
+
if (!rect) continue;
|
|
3369
|
+
union = union ? uniteRects(union, rect) : rect;
|
|
3370
|
+
}
|
|
3371
|
+
return union;
|
|
3372
|
+
},
|
|
3373
|
+
/** The node containing this edge (null = root). See `edgeOwner` option. */
|
|
3374
|
+
getEdgeOwner: (edgeId) => {
|
|
3375
|
+
const c = ctx();
|
|
3376
|
+
const edge = indexEdges(c.graph.edges).get(edgeId);
|
|
3377
|
+
if (!edge) return null;
|
|
3378
|
+
const custom = options.edgeOwner?.(edge, c);
|
|
3379
|
+
return options.edgeOwner && custom !== void 0 ? custom : getEdgeOwner(edge, c.graph.nodes);
|
|
3380
|
+
},
|
|
3381
|
+
/** Group geometry (overall bounds, content block, child area), or null. */
|
|
3382
|
+
getGroupGeometry: (nodeOrId) => {
|
|
3383
|
+
const node = typeof nodeOrId === "string" ? getNode(nodeOrId) : nodeOrId;
|
|
3384
|
+
if (!node) return null;
|
|
3385
|
+
return groupGeometryFor(ctx(), node);
|
|
3386
|
+
},
|
|
2158
3387
|
getGraphBounds: () => getGraphBounds(ctx().graph.nodes, ctx().graph.edges),
|
|
2159
3388
|
getSelectionBounds: () => {
|
|
2160
3389
|
const c = ctx();
|
|
2161
3390
|
return getSelectionBounds(c.graph.nodes, c.graph.edges, c.selection);
|
|
2162
3391
|
},
|
|
2163
3392
|
entitiesInRect: (rect) => {
|
|
2164
|
-
const
|
|
3393
|
+
const c = ctx();
|
|
3394
|
+
const { graph } = c;
|
|
2165
3395
|
const ids = [];
|
|
2166
|
-
|
|
3396
|
+
const edgePaths = getAllEdgePaths();
|
|
3397
|
+
for (const n of graph.nodes) {
|
|
3398
|
+
if (!isNodeVisible(n, c)) continue;
|
|
3399
|
+
if (rectsIntersect(rect, resolvedBounds(n))) ids.push(n.id);
|
|
3400
|
+
}
|
|
2167
3401
|
for (const e of graph.edges) {
|
|
3402
|
+
if (!isEdgeVisible(e, graph, c)) continue;
|
|
2168
3403
|
if (e.label) {
|
|
2169
3404
|
if (rectsIntersect(rect, {
|
|
2170
3405
|
x: e.label.x + (e.label.dx ?? 0),
|
|
@@ -2174,7 +3409,7 @@ function createFlowEngine(store, options = {}) {
|
|
|
2174
3409
|
})) ids.push(e.id);
|
|
2175
3410
|
continue;
|
|
2176
3411
|
}
|
|
2177
|
-
const path =
|
|
3412
|
+
const path = edgePaths.get(e.id);
|
|
2178
3413
|
if (path && path.segments.some((segment) => rectIntersectsPath(rect, segment))) ids.push(e.id);
|
|
2179
3414
|
}
|
|
2180
3415
|
return ids;
|
|
@@ -2208,48 +3443,10 @@ function createFlowEngine(store, options = {}) {
|
|
|
2208
3443
|
const srcNode = graph.nodes.find((n) => n.id === edge.sourceId);
|
|
2209
3444
|
const tgtNode = graph.nodes.find((n) => n.id === edge.targetId);
|
|
2210
3445
|
if (!srcNode || !tgtNode) return null;
|
|
2211
|
-
const
|
|
2212
|
-
const srcBounds = visualBounds(srcNode, snap);
|
|
2213
|
-
const tgtBounds = visualBounds(tgtNode, snap);
|
|
2214
|
-
let source;
|
|
2215
|
-
if (edge.sourcePort === "*" && srcNode.ports?.length) source = closestPortPosition(srcNode, {
|
|
2216
|
-
x: tgtBounds.x + tgtBounds.width / 2,
|
|
2217
|
-
y: tgtBounds.y + tgtBounds.height / 2
|
|
2218
|
-
});
|
|
2219
|
-
else if (edge.sourcePort && srcNode.ports) {
|
|
2220
|
-
const port = srcNode.ports.find((p) => p.name === edge.sourcePort);
|
|
2221
|
-
if (port) {
|
|
2222
|
-
const np = visualPosition(srcNode, snap);
|
|
2223
|
-
source = {
|
|
2224
|
-
x: np.x + port.x + port.width / 2,
|
|
2225
|
-
y: np.y + port.y + port.height / 2
|
|
2226
|
-
};
|
|
2227
|
-
} else source = {
|
|
2228
|
-
x: srcBounds.x + srcBounds.width / 2,
|
|
2229
|
-
y: srcBounds.y + srcBounds.height / 2
|
|
2230
|
-
};
|
|
2231
|
-
} else source = getShapeAnchor(srcNode, srcBounds, bestDirection(srcBounds, tgtBounds));
|
|
2232
|
-
let target;
|
|
2233
|
-
if (edge.targetPort === "*" && tgtNode.ports?.length) target = closestPortPosition(tgtNode, {
|
|
2234
|
-
x: srcBounds.x + srcBounds.width / 2,
|
|
2235
|
-
y: srcBounds.y + srcBounds.height / 2
|
|
2236
|
-
});
|
|
2237
|
-
else if (edge.targetPort && tgtNode.ports) {
|
|
2238
|
-
const port = tgtNode.ports.find((p) => p.name === edge.targetPort);
|
|
2239
|
-
if (port) {
|
|
2240
|
-
const np = visualPosition(tgtNode, snap);
|
|
2241
|
-
target = {
|
|
2242
|
-
x: np.x + port.x + port.width / 2,
|
|
2243
|
-
y: np.y + port.y + port.height / 2
|
|
2244
|
-
};
|
|
2245
|
-
} else target = {
|
|
2246
|
-
x: tgtBounds.x + tgtBounds.width / 2,
|
|
2247
|
-
y: tgtBounds.y + tgtBounds.height / 2
|
|
2248
|
-
};
|
|
2249
|
-
} else target = getShapeAnchor(tgtNode, tgtBounds, bestDirection(tgtBounds, srcBounds));
|
|
3446
|
+
const { sourcePoint, targetPoint } = resolveEdgeGeometry(edge, srcNode, tgtNode, getVisualSnapOffset());
|
|
2250
3447
|
return {
|
|
2251
|
-
source,
|
|
2252
|
-
target
|
|
3448
|
+
source: sourcePoint,
|
|
3449
|
+
target: targetPoint
|
|
2253
3450
|
};
|
|
2254
3451
|
},
|
|
2255
3452
|
validateConnection: (proposed) => {
|
|
@@ -2281,7 +3478,7 @@ function createFlowEngine(store, options = {}) {
|
|
|
2281
3478
|
};
|
|
2282
3479
|
const c = ctx();
|
|
2283
3480
|
return {
|
|
2284
|
-
hidden: entity.
|
|
3481
|
+
hidden: entity.type === "edge" ? !isEdgeVisible(entity, c.graph, c) : !isNodeVisible(entity, c),
|
|
2285
3482
|
selectable: entity.selectable ?? resolveBehavior(globalConfig.selectable, entity, c, true),
|
|
2286
3483
|
draggable: entity.draggable ?? resolveBehavior(globalConfig.draggable, entity, c, true),
|
|
2287
3484
|
connectable: entity.connectable ?? resolveBehavior(globalConfig.connectable, entity, c, true),
|
|
@@ -2291,61 +3488,30 @@ function createFlowEngine(store, options = {}) {
|
|
|
2291
3488
|
};
|
|
2292
3489
|
},
|
|
2293
3490
|
getVisibleEntities: () => {
|
|
3491
|
+
const c = ctx();
|
|
2294
3492
|
return {
|
|
2295
|
-
nodes:
|
|
2296
|
-
|
|
2297
|
-
if (globalConfig.visible && !resolveBehavior(globalConfig.visible, n, ctx())) return false;
|
|
2298
|
-
if (isVisibleHook && !isVisibleHook(n, ctx().viewport)) return false;
|
|
2299
|
-
return true;
|
|
2300
|
-
}),
|
|
2301
|
-
edges: ctx().graph.edges.filter((e) => {
|
|
2302
|
-
if (e.hidden) return false;
|
|
2303
|
-
if (globalConfig.visible && !resolveBehavior(globalConfig.visible, e, ctx())) return false;
|
|
2304
|
-
if (isVisibleHook && !isVisibleHook(e, ctx().viewport)) return false;
|
|
2305
|
-
return true;
|
|
2306
|
-
})
|
|
3493
|
+
nodes: c.graph.nodes.filter((n) => isNodeVisible(n, c)),
|
|
3494
|
+
edges: c.graph.edges.filter((e) => isEdgeVisible(e, c.graph, c))
|
|
2307
3495
|
};
|
|
2308
3496
|
},
|
|
3497
|
+
/**
|
|
3498
|
+
* Renderable (visible, optionally viewport-culled) entities. Memoized per
|
|
3499
|
+
* (store snapshot, cull/overscan) so several render layers can share one
|
|
3500
|
+
* computation per frame. Treat the result as read-only.
|
|
3501
|
+
*/
|
|
2309
3502
|
getRenderableEntities: (options = {}) => {
|
|
2310
3503
|
const c = ctx();
|
|
2311
|
-
const
|
|
2312
|
-
|
|
2313
|
-
const
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
|
|
2317
|
-
|
|
2318
|
-
|
|
2319
|
-
}
|
|
2320
|
-
|
|
2321
|
-
|
|
2322
|
-
if (globalConfig.visible && !resolveBehavior(globalConfig.visible, n, c)) return false;
|
|
2323
|
-
if (isVisibleHook && !isVisibleHook(n, vp)) return false;
|
|
2324
|
-
if (options.cull === "viewport" && !rectsIntersect(resolvedBounds(n), cullRect)) return false;
|
|
2325
|
-
return true;
|
|
2326
|
-
});
|
|
2327
|
-
const edgePaths = getAllEdgePaths();
|
|
2328
|
-
return {
|
|
2329
|
-
nodes,
|
|
2330
|
-
edges: c.graph.edges.filter((e) => {
|
|
2331
|
-
if (e.hidden) return false;
|
|
2332
|
-
if (globalConfig.visible && !resolveBehavior(globalConfig.visible, e, c)) return false;
|
|
2333
|
-
if (isVisibleHook && !isVisibleHook(e, vp)) return false;
|
|
2334
|
-
if (options.cull !== "viewport") return true;
|
|
2335
|
-
if (e.label) {
|
|
2336
|
-
if (rectsIntersect({
|
|
2337
|
-
x: e.label.x + (e.label.dx ?? 0),
|
|
2338
|
-
y: e.label.y + (e.label.dy ?? 0),
|
|
2339
|
-
width: e.label.width,
|
|
2340
|
-
height: e.label.height
|
|
2341
|
-
}, cullRect)) return true;
|
|
2342
|
-
}
|
|
2343
|
-
if (edgePaths.get(e.id)?.segments.some((segment) => rectIntersectsPath(cullRect, segment))) return true;
|
|
2344
|
-
const srcNode = getNode(e.sourceId);
|
|
2345
|
-
const tgtNode = getNode(e.targetId);
|
|
2346
|
-
return !!srcNode && !!tgtNode && (rectsIntersect(resolvedBounds(srcNode), cullRect) || rectsIntersect(resolvedBounds(tgtNode), cullRect));
|
|
2347
|
-
})
|
|
2348
|
-
};
|
|
3504
|
+
const cacheKey = `${options.cull ?? "none"}:${options.overscan ?? 0}`;
|
|
3505
|
+
let byKey = renderableCache.get(c);
|
|
3506
|
+
const cached = byKey?.get(cacheKey);
|
|
3507
|
+
if (cached) return cached;
|
|
3508
|
+
const result = computeRenderableEntities(c, options);
|
|
3509
|
+
if (!byKey) {
|
|
3510
|
+
byKey = /* @__PURE__ */ new Map();
|
|
3511
|
+
renderableCache.set(c, byKey);
|
|
3512
|
+
}
|
|
3513
|
+
byKey.set(cacheKey, result);
|
|
3514
|
+
return result;
|
|
2349
3515
|
},
|
|
2350
3516
|
getIntersectingNodes: (nodeId) => {
|
|
2351
3517
|
const node = getNode(nodeId);
|
|
@@ -2386,19 +3552,41 @@ function createFlowEngine(store, options = {}) {
|
|
|
2386
3552
|
},
|
|
2387
3553
|
/** Get in-progress resize bounds, or null if not resizing */
|
|
2388
3554
|
getResizeBounds: () => {
|
|
2389
|
-
|
|
2390
|
-
if (!c.resizeCorners || !c.resizingEntityId) return null;
|
|
2391
|
-
const [a, b] = c.resizeCorners;
|
|
2392
|
-
return {
|
|
2393
|
-
x: Math.min(a.x, b.x),
|
|
2394
|
-
y: Math.min(a.y, b.y),
|
|
2395
|
-
width: Math.max(10, Math.abs(b.x - a.x)),
|
|
2396
|
-
height: Math.max(10, Math.abs(b.y - a.y))
|
|
2397
|
-
};
|
|
3555
|
+
return getResizeBoundsFromContext(ctx());
|
|
2398
3556
|
},
|
|
2399
3557
|
subscribe
|
|
2400
3558
|
};
|
|
2401
3559
|
}
|
|
3560
|
+
function getResizeBoundsFromContext(c) {
|
|
3561
|
+
if (!c.resizeCorners || !c.resizingEntityId) return null;
|
|
3562
|
+
const [a, b] = c.resizeCorners;
|
|
3563
|
+
return {
|
|
3564
|
+
x: Math.min(a.x, b.x),
|
|
3565
|
+
y: Math.min(a.y, b.y),
|
|
3566
|
+
width: Math.max(10, Math.abs(b.x - a.x)),
|
|
3567
|
+
height: Math.max(10, Math.abs(b.y - a.y))
|
|
3568
|
+
};
|
|
3569
|
+
}
|
|
3570
|
+
function boundsForPoints(points) {
|
|
3571
|
+
if (points.length === 0) return null;
|
|
3572
|
+
let minX = Infinity;
|
|
3573
|
+
let minY = Infinity;
|
|
3574
|
+
let maxX = -Infinity;
|
|
3575
|
+
let maxY = -Infinity;
|
|
3576
|
+
for (const point of points) {
|
|
3577
|
+
minX = Math.min(minX, point.x);
|
|
3578
|
+
minY = Math.min(minY, point.y);
|
|
3579
|
+
maxX = Math.max(maxX, point.x);
|
|
3580
|
+
maxY = Math.max(maxY, point.y);
|
|
3581
|
+
}
|
|
3582
|
+
if (!isFinite(minX)) return null;
|
|
3583
|
+
return {
|
|
3584
|
+
x: minX,
|
|
3585
|
+
y: minY,
|
|
3586
|
+
width: maxX - minX,
|
|
3587
|
+
height: maxY - minY
|
|
3588
|
+
};
|
|
3589
|
+
}
|
|
2402
3590
|
function closestPortPosition(node, target) {
|
|
2403
3591
|
if (!node.ports?.length) {
|
|
2404
3592
|
const b = resolvedBounds(node);
|
|
@@ -2457,6 +3645,13 @@ function resolveLabelAnchor(anchor, label, sourceBounds, targetBounds) {
|
|
|
2457
3645
|
}
|
|
2458
3646
|
//#endregion
|
|
2459
3647
|
//#region src/createFlow.ts
|
|
3648
|
+
/**
|
|
3649
|
+
* The undoRedo store extension keeps `past`/`future` history on the snapshot
|
|
3650
|
+
* at runtime without exposing them in its public types — read them defensively.
|
|
3651
|
+
*/
|
|
3652
|
+
function getHistory(store) {
|
|
3653
|
+
return store.getSnapshot();
|
|
3654
|
+
}
|
|
2460
3655
|
function createFlow(options = {}) {
|
|
2461
3656
|
const store = createFlowStore(options.graph, options.store);
|
|
2462
3657
|
const engine = createFlowEngine(store, options.engine);
|
|
@@ -2470,7 +3665,7 @@ function createFlow(options = {}) {
|
|
|
2470
3665
|
cleanup.clear();
|
|
2471
3666
|
}
|
|
2472
3667
|
async function runLayout(layout = options.layout ?? void 0) {
|
|
2473
|
-
if (!layout) throw new Error("
|
|
3668
|
+
if (!layout) throw new Error("runLayout() was called without a layout algorithm. Pass one to createFlow({ layout }) or call flow.runLayout(myLayout) directly.");
|
|
2474
3669
|
const positions = await layout(store.getSnapshot().context.graph);
|
|
2475
3670
|
store.trigger.applyLayoutResult({ positions });
|
|
2476
3671
|
}
|
|
@@ -2478,19 +3673,27 @@ function createFlow(options = {}) {
|
|
|
2478
3673
|
store,
|
|
2479
3674
|
engine,
|
|
2480
3675
|
trigger: store.trigger,
|
|
3676
|
+
on: store.on,
|
|
2481
3677
|
destroy,
|
|
2482
3678
|
getContext: engine.getContext,
|
|
2483
3679
|
subscribe: engine.subscribe,
|
|
2484
3680
|
getNode: engine.getNode,
|
|
2485
3681
|
getEdge: engine.getEdge,
|
|
2486
3682
|
getEntity: engine.getEntity,
|
|
3683
|
+
getAddressable: engine.getAddressable,
|
|
3684
|
+
getOwnerEntity: engine.getOwnerEntity,
|
|
3685
|
+
getBounds: engine.getBounds,
|
|
2487
3686
|
screenToCanvas: engine.screenToCanvas,
|
|
2488
3687
|
canvasToScreen: engine.canvasToScreen,
|
|
2489
3688
|
getAllEdgePaths: engine.getAllEdgePaths,
|
|
2490
3689
|
getGraphBounds: engine.getGraphBounds,
|
|
2491
3690
|
getSelectionBounds: engine.getSelectionBounds,
|
|
2492
3691
|
getRenderableEntities: engine.getRenderableEntities,
|
|
2493
|
-
runLayout
|
|
3692
|
+
runLayout,
|
|
3693
|
+
undo: () => store.trigger.undo(),
|
|
3694
|
+
redo: () => store.trigger.redo(),
|
|
3695
|
+
canUndo: () => (getHistory(store).past?.length ?? 0) > 0,
|
|
3696
|
+
canRedo: () => (getHistory(store).future?.length ?? 0) > 0
|
|
2494
3697
|
};
|
|
2495
3698
|
}
|
|
2496
3699
|
//#endregion
|
|
@@ -2530,5 +3733,1007 @@ function createGraph(opts) {
|
|
|
2530
3733
|
...opts
|
|
2531
3734
|
};
|
|
2532
3735
|
}
|
|
3736
|
+
function createPort(opts) {
|
|
3737
|
+
return {
|
|
3738
|
+
x: 0,
|
|
3739
|
+
y: 0,
|
|
3740
|
+
width: 12,
|
|
3741
|
+
height: 12,
|
|
3742
|
+
data: {},
|
|
3743
|
+
...opts
|
|
3744
|
+
};
|
|
3745
|
+
}
|
|
3746
|
+
//#endregion
|
|
3747
|
+
//#region src/connectionPreview.ts
|
|
3748
|
+
const CONNECTION_PREVIEW_NODE_ID = "flow:connection-preview-node";
|
|
3749
|
+
const CONNECTION_PREVIEW_EDGE_ID = "flow:connection-preview-edge";
|
|
3750
|
+
function createConnectionPreviewGraph(graph, connection, options = {}) {
|
|
3751
|
+
if (!connection?.active) return graph;
|
|
3752
|
+
const nodeId = options.nodeId ?? "flow:connection-preview-node";
|
|
3753
|
+
const edgeId = options.edgeId ?? "flow:connection-preview-edge";
|
|
3754
|
+
const nodeSize = options.nodeSize ?? 2;
|
|
3755
|
+
const targetId = connection.targetId ?? nodeId;
|
|
3756
|
+
const nodes = connection.targetId ? [...graph.nodes] : [...graph.nodes, createPreviewNode(nodeId, connection.pointerPosition, nodeSize)];
|
|
3757
|
+
const edges = [...graph.edges, createEdge({
|
|
3758
|
+
id: edgeId,
|
|
3759
|
+
sourceId: connection.sourceId,
|
|
3760
|
+
targetId,
|
|
3761
|
+
sourcePort: connection.sourcePort ?? void 0,
|
|
3762
|
+
targetPort: connection.targetPort ?? void 0,
|
|
3763
|
+
selectable: false,
|
|
3764
|
+
draggable: false,
|
|
3765
|
+
deletable: false,
|
|
3766
|
+
data: {
|
|
3767
|
+
temp: { kind: "connection-preview" },
|
|
3768
|
+
isValid: connection.isValid
|
|
3769
|
+
}
|
|
3770
|
+
})];
|
|
3771
|
+
return {
|
|
3772
|
+
...graph,
|
|
3773
|
+
nodes,
|
|
3774
|
+
edges
|
|
3775
|
+
};
|
|
3776
|
+
}
|
|
3777
|
+
function createPreviewNode(id, point, size) {
|
|
3778
|
+
return createNode({
|
|
3779
|
+
id,
|
|
3780
|
+
x: point.x - size / 2,
|
|
3781
|
+
y: point.y - size / 2,
|
|
3782
|
+
width: size,
|
|
3783
|
+
height: size,
|
|
3784
|
+
selectable: false,
|
|
3785
|
+
draggable: false,
|
|
3786
|
+
deletable: false,
|
|
3787
|
+
data: { temp: { kind: "connection-preview" } }
|
|
3788
|
+
});
|
|
3789
|
+
}
|
|
3790
|
+
//#endregion
|
|
3791
|
+
//#region src/toolbar.ts
|
|
3792
|
+
function getToolbarPlacement(input) {
|
|
3793
|
+
const offset = input.offset ?? 10;
|
|
3794
|
+
const padding = input.padding ?? 8;
|
|
3795
|
+
const { toolbarWidth: tw, toolbarHeight: th, anchor } = input;
|
|
3796
|
+
const topPlacementTop = anchor.y - offset - th;
|
|
3797
|
+
const bottomPlacementTop = anchor.y + anchor.height + offset;
|
|
3798
|
+
const topFits = topPlacementTop >= padding;
|
|
3799
|
+
const bottomFits = bottomPlacementTop + th <= input.viewportHeight - padding;
|
|
3800
|
+
let side;
|
|
3801
|
+
if (topFits) side = "top";
|
|
3802
|
+
else if (bottomFits) side = "bottom";
|
|
3803
|
+
else side = anchor.y >= input.viewportHeight - (anchor.y + anchor.height) ? "top" : "bottom";
|
|
3804
|
+
let top = side === "top" ? topPlacementTop : bottomPlacementTop;
|
|
3805
|
+
const maxTop = Math.max(input.viewportHeight - padding - th, padding);
|
|
3806
|
+
top = Math.min(Math.max(top, padding), maxTop);
|
|
3807
|
+
let left = anchor.x + anchor.width / 2 - tw / 2;
|
|
3808
|
+
const maxLeft = Math.max(input.viewportWidth - padding - tw, padding);
|
|
3809
|
+
left = Math.min(Math.max(left, padding), maxLeft);
|
|
3810
|
+
return {
|
|
3811
|
+
left,
|
|
3812
|
+
top,
|
|
3813
|
+
side
|
|
3814
|
+
};
|
|
3815
|
+
}
|
|
3816
|
+
//#endregion
|
|
3817
|
+
//#region src/minimap.ts
|
|
3818
|
+
function getMinimapProjection(options) {
|
|
3819
|
+
const padding = options.padding ?? .1;
|
|
3820
|
+
const content = options.contentBounds;
|
|
3821
|
+
const visible = getVisibleBounds(options.viewport, options.viewportSize);
|
|
3822
|
+
const padX = content.width * padding;
|
|
3823
|
+
const padY = content.height * padding;
|
|
3824
|
+
let bounds = {
|
|
3825
|
+
x: content.x - padX,
|
|
3826
|
+
y: content.y - padY,
|
|
3827
|
+
width: content.width + padX * 2,
|
|
3828
|
+
height: content.height + padY * 2
|
|
3829
|
+
};
|
|
3830
|
+
if (visible.width > 0 && visible.height > 0) bounds = unionRect(bounds, visible);
|
|
3831
|
+
if (bounds.width <= 0 || bounds.height <= 0) bounds = {
|
|
3832
|
+
x: bounds.x,
|
|
3833
|
+
y: bounds.y,
|
|
3834
|
+
width: 1,
|
|
3835
|
+
height: 1
|
|
3836
|
+
};
|
|
3837
|
+
const scale = Math.min(options.width / bounds.width, options.height / bounds.height);
|
|
3838
|
+
const offsetX = (options.width - bounds.width * scale) / 2;
|
|
3839
|
+
const offsetY = (options.height - bounds.height * scale) / 2;
|
|
3840
|
+
const toMinimap = (point) => ({
|
|
3841
|
+
x: (point.x - bounds.x) * scale + offsetX,
|
|
3842
|
+
y: (point.y - bounds.y) * scale + offsetY
|
|
3843
|
+
});
|
|
3844
|
+
const toCanvas = (point) => ({
|
|
3845
|
+
x: (point.x - offsetX) / scale + bounds.x,
|
|
3846
|
+
y: (point.y - offsetY) / scale + bounds.y
|
|
3847
|
+
});
|
|
3848
|
+
const rectToMinimap = (rect) => {
|
|
3849
|
+
const origin = toMinimap(rect);
|
|
3850
|
+
return {
|
|
3851
|
+
x: origin.x,
|
|
3852
|
+
y: origin.y,
|
|
3853
|
+
width: rect.width * scale,
|
|
3854
|
+
height: rect.height * scale
|
|
3855
|
+
};
|
|
3856
|
+
};
|
|
3857
|
+
return {
|
|
3858
|
+
scale,
|
|
3859
|
+
bounds,
|
|
3860
|
+
viewportRect: rectToMinimap(visible),
|
|
3861
|
+
toMinimap,
|
|
3862
|
+
toCanvas,
|
|
3863
|
+
rectToMinimap
|
|
3864
|
+
};
|
|
3865
|
+
}
|
|
3866
|
+
//#endregion
|
|
3867
|
+
//#region src/input/modes.ts
|
|
3868
|
+
const preventDefault$1 = { type: "preventDefault" };
|
|
3869
|
+
function distance(a, b) {
|
|
3870
|
+
return Math.hypot(b.x - a.x, b.y - a.y);
|
|
3871
|
+
}
|
|
3872
|
+
function isMulti(event) {
|
|
3873
|
+
return event.modifiers.shift || event.modifiers.meta || event.modifiers.ctrl;
|
|
3874
|
+
}
|
|
3875
|
+
function keyboardMode(options = {}) {
|
|
3876
|
+
const allowDelete = options.allowDelete ?? true;
|
|
3877
|
+
const allowUndo = options.allowUndo ?? true;
|
|
3878
|
+
return {
|
|
3879
|
+
id: "keyboard",
|
|
3880
|
+
priority: 100,
|
|
3881
|
+
start(event, { context }) {
|
|
3882
|
+
if (event.kind !== "key.down") return null;
|
|
3883
|
+
if (event.editableTarget) return null;
|
|
3884
|
+
if (event.key === " ") return { commands: [preventDefault$1] };
|
|
3885
|
+
if (event.key === "Escape") return { commands: context.canvasState === "idle" ? [
|
|
3886
|
+
trigger("cancelConnection"),
|
|
3887
|
+
trigger("clearSelection"),
|
|
3888
|
+
trigger("clearFocus")
|
|
3889
|
+
] : [trigger("cancelConnection")] };
|
|
3890
|
+
const mod = event.modifiers.meta || event.modifiers.ctrl;
|
|
3891
|
+
if (allowUndo && mod && event.key.toLowerCase() === "z") return { commands: [preventDefault$1, event.modifiers.shift ? trigger("redo") : trigger("undo")] };
|
|
3892
|
+
if (allowDelete && (event.key === "Delete" || event.key === "Backspace")) {
|
|
3893
|
+
if (context.selection.size === 0) return null;
|
|
3894
|
+
return { commands: [preventDefault$1, trigger("deleteSelected")] };
|
|
3895
|
+
}
|
|
3896
|
+
return null;
|
|
3897
|
+
}
|
|
3898
|
+
};
|
|
3899
|
+
}
|
|
3900
|
+
const ARROW_DIRECTIONS = {
|
|
3901
|
+
ArrowLeft: {
|
|
3902
|
+
x: -1,
|
|
3903
|
+
y: 0
|
|
3904
|
+
},
|
|
3905
|
+
ArrowRight: {
|
|
3906
|
+
x: 1,
|
|
3907
|
+
y: 0
|
|
3908
|
+
},
|
|
3909
|
+
ArrowUp: {
|
|
3910
|
+
x: 0,
|
|
3911
|
+
y: -1
|
|
3912
|
+
},
|
|
3913
|
+
ArrowDown: {
|
|
3914
|
+
x: 0,
|
|
3915
|
+
y: 1
|
|
3916
|
+
}
|
|
3917
|
+
};
|
|
3918
|
+
function keyboardNavMode(options = {}) {
|
|
3919
|
+
const allowMove = options.allowMove ?? true;
|
|
3920
|
+
const step = options.step ?? 5;
|
|
3921
|
+
const shiftStep = options.shiftStep ?? 20;
|
|
3922
|
+
return {
|
|
3923
|
+
id: "keyboard-nav",
|
|
3924
|
+
priority: 95,
|
|
3925
|
+
start(event, { context }) {
|
|
3926
|
+
if (event.kind !== "key.down" || event.editableTarget) return null;
|
|
3927
|
+
if (event.key === "Enter" && context.focusedEntityId) return { commands: [preventDefault$1, trigger("select", {
|
|
3928
|
+
ids: [context.focusedEntityId],
|
|
3929
|
+
mode: event.modifiers.shift ? "multi" : "single"
|
|
3930
|
+
})] };
|
|
3931
|
+
if (!allowMove) return null;
|
|
3932
|
+
const direction = ARROW_DIRECTIONS[event.key];
|
|
3933
|
+
if (!direction || context.selection.size === 0) return null;
|
|
3934
|
+
return { commands: [
|
|
3935
|
+
preventDefault$1,
|
|
3936
|
+
trigger("moveNodes", {
|
|
3937
|
+
direction,
|
|
3938
|
+
factor: event.modifiers.shift ? shiftStep : step
|
|
3939
|
+
}),
|
|
3940
|
+
trigger("commitDrag")
|
|
3941
|
+
] };
|
|
3942
|
+
}
|
|
3943
|
+
};
|
|
3944
|
+
}
|
|
3945
|
+
function wheelMode() {
|
|
3946
|
+
return {
|
|
3947
|
+
id: "wheel",
|
|
3948
|
+
priority: 90,
|
|
3949
|
+
start(event, { context }) {
|
|
3950
|
+
if (event.kind !== "wheel") return null;
|
|
3951
|
+
if (isTrackpadPinch({
|
|
3952
|
+
ctrlKey: event.modifiers.ctrl,
|
|
3953
|
+
metaKey: event.modifiers.meta
|
|
3954
|
+
})) return { commands: [preventDefault$1, trigger("zoomTo", {
|
|
3955
|
+
zoom: context.viewport.zoom * Math.pow(2, wheelZoomDelta({
|
|
3956
|
+
deltaY: event.deltaY,
|
|
3957
|
+
deltaMode: event.deltaMode,
|
|
3958
|
+
ctrlKey: event.modifiers.ctrl
|
|
3959
|
+
})),
|
|
3960
|
+
center: event.screenPoint
|
|
3961
|
+
})] };
|
|
3962
|
+
return { commands: [preventDefault$1, trigger("panBy", normalizeWheelPan({
|
|
3963
|
+
deltaX: event.deltaX,
|
|
3964
|
+
deltaY: event.deltaY,
|
|
3965
|
+
deltaMode: event.deltaMode
|
|
3966
|
+
}))] };
|
|
3967
|
+
}
|
|
3968
|
+
};
|
|
3969
|
+
}
|
|
3970
|
+
function panMode() {
|
|
3971
|
+
return {
|
|
3972
|
+
id: "pan",
|
|
3973
|
+
priority: 80,
|
|
3974
|
+
start(event, { input }) {
|
|
3975
|
+
if (event.kind !== "pointer.down") return null;
|
|
3976
|
+
const middle = event.button === 1;
|
|
3977
|
+
const space = input.spaceHeld && event.button === 0;
|
|
3978
|
+
if (!middle && !space) return null;
|
|
3979
|
+
return {
|
|
3980
|
+
commands: [trigger("panStart", event.screenPoint)],
|
|
3981
|
+
session: panSession()
|
|
3982
|
+
};
|
|
3983
|
+
}
|
|
3984
|
+
};
|
|
3985
|
+
}
|
|
3986
|
+
function panSession() {
|
|
3987
|
+
return {
|
|
3988
|
+
update(event) {
|
|
3989
|
+
if (event.kind === "pointer.move") return { commands: [trigger("pan", event.screenPoint)] };
|
|
3990
|
+
if (event.kind === "pointer.up" || event.kind === "pointer.cancel") return {
|
|
3991
|
+
commands: [trigger("panEnd")],
|
|
3992
|
+
status: "end"
|
|
3993
|
+
};
|
|
3994
|
+
return {};
|
|
3995
|
+
},
|
|
3996
|
+
cancel() {
|
|
3997
|
+
return [trigger("panEnd")];
|
|
3998
|
+
}
|
|
3999
|
+
};
|
|
4000
|
+
}
|
|
4001
|
+
const REPARENT_HIGHLIGHT_KIND = "drop-target";
|
|
4002
|
+
function entityPressMode(options = {}) {
|
|
4003
|
+
const allowDrag = options.drag ?? true;
|
|
4004
|
+
const reparent = options.reparent === true ? {} : typeof options.reparent === "object" ? options.reparent : null;
|
|
4005
|
+
return {
|
|
4006
|
+
id: "entity-press",
|
|
4007
|
+
priority: 50,
|
|
4008
|
+
start(event, { context, engine }) {
|
|
4009
|
+
if (event.kind !== "pointer.down" || event.button !== 0) return null;
|
|
4010
|
+
if (event.target.kind !== "entity") return null;
|
|
4011
|
+
const id = event.target.id;
|
|
4012
|
+
const entity = engine.getEntity(id);
|
|
4013
|
+
if (!entity) return null;
|
|
4014
|
+
const config = engine.resolveEntityConfig(entity);
|
|
4015
|
+
const commands = [];
|
|
4016
|
+
if (config.selectable) {
|
|
4017
|
+
const selection = context.selection;
|
|
4018
|
+
if (!(selection.size > 1 && selection.has(id))) commands.push(trigger("select", {
|
|
4019
|
+
ids: [id],
|
|
4020
|
+
mode: isMulti(event) ? "multi" : "single"
|
|
4021
|
+
}));
|
|
4022
|
+
}
|
|
4023
|
+
const draggablePart = entity.type === "node" || event.target.part === "label";
|
|
4024
|
+
const handleAllows = !event.target.entityHasDragHandle || event.target.pressedDragHandle === true;
|
|
4025
|
+
if (!allowDrag || !draggablePart || !handleAllows || !config.draggable) return { commands };
|
|
4026
|
+
return {
|
|
4027
|
+
commands,
|
|
4028
|
+
session: dragSession(id, event, entity.type === "node" ? reparent : null)
|
|
4029
|
+
};
|
|
4030
|
+
}
|
|
4031
|
+
};
|
|
4032
|
+
}
|
|
4033
|
+
function dragSession(id, down, reparent) {
|
|
4034
|
+
const transactionId = `drag:${id}:${down.pointerId}:${Date.now()}`;
|
|
4035
|
+
let dragging = false;
|
|
4036
|
+
let baseOffset = {
|
|
4037
|
+
x: 0,
|
|
4038
|
+
y: 0
|
|
4039
|
+
};
|
|
4040
|
+
let candidateId = null;
|
|
4041
|
+
let candidateOverlap = 0;
|
|
4042
|
+
let candidateSince = 0;
|
|
4043
|
+
const minOverlap = reparent?.minOverlap ?? .5;
|
|
4044
|
+
const unparentModifier = reparent?.unparentModifier === void 0 ? "alt" : reparent.unparentModifier;
|
|
4045
|
+
/**
|
|
4046
|
+
* Currently applied drag offset of the primary entity. The session anchors
|
|
4047
|
+
* requests to the virtual pointer (`base + pointerTravel`) and asks the
|
|
4048
|
+
* store for the difference — so store-side clamping (extents, fixed-group
|
|
4049
|
+
* child areas) is idempotent against pointer position: overshoot stays
|
|
4050
|
+
* virtual and the entity doesn't move again until the pointer returns.
|
|
4051
|
+
*/
|
|
4052
|
+
function appliedOffset(ctx) {
|
|
4053
|
+
const node = ctx.engine.getNode(id);
|
|
4054
|
+
if (node) return {
|
|
4055
|
+
x: node.dx ?? 0,
|
|
4056
|
+
y: node.dy ?? 0
|
|
4057
|
+
};
|
|
4058
|
+
const edge = ctx.engine.getEdge(id);
|
|
4059
|
+
if (edge?.label) return {
|
|
4060
|
+
x: edge.label.dx ?? 0,
|
|
4061
|
+
y: edge.label.dy ?? 0
|
|
4062
|
+
};
|
|
4063
|
+
return {
|
|
4064
|
+
x: 0,
|
|
4065
|
+
y: 0
|
|
4066
|
+
};
|
|
4067
|
+
}
|
|
4068
|
+
/** Top-level moving nodes + the full excluded (moving) subtree. */
|
|
4069
|
+
function movingInfo(ctx) {
|
|
4070
|
+
const { context, engine } = ctx;
|
|
4071
|
+
const nodes = context.graph.nodes;
|
|
4072
|
+
const nodeIndex = indexNodes(nodes);
|
|
4073
|
+
const childrenIndex = indexChildren(nodes);
|
|
4074
|
+
const movingIds = /* @__PURE__ */ new Set();
|
|
4075
|
+
for (const selectedId of context.selection) {
|
|
4076
|
+
const node = nodeIndex.get(selectedId);
|
|
4077
|
+
if (node && node.draggable !== false) movingIds.add(selectedId);
|
|
4078
|
+
}
|
|
4079
|
+
const topLevel = [];
|
|
4080
|
+
for (const movingId of movingIds) {
|
|
4081
|
+
let ancestor = nodeIndex.get(movingId)?.parentId ?? null;
|
|
4082
|
+
let nested = false;
|
|
4083
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4084
|
+
while (ancestor && !seen.has(ancestor)) {
|
|
4085
|
+
if (movingIds.has(ancestor)) {
|
|
4086
|
+
nested = true;
|
|
4087
|
+
break;
|
|
4088
|
+
}
|
|
4089
|
+
seen.add(ancestor);
|
|
4090
|
+
ancestor = nodeIndex.get(ancestor)?.parentId ?? null;
|
|
4091
|
+
}
|
|
4092
|
+
if (!nested) topLevel.push(nodeIndex.get(movingId));
|
|
4093
|
+
}
|
|
4094
|
+
const exclude = new Set(movingIds);
|
|
4095
|
+
const queue = [...movingIds];
|
|
4096
|
+
while (queue.length > 0) {
|
|
4097
|
+
const current = queue.pop();
|
|
4098
|
+
for (const child of childrenIndex.get(current) ?? []) if (!exclude.has(child.id)) {
|
|
4099
|
+
exclude.add(child.id);
|
|
4100
|
+
queue.push(child.id);
|
|
4101
|
+
}
|
|
4102
|
+
}
|
|
4103
|
+
let bounds = null;
|
|
4104
|
+
for (const node of topLevel) {
|
|
4105
|
+
const b = engine.resolvedBounds(node.id);
|
|
4106
|
+
bounds = bounds ? {
|
|
4107
|
+
x: Math.min(bounds.x, b.x),
|
|
4108
|
+
y: Math.min(bounds.y, b.y),
|
|
4109
|
+
width: Math.max(bounds.x + bounds.width, b.x + b.width) - Math.min(bounds.x, b.x),
|
|
4110
|
+
height: Math.max(bounds.y + bounds.height, b.y + b.height) - Math.min(bounds.y, b.y)
|
|
4111
|
+
} : { ...b };
|
|
4112
|
+
}
|
|
4113
|
+
return {
|
|
4114
|
+
topLevel,
|
|
4115
|
+
exclude,
|
|
4116
|
+
bounds
|
|
4117
|
+
};
|
|
4118
|
+
}
|
|
4119
|
+
function trackCandidate(ctx, pending) {
|
|
4120
|
+
const { exclude, bounds } = movingInfo(ctx);
|
|
4121
|
+
const projected = bounds ? {
|
|
4122
|
+
...bounds,
|
|
4123
|
+
x: bounds.x + pending.x,
|
|
4124
|
+
y: bounds.y + pending.y
|
|
4125
|
+
} : null;
|
|
4126
|
+
const found = projected ? ctx.engine.getGroupDropTarget(projected, { exclude }) : null;
|
|
4127
|
+
const nextId = found && found.overlap >= minOverlap ? found.id : null;
|
|
4128
|
+
candidateOverlap = found?.overlap ?? 0;
|
|
4129
|
+
if (nextId === candidateId) return [];
|
|
4130
|
+
const commands = [];
|
|
4131
|
+
if (candidateId) commands.push(trigger("removeHighlights", {
|
|
4132
|
+
kind: REPARENT_HIGHLIGHT_KIND,
|
|
4133
|
+
ids: [candidateId]
|
|
4134
|
+
}));
|
|
4135
|
+
if (nextId) commands.push(trigger("addHighlights", {
|
|
4136
|
+
kind: REPARENT_HIGHLIGHT_KIND,
|
|
4137
|
+
entries: [{ id: nextId }]
|
|
4138
|
+
}));
|
|
4139
|
+
candidateId = nextId;
|
|
4140
|
+
candidateSince = Date.now();
|
|
4141
|
+
return commands;
|
|
4142
|
+
}
|
|
4143
|
+
function decideAndApply(event, ctx) {
|
|
4144
|
+
const commands = [];
|
|
4145
|
+
if (candidateId) commands.push(trigger("removeHighlights", {
|
|
4146
|
+
kind: REPARENT_HIGHLIGHT_KIND,
|
|
4147
|
+
ids: [candidateId]
|
|
4148
|
+
}));
|
|
4149
|
+
if (event.kind !== "pointer.up" || !reparent) return commands;
|
|
4150
|
+
const { topLevel, exclude, bounds } = movingInfo(ctx);
|
|
4151
|
+
if (!bounds || topLevel.length === 0) return commands;
|
|
4152
|
+
const found = ctx.engine.getGroupDropTarget(bounds, { exclude });
|
|
4153
|
+
const finalId = found && found.overlap >= minOverlap ? found.id : null;
|
|
4154
|
+
const hoverMs = finalId && finalId === candidateId ? Date.now() - candidateSince : 0;
|
|
4155
|
+
candidateId = finalId;
|
|
4156
|
+
candidateOverlap = found?.overlap ?? 0;
|
|
4157
|
+
const drop = {
|
|
4158
|
+
nodes: topLevel,
|
|
4159
|
+
bounds,
|
|
4160
|
+
candidate: candidateId ? {
|
|
4161
|
+
id: candidateId,
|
|
4162
|
+
overlap: candidateOverlap
|
|
4163
|
+
} : null,
|
|
4164
|
+
hoverMs,
|
|
4165
|
+
modifiers: event.modifiers
|
|
4166
|
+
};
|
|
4167
|
+
let decision;
|
|
4168
|
+
if (reparent.decide) decision = reparent.decide(drop, ctx);
|
|
4169
|
+
else if (unparentModifier && event.modifiers[unparentModifier]) decision = null;
|
|
4170
|
+
else if (candidateId && hoverMs >= (reparent.delayMs ?? 0)) decision = candidateId;
|
|
4171
|
+
else if (!candidateId && (reparent.unparent ?? true)) decision = topLevel.some((node) => {
|
|
4172
|
+
if (!node.parentId) return false;
|
|
4173
|
+
const geometry = ctx.engine.getGroupGeometry(node.parentId);
|
|
4174
|
+
return geometry ? !rectsIntersect(bounds, geometry.childArea) : false;
|
|
4175
|
+
}) ? null : void 0;
|
|
4176
|
+
if (decision !== void 0) for (const node of topLevel) {
|
|
4177
|
+
if ((node.parentId ?? null) === decision || node.id === decision) continue;
|
|
4178
|
+
commands.push(trigger("reparentNode", {
|
|
4179
|
+
id: node.id,
|
|
4180
|
+
parentId: decision,
|
|
4181
|
+
transactionId
|
|
4182
|
+
}));
|
|
4183
|
+
}
|
|
4184
|
+
return commands;
|
|
4185
|
+
}
|
|
4186
|
+
return {
|
|
4187
|
+
update(event, ctx) {
|
|
4188
|
+
const { context } = ctx;
|
|
4189
|
+
if (event.kind === "pointer.move") {
|
|
4190
|
+
if (!dragging) {
|
|
4191
|
+
if (distance(down.screenPoint, event.screenPoint) < context.dragThreshold) return {};
|
|
4192
|
+
dragging = true;
|
|
4193
|
+
baseOffset = appliedOffset(ctx);
|
|
4194
|
+
}
|
|
4195
|
+
const applied = appliedOffset(ctx);
|
|
4196
|
+
const dx = baseOffset.x + (event.point.x - down.point.x) - applied.x;
|
|
4197
|
+
const dy = baseOffset.y + (event.point.y - down.point.y) - applied.y;
|
|
4198
|
+
return { commands: [
|
|
4199
|
+
...!context.draggedEntityId ? [trigger("dragStart", {
|
|
4200
|
+
id,
|
|
4201
|
+
transactionId
|
|
4202
|
+
})] : [],
|
|
4203
|
+
trigger("dragMove", {
|
|
4204
|
+
dx,
|
|
4205
|
+
dy
|
|
4206
|
+
}),
|
|
4207
|
+
...reparent ? trackCandidate(ctx, {
|
|
4208
|
+
x: dx,
|
|
4209
|
+
y: dy
|
|
4210
|
+
}) : []
|
|
4211
|
+
] };
|
|
4212
|
+
}
|
|
4213
|
+
if (event.kind === "pointer.up" || event.kind === "pointer.cancel") return {
|
|
4214
|
+
commands: dragging ? [
|
|
4215
|
+
trigger("dragEnd"),
|
|
4216
|
+
trigger("commitDrag"),
|
|
4217
|
+
...reparent ? decideAndApply(event, ctx) : []
|
|
4218
|
+
] : [],
|
|
4219
|
+
status: "end"
|
|
4220
|
+
};
|
|
4221
|
+
return {};
|
|
4222
|
+
},
|
|
4223
|
+
cancel() {
|
|
4224
|
+
const commands = dragging ? [trigger("dragEnd"), trigger("commitDrag")] : [];
|
|
4225
|
+
if (candidateId) commands.push(trigger("removeHighlights", {
|
|
4226
|
+
kind: REPARENT_HIGHLIGHT_KIND,
|
|
4227
|
+
ids: [candidateId]
|
|
4228
|
+
}));
|
|
4229
|
+
return commands;
|
|
4230
|
+
}
|
|
4231
|
+
};
|
|
4232
|
+
}
|
|
4233
|
+
function connectMode(options = {}) {
|
|
4234
|
+
return {
|
|
4235
|
+
id: "connect",
|
|
4236
|
+
priority: 60,
|
|
4237
|
+
start(event, { engine }) {
|
|
4238
|
+
if (event.kind !== "pointer.down" || event.button !== 0) return null;
|
|
4239
|
+
if (event.target.kind !== "port") return null;
|
|
4240
|
+
const { nodeId, portName } = event.target;
|
|
4241
|
+
const node = engine.getNode(nodeId);
|
|
4242
|
+
if (!node?.ports?.some((port) => port.name === portName)) return null;
|
|
4243
|
+
if (!engine.resolveEntityConfig(node).connectable) return null;
|
|
4244
|
+
return {
|
|
4245
|
+
commands: [trigger("connectionStart", {
|
|
4246
|
+
sourceId: nodeId,
|
|
4247
|
+
sourcePort: portName,
|
|
4248
|
+
position: event.point
|
|
4249
|
+
})],
|
|
4250
|
+
session: connectSession({
|
|
4251
|
+
nodeId,
|
|
4252
|
+
port: portName
|
|
4253
|
+
}, options)
|
|
4254
|
+
};
|
|
4255
|
+
}
|
|
4256
|
+
};
|
|
4257
|
+
}
|
|
4258
|
+
function defaultConnectionValidity(args, ctx) {
|
|
4259
|
+
const { source, target } = args;
|
|
4260
|
+
if (target.kind === "port") {
|
|
4261
|
+
if (target.nodeId === source.nodeId) return false;
|
|
4262
|
+
return ctx.engine.validateConnection({
|
|
4263
|
+
sourceId: source.nodeId,
|
|
4264
|
+
sourcePort: source.port,
|
|
4265
|
+
targetId: target.nodeId,
|
|
4266
|
+
targetPort: target.portName
|
|
4267
|
+
});
|
|
4268
|
+
}
|
|
4269
|
+
if (target.kind === "entity") {
|
|
4270
|
+
if (target.id === source.nodeId) return false;
|
|
4271
|
+
if (!ctx.engine.getNode(target.id)) return false;
|
|
4272
|
+
return ctx.engine.validateConnection({
|
|
4273
|
+
sourceId: source.nodeId,
|
|
4274
|
+
sourcePort: source.port,
|
|
4275
|
+
targetId: target.id,
|
|
4276
|
+
targetPort: null
|
|
4277
|
+
});
|
|
4278
|
+
}
|
|
4279
|
+
return false;
|
|
4280
|
+
}
|
|
4281
|
+
function connectEndTargetFor(target, point, ctx) {
|
|
4282
|
+
if (target.kind === "port") return {
|
|
4283
|
+
kind: "node",
|
|
4284
|
+
nodeId: target.nodeId,
|
|
4285
|
+
port: target.portName
|
|
4286
|
+
};
|
|
4287
|
+
if (target.kind === "entity") {
|
|
4288
|
+
const entity = ctx.engine.getEntity(target.id);
|
|
4289
|
+
if (entity?.type === "node") return {
|
|
4290
|
+
kind: "node",
|
|
4291
|
+
nodeId: target.id,
|
|
4292
|
+
port: null
|
|
4293
|
+
};
|
|
4294
|
+
if (entity?.type === "edge") return {
|
|
4295
|
+
kind: "edge",
|
|
4296
|
+
edgeId: target.id
|
|
4297
|
+
};
|
|
4298
|
+
}
|
|
4299
|
+
if (target.kind === "pane") return {
|
|
4300
|
+
kind: "canvas",
|
|
4301
|
+
position: point
|
|
4302
|
+
};
|
|
4303
|
+
return { kind: "none" };
|
|
4304
|
+
}
|
|
4305
|
+
function connectSession(source, options) {
|
|
4306
|
+
const isValid = options.isValid ?? defaultConnectionValidity;
|
|
4307
|
+
return {
|
|
4308
|
+
update(event, ctx) {
|
|
4309
|
+
if (!ctx.context.connection) return { status: "end" };
|
|
4310
|
+
if (event.kind === "pointer.move") {
|
|
4311
|
+
const target = event.target;
|
|
4312
|
+
let targetId = null;
|
|
4313
|
+
let targetPort = null;
|
|
4314
|
+
if (target.kind === "port") {
|
|
4315
|
+
targetId = target.nodeId;
|
|
4316
|
+
targetPort = target.portName;
|
|
4317
|
+
} else if (target.kind === "entity" && ctx.engine.getNode(target.id)) targetId = target.id;
|
|
4318
|
+
return { commands: [trigger("connectionMove", {
|
|
4319
|
+
position: event.point,
|
|
4320
|
+
targetId,
|
|
4321
|
+
targetPort,
|
|
4322
|
+
isValid: isValid({
|
|
4323
|
+
source,
|
|
4324
|
+
target,
|
|
4325
|
+
point: event.point
|
|
4326
|
+
}, ctx)
|
|
4327
|
+
})] };
|
|
4328
|
+
}
|
|
4329
|
+
if (event.kind === "pointer.up") return {
|
|
4330
|
+
commands: [trigger("connectionEnd", { target: connectEndTargetFor(event.target, event.point, ctx) })],
|
|
4331
|
+
status: "end"
|
|
4332
|
+
};
|
|
4333
|
+
if (event.kind === "pointer.cancel") return {
|
|
4334
|
+
commands: [trigger("cancelConnection")],
|
|
4335
|
+
status: "end"
|
|
4336
|
+
};
|
|
4337
|
+
return {};
|
|
4338
|
+
},
|
|
4339
|
+
cancel() {
|
|
4340
|
+
return [trigger("cancelConnection")];
|
|
4341
|
+
}
|
|
4342
|
+
};
|
|
4343
|
+
}
|
|
4344
|
+
function hoverMode() {
|
|
4345
|
+
return {
|
|
4346
|
+
id: "hover",
|
|
4347
|
+
priority: 5,
|
|
4348
|
+
start(event, { context }) {
|
|
4349
|
+
if (event.kind !== "pointer.move") return null;
|
|
4350
|
+
const target = event.target;
|
|
4351
|
+
const id = target.kind === "entity" ? target.id : target.kind === "port" ? target.nodeId : null;
|
|
4352
|
+
const current = context.hoveredEntityIds;
|
|
4353
|
+
if (id) {
|
|
4354
|
+
if (current.size === 1 && current.has(id)) return null;
|
|
4355
|
+
return { commands: [trigger("setHover", { ids: [id] })] };
|
|
4356
|
+
}
|
|
4357
|
+
if (current.size === 0) return null;
|
|
4358
|
+
return { commands: [trigger("clearHover")] };
|
|
4359
|
+
}
|
|
4360
|
+
};
|
|
4361
|
+
}
|
|
4362
|
+
function panePressMode() {
|
|
4363
|
+
return {
|
|
4364
|
+
id: "pane-press",
|
|
4365
|
+
priority: 10,
|
|
4366
|
+
start(event) {
|
|
4367
|
+
if (event.kind !== "pointer.down" || event.button !== 0) return null;
|
|
4368
|
+
if (event.target.kind !== "pane") return null;
|
|
4369
|
+
return { session: panePressSession(event) };
|
|
4370
|
+
}
|
|
4371
|
+
};
|
|
4372
|
+
}
|
|
4373
|
+
function panePressSession(down) {
|
|
4374
|
+
let panning = false;
|
|
4375
|
+
return {
|
|
4376
|
+
update(event, { context }) {
|
|
4377
|
+
if (event.kind === "pointer.move") {
|
|
4378
|
+
if (!panning) {
|
|
4379
|
+
if (distance(down.screenPoint, event.screenPoint) < context.dragThreshold) return {};
|
|
4380
|
+
panning = true;
|
|
4381
|
+
return { commands: [
|
|
4382
|
+
trigger("clearSelection"),
|
|
4383
|
+
trigger("panStart", down.screenPoint),
|
|
4384
|
+
trigger("pan", event.screenPoint)
|
|
4385
|
+
] };
|
|
4386
|
+
}
|
|
4387
|
+
return { commands: [trigger("pan", event.screenPoint)] };
|
|
4388
|
+
}
|
|
4389
|
+
if (event.kind === "pointer.up") return {
|
|
4390
|
+
commands: panning ? [trigger("panEnd")] : [trigger("clearSelection")],
|
|
4391
|
+
status: "end"
|
|
4392
|
+
};
|
|
4393
|
+
if (event.kind === "pointer.cancel") return {
|
|
4394
|
+
commands: panning ? [trigger("panEnd")] : [],
|
|
4395
|
+
status: "end"
|
|
4396
|
+
};
|
|
4397
|
+
return {};
|
|
4398
|
+
},
|
|
4399
|
+
cancel() {
|
|
4400
|
+
return panning ? [trigger("panEnd")] : [];
|
|
4401
|
+
}
|
|
4402
|
+
};
|
|
4403
|
+
}
|
|
4404
|
+
/** Interactive default: full editing gestures. */
|
|
4405
|
+
const defaultInputModes = [
|
|
4406
|
+
keyboardMode(),
|
|
4407
|
+
keyboardNavMode(),
|
|
4408
|
+
wheelMode(),
|
|
4409
|
+
panMode(),
|
|
4410
|
+
connectMode(),
|
|
4411
|
+
entityPressMode(),
|
|
4412
|
+
panePressMode(),
|
|
4413
|
+
hoverMode()
|
|
4414
|
+
];
|
|
4415
|
+
/** Readonly: select and navigate, no mutation gestures. */
|
|
4416
|
+
const readonlyInputModes = [
|
|
4417
|
+
keyboardMode({
|
|
4418
|
+
allowDelete: false,
|
|
4419
|
+
allowUndo: false
|
|
4420
|
+
}),
|
|
4421
|
+
keyboardNavMode({ allowMove: false }),
|
|
4422
|
+
wheelMode(),
|
|
4423
|
+
panMode(),
|
|
4424
|
+
entityPressMode({ drag: false }),
|
|
4425
|
+
panePressMode(),
|
|
4426
|
+
hoverMode()
|
|
4427
|
+
];
|
|
4428
|
+
//#endregion
|
|
4429
|
+
//#region src/input/index.ts
|
|
4430
|
+
/** Typed constructor for trigger commands. */
|
|
4431
|
+
function trigger(name, ...params) {
|
|
4432
|
+
return {
|
|
4433
|
+
type: "trigger",
|
|
4434
|
+
name,
|
|
4435
|
+
params: params[0]
|
|
4436
|
+
};
|
|
4437
|
+
}
|
|
4438
|
+
const preventDefault = { type: "preventDefault" };
|
|
4439
|
+
function createFlowInputRuntime(options) {
|
|
4440
|
+
const { store, engine } = options;
|
|
4441
|
+
const resolveModes = typeof options.modes === "function" ? options.modes : options.modes ? () => options.modes : defaultModesForContext;
|
|
4442
|
+
const input = { spaceHeld: false };
|
|
4443
|
+
let session = null;
|
|
4444
|
+
function modeContext() {
|
|
4445
|
+
return {
|
|
4446
|
+
context: store.getSnapshot().context,
|
|
4447
|
+
engine,
|
|
4448
|
+
input
|
|
4449
|
+
};
|
|
4450
|
+
}
|
|
4451
|
+
function apply(commands, effects) {
|
|
4452
|
+
if (!commands) return;
|
|
4453
|
+
for (const command of commands) if (command.type === "preventDefault") effects.preventDefault = true;
|
|
4454
|
+
else if (command.type === "stopPropagation") effects.stopPropagation = true;
|
|
4455
|
+
else {
|
|
4456
|
+
const fire = store.trigger[command.name];
|
|
4457
|
+
fire?.(command.params);
|
|
4458
|
+
}
|
|
4459
|
+
}
|
|
4460
|
+
function send(event) {
|
|
4461
|
+
const effects = {
|
|
4462
|
+
preventDefault: false,
|
|
4463
|
+
stopPropagation: false,
|
|
4464
|
+
sessionStarted: false,
|
|
4465
|
+
sessionEnded: false,
|
|
4466
|
+
handledBy: null
|
|
4467
|
+
};
|
|
4468
|
+
if (event.kind === "key.down" && event.key === " " && !event.editableTarget) input.spaceHeld = true;
|
|
4469
|
+
else if (event.kind === "key.up" && event.key === " ") input.spaceHeld = false;
|
|
4470
|
+
const isPointer = event.kind === "pointer.down" || event.kind === "pointer.move" || event.kind === "pointer.up" || event.kind === "pointer.cancel";
|
|
4471
|
+
if (session && isPointer) {
|
|
4472
|
+
const pointer = event;
|
|
4473
|
+
if (session.pointerId === null || pointer.pointerId === session.pointerId) {
|
|
4474
|
+
const result = session.handlers.update(event, modeContext());
|
|
4475
|
+
apply(result?.commands, effects);
|
|
4476
|
+
effects.handledBy = session.modeId;
|
|
4477
|
+
if (result?.status === "end") {
|
|
4478
|
+
session = null;
|
|
4479
|
+
effects.sessionEnded = true;
|
|
4480
|
+
}
|
|
4481
|
+
return effects;
|
|
4482
|
+
}
|
|
4483
|
+
if (event.kind === "pointer.down") {
|
|
4484
|
+
effects.handledBy = session.modeId;
|
|
4485
|
+
return effects;
|
|
4486
|
+
}
|
|
4487
|
+
return effects;
|
|
4488
|
+
}
|
|
4489
|
+
const ctx = modeContext();
|
|
4490
|
+
const modes = [...resolveModes(ctx.context)].sort((a, b) => (b.priority ?? 0) - (a.priority ?? 0));
|
|
4491
|
+
for (const mode of modes) {
|
|
4492
|
+
const reaction = mode.start(event, ctx);
|
|
4493
|
+
if (!reaction) continue;
|
|
4494
|
+
apply(reaction.commands, effects);
|
|
4495
|
+
effects.handledBy = mode.id;
|
|
4496
|
+
if (reaction.session) {
|
|
4497
|
+
session = {
|
|
4498
|
+
handlers: reaction.session,
|
|
4499
|
+
modeId: mode.id,
|
|
4500
|
+
pointerId: isPointer ? event.pointerId : null
|
|
4501
|
+
};
|
|
4502
|
+
effects.sessionStarted = true;
|
|
4503
|
+
}
|
|
4504
|
+
break;
|
|
4505
|
+
}
|
|
4506
|
+
return effects;
|
|
4507
|
+
}
|
|
4508
|
+
function cancelSession() {
|
|
4509
|
+
if (!session) return;
|
|
4510
|
+
const effects = {
|
|
4511
|
+
preventDefault: false,
|
|
4512
|
+
stopPropagation: false,
|
|
4513
|
+
sessionStarted: false,
|
|
4514
|
+
sessionEnded: true,
|
|
4515
|
+
handledBy: session.modeId
|
|
4516
|
+
};
|
|
4517
|
+
const commands = session.handlers.cancel?.(modeContext());
|
|
4518
|
+
session = null;
|
|
4519
|
+
apply(commands ?? void 0, effects);
|
|
4520
|
+
}
|
|
4521
|
+
return {
|
|
4522
|
+
send,
|
|
4523
|
+
cancelSession,
|
|
4524
|
+
getActiveSession: () => session?.modeId ?? null
|
|
4525
|
+
};
|
|
4526
|
+
}
|
|
4527
|
+
/** Default mode resolution: follow the store's `context.mode`. */
|
|
4528
|
+
function defaultModesForContext(context) {
|
|
4529
|
+
return context.mode === "readonly" ? readonlyInputModes : defaultInputModes;
|
|
4530
|
+
}
|
|
4531
|
+
/**
|
|
4532
|
+
* Mode resolution driven by a store profile: the active store mode's
|
|
4533
|
+
* `input.modes` wins, then the profile-level `input.modes`, then the
|
|
4534
|
+
* built-in defaults.
|
|
4535
|
+
*/
|
|
4536
|
+
function profileModesForContext(profile) {
|
|
4537
|
+
return (context) => {
|
|
4538
|
+
const mode = profile.modes[context.mode] ? context.mode : profile.defaultMode;
|
|
4539
|
+
return profile.modes[mode]?.input?.modes ?? profile.input?.modes ?? defaultModesForContext(context);
|
|
4540
|
+
};
|
|
4541
|
+
}
|
|
4542
|
+
/**
|
|
4543
|
+
* Per-frame auto-pan step while a drag or connection gesture is near the
|
|
4544
|
+
* container edge. Returns the `calcAutoPan` screen-space delta (negate it for
|
|
4545
|
+
* `panBy`), or null when auto-pan should not run. Adapters call this from a
|
|
4546
|
+
* frame ticker while a gesture session is active and re-send the stationary
|
|
4547
|
+
* pointer afterwards so the dragged entity follows the panned canvas.
|
|
4548
|
+
*/
|
|
4549
|
+
function getAutoPanDelta(context, screenPoint, engine) {
|
|
4550
|
+
if (!context.autoPanOnDrag) return null;
|
|
4551
|
+
if (context.canvasState !== "dragging" && context.canvasState !== "connecting") return null;
|
|
4552
|
+
const size = context.viewportSize;
|
|
4553
|
+
if (size.width === 0 || size.height === 0) return null;
|
|
4554
|
+
const delta = calcAutoPan(screenPoint, size, context.autoPanThreshold, context.autoPanSpeed);
|
|
4555
|
+
if (delta.x === 0 && delta.y === 0) return null;
|
|
4556
|
+
if (engine && context.canvasState === "dragging") {
|
|
4557
|
+
let union = null;
|
|
4558
|
+
for (const selectedId of context.selection) {
|
|
4559
|
+
const node = engine.getNode(selectedId);
|
|
4560
|
+
if (!node || node.draggable === false) continue;
|
|
4561
|
+
const b = engine.resolvedBounds(selectedId);
|
|
4562
|
+
const x0 = b.x * context.viewport.zoom + context.viewport.x;
|
|
4563
|
+
const y0 = b.y * context.viewport.zoom + context.viewport.y;
|
|
4564
|
+
const x1 = x0 + b.width * context.viewport.zoom;
|
|
4565
|
+
const y1 = y0 + b.height * context.viewport.zoom;
|
|
4566
|
+
union = union ? {
|
|
4567
|
+
x0: Math.min(union.x0, x0),
|
|
4568
|
+
y0: Math.min(union.y0, y0),
|
|
4569
|
+
x1: Math.max(union.x1, x1),
|
|
4570
|
+
y1: Math.max(union.y1, y1)
|
|
4571
|
+
} : {
|
|
4572
|
+
x0,
|
|
4573
|
+
y0,
|
|
4574
|
+
x1,
|
|
4575
|
+
y1
|
|
4576
|
+
};
|
|
4577
|
+
}
|
|
4578
|
+
if (union) {
|
|
4579
|
+
const edge = context.autoPanThreshold;
|
|
4580
|
+
if (delta.x < 0 && union.x0 > edge) delta.x = 0;
|
|
4581
|
+
if (delta.x > 0 && union.x1 < size.width - edge) delta.x = 0;
|
|
4582
|
+
if (delta.y < 0 && union.y0 > edge) delta.y = 0;
|
|
4583
|
+
if (delta.y > 0 && union.y1 < size.height - edge) delta.y = 0;
|
|
4584
|
+
if (delta.x === 0 && delta.y === 0) return null;
|
|
4585
|
+
}
|
|
4586
|
+
}
|
|
4587
|
+
return delta;
|
|
4588
|
+
}
|
|
4589
|
+
//#endregion
|
|
4590
|
+
//#region src/entity-changes.ts
|
|
4591
|
+
const adjacencyCache = /* @__PURE__ */ new WeakMap();
|
|
4592
|
+
function adjacency(edges) {
|
|
4593
|
+
let index = adjacencyCache.get(edges);
|
|
4594
|
+
if (!index) {
|
|
4595
|
+
index = /* @__PURE__ */ new Map();
|
|
4596
|
+
for (const edge of edges) {
|
|
4597
|
+
let source = index.get(edge.sourceId);
|
|
4598
|
+
if (!source) index.set(edge.sourceId, source = []);
|
|
4599
|
+
source.push(edge.id);
|
|
4600
|
+
if (edge.targetId !== edge.sourceId) {
|
|
4601
|
+
let target = index.get(edge.targetId);
|
|
4602
|
+
if (!target) index.set(edge.targetId, target = []);
|
|
4603
|
+
target.push(edge.id);
|
|
4604
|
+
}
|
|
4605
|
+
}
|
|
4606
|
+
adjacencyCache.set(edges, index);
|
|
4607
|
+
}
|
|
4608
|
+
return index;
|
|
4609
|
+
}
|
|
4610
|
+
function computeDirty(prev, next) {
|
|
4611
|
+
if (next.canvasState !== prev.canvasState) return "all";
|
|
4612
|
+
if (next.mode !== prev.mode) return "all";
|
|
4613
|
+
if (next.enableSnapLines && next.canvasState === "dragging") return "all";
|
|
4614
|
+
const dirty = /* @__PURE__ */ new Set();
|
|
4615
|
+
const markIncidentEdges = (nodeId, edges) => {
|
|
4616
|
+
const incident = adjacency(edges).get(nodeId);
|
|
4617
|
+
if (incident) for (const edgeId of incident) dirty.add(edgeId);
|
|
4618
|
+
};
|
|
4619
|
+
const markAncestors = (node) => {
|
|
4620
|
+
const nextIndex = indexNodes(next.graph.nodes);
|
|
4621
|
+
const seen = /* @__PURE__ */ new Set();
|
|
4622
|
+
let parentId = node.parentId ?? null;
|
|
4623
|
+
while (parentId && !seen.has(parentId)) {
|
|
4624
|
+
seen.add(parentId);
|
|
4625
|
+
dirty.add(parentId);
|
|
4626
|
+
markIncidentEdges(parentId, next.graph.edges);
|
|
4627
|
+
parentId = nextIndex.get(parentId)?.parentId ?? null;
|
|
4628
|
+
}
|
|
4629
|
+
};
|
|
4630
|
+
if (next.graph.nodes !== prev.graph.nodes) {
|
|
4631
|
+
const prevIndex = indexNodes(prev.graph.nodes);
|
|
4632
|
+
const nextIndex = indexNodes(next.graph.nodes);
|
|
4633
|
+
for (const node of next.graph.nodes) {
|
|
4634
|
+
const before = prevIndex.get(node.id);
|
|
4635
|
+
if (before !== node) {
|
|
4636
|
+
dirty.add(node.id);
|
|
4637
|
+
markIncidentEdges(node.id, next.graph.edges);
|
|
4638
|
+
markAncestors(node);
|
|
4639
|
+
if (before && (before.parentId ?? null) !== (node.parentId ?? null)) markAncestors(before);
|
|
4640
|
+
}
|
|
4641
|
+
}
|
|
4642
|
+
for (const node of prev.graph.nodes) if (!nextIndex.has(node.id)) {
|
|
4643
|
+
dirty.add(node.id);
|
|
4644
|
+
markIncidentEdges(node.id, prev.graph.edges);
|
|
4645
|
+
markAncestors(node);
|
|
4646
|
+
}
|
|
4647
|
+
}
|
|
4648
|
+
if (next.graph.edges !== prev.graph.edges) {
|
|
4649
|
+
const prevIndex = indexEdges(prev.graph.edges);
|
|
4650
|
+
const nextIndex = indexEdges(next.graph.edges);
|
|
4651
|
+
const nodeIndex = indexNodes(next.graph.nodes);
|
|
4652
|
+
const markEndpointAncestors = (edge) => {
|
|
4653
|
+
const source = nodeIndex.get(edge.sourceId);
|
|
4654
|
+
const target = nodeIndex.get(edge.targetId);
|
|
4655
|
+
markAncestors(source ?? {});
|
|
4656
|
+
markAncestors(target ?? {});
|
|
4657
|
+
if (source?.group) dirty.add(source.id);
|
|
4658
|
+
if (target?.group) dirty.add(target.id);
|
|
4659
|
+
};
|
|
4660
|
+
for (const edge of next.graph.edges) if (prevIndex.get(edge.id) !== edge) {
|
|
4661
|
+
dirty.add(edge.id);
|
|
4662
|
+
markEndpointAncestors(edge);
|
|
4663
|
+
}
|
|
4664
|
+
for (const edge of prev.graph.edges) if (!nextIndex.has(edge.id)) {
|
|
4665
|
+
dirty.add(edge.id);
|
|
4666
|
+
markEndpointAncestors(edge);
|
|
4667
|
+
}
|
|
4668
|
+
}
|
|
4669
|
+
if (next.selection !== prev.selection) {
|
|
4670
|
+
for (const id of next.selection) if (!prev.selection.has(id)) dirty.add(id);
|
|
4671
|
+
for (const id of prev.selection) if (!next.selection.has(id)) dirty.add(id);
|
|
4672
|
+
}
|
|
4673
|
+
if (next.hoveredEntityIds !== prev.hoveredEntityIds) {
|
|
4674
|
+
for (const id of next.hoveredEntityIds) if (!prev.hoveredEntityIds.has(id)) dirty.add(id);
|
|
4675
|
+
for (const id of prev.hoveredEntityIds) if (!next.hoveredEntityIds.has(id)) dirty.add(id);
|
|
4676
|
+
}
|
|
4677
|
+
if (next.highlights !== prev.highlights) {
|
|
4678
|
+
const kinds = new Set([...prev.highlights.keys(), ...next.highlights.keys()]);
|
|
4679
|
+
for (const kind of kinds) {
|
|
4680
|
+
const before = prev.highlights.get(kind);
|
|
4681
|
+
const after = next.highlights.get(kind);
|
|
4682
|
+
if (before === after) continue;
|
|
4683
|
+
if (before) for (const id of before.keys()) dirty.add(id);
|
|
4684
|
+
if (after) for (const id of after.keys()) dirty.add(id);
|
|
4685
|
+
}
|
|
4686
|
+
}
|
|
4687
|
+
const idPairs = [
|
|
4688
|
+
[prev.draggedEntityId, next.draggedEntityId],
|
|
4689
|
+
[prev.focusedEntityId, next.focusedEntityId],
|
|
4690
|
+
[prev.resizingEntityId, next.resizingEntityId]
|
|
4691
|
+
];
|
|
4692
|
+
for (const [before, after] of idPairs) if (before !== after) {
|
|
4693
|
+
if (before) dirty.add(before);
|
|
4694
|
+
if (after) dirty.add(after);
|
|
4695
|
+
}
|
|
4696
|
+
if (next.resizeCorners !== prev.resizeCorners && next.resizingEntityId) {
|
|
4697
|
+
dirty.add(next.resizingEntityId);
|
|
4698
|
+
markIncidentEdges(next.resizingEntityId, next.graph.edges);
|
|
4699
|
+
}
|
|
4700
|
+
return dirty;
|
|
4701
|
+
}
|
|
4702
|
+
function createEntityChangeNotifier(store) {
|
|
4703
|
+
const listeners = /* @__PURE__ */ new Map();
|
|
4704
|
+
let prev = store.getSnapshot().context;
|
|
4705
|
+
const subscription = store.subscribe((snapshot) => {
|
|
4706
|
+
const next = snapshot.context;
|
|
4707
|
+
if (next === prev) return;
|
|
4708
|
+
const dirty = computeDirty(prev, next);
|
|
4709
|
+
prev = next;
|
|
4710
|
+
if (dirty === "all") {
|
|
4711
|
+
for (const set of listeners.values()) for (const listener of [...set]) listener();
|
|
4712
|
+
return;
|
|
4713
|
+
}
|
|
4714
|
+
for (const id of dirty) {
|
|
4715
|
+
const set = listeners.get(id);
|
|
4716
|
+
if (set) for (const listener of [...set]) listener();
|
|
4717
|
+
}
|
|
4718
|
+
});
|
|
4719
|
+
return {
|
|
4720
|
+
subscribe(id, listener) {
|
|
4721
|
+
let set = listeners.get(id);
|
|
4722
|
+
if (!set) {
|
|
4723
|
+
set = /* @__PURE__ */ new Set();
|
|
4724
|
+
listeners.set(id, set);
|
|
4725
|
+
}
|
|
4726
|
+
set.add(listener);
|
|
4727
|
+
return () => {
|
|
4728
|
+
set.delete(listener);
|
|
4729
|
+
if (set.size === 0) listeners.delete(id);
|
|
4730
|
+
};
|
|
4731
|
+
},
|
|
4732
|
+
destroy() {
|
|
4733
|
+
subscription.unsubscribe();
|
|
4734
|
+
listeners.clear();
|
|
4735
|
+
}
|
|
4736
|
+
};
|
|
4737
|
+
}
|
|
2533
4738
|
//#endregion
|
|
2534
|
-
export { bestDirection, bezierRouter, boxToRect, calcAutoPan, canvasToScreen, clampZoom, constrainViewport, createEdge, createFlow, createFlowEngine, createFlowStore, createGraph, createNode, createViewport, defaultConfig, getAnchorTransform, getGraphBounds, getSelectionBounds, getShape, getShapeOrDefault, getSnapLines, getVisibleBounds, isTrackpadPinch, normalizeWheelPan, panBy, pathToSVG, readonlyConfig, rectContains, rectsIntersect, registerShape, resolveBehavior, resolvedBounds, resolvedPosition, screenToCanvas, selectMode, snapPointToGrid, snapToGrid, snappedBounds, stepRouter, straightRouter, toSVGTransform, toViewBox, viewportForBounds, wheelInteraction, wheelZoomDelta, zoomTo };
|
|
4739
|
+
export { CONNECTION_PREVIEW_EDGE_ID, CONNECTION_PREVIEW_NODE_ID, bestDirection, bezierRouter, boxToRect, calcAutoPan, canvasToScreen, clampZoom, connectMode, constrainViewport, createConnectionPreviewGraph, createEdge, createEntityChangeNotifier, createFlow, createFlowEngine, createFlowInputRuntime, createFlowStore, createGraph, createNode, createPort, createViewport, defaultConfig, defaultFlowProfile, defaultInputModes, defaultModesForContext, defineFlowProfile, deriveGroupGeometry, entityPressMode, fixedGroupGeometry, getAnchorTransform, getAutoPanDelta, getEdgeOwner, getGraphBounds, getMinimapProjection, getSelectionBounds, getShape, getShapeOrDefault, getSnapLines, getToolbarPlacement, getVisibleBounds, hoverMode, indexChildren, indexEdges, indexNodes, isDerivedGroup, isGroup, isTrackpadPinch, keyboardMode, keyboardNavMode, nodeDepths, normalizeWheelPan, panBy, panMode, panePressMode, pathToSVG, preventDefault, profileModesForContext, readonlyConfig, readonlyInputModes, rectContains, rectsIntersect, registerShape, resolveBehavior, resolveGroupPadding, resolvedBounds, resolvedPosition, screenToCanvas, selectMode, snapPointToGrid, snapToGrid, snappedBounds, stepRouter, straightRouter, toSVGTransform, toViewBox, trigger, unionRect, viewportCenteredOn, viewportForBounds, wheelInteraction, wheelMode, wheelZoomDelta, zoomTo };
|