@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
package/src/view.ts
ADDED
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import { getEdgeOwner } from './hierarchy';
|
|
2
|
+
import type { FlowGraph, FlowNode } from './types';
|
|
3
|
+
|
|
4
|
+
export type FlowViewScope = Readonly<{
|
|
5
|
+
/** Null addresses the graph's virtual root. */
|
|
6
|
+
parentId: string | null;
|
|
7
|
+
/** Descendant levels to expose. Zero is empty; null is unlimited. */
|
|
8
|
+
depth: number | null;
|
|
9
|
+
}>;
|
|
10
|
+
|
|
11
|
+
export type FlowViewSelectionPolicy = 'prune' | 'clear';
|
|
12
|
+
|
|
13
|
+
export type FlowViewEndpoint = Readonly<{
|
|
14
|
+
originalId: string;
|
|
15
|
+
viewId: string;
|
|
16
|
+
boundary: boolean;
|
|
17
|
+
}>;
|
|
18
|
+
|
|
19
|
+
export type FlowViewBoundaryEndpoints = Readonly<{
|
|
20
|
+
edgeId: string;
|
|
21
|
+
source: FlowViewEndpoint;
|
|
22
|
+
target: FlowViewEndpoint;
|
|
23
|
+
}>;
|
|
24
|
+
|
|
25
|
+
export type FlowViewEntities<
|
|
26
|
+
TNodeData = any,
|
|
27
|
+
TEdgeData = any,
|
|
28
|
+
TLabelData = any,
|
|
29
|
+
> = Readonly<{
|
|
30
|
+
nodes: readonly FlowGraph<TNodeData, TEdgeData, TLabelData>['nodes'][number][];
|
|
31
|
+
edges: readonly FlowGraph<TNodeData, TEdgeData, TLabelData>['edges'][number][];
|
|
32
|
+
boundaryEndpoints: readonly FlowViewBoundaryEndpoints[];
|
|
33
|
+
}>;
|
|
34
|
+
|
|
35
|
+
type DeriveFlowViewOptions<TNodeData, TEdgeData, TLabelData> = {
|
|
36
|
+
nodes?: readonly FlowGraph<TNodeData, TEdgeData, TLabelData>['nodes'][number][];
|
|
37
|
+
edges?: readonly FlowGraph<TNodeData, TEdgeData, TLabelData>['edges'][number][];
|
|
38
|
+
edgeOwner?: (
|
|
39
|
+
edge: FlowGraph<TNodeData, TEdgeData, TLabelData>['edges'][number],
|
|
40
|
+
) => string | null;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
function relativeDepth(
|
|
44
|
+
nodeId: string,
|
|
45
|
+
parentId: string | null,
|
|
46
|
+
nodes: ReadonlyMap<string, FlowNode>,
|
|
47
|
+
): number | null {
|
|
48
|
+
let current = nodes.get(nodeId);
|
|
49
|
+
let depth = 0;
|
|
50
|
+
const seen = new Set<string>();
|
|
51
|
+
while (current && !seen.has(current.id)) {
|
|
52
|
+
if (current.id === parentId) return depth;
|
|
53
|
+
seen.add(current.id);
|
|
54
|
+
depth += 1;
|
|
55
|
+
if (current.parentId == null) return parentId === null ? depth : null;
|
|
56
|
+
current = nodes.get(current.parentId);
|
|
57
|
+
}
|
|
58
|
+
return null;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function endpointAtDepth(
|
|
62
|
+
nodeId: string,
|
|
63
|
+
scope: FlowViewScope,
|
|
64
|
+
nodes: ReadonlyMap<string, FlowNode>,
|
|
65
|
+
): string | null {
|
|
66
|
+
const initialDepth = relativeDepth(nodeId, scope.parentId, nodes);
|
|
67
|
+
if (initialDepth === null || initialDepth === 0 || scope.depth === 0) {
|
|
68
|
+
return null;
|
|
69
|
+
}
|
|
70
|
+
if (scope.depth === null || initialDepth <= scope.depth) return nodeId;
|
|
71
|
+
|
|
72
|
+
let current = nodes.get(nodeId);
|
|
73
|
+
let depth = initialDepth;
|
|
74
|
+
while (current && depth > scope.depth) {
|
|
75
|
+
current = current.parentId ? nodes.get(current.parentId) : undefined;
|
|
76
|
+
depth -= 1;
|
|
77
|
+
}
|
|
78
|
+
return current?.id ?? null;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
function ownerIsExposed(
|
|
82
|
+
ownerId: string | null,
|
|
83
|
+
scope: FlowViewScope,
|
|
84
|
+
nodes: ReadonlyMap<string, FlowNode>,
|
|
85
|
+
): boolean {
|
|
86
|
+
if (ownerId === scope.parentId) return true;
|
|
87
|
+
if (ownerId === null) return false;
|
|
88
|
+
const depth = relativeDepth(ownerId, scope.parentId, nodes);
|
|
89
|
+
if (depth === null || depth === 0) return false;
|
|
90
|
+
return scope.depth === null || depth < scope.depth;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function frozenEndpoint(
|
|
94
|
+
originalId: string,
|
|
95
|
+
viewId: string,
|
|
96
|
+
): FlowViewEndpoint {
|
|
97
|
+
return Object.freeze({
|
|
98
|
+
originalId,
|
|
99
|
+
viewId,
|
|
100
|
+
boundary: originalId !== viewId,
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Derive a structural view without mutating the canonical graph. */
|
|
105
|
+
export function deriveFlowView<
|
|
106
|
+
TNodeData = any,
|
|
107
|
+
TEdgeData = any,
|
|
108
|
+
TLabelData = any,
|
|
109
|
+
>(
|
|
110
|
+
graph: FlowGraph<TNodeData, TEdgeData, TLabelData>,
|
|
111
|
+
scope: FlowViewScope,
|
|
112
|
+
options: DeriveFlowViewOptions<TNodeData, TEdgeData, TLabelData> = {},
|
|
113
|
+
): FlowViewEntities<TNodeData, TEdgeData, TLabelData> {
|
|
114
|
+
const allNodes = new Map(
|
|
115
|
+
graph.nodes.map((node) => [node.id, node as FlowNode]),
|
|
116
|
+
);
|
|
117
|
+
const candidateNodes = options.nodes ?? graph.nodes;
|
|
118
|
+
const candidateNodeIds = new Set(candidateNodes.map((node) => node.id));
|
|
119
|
+
const nodes = candidateNodes.filter((node) => {
|
|
120
|
+
const depth = relativeDepth(node.id, scope.parentId, allNodes);
|
|
121
|
+
return (
|
|
122
|
+
depth !== null &&
|
|
123
|
+
depth > 0 &&
|
|
124
|
+
candidateNodeIds.has(node.id) &&
|
|
125
|
+
(scope.depth === null || depth <= scope.depth)
|
|
126
|
+
);
|
|
127
|
+
});
|
|
128
|
+
const viewNodeIds = new Set(nodes.map((node) => node.id));
|
|
129
|
+
const edges: FlowViewEntities<TNodeData, TEdgeData, TLabelData>['edges'][number][] = [];
|
|
130
|
+
const boundaryEndpoints: FlowViewBoundaryEndpoints[] = [];
|
|
131
|
+
|
|
132
|
+
for (const edge of options.edges ?? graph.edges) {
|
|
133
|
+
const customOwner = options.edgeOwner?.(edge);
|
|
134
|
+
const owner = customOwner === undefined
|
|
135
|
+
? getEdgeOwner(edge, graph.nodes)
|
|
136
|
+
: customOwner;
|
|
137
|
+
if (!ownerIsExposed(owner, scope, allNodes)) continue;
|
|
138
|
+
const sourceId = endpointAtDepth(edge.sourceId, scope, allNodes);
|
|
139
|
+
const targetId = endpointAtDepth(edge.targetId, scope, allNodes);
|
|
140
|
+
if (
|
|
141
|
+
!sourceId ||
|
|
142
|
+
!targetId ||
|
|
143
|
+
!viewNodeIds.has(sourceId) ||
|
|
144
|
+
!viewNodeIds.has(targetId)
|
|
145
|
+
) {
|
|
146
|
+
continue;
|
|
147
|
+
}
|
|
148
|
+
if (sourceId === targetId && edge.sourceId !== edge.targetId) continue;
|
|
149
|
+
|
|
150
|
+
const source = frozenEndpoint(edge.sourceId, sourceId);
|
|
151
|
+
const target = frozenEndpoint(edge.targetId, targetId);
|
|
152
|
+
const boundary = Object.freeze({ edgeId: edge.id, source, target });
|
|
153
|
+
boundaryEndpoints.push(boundary);
|
|
154
|
+
if (!source.boundary && !target.boundary) {
|
|
155
|
+
edges.push(edge);
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
edges.push(Object.freeze({
|
|
159
|
+
...edge,
|
|
160
|
+
sourceId,
|
|
161
|
+
targetId,
|
|
162
|
+
sourcePort: source.boundary ? undefined : edge.sourcePort,
|
|
163
|
+
targetPort: target.boundary ? undefined : edge.targetPort,
|
|
164
|
+
}));
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
return Object.freeze({
|
|
168
|
+
nodes: Object.freeze([...nodes]),
|
|
169
|
+
edges: Object.freeze(edges),
|
|
170
|
+
boundaryEndpoints: Object.freeze(boundaryEndpoints),
|
|
171
|
+
});
|
|
172
|
+
}
|
|
@@ -0,0 +1,285 @@
|
|
|
1
|
+
import type { FlowStore } from '../store';
|
|
2
|
+
import type { Point, Viewport, ViewportAnimationOptions } from '../types';
|
|
3
|
+
|
|
4
|
+
declare const requestAnimationFrame:
|
|
5
|
+
| ((callback: (time: number) => void) => number)
|
|
6
|
+
| undefined;
|
|
7
|
+
declare const cancelAnimationFrame: ((id: number) => void) | undefined;
|
|
8
|
+
declare const setTimeout: (callback: () => void, ms: number) => number;
|
|
9
|
+
declare const clearTimeout: (id: number) => void;
|
|
10
|
+
|
|
11
|
+
export type ViewportAnimationFrame = (callback: (time: number) => void) => number;
|
|
12
|
+
export type ViewportAnimationCancelFrame = (id: number) => void;
|
|
13
|
+
|
|
14
|
+
export type ViewportAnimationControls = {
|
|
15
|
+
cancel: () => void;
|
|
16
|
+
/** Resolves true on arrival, false on cancellation. */
|
|
17
|
+
done: Promise<boolean>;
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
export type AnimateViewportOptions = ViewportAnimationOptions & {
|
|
21
|
+
raf?: ViewportAnimationFrame;
|
|
22
|
+
cancelRaf?: ViewportAnimationCancelFrame;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
function defaultEasing(t: number): number {
|
|
26
|
+
return t < 0.5 ? 4 * t * t * t : 1 - Math.pow(-2 * t + 2, 3) / 2;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function defaultRaf(callback: (time: number) => void): number {
|
|
30
|
+
if (typeof requestAnimationFrame === 'function') {
|
|
31
|
+
return requestAnimationFrame(callback);
|
|
32
|
+
}
|
|
33
|
+
return setTimeout(() => callback(Date.now()), 16) as unknown as number;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function defaultCancelRaf(id: number): void {
|
|
37
|
+
if (typeof cancelAnimationFrame === 'function') {
|
|
38
|
+
cancelAnimationFrame(id);
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
clearTimeout(id as unknown as ReturnType<typeof setTimeout>);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
function safeLog(zoom: number): number {
|
|
45
|
+
return Math.log(Math.max(zoom, Number.EPSILON));
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Focal-zoom state: the animation interpolates the zoom (in log space) and the
|
|
50
|
+
* screen-space position of a fixed canvas anchor. Deriving the viewport from
|
|
51
|
+
* those each frame keeps the anchor pinned to a monotonically-moving screen
|
|
52
|
+
* position, so the content zooms straight in/out instead of swimming along an
|
|
53
|
+
* arc.
|
|
54
|
+
*/
|
|
55
|
+
type FocalPlan = {
|
|
56
|
+
/** Canvas-space anchor kept visually fixed through the animation. */
|
|
57
|
+
focal: Point;
|
|
58
|
+
logZoomFrom: number;
|
|
59
|
+
logZoomTo: number;
|
|
60
|
+
/** Anchor's screen position in the start viewport. */
|
|
61
|
+
screenFrom: Point;
|
|
62
|
+
/** Anchor's screen position in the target viewport. */
|
|
63
|
+
screenTo: Point;
|
|
64
|
+
};
|
|
65
|
+
|
|
66
|
+
function planFocalZoom(
|
|
67
|
+
from: Viewport,
|
|
68
|
+
to: Viewport,
|
|
69
|
+
pivot: Point | undefined,
|
|
70
|
+
viewportSize: { width: number; height: number } | undefined,
|
|
71
|
+
): FocalPlan {
|
|
72
|
+
// Choose the focal anchor in canvas space. Prefer an explicit pivot (a
|
|
73
|
+
// screen-space point that should stay put). Otherwise anchor on the canvas
|
|
74
|
+
// point the TARGET viewport places at the screen CENTER — that point ends the
|
|
75
|
+
// animation dead-center, so the view glides straight to its final focus and
|
|
76
|
+
// scales around it (a natural fit/zoom feel). Anchoring on the canvas origin
|
|
77
|
+
// instead makes content swim toward the top-left corner.
|
|
78
|
+
const screenCenter: Point | null =
|
|
79
|
+
viewportSize && viewportSize.width > 0 && viewportSize.height > 0
|
|
80
|
+
? { x: viewportSize.width / 2, y: viewportSize.height / 2 }
|
|
81
|
+
: null;
|
|
82
|
+
const focal: Point = pivot
|
|
83
|
+
? {
|
|
84
|
+
x: (pivot.x - from.x) / from.zoom,
|
|
85
|
+
y: (pivot.y - from.y) / from.zoom,
|
|
86
|
+
}
|
|
87
|
+
: screenCenter
|
|
88
|
+
? {
|
|
89
|
+
x: (screenCenter.x - to.x) / to.zoom,
|
|
90
|
+
y: (screenCenter.y - to.y) / to.zoom,
|
|
91
|
+
}
|
|
92
|
+
: { x: 0, y: 0 };
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
focal,
|
|
96
|
+
logZoomFrom: safeLog(from.zoom),
|
|
97
|
+
logZoomTo: safeLog(to.zoom),
|
|
98
|
+
screenFrom: {
|
|
99
|
+
x: focal.x * from.zoom + from.x,
|
|
100
|
+
y: focal.y * from.zoom + from.y,
|
|
101
|
+
},
|
|
102
|
+
screenTo: {
|
|
103
|
+
x: focal.x * to.zoom + to.x,
|
|
104
|
+
y: focal.y * to.zoom + to.y,
|
|
105
|
+
},
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/**
|
|
110
|
+
* Derive a viewport at eased progress `t` such that the focal anchor sits at the
|
|
111
|
+
* linearly-interpolated screen position under the log-interpolated zoom.
|
|
112
|
+
*/
|
|
113
|
+
function focalViewport(plan: FocalPlan, t: number): Viewport {
|
|
114
|
+
const zoom = Math.exp(plan.logZoomFrom + (plan.logZoomTo - plan.logZoomFrom) * t);
|
|
115
|
+
const screenX = plan.screenFrom.x + (plan.screenTo.x - plan.screenFrom.x) * t;
|
|
116
|
+
const screenY = plan.screenFrom.y + (plan.screenTo.y - plan.screenFrom.y) * t;
|
|
117
|
+
return {
|
|
118
|
+
x: screenX - plan.focal.x * zoom,
|
|
119
|
+
y: screenY - plan.focal.y * zoom,
|
|
120
|
+
zoom,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
function linearViewport(from: Viewport, to: Viewport, t: number): Viewport {
|
|
125
|
+
return {
|
|
126
|
+
x: from.x + (to.x - from.x) * t,
|
|
127
|
+
y: from.y + (to.y - from.y) * t,
|
|
128
|
+
zoom: from.zoom + (to.zoom - from.zoom) * t,
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
// Spring defaults tuned to be near-critically-damped for a smooth, overshoot-
|
|
133
|
+
// free settle.
|
|
134
|
+
const DEFAULT_SPRING_STIFFNESS = 170;
|
|
135
|
+
const DEFAULT_SPRING_DAMPING = 26;
|
|
136
|
+
const DEFAULT_SPRING_MASS = 1;
|
|
137
|
+
const SPRING_REST_DELTA = 1e-3;
|
|
138
|
+
const SPRING_REST_SPEED = 1e-3;
|
|
139
|
+
const MAX_SPRING_STEP_MS = 1000 / 60;
|
|
140
|
+
const LERP_FRAME_MS = 1000 / 60;
|
|
141
|
+
const LERP_POSITION_REST_DELTA = 0.1;
|
|
142
|
+
const LERP_ZOOM_REST_DELTA = 1e-4;
|
|
143
|
+
|
|
144
|
+
export function animateViewport(
|
|
145
|
+
store: FlowStore,
|
|
146
|
+
target: Viewport,
|
|
147
|
+
options: AnimateViewportOptions = {},
|
|
148
|
+
): ViewportAnimationControls {
|
|
149
|
+
const useSpring = options.spring !== undefined;
|
|
150
|
+
const useLerp = options.lerp !== undefined;
|
|
151
|
+
if (useSpring && useLerp) {
|
|
152
|
+
throw new TypeError('Viewport animation cannot use both spring and lerp.');
|
|
153
|
+
}
|
|
154
|
+
if (
|
|
155
|
+
useLerp &&
|
|
156
|
+
(!Number.isFinite(options.lerp) || options.lerp! <= 0 || options.lerp! > 1)
|
|
157
|
+
) {
|
|
158
|
+
throw new TypeError('Viewport lerp factor must be finite and in (0, 1].');
|
|
159
|
+
}
|
|
160
|
+
const duration = options.duration ?? 0;
|
|
161
|
+
if (!useSpring && !useLerp && duration <= 0) {
|
|
162
|
+
store.trigger.setViewport(target);
|
|
163
|
+
return { cancel: () => {}, done: Promise.resolve(true) };
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
const raf = options.raf ?? defaultRaf;
|
|
167
|
+
const cancelRaf = options.cancelRaf ?? defaultCancelRaf;
|
|
168
|
+
const easing = options.easing ?? defaultEasing;
|
|
169
|
+
const from = { ...store.getSnapshot().context.viewport };
|
|
170
|
+
const plan = planFocalZoom(
|
|
171
|
+
from,
|
|
172
|
+
target,
|
|
173
|
+
options.center,
|
|
174
|
+
store.getSnapshot().context.viewportSize,
|
|
175
|
+
);
|
|
176
|
+
|
|
177
|
+
let frame: number | null = null;
|
|
178
|
+
let startTime: number | null = null;
|
|
179
|
+
let lastTime: number | null = null;
|
|
180
|
+
let canceled = false;
|
|
181
|
+
let resolveDone: (reached: boolean) => void = () => {};
|
|
182
|
+
const done = new Promise<boolean>((resolve) => {
|
|
183
|
+
resolveDone = resolve;
|
|
184
|
+
});
|
|
185
|
+
|
|
186
|
+
const cancel = () => {
|
|
187
|
+
if (canceled) return;
|
|
188
|
+
canceled = true;
|
|
189
|
+
if (frame !== null) {
|
|
190
|
+
cancelRaf(frame);
|
|
191
|
+
frame = null;
|
|
192
|
+
}
|
|
193
|
+
resolveDone(false);
|
|
194
|
+
};
|
|
195
|
+
|
|
196
|
+
const finish = () => {
|
|
197
|
+
store.trigger.setViewport(target);
|
|
198
|
+
frame = null;
|
|
199
|
+
resolveDone(true);
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
// Spring integrator: drives a normalized progress value `p` (0 -> 1) with a
|
|
203
|
+
// semi-implicit Euler step, then maps it through the same focal-zoom
|
|
204
|
+
// derivation so the anchor stays pinned. Because both zoom-log and focal
|
|
205
|
+
// screen-pos are linear in `p`, one spring settles the whole motion.
|
|
206
|
+
const springConfig = options.spring ?? {};
|
|
207
|
+
const stiffness = springConfig.stiffness ?? DEFAULT_SPRING_STIFFNESS;
|
|
208
|
+
const damping = springConfig.damping ?? DEFAULT_SPRING_DAMPING;
|
|
209
|
+
const mass = springConfig.mass ?? DEFAULT_SPRING_MASS;
|
|
210
|
+
let springValue = 0;
|
|
211
|
+
let springVelocity = 0;
|
|
212
|
+
let lerpValue = from;
|
|
213
|
+
|
|
214
|
+
const springStep = (dtMs: number): boolean => {
|
|
215
|
+
// Sub-step for stability if the frame delta is large.
|
|
216
|
+
let remaining = dtMs;
|
|
217
|
+
while (remaining > 0) {
|
|
218
|
+
const stepMs = Math.min(remaining, MAX_SPRING_STEP_MS);
|
|
219
|
+
const dt = stepMs / 1000;
|
|
220
|
+
const springForce = -stiffness * (springValue - 1);
|
|
221
|
+
const dampingForce = -damping * springVelocity;
|
|
222
|
+
const accel = (springForce + dampingForce) / mass;
|
|
223
|
+
springVelocity += accel * dt;
|
|
224
|
+
springValue += springVelocity * dt;
|
|
225
|
+
remaining -= stepMs;
|
|
226
|
+
}
|
|
227
|
+
const settled =
|
|
228
|
+
Math.abs(springVelocity) < SPRING_REST_SPEED &&
|
|
229
|
+
Math.abs(1 - springValue) < SPRING_REST_DELTA;
|
|
230
|
+
return settled;
|
|
231
|
+
};
|
|
232
|
+
|
|
233
|
+
const step = (time: number) => {
|
|
234
|
+
if (canceled) return;
|
|
235
|
+
if (startTime === null) {
|
|
236
|
+
startTime = time;
|
|
237
|
+
lastTime = time;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
if (useSpring) {
|
|
241
|
+
const dtMs = Math.max(0, time - (lastTime ?? time));
|
|
242
|
+
lastTime = time;
|
|
243
|
+
const settled = springStep(dtMs);
|
|
244
|
+
if (settled) {
|
|
245
|
+
finish();
|
|
246
|
+
return;
|
|
247
|
+
}
|
|
248
|
+
store.trigger.setViewport(focalViewport(plan, springValue));
|
|
249
|
+
frame = raf(step);
|
|
250
|
+
return;
|
|
251
|
+
}
|
|
252
|
+
|
|
253
|
+
if (useLerp) {
|
|
254
|
+
const dtMs = Math.max(0, time - (lastTime ?? time));
|
|
255
|
+
lastTime = time;
|
|
256
|
+
if (dtMs > 0) {
|
|
257
|
+
const frameFactor =
|
|
258
|
+
1 - Math.pow(1 - options.lerp!, dtMs / LERP_FRAME_MS);
|
|
259
|
+
lerpValue = linearViewport(lerpValue, target, frameFactor);
|
|
260
|
+
}
|
|
261
|
+
const settled =
|
|
262
|
+
Math.abs(target.x - lerpValue.x) <= LERP_POSITION_REST_DELTA &&
|
|
263
|
+
Math.abs(target.y - lerpValue.y) <= LERP_POSITION_REST_DELTA &&
|
|
264
|
+
Math.abs(target.zoom - lerpValue.zoom) <= LERP_ZOOM_REST_DELTA;
|
|
265
|
+
if (settled) {
|
|
266
|
+
finish();
|
|
267
|
+
return;
|
|
268
|
+
}
|
|
269
|
+
store.trigger.setViewport(lerpValue);
|
|
270
|
+
frame = raf(step);
|
|
271
|
+
return;
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
const progress = Math.min(1, (time - startTime) / duration);
|
|
275
|
+
if (progress >= 1) {
|
|
276
|
+
finish();
|
|
277
|
+
return;
|
|
278
|
+
}
|
|
279
|
+
store.trigger.setViewport(focalViewport(plan, easing(progress)));
|
|
280
|
+
frame = raf(step);
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
frame = raf(step);
|
|
284
|
+
return { cancel, done };
|
|
285
|
+
}
|