@pixerate/editor 0.8.1 → 0.10.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/dist/agent/index.cjs +512 -0
- package/dist/agent/index.cjs.map +1 -0
- package/dist/agent/index.d.cts +369 -0
- package/dist/agent/index.d.ts +369 -0
- package/dist/agent/index.js +471 -0
- package/dist/agent/index.js.map +1 -0
- package/dist/canvas/index.cjs +797 -0
- package/dist/canvas/index.cjs.map +1 -0
- package/dist/canvas/index.d.cts +187 -0
- package/dist/canvas/index.d.ts +187 -0
- package/dist/canvas/index.js +731 -0
- package/dist/canvas/index.js.map +1 -0
- package/dist/extensions/index.cjs.map +1 -1
- package/dist/extensions/index.d.cts +5 -27
- package/dist/extensions/index.d.ts +5 -27
- package/dist/extensions/index.js.map +1 -1
- package/dist/index.cjs +1274 -6
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +41 -5
- package/dist/index.d.ts +41 -5
- package/dist/index.js +1232 -6
- package/dist/index.js.map +1 -1
- package/dist/mention-Dcevh3ff.d.cts +32 -0
- package/dist/mention-Dcevh3ff.d.ts +32 -0
- package/dist/serializers/index.cjs +194 -4
- package/dist/serializers/index.cjs.map +1 -1
- package/dist/serializers/index.d.cts +89 -2
- package/dist/serializers/index.d.ts +89 -2
- package/dist/serializers/index.js +186 -3
- package/dist/serializers/index.js.map +1 -1
- package/dist/types-lGtRobRV.d.cts +213 -0
- package/dist/types-lGtRobRV.d.ts +213 -0
- package/package.json +13 -1
|
@@ -0,0 +1,731 @@
|
|
|
1
|
+
// src/canvas/layout.ts
|
|
2
|
+
import dagre from "@dagrejs/dagre";
|
|
3
|
+
var DEFAULT_NODE_WIDTH = 200;
|
|
4
|
+
var DEFAULT_NODE_HEIGHT = 60;
|
|
5
|
+
var DEFAULT_PLACEMENT_GAP = 48;
|
|
6
|
+
function getCenteredNodePosition(width = 0, height = 0, screenToFlowPosition, screenDimensions) {
|
|
7
|
+
const rect = screenDimensions ?? (typeof document !== "undefined" ? { width: document.documentElement.clientWidth, height: document.documentElement.clientHeight } : { width: 1920, height: 1080 });
|
|
8
|
+
const flowPos = screenToFlowPosition({ x: rect.width / 2, y: rect.height / 2 });
|
|
9
|
+
return {
|
|
10
|
+
x: flowPos.x - width / 2,
|
|
11
|
+
y: flowPos.y - height / 2
|
|
12
|
+
};
|
|
13
|
+
}
|
|
14
|
+
function centerNodes(nodes, screenToFlowPosition, getNodesBounds, screenDimensions) {
|
|
15
|
+
if (nodes.length === 0) {
|
|
16
|
+
return [];
|
|
17
|
+
}
|
|
18
|
+
const bounds = getNodesBounds(nodes);
|
|
19
|
+
const boundingBoxOrigin = { x: bounds.x, y: bounds.y };
|
|
20
|
+
const centeredPosition = getCenteredNodePosition(
|
|
21
|
+
bounds.width,
|
|
22
|
+
bounds.height,
|
|
23
|
+
screenToFlowPosition,
|
|
24
|
+
screenDimensions
|
|
25
|
+
);
|
|
26
|
+
const deltaX = centeredPosition.x - boundingBoxOrigin.x;
|
|
27
|
+
const deltaY = centeredPosition.y - boundingBoxOrigin.y;
|
|
28
|
+
return nodes.map((node) => ({
|
|
29
|
+
...node,
|
|
30
|
+
position: {
|
|
31
|
+
x: node.position.x + deltaX,
|
|
32
|
+
y: node.position.y + deltaY
|
|
33
|
+
}
|
|
34
|
+
}));
|
|
35
|
+
}
|
|
36
|
+
function getLayoutedNodes(nodes, edges, getNodesBounds, options = {}) {
|
|
37
|
+
if (nodes.length === 0) return [];
|
|
38
|
+
const {
|
|
39
|
+
direction = "LR",
|
|
40
|
+
ranksep = 100,
|
|
41
|
+
nodesep = 25,
|
|
42
|
+
defaultNodeWidth = DEFAULT_NODE_WIDTH,
|
|
43
|
+
defaultNodeHeight = DEFAULT_NODE_HEIGHT
|
|
44
|
+
} = options;
|
|
45
|
+
const dagreGraph = new dagre.graphlib.Graph();
|
|
46
|
+
dagreGraph.setDefaultEdgeLabel(() => ({}));
|
|
47
|
+
dagreGraph.setGraph({ rankdir: direction, ranksep, nodesep });
|
|
48
|
+
const nodeIds = new Set(nodes.map((node) => node.id));
|
|
49
|
+
nodes.forEach((node) => {
|
|
50
|
+
const nodeWidth = node.measured?.width ?? node.width ?? defaultNodeWidth;
|
|
51
|
+
const nodeHeight = node.measured?.height ?? node.height ?? defaultNodeHeight;
|
|
52
|
+
dagreGraph.setNode(node.id, { width: nodeWidth, height: nodeHeight });
|
|
53
|
+
});
|
|
54
|
+
edges.forEach((edge) => {
|
|
55
|
+
if (nodeIds.has(edge.source) && nodeIds.has(edge.target)) {
|
|
56
|
+
dagreGraph.setEdge(edge.source, edge.target);
|
|
57
|
+
}
|
|
58
|
+
});
|
|
59
|
+
dagre.layout(dagreGraph);
|
|
60
|
+
return nodes.map((node) => {
|
|
61
|
+
const nodeWithPosition = dagreGraph.node(node.id);
|
|
62
|
+
const nodeWidth = node.measured?.width ?? node.width ?? defaultNodeWidth;
|
|
63
|
+
const nodeHeight = node.measured?.height ?? node.height ?? defaultNodeHeight;
|
|
64
|
+
return {
|
|
65
|
+
...node,
|
|
66
|
+
position: {
|
|
67
|
+
x: nodeWithPosition.x - nodeWidth / 2,
|
|
68
|
+
y: nodeWithPosition.y - nodeHeight / 2
|
|
69
|
+
}
|
|
70
|
+
};
|
|
71
|
+
});
|
|
72
|
+
}
|
|
73
|
+
function estimateNodeSize(node, defaultWidth = DEFAULT_NODE_WIDTH, defaultHeight = DEFAULT_NODE_HEIGHT) {
|
|
74
|
+
const label = String(node.data?.label ?? node.data?.title ?? node.id ?? "");
|
|
75
|
+
const lines = label.split(/\n|<br\s*\/?>/);
|
|
76
|
+
const longest = Math.max(...lines.map((l) => l.replace(/<[^>]+>/g, "").length), 1);
|
|
77
|
+
const width = Math.min(480, Math.max(defaultWidth, longest * 8 + 48));
|
|
78
|
+
const height = Math.max(defaultHeight, lines.length * 20 + 24);
|
|
79
|
+
return { width, height };
|
|
80
|
+
}
|
|
81
|
+
function boundsOf(rects) {
|
|
82
|
+
if (!rects.length) return null;
|
|
83
|
+
const minX = Math.min(...rects.map((r) => r.x));
|
|
84
|
+
const minY = Math.min(...rects.map((r) => r.y));
|
|
85
|
+
const maxX = Math.max(...rects.map((r) => r.x + r.width));
|
|
86
|
+
const maxY = Math.max(...rects.map((r) => r.y + r.height));
|
|
87
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
88
|
+
}
|
|
89
|
+
function centre(r) {
|
|
90
|
+
return { x: r.x + r.width / 2, y: r.y + r.height / 2 };
|
|
91
|
+
}
|
|
92
|
+
function overlaps(a, b, pad = 16) {
|
|
93
|
+
return a.x < b.x + b.width + pad && a.x + a.width + pad > b.x && a.y < b.y + b.height + pad && a.y + a.height + pad > b.y;
|
|
94
|
+
}
|
|
95
|
+
function flowSide(direction = "LR") {
|
|
96
|
+
switch (direction) {
|
|
97
|
+
case "LR":
|
|
98
|
+
return "right";
|
|
99
|
+
case "RL":
|
|
100
|
+
return "left";
|
|
101
|
+
case "BT":
|
|
102
|
+
return "above";
|
|
103
|
+
case "TB":
|
|
104
|
+
default:
|
|
105
|
+
return "below";
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
function ensureLayout(nodes, edges, options = {}) {
|
|
109
|
+
if (nodes.length === 0) return [];
|
|
110
|
+
const stored = options.storedPositions ?? {};
|
|
111
|
+
const missing = nodes.filter((n) => !stored[n.id]);
|
|
112
|
+
if (missing.length === 0) {
|
|
113
|
+
return nodes.map((n) => ({
|
|
114
|
+
...n,
|
|
115
|
+
position: { ...stored[n.id] }
|
|
116
|
+
}));
|
|
117
|
+
}
|
|
118
|
+
const autoLayouted = getLayoutedNodes(nodes, edges, void 0, options);
|
|
119
|
+
const autoMap = new Map(
|
|
120
|
+
autoLayouted.map((n) => [n.id, n.position])
|
|
121
|
+
);
|
|
122
|
+
const both = nodes.filter((n) => stored[n.id] && autoMap.has(n.id));
|
|
123
|
+
if (both.length === 0) {
|
|
124
|
+
return autoLayouted;
|
|
125
|
+
}
|
|
126
|
+
let dx = 0;
|
|
127
|
+
let dy = 0;
|
|
128
|
+
for (const n of both) {
|
|
129
|
+
const s = stored[n.id];
|
|
130
|
+
const a = autoMap.get(n.id);
|
|
131
|
+
dx += s.x - a.x;
|
|
132
|
+
dy += s.y - a.y;
|
|
133
|
+
}
|
|
134
|
+
dx /= both.length;
|
|
135
|
+
dy /= both.length;
|
|
136
|
+
return nodes.map((node) => {
|
|
137
|
+
if (stored[node.id]) {
|
|
138
|
+
return {
|
|
139
|
+
...node,
|
|
140
|
+
position: { ...stored[node.id] }
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
const autoPos = autoMap.get(node.id) ?? { x: 0, y: 0 };
|
|
144
|
+
return {
|
|
145
|
+
...node,
|
|
146
|
+
position: {
|
|
147
|
+
x: Math.round(autoPos.x + dx),
|
|
148
|
+
y: Math.round(autoPos.y + dy)
|
|
149
|
+
}
|
|
150
|
+
};
|
|
151
|
+
});
|
|
152
|
+
}
|
|
153
|
+
function placeBeside(rects, size, side, nearNodeId, options = {}) {
|
|
154
|
+
const {
|
|
155
|
+
gap = DEFAULT_PLACEMENT_GAP,
|
|
156
|
+
ignore = [],
|
|
157
|
+
maxAttempts = 32
|
|
158
|
+
} = options;
|
|
159
|
+
const skip = new Set([nearNodeId, ...ignore].filter(Boolean));
|
|
160
|
+
const obstacles = Object.entries(rects).filter(([id]) => !skip.has(id)).map(([, r]) => r);
|
|
161
|
+
const anchor = nearNodeId && rects[nearNodeId] || boundsOf(Object.values(rects)) || { x: 0, y: 0, width: 0, height: 0 };
|
|
162
|
+
const anchorCenter = centre(anchor);
|
|
163
|
+
let baseX;
|
|
164
|
+
let baseY;
|
|
165
|
+
switch (side) {
|
|
166
|
+
case "right":
|
|
167
|
+
baseX = anchor.x + anchor.width + gap;
|
|
168
|
+
baseY = anchorCenter.y - size.height / 2;
|
|
169
|
+
break;
|
|
170
|
+
case "left":
|
|
171
|
+
baseX = anchor.x - gap - size.width;
|
|
172
|
+
baseY = anchorCenter.y - size.height / 2;
|
|
173
|
+
break;
|
|
174
|
+
case "above":
|
|
175
|
+
baseX = anchorCenter.x - size.width / 2;
|
|
176
|
+
baseY = anchor.y - gap - size.height;
|
|
177
|
+
break;
|
|
178
|
+
case "below":
|
|
179
|
+
default:
|
|
180
|
+
baseX = anchorCenter.x - size.width / 2;
|
|
181
|
+
baseY = anchor.y + anchor.height + gap;
|
|
182
|
+
break;
|
|
183
|
+
}
|
|
184
|
+
const isHorizontal = side === "right" || side === "left";
|
|
185
|
+
const step = (isHorizontal ? size.height : size.width) + 20;
|
|
186
|
+
for (let i = 0; i < maxAttempts; i++) {
|
|
187
|
+
const offset = i === 0 ? 0 : Math.ceil(i / 2) * step * (i % 2 === 1 ? 1 : -1);
|
|
188
|
+
const candidateBox = {
|
|
189
|
+
x: isHorizontal ? baseX : baseX + offset,
|
|
190
|
+
y: isHorizontal ? baseY + offset : baseY,
|
|
191
|
+
width: size.width,
|
|
192
|
+
height: size.height
|
|
193
|
+
};
|
|
194
|
+
if (!obstacles.some((obstacle) => overlaps(candidateBox, obstacle, 8))) {
|
|
195
|
+
return {
|
|
196
|
+
x: Math.round(candidateBox.x),
|
|
197
|
+
y: Math.round(candidateBox.y)
|
|
198
|
+
};
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
return {
|
|
202
|
+
x: Math.round(baseX),
|
|
203
|
+
y: Math.round(baseY)
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// src/canvas/trajectory.ts
|
|
208
|
+
var defaultEasing = (t) => --t * t * t + 1;
|
|
209
|
+
var linearTrajectory = (origin, target, progress) => {
|
|
210
|
+
return {
|
|
211
|
+
position: {
|
|
212
|
+
x: origin.x + (target.x - origin.x) * progress,
|
|
213
|
+
y: origin.y + (target.y - origin.y) * progress
|
|
214
|
+
},
|
|
215
|
+
scale: progress,
|
|
216
|
+
opacity: Math.min(1, progress * 1.5)
|
|
217
|
+
};
|
|
218
|
+
};
|
|
219
|
+
var linearPositionTrajectory = (origin, target, progress) => {
|
|
220
|
+
return {
|
|
221
|
+
position: {
|
|
222
|
+
x: origin.x + (target.x - origin.x) * progress,
|
|
223
|
+
y: origin.y + (target.y - origin.y) * progress
|
|
224
|
+
}
|
|
225
|
+
};
|
|
226
|
+
};
|
|
227
|
+
function createFanOutTrajectory(options = {}) {
|
|
228
|
+
const {
|
|
229
|
+
curvature = 0.4,
|
|
230
|
+
startScale = 0,
|
|
231
|
+
endScale = 1,
|
|
232
|
+
startOpacity = 0,
|
|
233
|
+
endOpacity = 1
|
|
234
|
+
} = options;
|
|
235
|
+
return (origin, target, progress, index, total) => {
|
|
236
|
+
const dx = target.x - origin.x;
|
|
237
|
+
const dy = target.y - origin.y;
|
|
238
|
+
const normIndex = total > 1 ? (index - (total - 1) / 2) / ((total - 1) / 2) : 0;
|
|
239
|
+
const midX = origin.x + dx * 0.4;
|
|
240
|
+
const midY = origin.y + dy * 0.1 - normIndex * Math.abs(dx) * 0.15 * curvature;
|
|
241
|
+
const t = progress;
|
|
242
|
+
const oneMinusT = 1 - t;
|
|
243
|
+
const x = oneMinusT * oneMinusT * origin.x + 2 * oneMinusT * t * midX + t * t * target.x;
|
|
244
|
+
const y = oneMinusT * oneMinusT * origin.y + 2 * oneMinusT * t * midY + t * t * target.y;
|
|
245
|
+
const scale = startScale + (endScale - startScale) * progress;
|
|
246
|
+
const opacity = startOpacity + (endOpacity - startOpacity) * progress;
|
|
247
|
+
return {
|
|
248
|
+
position: { x, y },
|
|
249
|
+
scale,
|
|
250
|
+
opacity
|
|
251
|
+
};
|
|
252
|
+
};
|
|
253
|
+
}
|
|
254
|
+
function createBezierTrajectory(getControlPoint) {
|
|
255
|
+
return (origin, target, progress) => {
|
|
256
|
+
const cp = getControlPoint(origin, target);
|
|
257
|
+
const t = progress;
|
|
258
|
+
const oneMinusT = 1 - t;
|
|
259
|
+
const x = oneMinusT * oneMinusT * origin.x + 2 * oneMinusT * t * cp.x + t * t * target.x;
|
|
260
|
+
const y = oneMinusT * oneMinusT * origin.y + 2 * oneMinusT * t * cp.y + t * t * target.y;
|
|
261
|
+
return {
|
|
262
|
+
position: { x, y },
|
|
263
|
+
scale: progress,
|
|
264
|
+
opacity: progress
|
|
265
|
+
};
|
|
266
|
+
};
|
|
267
|
+
}
|
|
268
|
+
function runMultiNodeTransition(transitions, options = {}) {
|
|
269
|
+
if (transitions.length === 0) {
|
|
270
|
+
options.onComplete?.();
|
|
271
|
+
return () => {
|
|
272
|
+
};
|
|
273
|
+
}
|
|
274
|
+
const { defaultDuration = 400, defaultEasing: easingFunc = defaultEasing, onUpdate, onComplete } = options;
|
|
275
|
+
let isCancelled = false;
|
|
276
|
+
const startTime = Date.now();
|
|
277
|
+
const items = transitions.map((tr, i) => ({
|
|
278
|
+
...tr,
|
|
279
|
+
index: tr.index ?? i,
|
|
280
|
+
total: tr.total ?? transitions.length,
|
|
281
|
+
duration: tr.duration ?? defaultDuration,
|
|
282
|
+
easing: tr.easing ?? easingFunc,
|
|
283
|
+
delay: tr.delay ?? 0,
|
|
284
|
+
trajectory: tr.trajectory ?? linearTrajectory
|
|
285
|
+
}));
|
|
286
|
+
function frame() {
|
|
287
|
+
if (isCancelled) return;
|
|
288
|
+
const now = Date.now();
|
|
289
|
+
let allCompleted = true;
|
|
290
|
+
for (let i = 0; i < items.length; i++) {
|
|
291
|
+
const item = items[i];
|
|
292
|
+
const elapsed = now - (startTime + item.delay);
|
|
293
|
+
if (elapsed < 0) {
|
|
294
|
+
allCompleted = false;
|
|
295
|
+
continue;
|
|
296
|
+
}
|
|
297
|
+
const rawProgress = item.duration <= 0 ? 1 : Math.min(elapsed / item.duration, 1);
|
|
298
|
+
const progress = item.duration <= 0 ? 1 : item.easing(rawProgress);
|
|
299
|
+
const point = item.trajectory(item.from, item.to, progress, item.index, item.total);
|
|
300
|
+
item.node.position = { ...point.position };
|
|
301
|
+
if (point.scale !== void 0 || point.opacity !== void 0) {
|
|
302
|
+
if (!item.node.data) {
|
|
303
|
+
item.node.data = {};
|
|
304
|
+
}
|
|
305
|
+
if (point.scale !== void 0) {
|
|
306
|
+
item.node.data.scale = point.scale;
|
|
307
|
+
}
|
|
308
|
+
if (point.opacity !== void 0) {
|
|
309
|
+
item.node.data.opacity = point.opacity;
|
|
310
|
+
const currentStyle = (item.node.style || "").replace(/opacity:\s*[^;]+;?/g, "").trim();
|
|
311
|
+
item.node.style = `${currentStyle ? currentStyle + "; " : ""}opacity: ${point.opacity};`;
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
if (rawProgress < 1) {
|
|
315
|
+
allCompleted = false;
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
onUpdate?.();
|
|
319
|
+
if (!allCompleted) {
|
|
320
|
+
if (typeof requestAnimationFrame !== "undefined") {
|
|
321
|
+
requestAnimationFrame(frame);
|
|
322
|
+
}
|
|
323
|
+
} else {
|
|
324
|
+
onComplete?.();
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
frame();
|
|
328
|
+
return () => {
|
|
329
|
+
isCancelled = true;
|
|
330
|
+
};
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
// src/canvas/displacement.ts
|
|
334
|
+
import dagre2 from "@dagrejs/dagre";
|
|
335
|
+
function computeNodesBoundingBox(nodes, defaultWidth = 200, defaultHeight = 60) {
|
|
336
|
+
if (nodes.length === 0) {
|
|
337
|
+
return { x: 0, y: 0, width: 0, height: 0 };
|
|
338
|
+
}
|
|
339
|
+
let minX = Infinity;
|
|
340
|
+
let minY = Infinity;
|
|
341
|
+
let maxX = -Infinity;
|
|
342
|
+
let maxY = -Infinity;
|
|
343
|
+
for (const node of nodes) {
|
|
344
|
+
const width = node.measured?.width ?? node.width ?? defaultWidth;
|
|
345
|
+
const height = node.measured?.height ?? node.height ?? defaultHeight;
|
|
346
|
+
minX = Math.min(minX, node.position.x);
|
|
347
|
+
minY = Math.min(minY, node.position.y);
|
|
348
|
+
maxX = Math.max(maxX, node.position.x + width);
|
|
349
|
+
maxY = Math.max(maxY, node.position.y + height);
|
|
350
|
+
}
|
|
351
|
+
return {
|
|
352
|
+
x: minX,
|
|
353
|
+
y: minY,
|
|
354
|
+
width: maxX - minX,
|
|
355
|
+
height: maxY - minY
|
|
356
|
+
};
|
|
357
|
+
}
|
|
358
|
+
function calculateDirectionalDisplacement(params) {
|
|
359
|
+
const {
|
|
360
|
+
existingNodes,
|
|
361
|
+
originNode,
|
|
362
|
+
childNodes,
|
|
363
|
+
getNodesBounds,
|
|
364
|
+
options = {}
|
|
365
|
+
} = params;
|
|
366
|
+
const {
|
|
367
|
+
direction = "right",
|
|
368
|
+
gap = 40,
|
|
369
|
+
spreadSiblingLanes = false,
|
|
370
|
+
siblingTolerance = 50,
|
|
371
|
+
minY
|
|
372
|
+
} = options;
|
|
373
|
+
const displacedMap = /* @__PURE__ */ new Map();
|
|
374
|
+
if (childNodes.length === 0 || existingNodes.length === 0) {
|
|
375
|
+
return displacedMap;
|
|
376
|
+
}
|
|
377
|
+
const childBounds = getNodesBounds ? getNodesBounds(childNodes) : computeNodesBoundingBox(childNodes);
|
|
378
|
+
const originWidth = originNode.measured?.width ?? originNode.width ?? 200;
|
|
379
|
+
const originHeight = originNode.measured?.height ?? originNode.height ?? 60;
|
|
380
|
+
const deltaPrimary = direction === "right" || direction === "left" ? childBounds.width + gap : childBounds.height + gap;
|
|
381
|
+
const deltaUp = Math.max(0, originNode.position.y - childBounds.y + gap);
|
|
382
|
+
const deltaDown = Math.max(
|
|
383
|
+
0,
|
|
384
|
+
childBounds.y + childBounds.height - (originNode.position.y + originHeight) + gap
|
|
385
|
+
);
|
|
386
|
+
const equalDelta = Math.max(deltaUp, deltaDown);
|
|
387
|
+
for (const node of existingNodes) {
|
|
388
|
+
if (node.id === originNode.id) continue;
|
|
389
|
+
const isSiblingRow = Math.abs(node.position.y - originNode.position.y) < siblingTolerance;
|
|
390
|
+
switch (direction) {
|
|
391
|
+
case "right": {
|
|
392
|
+
if (node.position.x > originNode.position.x) {
|
|
393
|
+
displacedMap.set(node.id, {
|
|
394
|
+
x: node.position.x + deltaPrimary,
|
|
395
|
+
y: node.position.y
|
|
396
|
+
});
|
|
397
|
+
}
|
|
398
|
+
break;
|
|
399
|
+
}
|
|
400
|
+
case "left": {
|
|
401
|
+
if (node.position.x < originNode.position.x) {
|
|
402
|
+
displacedMap.set(node.id, {
|
|
403
|
+
x: node.position.x - deltaPrimary,
|
|
404
|
+
y: node.position.y
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
break;
|
|
408
|
+
}
|
|
409
|
+
case "down": {
|
|
410
|
+
if (node.position.y > originNode.position.y) {
|
|
411
|
+
displacedMap.set(node.id, {
|
|
412
|
+
x: node.position.x,
|
|
413
|
+
y: node.position.y + deltaPrimary
|
|
414
|
+
});
|
|
415
|
+
}
|
|
416
|
+
break;
|
|
417
|
+
}
|
|
418
|
+
case "up": {
|
|
419
|
+
if (node.position.y < originNode.position.y) {
|
|
420
|
+
displacedMap.set(node.id, {
|
|
421
|
+
x: node.position.x,
|
|
422
|
+
y: node.position.y - deltaPrimary
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
break;
|
|
426
|
+
}
|
|
427
|
+
case "vertical": {
|
|
428
|
+
if (isSiblingRow) break;
|
|
429
|
+
if (node.position.y < originNode.position.y) {
|
|
430
|
+
displacedMap.set(node.id, {
|
|
431
|
+
x: node.position.x,
|
|
432
|
+
y: node.position.y - equalDelta
|
|
433
|
+
});
|
|
434
|
+
} else if (node.position.y > originNode.position.y) {
|
|
435
|
+
displacedMap.set(node.id, {
|
|
436
|
+
x: node.position.x,
|
|
437
|
+
y: node.position.y + equalDelta
|
|
438
|
+
});
|
|
439
|
+
}
|
|
440
|
+
break;
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
}
|
|
444
|
+
if (spreadSiblingLanes && (direction === "right" || direction === "left")) {
|
|
445
|
+
for (const [nodeId, pos] of displacedMap.entries()) {
|
|
446
|
+
const node = existingNodes.find((n) => n.id === nodeId);
|
|
447
|
+
if (node && Math.abs(node.position.y - originNode.position.y) < siblingTolerance) {
|
|
448
|
+
const isAbove = node.position.y < originNode.position.y;
|
|
449
|
+
displacedMap.set(nodeId, {
|
|
450
|
+
x: pos.x,
|
|
451
|
+
y: pos.y + (isAbove ? -deltaUp : deltaDown)
|
|
452
|
+
});
|
|
453
|
+
}
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
if (minY !== void 0) {
|
|
457
|
+
for (const [nodeId, pos] of displacedMap.entries()) {
|
|
458
|
+
if (pos.y < minY) {
|
|
459
|
+
displacedMap.set(nodeId, { x: pos.x, y: minY });
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
return displacedMap;
|
|
464
|
+
}
|
|
465
|
+
function calculateDagreReflowDisplacement(params) {
|
|
466
|
+
const {
|
|
467
|
+
currentNodes,
|
|
468
|
+
futureNodes,
|
|
469
|
+
futureEdges,
|
|
470
|
+
options
|
|
471
|
+
} = params;
|
|
472
|
+
const {
|
|
473
|
+
anchorNodeId,
|
|
474
|
+
direction = "LR",
|
|
475
|
+
ranksep = 100,
|
|
476
|
+
nodesep = 25,
|
|
477
|
+
defaultNodeWidth = 200,
|
|
478
|
+
defaultNodeHeight = 60
|
|
479
|
+
} = options;
|
|
480
|
+
const anchorNode = currentNodes.find((n) => n.id === anchorNodeId);
|
|
481
|
+
if (!anchorNode) return /* @__PURE__ */ new Map();
|
|
482
|
+
const g = new dagre2.graphlib.Graph();
|
|
483
|
+
g.setDefaultEdgeLabel(() => ({}));
|
|
484
|
+
g.setGraph({ rankdir: direction, ranksep, nodesep });
|
|
485
|
+
const nodeIds = new Set(futureNodes.map((n) => n.id));
|
|
486
|
+
futureNodes.forEach((node) => {
|
|
487
|
+
const nodeWidth = node.measured?.width ?? node.width ?? defaultNodeWidth;
|
|
488
|
+
const nodeHeight = node.measured?.height ?? node.height ?? defaultNodeHeight;
|
|
489
|
+
g.setNode(node.id, { width: nodeWidth, height: nodeHeight });
|
|
490
|
+
});
|
|
491
|
+
futureEdges.forEach((edge) => {
|
|
492
|
+
if (nodeIds.has(edge.source) && nodeIds.has(edge.target)) {
|
|
493
|
+
g.setEdge(edge.source, edge.target);
|
|
494
|
+
}
|
|
495
|
+
});
|
|
496
|
+
dagre2.layout(g);
|
|
497
|
+
const anchorLayoutPos = g.node(anchorNodeId);
|
|
498
|
+
if (!anchorLayoutPos) return /* @__PURE__ */ new Map();
|
|
499
|
+
const anchorWidth = anchorNode.measured?.width ?? anchorNode.width ?? defaultNodeWidth;
|
|
500
|
+
const anchorHeight = anchorNode.measured?.height ?? anchorNode.height ?? defaultNodeHeight;
|
|
501
|
+
const anchorLayoutUpperLeft = {
|
|
502
|
+
x: anchorLayoutPos.x - anchorWidth / 2,
|
|
503
|
+
y: anchorLayoutPos.y - anchorHeight / 2
|
|
504
|
+
};
|
|
505
|
+
const deltaX = anchorNode.position.x - anchorLayoutUpperLeft.x;
|
|
506
|
+
const deltaY = anchorNode.position.y - anchorLayoutUpperLeft.y;
|
|
507
|
+
const displacementMap = /* @__PURE__ */ new Map();
|
|
508
|
+
currentNodes.forEach((node) => {
|
|
509
|
+
if (node.id === anchorNodeId) return;
|
|
510
|
+
const layoutNode = g.node(node.id);
|
|
511
|
+
if (!layoutNode) return;
|
|
512
|
+
const nodeWidth = node.measured?.width ?? node.width ?? defaultNodeWidth;
|
|
513
|
+
const nodeHeight = node.measured?.height ?? node.height ?? defaultNodeHeight;
|
|
514
|
+
const upperLeftX = layoutNode.x - nodeWidth / 2 + deltaX;
|
|
515
|
+
const upperLeftY = layoutNode.y - nodeHeight / 2 + deltaY;
|
|
516
|
+
if (Math.abs(upperLeftX - node.position.x) > 1 || Math.abs(upperLeftY - node.position.y) > 1) {
|
|
517
|
+
displacementMap.set(node.id, { x: upperLeftX, y: upperLeftY });
|
|
518
|
+
}
|
|
519
|
+
});
|
|
520
|
+
return displacementMap;
|
|
521
|
+
}
|
|
522
|
+
|
|
523
|
+
// src/canvas/clipboard.ts
|
|
524
|
+
function isEventFromTextInput(event) {
|
|
525
|
+
const target = event.target;
|
|
526
|
+
if (!target) return false;
|
|
527
|
+
if (target.tagName === "INPUT" || target.tagName === "TEXTAREA" || target.tagName === "SELECT" || target.isContentEditable || target.closest?.('[contenteditable="true"]') || target.closest?.(".ProseMirror")) {
|
|
528
|
+
return true;
|
|
529
|
+
}
|
|
530
|
+
return false;
|
|
531
|
+
}
|
|
532
|
+
function isValidCanvasNode(obj) {
|
|
533
|
+
return typeof obj === "object" && obj !== null && "id" in obj && typeof obj.id === "string" && "position" in obj && typeof obj.position === "object" && obj.position !== null && "x" in obj.position && "y" in obj.position;
|
|
534
|
+
}
|
|
535
|
+
function isValidUrl(url) {
|
|
536
|
+
try {
|
|
537
|
+
const parsed = new URL(url);
|
|
538
|
+
return parsed.protocol === "http:" || parsed.protocol === "https:";
|
|
539
|
+
} catch {
|
|
540
|
+
return false;
|
|
541
|
+
}
|
|
542
|
+
}
|
|
543
|
+
function serializeCanvasNodes(nodes, edges) {
|
|
544
|
+
const payload = {
|
|
545
|
+
version: 1,
|
|
546
|
+
nodes,
|
|
547
|
+
edges
|
|
548
|
+
};
|
|
549
|
+
return JSON.stringify(payload);
|
|
550
|
+
}
|
|
551
|
+
function parseCanvasClipboardData(raw) {
|
|
552
|
+
try {
|
|
553
|
+
const parsed = JSON.parse(raw);
|
|
554
|
+
if (typeof parsed === "object" && parsed !== null && Array.isArray(parsed.nodes) && parsed.nodes.length > 0 && parsed.nodes.every(isValidCanvasNode)) {
|
|
555
|
+
return parsed;
|
|
556
|
+
}
|
|
557
|
+
} catch {
|
|
558
|
+
}
|
|
559
|
+
return null;
|
|
560
|
+
}
|
|
561
|
+
function remapPastedNodes(nodes, edges = [], offset = { x: 30, y: 30 }, idGenerator = () => `node-${Math.random().toString(36).substring(2, 9)}`) {
|
|
562
|
+
const idMap = /* @__PURE__ */ new Map();
|
|
563
|
+
const newNodes = nodes.map((node) => {
|
|
564
|
+
const newId = idGenerator();
|
|
565
|
+
idMap.set(node.id, newId);
|
|
566
|
+
return {
|
|
567
|
+
...node,
|
|
568
|
+
id: newId,
|
|
569
|
+
position: {
|
|
570
|
+
x: node.position.x + offset.x,
|
|
571
|
+
y: node.position.y + offset.y
|
|
572
|
+
},
|
|
573
|
+
selected: true
|
|
574
|
+
};
|
|
575
|
+
});
|
|
576
|
+
const newEdges = edges.filter((e) => idMap.has(e.source) && idMap.has(e.target)).map((edge) => ({
|
|
577
|
+
...edge,
|
|
578
|
+
id: `edge-${Math.random().toString(36).substring(2, 9)}`,
|
|
579
|
+
source: idMap.get(edge.source),
|
|
580
|
+
target: idMap.get(edge.target),
|
|
581
|
+
selected: false
|
|
582
|
+
}));
|
|
583
|
+
return { nodes: newNodes, edges: newEdges };
|
|
584
|
+
}
|
|
585
|
+
function repositionNodesTo(nodes, targetOrigin) {
|
|
586
|
+
if (nodes.length === 0) return [];
|
|
587
|
+
const minX = Math.min(...nodes.map((n) => n.position.x));
|
|
588
|
+
const minY = Math.min(...nodes.map((n) => n.position.y));
|
|
589
|
+
const deltaX = targetOrigin.x - minX;
|
|
590
|
+
const deltaY = targetOrigin.y - minY;
|
|
591
|
+
return nodes.map((n) => ({
|
|
592
|
+
...n,
|
|
593
|
+
position: {
|
|
594
|
+
x: n.position.x + deltaX,
|
|
595
|
+
y: n.position.y + deltaY
|
|
596
|
+
}
|
|
597
|
+
}));
|
|
598
|
+
}
|
|
599
|
+
|
|
600
|
+
// src/canvas/streaming.ts
|
|
601
|
+
var StreamingLayoutManager = class {
|
|
602
|
+
throttleMs;
|
|
603
|
+
glideDurationMs;
|
|
604
|
+
layoutOptions;
|
|
605
|
+
onLayoutUpdated;
|
|
606
|
+
pendingTimer = null;
|
|
607
|
+
activeTransitionCancel = null;
|
|
608
|
+
targetPositions = /* @__PURE__ */ new Map();
|
|
609
|
+
constructor(options = {}) {
|
|
610
|
+
this.throttleMs = options.throttleMs ?? 200;
|
|
611
|
+
this.glideDurationMs = options.glideDurationMs ?? 300;
|
|
612
|
+
this.layoutOptions = options.layoutOptions ?? { direction: "LR" };
|
|
613
|
+
this.onLayoutUpdated = options.onLayoutUpdated;
|
|
614
|
+
}
|
|
615
|
+
/**
|
|
616
|
+
* Feed incoming nodes and edges into the streaming manager.
|
|
617
|
+
* Coalesces rapid streaming bursts and triggers smooth layout gliding.
|
|
618
|
+
*/
|
|
619
|
+
pushStreamUpdate(nodes, edges, currentPositions) {
|
|
620
|
+
if (this.pendingTimer) {
|
|
621
|
+
clearTimeout(this.pendingTimer);
|
|
622
|
+
}
|
|
623
|
+
this.pendingTimer = setTimeout(() => {
|
|
624
|
+
this.pendingTimer = null;
|
|
625
|
+
this.recalculateAndGlide(nodes, edges, currentPositions);
|
|
626
|
+
}, this.throttleMs);
|
|
627
|
+
}
|
|
628
|
+
/**
|
|
629
|
+
* Forces immediate calculation and cancels pending throttled timers.
|
|
630
|
+
*/
|
|
631
|
+
flush(nodes, edges, currentPositions) {
|
|
632
|
+
if (this.pendingTimer) {
|
|
633
|
+
clearTimeout(this.pendingTimer);
|
|
634
|
+
this.pendingTimer = null;
|
|
635
|
+
}
|
|
636
|
+
this.recalculateAndGlide(nodes, edges, currentPositions);
|
|
637
|
+
}
|
|
638
|
+
recalculateAndGlide(nodes, edges, currentPositions) {
|
|
639
|
+
if (nodes.length === 0) return;
|
|
640
|
+
if (this.activeTransitionCancel) {
|
|
641
|
+
this.activeTransitionCancel();
|
|
642
|
+
this.activeTransitionCancel = null;
|
|
643
|
+
}
|
|
644
|
+
const stored = {};
|
|
645
|
+
for (const [id, pos] of currentPositions.entries()) {
|
|
646
|
+
stored[id] = pos;
|
|
647
|
+
}
|
|
648
|
+
const layouted = ensureLayout(nodes, edges, {
|
|
649
|
+
...this.layoutOptions,
|
|
650
|
+
storedPositions: stored
|
|
651
|
+
});
|
|
652
|
+
const nextPositions = /* @__PURE__ */ new Map();
|
|
653
|
+
const transitions = [];
|
|
654
|
+
for (const node of layouted) {
|
|
655
|
+
const target = node.position;
|
|
656
|
+
nextPositions.set(node.id, target);
|
|
657
|
+
const current = currentPositions.get(node.id) ?? target;
|
|
658
|
+
const distance = Math.hypot(target.x - current.x, target.y - current.y);
|
|
659
|
+
if (distance > 2) {
|
|
660
|
+
transitions.push({
|
|
661
|
+
node,
|
|
662
|
+
from: current,
|
|
663
|
+
to: target,
|
|
664
|
+
trajectory: linearPositionTrajectory,
|
|
665
|
+
duration: this.glideDurationMs
|
|
666
|
+
});
|
|
667
|
+
}
|
|
668
|
+
}
|
|
669
|
+
this.targetPositions = nextPositions;
|
|
670
|
+
if (transitions.length > 0) {
|
|
671
|
+
this.activeTransitionCancel = runMultiNodeTransition(transitions, {
|
|
672
|
+
onUpdate: () => {
|
|
673
|
+
const liveMap = new Map(currentPositions);
|
|
674
|
+
for (const trans of transitions) {
|
|
675
|
+
liveMap.set(trans.node.id, { ...trans.node.position });
|
|
676
|
+
}
|
|
677
|
+
this.onLayoutUpdated?.(liveMap);
|
|
678
|
+
},
|
|
679
|
+
onComplete: () => {
|
|
680
|
+
this.activeTransitionCancel = null;
|
|
681
|
+
this.onLayoutUpdated?.(nextPositions);
|
|
682
|
+
}
|
|
683
|
+
});
|
|
684
|
+
} else {
|
|
685
|
+
this.onLayoutUpdated?.(nextPositions);
|
|
686
|
+
}
|
|
687
|
+
}
|
|
688
|
+
destroy() {
|
|
689
|
+
if (this.pendingTimer) {
|
|
690
|
+
clearTimeout(this.pendingTimer);
|
|
691
|
+
this.pendingTimer = null;
|
|
692
|
+
}
|
|
693
|
+
if (this.activeTransitionCancel) {
|
|
694
|
+
this.activeTransitionCancel();
|
|
695
|
+
this.activeTransitionCancel = null;
|
|
696
|
+
}
|
|
697
|
+
}
|
|
698
|
+
};
|
|
699
|
+
export {
|
|
700
|
+
DEFAULT_NODE_HEIGHT,
|
|
701
|
+
DEFAULT_NODE_WIDTH,
|
|
702
|
+
DEFAULT_PLACEMENT_GAP,
|
|
703
|
+
StreamingLayoutManager,
|
|
704
|
+
boundsOf,
|
|
705
|
+
calculateDagreReflowDisplacement,
|
|
706
|
+
calculateDirectionalDisplacement,
|
|
707
|
+
centerNodes,
|
|
708
|
+
centre,
|
|
709
|
+
computeNodesBoundingBox,
|
|
710
|
+
createBezierTrajectory,
|
|
711
|
+
createFanOutTrajectory,
|
|
712
|
+
defaultEasing,
|
|
713
|
+
ensureLayout,
|
|
714
|
+
estimateNodeSize,
|
|
715
|
+
flowSide,
|
|
716
|
+
getCenteredNodePosition,
|
|
717
|
+
getLayoutedNodes,
|
|
718
|
+
isEventFromTextInput,
|
|
719
|
+
isValidCanvasNode,
|
|
720
|
+
isValidUrl,
|
|
721
|
+
linearPositionTrajectory,
|
|
722
|
+
linearTrajectory,
|
|
723
|
+
overlaps,
|
|
724
|
+
parseCanvasClipboardData,
|
|
725
|
+
placeBeside,
|
|
726
|
+
remapPastedNodes,
|
|
727
|
+
repositionNodesTo,
|
|
728
|
+
runMultiNodeTransition,
|
|
729
|
+
serializeCanvasNodes
|
|
730
|
+
};
|
|
731
|
+
//# sourceMappingURL=index.js.map
|