@statelyai/flow 0.13.4 → 0.15.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 +5 -1
- package/dist/esm/index.d.ts +13 -2
- package/dist/esm/index.d.ts.map +1 -0
- package/dist/esm/index.js +39 -14
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/layout/dagre.d.ts +2 -1
- package/dist/esm/layout/dagre.d.ts.map +1 -0
- package/dist/esm/layout/dagre.js +2 -0
- package/dist/esm/layout/dagre.js.map +1 -0
- package/dist/esm/layout/elk.d.ts +2 -1
- package/dist/esm/layout/elk.d.ts.map +1 -0
- package/dist/esm/layout/elk.js +2 -0
- package/dist/esm/layout/elk.js.map +1 -0
- package/dist/esm/layout/stately.d.ts +2 -1
- package/dist/esm/layout/stately.d.ts.map +1 -0
- package/dist/esm/layout/stately.js +2 -0
- package/dist/esm/layout/stately.js.map +1 -0
- package/dist/esm/shared.d.ts +2 -1
- package/dist/esm/shared.d.ts.map +1 -0
- package/dist/esm/shared.js +2 -0
- package/dist/esm/shared.js.map +1 -0
- package/package.json +4 -3
- package/src/__fixtures__/nested.ts +106 -0
- package/src/appearances.test.ts +428 -0
- package/src/appearances.ts +144 -0
- package/src/authoritative-route.ts +14 -0
- package/src/changeSet.test.ts +132 -0
- package/src/changeSet.ts +337 -0
- package/src/config.ts +41 -0
- package/src/configure.test.ts +212 -0
- package/src/connection-lifetime.ts +1 -0
- package/src/connectionPreview.test.ts +102 -0
- package/src/connectionPreview.ts +82 -0
- package/src/content.ts +203 -0
- package/src/createFlow.fit-frame.test.ts +256 -0
- package/src/createFlow.modern.test.ts +743 -0
- package/src/createFlow.test.ts +632 -0
- package/src/createFlow.ts +3463 -0
- package/src/createFlow.view.test.ts +212 -0
- package/src/createFlow.viewport-controls.test.ts +221 -0
- package/src/edge-geometry.ts +318 -0
- package/src/engine.cache.test.ts +728 -0
- package/src/engine.hierarchy.test.ts +1023 -0
- package/src/engine.test.ts +1965 -0
- package/src/engine.ts +3385 -0
- package/src/entity-changes.test.ts +309 -0
- package/src/entity-changes.ts +395 -0
- package/src/examples/flow-based-math.test.ts +25 -0
- package/src/examples/flow-based-math.ts +210 -0
- package/src/external-edit.test.ts +337 -0
- package/src/external-edit.ts +358 -0
- package/src/factories.test.ts +104 -0
- package/src/factories.ts +70 -0
- package/src/geometry.ts +13 -0
- package/src/graph-index.test.ts +56 -0
- package/src/graph-index.ts +194 -0
- package/src/hierarchy.test.ts +175 -0
- package/src/hierarchy.ts +330 -0
- package/src/highlights.test.ts +130 -0
- package/src/index.ts +469 -0
- package/src/input/index.ts +615 -0
- package/src/input/input.test.ts +2577 -0
- package/src/input/keymap.ts +111 -0
- package/src/input/modes.ts +1715 -0
- package/src/input/reparent.test.ts +214 -0
- package/src/layout/dagre.ts +138 -0
- package/src/layout/elk.ts +1382 -0
- package/src/layout/index.ts +12 -0
- package/src/layout/layout.test.ts +1248 -0
- package/src/layout/shared.ts +260 -0
- package/src/layout/stately-globals.d.ts +9 -0
- package/src/layout/stately.test.ts +246 -0
- package/src/layout/stately.ts +528 -0
- package/src/layout-animation.test.ts +223 -0
- package/src/layout-contract.test.ts +414 -0
- package/src/marker.test.ts +23 -0
- package/src/marker.ts +23 -0
- package/src/measurement-anchor.test.ts +232 -0
- package/src/minimap.test.ts +96 -0
- package/src/minimap.ts +137 -0
- package/src/nested-fixture.test.ts +178 -0
- package/src/port-measurement.test.ts +27 -0
- package/src/port-measurements.test.ts +187 -0
- package/src/profile.ts +124 -0
- package/src/projections.test.ts +103 -0
- package/src/projections.ts +295 -0
- package/src/render-invalidation.test.ts +62 -0
- package/src/render-invalidation.ts +55 -0
- package/src/renderer-geometry.test.ts +356 -0
- package/src/renderer-transactions.test.ts +234 -0
- package/src/routing/avoid.ts +417 -0
- package/src/routing/index.test.ts +361 -0
- package/src/routing/index.ts +366 -0
- package/src/routing/readTracking.test.ts +103 -0
- package/src/routing/readTracking.ts +233 -0
- package/src/selection-commands.test.ts +355 -0
- package/src/serialize.test.ts +167 -0
- package/src/serialize.ts +254 -0
- package/src/shapes/index.test.ts +45 -0
- package/src/shapes/index.ts +142 -0
- package/src/snap.test.ts +123 -0
- package/src/snap.ts +395 -0
- package/src/spatial.test.ts +676 -0
- package/src/spatial.ts +457 -0
- package/src/store/index.ts +4121 -0
- package/src/store.test.ts +2903 -0
- package/src/toolbar.test.ts +31 -0
- package/src/toolbar.ts +62 -0
- package/src/types.test.ts +15 -0
- package/src/types.ts +487 -0
- package/src/validation.ts +558 -0
- package/src/view.ts +172 -0
- package/src/viewport/animate.ts +285 -0
- package/src/viewport/index.ts +296 -0
- package/src/viewport.test.ts +569 -0
|
@@ -0,0 +1,296 @@
|
|
|
1
|
+
import type { Point, Rect, Size, Viewport, FitViewOptions } from '../types';
|
|
2
|
+
export { animateViewport } from './animate';
|
|
3
|
+
export type {
|
|
4
|
+
AnimateViewportOptions,
|
|
5
|
+
ViewportAnimationCancelFrame,
|
|
6
|
+
ViewportAnimationControls,
|
|
7
|
+
ViewportAnimationFrame,
|
|
8
|
+
} from './animate';
|
|
9
|
+
|
|
10
|
+
export type FitViewExclusionSide = 'top' | 'right' | 'bottom' | 'left' | 'nearest';
|
|
11
|
+
|
|
12
|
+
export type FitViewExclusion = {
|
|
13
|
+
/** Read the current exclusion in viewport-local screen pixels. */
|
|
14
|
+
getRect: () => Rect | null;
|
|
15
|
+
/** Edge whose band this overlay occupies. Defaults to the nearest edge. */
|
|
16
|
+
side?: FitViewExclusionSide;
|
|
17
|
+
/** Additional screen-pixel clearance beyond the measured overlay. */
|
|
18
|
+
gap?: number;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
export type ResolvedFitViewExclusion = Omit<FitViewExclusion, 'getRect'> & {
|
|
22
|
+
rect: Rect;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
// Viewport utilities
|
|
27
|
+
//
|
|
28
|
+
// First-class Viewport type with SVG-friendly helpers.
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
/** Create a default viewport (origin, no zoom) */
|
|
32
|
+
export function createViewport(
|
|
33
|
+
x = 0,
|
|
34
|
+
y = 0,
|
|
35
|
+
zoom = 1,
|
|
36
|
+
): Viewport {
|
|
37
|
+
return { x, y, zoom };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
// SVG helpers
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Convert viewport to SVG transform string.
|
|
46
|
+
* Use on the root <g> wrapping all entities.
|
|
47
|
+
*
|
|
48
|
+
* @example
|
|
49
|
+
* <g transform={toSVGTransform(viewport)}>...</g>
|
|
50
|
+
*/
|
|
51
|
+
export function toSVGTransform(viewport: Viewport): string {
|
|
52
|
+
return `translate(${viewport.x}, ${viewport.y}) scale(${viewport.zoom})`;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Convert viewport + container size to an SVG viewBox string.
|
|
57
|
+
*
|
|
58
|
+
* @example
|
|
59
|
+
* <svg viewBox={toViewBox(viewport, { width: 800, height: 600 })}>...</svg>
|
|
60
|
+
*/
|
|
61
|
+
export function toViewBox(viewport: Viewport, size: Size): string {
|
|
62
|
+
const x = -viewport.x / viewport.zoom;
|
|
63
|
+
const y = -viewport.y / viewport.zoom;
|
|
64
|
+
const w = size.width / viewport.zoom;
|
|
65
|
+
const h = size.height / viewport.zoom;
|
|
66
|
+
return `${x} ${y} ${w} ${h}`;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
// Coordinate conversion
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
/** Convert a screen-space point to canvas-space */
|
|
74
|
+
export function screenToCanvas(point: Point, viewport: Viewport): Point {
|
|
75
|
+
return {
|
|
76
|
+
x: (point.x - viewport.x) / viewport.zoom,
|
|
77
|
+
y: (point.y - viewport.y) / viewport.zoom,
|
|
78
|
+
};
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
/** Convert a canvas-space point to screen-space */
|
|
82
|
+
export function canvasToScreen(point: Point, viewport: Viewport): Point {
|
|
83
|
+
return {
|
|
84
|
+
x: point.x * viewport.zoom + viewport.x,
|
|
85
|
+
y: point.y * viewport.zoom + viewport.y,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
// Bounds
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
/** Get the canvas-space bounding rect of what's visible on screen */
|
|
94
|
+
export function getVisibleBounds(viewport: Viewport, size: Size): Rect {
|
|
95
|
+
return {
|
|
96
|
+
x: -viewport.x / viewport.zoom,
|
|
97
|
+
y: -viewport.y / viewport.zoom,
|
|
98
|
+
width: size.width / viewport.zoom,
|
|
99
|
+
height: size.height / viewport.zoom,
|
|
100
|
+
};
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
/**
|
|
104
|
+
* Compute a viewport that fits the given bounds into the given screen size.
|
|
105
|
+
*/
|
|
106
|
+
export function viewportForBounds(
|
|
107
|
+
bounds: Rect,
|
|
108
|
+
size: Size,
|
|
109
|
+
options: FitViewOptions = {},
|
|
110
|
+
): Viewport {
|
|
111
|
+
const { padding = 0.1, minZoom = 0.1, maxZoom = 2 } = options;
|
|
112
|
+
const frame = options.frame
|
|
113
|
+
? validateFitFrame(options.frame)
|
|
114
|
+
: { x: 0, y: 0, width: size.width, height: size.height };
|
|
115
|
+
|
|
116
|
+
const paddedWidth = bounds.width * (1 + padding);
|
|
117
|
+
const paddedHeight = bounds.height * (1 + padding);
|
|
118
|
+
|
|
119
|
+
if (paddedWidth === 0 || paddedHeight === 0) {
|
|
120
|
+
return { x: 0, y: 0, zoom: 1 };
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const zoomX = frame.width / paddedWidth;
|
|
124
|
+
const zoomY = frame.height / paddedHeight;
|
|
125
|
+
const zoom = Math.min(Math.max(Math.min(zoomX, zoomY), minZoom), maxZoom);
|
|
126
|
+
|
|
127
|
+
const centerX = bounds.x + bounds.width / 2;
|
|
128
|
+
const centerY = bounds.y + bounds.height / 2;
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
x: frame.x + frame.width / 2 - centerX * zoom,
|
|
132
|
+
y: frame.y + frame.height / 2 - centerY * zoom,
|
|
133
|
+
zoom,
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
// Viewport math helpers
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
/** Pan viewport by a delta */
|
|
142
|
+
export function panBy(
|
|
143
|
+
viewport: Viewport,
|
|
144
|
+
delta: Point,
|
|
145
|
+
): Viewport {
|
|
146
|
+
return {
|
|
147
|
+
x: viewport.x + delta.x,
|
|
148
|
+
y: viewport.y + delta.y,
|
|
149
|
+
zoom: viewport.zoom,
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
/** Zoom to a specific level, keeping a screen-space point fixed */
|
|
154
|
+
export function zoomTo(
|
|
155
|
+
viewport: Viewport,
|
|
156
|
+
zoom: number,
|
|
157
|
+
center: Point,
|
|
158
|
+
): Viewport {
|
|
159
|
+
const clampedZoom = zoom;
|
|
160
|
+
const ratio = clampedZoom / viewport.zoom;
|
|
161
|
+
return {
|
|
162
|
+
x: center.x - (center.x - viewport.x) * ratio,
|
|
163
|
+
y: center.y - (center.y - viewport.y) * ratio,
|
|
164
|
+
zoom: clampedZoom,
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
/** Clamp zoom to min/max bounds */
|
|
169
|
+
export function clampZoom(
|
|
170
|
+
zoom: number,
|
|
171
|
+
minZoom: number,
|
|
172
|
+
maxZoom: number,
|
|
173
|
+
): number {
|
|
174
|
+
return Math.min(Math.max(zoom, minZoom), maxZoom);
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** Viewport that places the given canvas point at the center of the screen. */
|
|
178
|
+
export function viewportCenteredOn(
|
|
179
|
+
center: Point,
|
|
180
|
+
size: Size,
|
|
181
|
+
zoom: number,
|
|
182
|
+
frame?: Rect,
|
|
183
|
+
): Viewport {
|
|
184
|
+
const target = frame
|
|
185
|
+
? validateFitFrame(frame)
|
|
186
|
+
: { x: 0, y: 0, width: size.width, height: size.height };
|
|
187
|
+
return {
|
|
188
|
+
x: target.x + target.width / 2 - center.x * zoom,
|
|
189
|
+
y: target.y + target.height / 2 - center.y * zoom,
|
|
190
|
+
zoom,
|
|
191
|
+
};
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/** Validate and clone a positive finite screen-space fit frame. */
|
|
195
|
+
export function validateFitFrame(frame: Rect): Rect {
|
|
196
|
+
if (
|
|
197
|
+
!Number.isFinite(frame.x) ||
|
|
198
|
+
!Number.isFinite(frame.y) ||
|
|
199
|
+
!Number.isFinite(frame.width) ||
|
|
200
|
+
!Number.isFinite(frame.height) ||
|
|
201
|
+
frame.width <= 0 ||
|
|
202
|
+
frame.height <= 0
|
|
203
|
+
) {
|
|
204
|
+
throw new RangeError(
|
|
205
|
+
'Fit frame must have finite x/y and positive finite width/height.',
|
|
206
|
+
);
|
|
207
|
+
}
|
|
208
|
+
return {
|
|
209
|
+
x: frame.x,
|
|
210
|
+
y: frame.y,
|
|
211
|
+
width: frame.width,
|
|
212
|
+
height: frame.height,
|
|
213
|
+
};
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* Reduce a fit frame by measured floating overlays. Each overlay reserves the
|
|
218
|
+
* band between itself and its assigned (or nearest) frame edge.
|
|
219
|
+
*/
|
|
220
|
+
export function fitFrameAroundExclusions(
|
|
221
|
+
frame: Rect,
|
|
222
|
+
exclusions: Iterable<ResolvedFitViewExclusion>,
|
|
223
|
+
): Rect {
|
|
224
|
+
const base = validateFitFrame(frame);
|
|
225
|
+
const right = base.x + base.width;
|
|
226
|
+
const bottom = base.y + base.height;
|
|
227
|
+
let leftInset = 0;
|
|
228
|
+
let rightInset = 0;
|
|
229
|
+
let topInset = 0;
|
|
230
|
+
let bottomInset = 0;
|
|
231
|
+
|
|
232
|
+
for (const exclusion of exclusions) {
|
|
233
|
+
const rect = exclusion.rect;
|
|
234
|
+
if (
|
|
235
|
+
!Number.isFinite(rect.x) ||
|
|
236
|
+
!Number.isFinite(rect.y) ||
|
|
237
|
+
!Number.isFinite(rect.width) ||
|
|
238
|
+
!Number.isFinite(rect.height) ||
|
|
239
|
+
rect.width <= 0 ||
|
|
240
|
+
rect.height <= 0
|
|
241
|
+
) {
|
|
242
|
+
continue;
|
|
243
|
+
}
|
|
244
|
+
const clippedLeft = Math.max(base.x, rect.x);
|
|
245
|
+
const clippedTop = Math.max(base.y, rect.y);
|
|
246
|
+
const clippedRight = Math.min(right, rect.x + rect.width);
|
|
247
|
+
const clippedBottom = Math.min(bottom, rect.y + rect.height);
|
|
248
|
+
if (clippedRight <= clippedLeft || clippedBottom <= clippedTop) continue;
|
|
249
|
+
|
|
250
|
+
const gap = Number.isFinite(exclusion.gap)
|
|
251
|
+
? Math.max(0, exclusion.gap ?? 0)
|
|
252
|
+
: 0;
|
|
253
|
+
const side =
|
|
254
|
+
exclusion.side && exclusion.side !== 'nearest'
|
|
255
|
+
? exclusion.side
|
|
256
|
+
: nearestFrameEdge(base, {
|
|
257
|
+
x: clippedLeft,
|
|
258
|
+
y: clippedTop,
|
|
259
|
+
width: clippedRight - clippedLeft,
|
|
260
|
+
height: clippedBottom - clippedTop,
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
if (side === 'left') {
|
|
264
|
+
leftInset = Math.max(leftInset, clippedRight + gap - base.x);
|
|
265
|
+
} else if (side === 'right') {
|
|
266
|
+
rightInset = Math.max(rightInset, right - clippedLeft + gap);
|
|
267
|
+
} else if (side === 'top') {
|
|
268
|
+
topInset = Math.max(topInset, clippedBottom + gap - base.y);
|
|
269
|
+
} else {
|
|
270
|
+
bottomInset = Math.max(bottomInset, bottom - clippedTop + gap);
|
|
271
|
+
}
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const width = base.width - leftInset - rightInset;
|
|
275
|
+
const height = base.height - topInset - bottomInset;
|
|
276
|
+
return width > 0 && height > 0
|
|
277
|
+
? {
|
|
278
|
+
x: base.x + leftInset,
|
|
279
|
+
y: base.y + topInset,
|
|
280
|
+
width,
|
|
281
|
+
height,
|
|
282
|
+
}
|
|
283
|
+
: base;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
function nearestFrameEdge(frame: Rect, rect: Rect): Exclude<FitViewExclusionSide, 'nearest'> {
|
|
287
|
+
const distances = [
|
|
288
|
+
['left', Math.abs(rect.x - frame.x)],
|
|
289
|
+
['right', Math.abs(frame.x + frame.width - (rect.x + rect.width))],
|
|
290
|
+
['top', Math.abs(rect.y - frame.y)],
|
|
291
|
+
['bottom', Math.abs(frame.y + frame.height - (rect.y + rect.height))],
|
|
292
|
+
] as const;
|
|
293
|
+
return distances.reduce((best, candidate) =>
|
|
294
|
+
candidate[1] < best[1] ? candidate : best,
|
|
295
|
+
)[0];
|
|
296
|
+
}
|