@plait/graph-viz 0.62.0-next.7

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.
Files changed (40) hide show
  1. package/README.md +4 -0
  2. package/constants/default.d.ts +2 -0
  3. package/esm2022/constants/default.mjs +5 -0
  4. package/esm2022/force-atlas/constants.mjs +28 -0
  5. package/esm2022/force-atlas/edge.flavour.mjs +24 -0
  6. package/esm2022/force-atlas/force-atlas.flavour.mjs +51 -0
  7. package/esm2022/force-atlas/generators/edge.generator.mjs +29 -0
  8. package/esm2022/force-atlas/generators/node.generator.mjs +15 -0
  9. package/esm2022/force-atlas/node.flavour.mjs +55 -0
  10. package/esm2022/force-atlas/types.mjs +7 -0
  11. package/esm2022/force-atlas/utils/draw.mjs +70 -0
  12. package/esm2022/force-atlas/utils/edge.mjs +72 -0
  13. package/esm2022/force-atlas/utils/node.mjs +46 -0
  14. package/esm2022/force-atlas/with-force-atlas.mjs +70 -0
  15. package/esm2022/interfaces/element.mjs +12 -0
  16. package/esm2022/interfaces/index.mjs +2 -0
  17. package/esm2022/perfect-arrows/get-arrow.mjs +91 -0
  18. package/esm2022/perfect-arrows/utils.mjs +111 -0
  19. package/esm2022/plait-graph-viz.mjs +5 -0
  20. package/esm2022/public-api.mjs +8 -0
  21. package/fesm2022/plait-graph-viz.mjs +655 -0
  22. package/fesm2022/plait-graph-viz.mjs.map +1 -0
  23. package/force-atlas/constants.d.ts +19 -0
  24. package/force-atlas/edge.flavour.d.ts +15 -0
  25. package/force-atlas/force-atlas.flavour.d.ts +13 -0
  26. package/force-atlas/generators/edge.generator.d.ts +15 -0
  27. package/force-atlas/generators/node.generator.d.ts +10 -0
  28. package/force-atlas/node.flavour.d.ts +15 -0
  29. package/force-atlas/types.d.ts +28 -0
  30. package/force-atlas/utils/draw.d.ts +12 -0
  31. package/force-atlas/utils/edge.d.ts +14 -0
  32. package/force-atlas/utils/node.d.ts +10 -0
  33. package/force-atlas/with-force-atlas.d.ts +2 -0
  34. package/index.d.ts +5 -0
  35. package/interfaces/element.d.ts +19 -0
  36. package/interfaces/index.d.ts +1 -0
  37. package/package.json +32 -0
  38. package/perfect-arrows/get-arrow.d.ts +42 -0
  39. package/perfect-arrows/utils.d.ts +64 -0
  40. package/public-api.d.ts +4 -0
@@ -0,0 +1,655 @@
1
+ import { cacheSelectedElements, PlaitBoard, normalizePoint, drawCircle, NS, createG, createPath, RectangleClient, PlaitElement, PlaitNode, getSelectedElements, PlaitPluginKey } from '@plait/core';
2
+ import { CommonElementFlavour, Generator, animate, linear } from '@plait/common';
3
+ import Graph from 'graphology';
4
+ import circular from 'graphology-layout/circular';
5
+ import forceAtlas2 from 'graphology-layout-forceatlas2';
6
+
7
+ const DEFAULT_STYLES = {
8
+ fillStyle: 'solid',
9
+ strokeWidth: 1
10
+ };
11
+
12
+ var EdgeDirection;
13
+ (function (EdgeDirection) {
14
+ EdgeDirection[EdgeDirection["IN"] = 0] = "IN";
15
+ EdgeDirection[EdgeDirection["OUT"] = 1] = "OUT";
16
+ EdgeDirection[EdgeDirection["NONE"] = 2] = "NONE";
17
+ })(EdgeDirection || (EdgeDirection = {}));
18
+
19
+ const DEFAULT_EDGE_STYLES = {
20
+ ...DEFAULT_STYLES,
21
+ stroke: '#ddd'
22
+ };
23
+ const DEFAULT_NODE_SIZE = 60;
24
+ const DEFAULT_ACTIVE_NODE_SIZE_MULTIPLIER = 1.2;
25
+ const DEFAULT_ACTIVE_WAVE_NODE_SIZE_MULTIPLIER = 1.6;
26
+ const DEFAULT_NODE_LABEL_MARGIN_TOP = 4;
27
+ const DEFAULT_NODE_LABEL_FONT_SIZE = 12;
28
+ const SECOND_DEPTH_NODE_ALPHA = 0.5;
29
+ const SECOND_DEPTH_LINE_ALPHA = 0.5;
30
+ const ACTIVE_BACKGROUND_NODE_ALPHA = 0.1;
31
+ const DEFAULT_NODE_STYLES = {
32
+ ...DEFAULT_STYLES,
33
+ fill: '#6698FF',
34
+ strokeWidth: 0
35
+ };
36
+ const DEFAULT_NODE_SCALING_RATIO = 600;
37
+ const DEFAULT_LINE_STYLES = {
38
+ color: {
39
+ [EdgeDirection.IN]: '#73D897',
40
+ [EdgeDirection.OUT]: '#6698FF',
41
+ [EdgeDirection.NONE]: `#ddd`
42
+ }
43
+ };
44
+
45
+ const ForceAtlasElement = {
46
+ isForceAtlas: (value) => {
47
+ return value?.type === 'force-atlas';
48
+ },
49
+ isForceAtlasNodeElement: (value) => {
50
+ return value && value.label && value.icon;
51
+ },
52
+ isForceAtlasEdgeElement: (value) => {
53
+ return value && value.source && value.target;
54
+ }
55
+ };
56
+
57
+ class ForceAtlasFlavour extends CommonElementFlavour {
58
+ constructor() {
59
+ super();
60
+ }
61
+ initializeGraph() {
62
+ this.graph = new Graph();
63
+ this.element.children?.forEach(child => {
64
+ if (ForceAtlasElement.isForceAtlasNodeElement(child)) {
65
+ if (typeof child?.size === 'undefined') {
66
+ child.size = DEFAULT_NODE_SIZE;
67
+ }
68
+ if (child.isActive) {
69
+ cacheSelectedElements(this.board, [child]);
70
+ }
71
+ this.graph.addNode(child.id, child);
72
+ }
73
+ else if (ForceAtlasElement.isForceAtlasEdgeElement(child)) {
74
+ this.graph.addEdge(child.source, child.target);
75
+ }
76
+ });
77
+ circular.assign(this.graph);
78
+ const settings = forceAtlas2.inferSettings(this.graph);
79
+ settings.adjustSizes = true;
80
+ settings.scalingRatio = DEFAULT_NODE_SCALING_RATIO;
81
+ settings.barnesHutOptimize = true;
82
+ const positions = forceAtlas2(this.graph, { iterations: 500, settings });
83
+ this.element.children?.forEach(child => {
84
+ if (ForceAtlasElement.isForceAtlasNodeElement(child)) {
85
+ const pos = positions[child.id];
86
+ child.points = [[pos.x, pos.y]];
87
+ }
88
+ });
89
+ }
90
+ initialize() {
91
+ super.initialize();
92
+ this.initializeGraph();
93
+ }
94
+ onContextChanged(value, previous) { }
95
+ updateText(previousElement, currentElement) { }
96
+ destroy() {
97
+ super.destroy();
98
+ }
99
+ }
100
+
101
+ // Credits to perfect-arrows
102
+ // https://github.com/steveruizok/perfect-arrows/blob/master/src/lib/utils.ts
103
+ const PI = Math.PI;
104
+ /**
105
+ * Modulate a value between two ranges.
106
+ * @param value
107
+ * @param rangeA from [low, high]
108
+ * @param rangeB to [low, high]
109
+ * @param clamp
110
+ */
111
+ function modulate(value, rangeA, rangeB, clamp = false) {
112
+ const [fromLow, fromHigh] = rangeA;
113
+ const [toLow, toHigh] = rangeB;
114
+ const result = toLow + ((value - fromLow) / (fromHigh - fromLow)) * (toHigh - toLow);
115
+ if (clamp === true) {
116
+ if (toLow < toHigh) {
117
+ if (result < toLow) {
118
+ return toLow;
119
+ }
120
+ if (result > toHigh) {
121
+ return toHigh;
122
+ }
123
+ }
124
+ else {
125
+ if (result > toLow) {
126
+ return toLow;
127
+ }
128
+ if (result < toHigh) {
129
+ return toHigh;
130
+ }
131
+ }
132
+ }
133
+ return result;
134
+ }
135
+ /**
136
+ * Rotate a point around a center.
137
+ * @param x The x-axis coordinate of the point.
138
+ * @param y The y-axis coordinate of the point.
139
+ * @param cx The x-axis coordinate of the point to rotate round.
140
+ * @param cy The y-axis coordinate of the point to rotate round.
141
+ * @param angle The distance (in radians) to rotate.
142
+ */
143
+ function rotatePoint(x, y, cx, cy, angle) {
144
+ const s = Math.sin(angle);
145
+ const c = Math.cos(angle);
146
+ const px = x - cx;
147
+ const py = y - cy;
148
+ const nx = px * c - py * s;
149
+ const ny = px * s + py * c;
150
+ return [nx + cx, ny + cy];
151
+ }
152
+ /**
153
+ * Get the distance between two points.
154
+ * @param x0 The x-axis coordinate of the first point.
155
+ * @param y0 The y-axis coordinate of the first point.
156
+ * @param x1 The x-axis coordinate of the second point.
157
+ * @param y1 The y-axis coordinate of the second point.
158
+ */
159
+ function getDistance(x0, y0, x1, y1) {
160
+ return Math.hypot(y1 - y0, x1 - x0);
161
+ }
162
+ /**
163
+ * Get an angle (radians) between two points.
164
+ * @param x0 The x-axis coordinate of the first point.
165
+ * @param y0 The y-axis coordinate of the first point.
166
+ * @param x1 The x-axis coordinate of the second point.
167
+ * @param y1 The y-axis coordinate of the second point.
168
+ */
169
+ function getAngle(x0, y0, x1, y1) {
170
+ return Math.atan2(y1 - y0, x1 - x0);
171
+ }
172
+ /**
173
+ * Move a point in an angle by a distance.
174
+ * @param x0
175
+ * @param y0
176
+ * @param a angle (radians)
177
+ * @param d distance
178
+ */
179
+ function projectPoint(x0, y0, a, d) {
180
+ return [Math.cos(a) * d + x0, Math.sin(a) * d + y0];
181
+ }
182
+ /**
183
+ * Get a point between two points.
184
+ * @param x0 The x-axis coordinate of the first point.
185
+ * @param y0 The y-axis coordinate of the first point.
186
+ * @param x1 The x-axis coordinate of the second point.
187
+ * @param y1 The y-axis coordinate of the second point.
188
+ * @param d Normalized
189
+ */
190
+ function getPointBetween(x0, y0, x1, y1, d = 0.5) {
191
+ return [x0 + (x1 - x0) * d, y0 + (y1 - y0) * d];
192
+ }
193
+ /**
194
+ * Get the sector of an angle (e.g. quadrant, octant)
195
+ * @param a The angle to check.
196
+ * @param s The number of sectors to check.
197
+ */
198
+ function getSector(a, s = 8) {
199
+ return Math.floor(s * (0.5 + ((a / (PI * 2)) % s)));
200
+ }
201
+ /**
202
+ * Get a normal value representing how close two points are from being at a 45 degree angle.
203
+ * @param x0 The x-axis coordinate of the first point.
204
+ * @param y0 The y-axis coordinate of the first point.
205
+ * @param x1 The x-axis coordinate of the second point.
206
+ * @param y1 The y-axis coordinate of the second point.
207
+ */
208
+ function getAngliness(x0, y0, x1, y1) {
209
+ return Math.abs((x1 - x0) / 2 / ((y1 - y0) / 2));
210
+ }
211
+
212
+ // Credits to perfect-arrows
213
+ // https://github.com/steveruizok/perfect-arrows/blob/master/src/lib/getArrow.ts
214
+ /**
215
+ * getArrow
216
+ * Get the points for a linking line between two points.
217
+ * @description Draw an arrow between two points.
218
+ * @param x0 The x position of the "from" point.
219
+ * @param y0 The y position of the "from" point.
220
+ * @param x1 The x position of the "to" point.
221
+ * @param y1 The y position of the "to" point.
222
+ * @param options Additional options for computing the line.
223
+ * @returns [sx, sy, cx, cy, e1, e2, ae, as, ac]
224
+ * @example
225
+ * const arrow = getArrow(0, 0, 100, 200, {
226
+ bow: 0
227
+ stretch: .5
228
+ stretchMin: 0
229
+ stretchMax: 420
230
+ padStart: 0
231
+ padEnd: 0
232
+ flip: false
233
+ straights: true
234
+ * })
235
+ *
236
+ * const [
237
+ * startX, startY,
238
+ * controlX, controlY,
239
+ * endX, endY,
240
+ * endAngle,
241
+ * startAngle,
242
+ * controlAngle
243
+ * ] = arrow
244
+ */
245
+ function getArrow(x0, y0, x1, y1, options = {}) {
246
+ const { bow = 0, stretch = 0.5, stretchMin = 0, stretchMax = 420, padStart = 0, padEnd = 0, flip = false, straights = true } = options;
247
+ const angle = getAngle(x0, y0, x1, y1);
248
+ const dist = getDistance(x0, y0, x1, y1);
249
+ const angliness = getAngliness(x0, y0, x1, y1);
250
+ // Step 0 ⤜⤏ Should the arrow be straight?
251
+ if (dist < (padStart + padEnd) * 2 || // Too short
252
+ (bow === 0 && stretch === 0) || // No bow, no stretch
253
+ (straights && [0, 1, Infinity].includes(angliness)) // 45 degree angle
254
+ ) {
255
+ // ⤜⤏ Arrow is straight! Just pad start and end points.
256
+ // Padding distances
257
+ const ps = Math.max(0, Math.min(dist - padStart, padStart));
258
+ const pe = Math.max(0, Math.min(dist - ps, padEnd));
259
+ // Move start point toward end point
260
+ let [px0, py0] = projectPoint(x0, y0, angle, ps);
261
+ // Move end point toward start point
262
+ let [px1, py1] = projectPoint(x1, y1, angle + Math.PI, pe);
263
+ // Get midpoint between new points
264
+ const [mx, my] = getPointBetween(px0, py0, px1, py1, 0.5);
265
+ return [px0, py0, mx, my, px1, py1, angle, angle, angle];
266
+ }
267
+ // ⤜⤏ Arrow is an arc!
268
+ // Is the arc clockwise or counterclockwise?
269
+ let rot = (getSector(angle) % 2 === 0 ? 1 : -1) * (flip ? -1 : 1);
270
+ // Calculate how much the line should "bow" away from center
271
+ const arc = bow + modulate(dist, [stretchMin, stretchMax], [1, 0], true) * stretch;
272
+ // Step 1 ⤜⤏ Find padded points.
273
+ // Get midpoint.
274
+ const [mx, my] = getPointBetween(x0, y0, x1, y1, 0.5);
275
+ // Get control point.
276
+ let [cx, cy] = getPointBetween(x0, y0, x1, y1, 0.5 - arc);
277
+ // Rotate control point (clockwise or counterclockwise).
278
+ [cx, cy] = rotatePoint(cx, cy, mx, my, (Math.PI / 2) * rot);
279
+ // Get padded start point.
280
+ const a0 = getAngle(x0, y0, cx, cy);
281
+ const [px0, py0] = projectPoint(x0, y0, a0, padStart);
282
+ // Get padded end point.
283
+ const a1 = getAngle(x1, y1, cx, cy);
284
+ const [px1, py1] = projectPoint(x1, y1, a1, padEnd);
285
+ // Step 2 ⤜⤏ Find start and end angles.
286
+ // Start angle
287
+ const as = getAngle(cx, cy, x0, y0);
288
+ // End angle
289
+ const ae = getAngle(cx, cy, x1, y1);
290
+ // Step 3 ⤜⤏ Find control point for padded points.
291
+ // Get midpoint between padded start / end points.
292
+ const [mx1, my1] = getPointBetween(px0, py0, px1, py1, 0.5);
293
+ // Get control point for padded start / end points.
294
+ let [cx1, cy1] = getPointBetween(px0, py0, px1, py1, 0.5 - arc);
295
+ // Rotate control point (clockwise or counterclockwise).
296
+ [cx1, cy1] = rotatePoint(cx1, cy1, mx1, my1, (Math.PI / 2) * rot);
297
+ // Finally, average the two control points.
298
+ let [cx2, cy2] = getPointBetween(cx, cy, cx1, cy1, 0.5);
299
+ return [px0, py0, cx2, cy2, px1, py1, ae, as, angle];
300
+ }
301
+
302
+ function drawNode(board, node, point, options) {
303
+ const roughSVG = PlaitBoard.getRoughSVG(board);
304
+ let nodeStyles = {
305
+ ...DEFAULT_NODE_STYLES,
306
+ ...(node.styles || {})
307
+ };
308
+ let { x, y } = normalizePoint(point);
309
+ let nodeRadius = (node.size ?? DEFAULT_NODE_SIZE) / 2;
310
+ if (options.isActive) {
311
+ nodeRadius = nodeRadius * DEFAULT_ACTIVE_NODE_SIZE_MULTIPLIER;
312
+ }
313
+ const nodeG = drawCircle(roughSVG, [0, 0], nodeRadius, nodeStyles);
314
+ nodeG.setAttribute('transform', `translate(${x}, ${y})`);
315
+ const text = document.createElementNS(NS, 'text');
316
+ text.textContent = node.label || '';
317
+ text.setAttribute('text-anchor', `middle`);
318
+ text.setAttribute('dominant-baseline', `hanging`);
319
+ text.setAttribute('x', `0`);
320
+ text.setAttribute('font-size', `${DEFAULT_NODE_LABEL_FONT_SIZE}px`);
321
+ if (options.isActive) {
322
+ const waveRadius = nodeRadius * DEFAULT_ACTIVE_WAVE_NODE_SIZE_MULTIPLIER;
323
+ const waveCircle = drawCircle(roughSVG, [0, 0], waveRadius, DEFAULT_NODE_STYLES);
324
+ waveCircle.setAttribute('opacity', ACTIVE_BACKGROUND_NODE_ALPHA.toString());
325
+ nodeG.append(waveCircle);
326
+ text.setAttribute('y', `${waveRadius / 2 + DEFAULT_NODE_LABEL_MARGIN_TOP}`);
327
+ }
328
+ else {
329
+ if (!options.isFirstDepth) {
330
+ nodeG.setAttribute('opacity', SECOND_DEPTH_NODE_ALPHA.toString());
331
+ }
332
+ text.setAttribute('y', `${nodeRadius / 2 + DEFAULT_NODE_LABEL_MARGIN_TOP}`);
333
+ }
334
+ nodeG.append(text);
335
+ return nodeG;
336
+ }
337
+ function drawEdge(startPoint, endPoint, direction, isMutual) {
338
+ const arrow = getArrow(startPoint[0], startPoint[1], endPoint[0], endPoint[1], {
339
+ stretch: 0.4,
340
+ flip: direction === EdgeDirection.NONE ? false : isMutual,
341
+ padEnd: DEFAULT_NODE_SIZE / 4,
342
+ padStart: DEFAULT_NODE_SIZE / 4
343
+ });
344
+ const [sx, sy, cx, cy, ex, ey, ae, as, ec] = arrow;
345
+ const g = createG();
346
+ const path = createPath();
347
+ path.setAttribute('d', `M${sx},${sy} Q${cx},${cy} ${ex},${ey}`);
348
+ path.setAttribute('fill', 'none');
349
+ path.setAttribute('stroke', DEFAULT_LINE_STYLES.color[direction]);
350
+ g.append(path);
351
+ return {
352
+ g,
353
+ path
354
+ };
355
+ }
356
+ function drawParticle(board, startPoint, direction) {
357
+ const roughSVG = PlaitBoard.getRoughSVG(board);
358
+ const pointG = drawCircle(roughSVG, [0, 0], 5, {
359
+ ...DEFAULT_STYLES,
360
+ strokeWidth: 0,
361
+ fill: DEFAULT_LINE_STYLES.color[direction]
362
+ });
363
+ pointG.setAttribute('transform', `translate(${startPoint[0]}, ${startPoint[1]})`);
364
+ return pointG;
365
+ }
366
+
367
+ class ForceAtlasNodeGenerator extends Generator {
368
+ static { this.key = 'force-atlas-node'; }
369
+ constructor(board) {
370
+ super(board);
371
+ }
372
+ canDraw(element) {
373
+ return true;
374
+ }
375
+ draw(element, data) {
376
+ return drawNode(this.board, element, element?.points?.[0] || [0, 0], data);
377
+ }
378
+ }
379
+
380
+ class ForceAtlasEdgeGenerator extends Generator {
381
+ static { this.key = 'force-atlas-edge'; }
382
+ constructor(board) {
383
+ super(board);
384
+ }
385
+ canDraw(element) {
386
+ return true;
387
+ }
388
+ draw(element, data) {
389
+ const edgeG = createG();
390
+ const edgeElement = drawEdge(data.startPoint, data.endPoint, data.direction, data.isSourceActive && data.isTargetActive);
391
+ edgeG.append(edgeElement.g);
392
+ if (data.direction !== EdgeDirection.NONE) {
393
+ const particle = drawParticle(this.board, data.startPoint, data.direction);
394
+ edgeElement.g.append(particle);
395
+ this.particleAnimation = playEdgeParticleAnimate(edgeElement.path, particle);
396
+ }
397
+ return edgeG;
398
+ }
399
+ destroy() {
400
+ this.particleAnimation?.stop();
401
+ }
402
+ }
403
+
404
+ function getNodes(forceAtlasElement, andBack) {
405
+ return forceAtlasElement.children?.filter(f => ForceAtlasElement.isForceAtlasNodeElement(f) && (andBack?.(f) ?? true));
406
+ }
407
+ function getNodeById(id, forceAtlasElement) {
408
+ const node = getNodes(forceAtlasElement, node => node.id === id)?.[0];
409
+ if (!node) {
410
+ throw new Error('can not find node.');
411
+ }
412
+ return node;
413
+ }
414
+ function getIsNodeActive(id, selectElements) {
415
+ return selectElements.some(node => node.id === id);
416
+ }
417
+ function isHitNode(node, point) {
418
+ const { x, y } = normalizePoint(node.points[0]);
419
+ const size = node.size;
420
+ const hitFlowNode = RectangleClient.isHit(RectangleClient.getRectangleByPoints(point), {
421
+ x: x - size / 2,
422
+ y: y - size / 2,
423
+ width: size,
424
+ height: size
425
+ });
426
+ return hitFlowNode;
427
+ }
428
+ function getAssociatedNodesById(id, forceAtlasElement) {
429
+ const edges = getEdgesInSourceOrTarget(id, forceAtlasElement);
430
+ const nodes = [];
431
+ edges.forEach(edge => {
432
+ nodes.push(getNodeById(edge.source, forceAtlasElement));
433
+ nodes.push(getNodeById(edge.target, forceAtlasElement));
434
+ });
435
+ return nodes;
436
+ }
437
+ function getNodeGenerator(node) {
438
+ const edgeRef = PlaitElement.getElementRef(node);
439
+ return edgeRef.getGenerator(ForceAtlasNodeGenerator.key);
440
+ }
441
+ function isFirstDepthNode(currentNodeId, activeNodeId, forceAtlasElement) {
442
+ const edges = getEdges(forceAtlasElement);
443
+ return edges.some(s => (s.source === activeNodeId && s.target === currentNodeId) || (s.target === activeNodeId && s.source === currentNodeId));
444
+ }
445
+
446
+ function getEdges(forceAtlasElement, andCallBack) {
447
+ return forceAtlasElement.children?.filter(f => ForceAtlasElement.isForceAtlasEdgeElement(f) && (andCallBack?.(f) ?? true));
448
+ }
449
+ function getEdgeById(id, forceAtlasElement) {
450
+ const edge = getEdges(forceAtlasElement, e => e.id === id)?.[0];
451
+ if (!edge) {
452
+ throw new Error('can not find edge.');
453
+ }
454
+ return edge;
455
+ }
456
+ function getEdgesInSourceOrTarget(id, forceAtlasElement) {
457
+ const edges = getEdges(forceAtlasElement, edge => edge.source === id || edge.target === id);
458
+ return edges;
459
+ }
460
+ function getEdgeGenerator(edge) {
461
+ const edgeRef = PlaitElement.getElementRef(edge);
462
+ return edgeRef.getGenerator(ForceAtlasEdgeGenerator.key);
463
+ }
464
+ function getEdgeDirection(isSourceActive, isTargetActive) {
465
+ if (isSourceActive) {
466
+ return EdgeDirection.OUT;
467
+ }
468
+ else if (isTargetActive) {
469
+ return EdgeDirection.IN;
470
+ }
471
+ return EdgeDirection.NONE;
472
+ }
473
+ function getEdgeGeneratorData(edge, board) {
474
+ const forceAtlasElement = PlaitNode.parent(board, PlaitBoard.findPath(board, edge));
475
+ const sourceNode = getNodeById(edge.source, forceAtlasElement);
476
+ const targetNode = getNodeById(edge.target, forceAtlasElement);
477
+ if (!sourceNode?.points || !targetNode?.points) {
478
+ throw new Error("Source or target node doesn't have points");
479
+ }
480
+ const startPoint = sourceNode.points[0];
481
+ const endPoint = targetNode.points[0];
482
+ const selectElements = getSelectedElements(board);
483
+ const isSourceActive = getIsNodeActive(sourceNode.id, selectElements);
484
+ const isTargetActive = getIsNodeActive(targetNode.id, selectElements);
485
+ const direction = getEdgeDirection(isSourceActive, isTargetActive);
486
+ return {
487
+ startPoint,
488
+ endPoint,
489
+ direction,
490
+ isSourceActive,
491
+ isTargetActive
492
+ };
493
+ }
494
+ function playEdgeParticleAnimate(path, pointG) {
495
+ const pathLength = path.getTotalLength();
496
+ let anim = animate((t) => {
497
+ const point = path.getPointAtLength(t * pathLength);
498
+ pointG.setAttribute('transform', `translate(${point.x}, ${point.y})`);
499
+ }, 1000, linear, () => {
500
+ anim = playEdgeParticleAnimate(path, pointG);
501
+ });
502
+ return {
503
+ stop: () => {
504
+ anim.stop();
505
+ },
506
+ start: () => {
507
+ anim.start();
508
+ }
509
+ };
510
+ }
511
+
512
+ class ForceAtlasNodeFlavour extends CommonElementFlavour {
513
+ constructor() {
514
+ super();
515
+ }
516
+ initializeGenerator() {
517
+ this.nodeGenerator = new ForceAtlasNodeGenerator(this.board);
518
+ this.getRef().addGenerator(ForceAtlasNodeGenerator.key, this.nodeGenerator);
519
+ }
520
+ initialize() {
521
+ super.initialize();
522
+ this.initializeGenerator();
523
+ const parent = PlaitNode.parent(this.board, PlaitBoard.findPath(this.board, this.element));
524
+ const selectElements = getSelectedElements(this.board);
525
+ const activeNodeId = selectElements[0]?.id;
526
+ const isActive = activeNodeId === this.element.id;
527
+ this.nodeGenerator.processDrawing(this.element, isActive ? PlaitBoard.getElementActiveHost(this.board) : this.getElementG(), {
528
+ isActive,
529
+ isFirstDepth: isFirstDepthNode(this.element.id, activeNodeId, parent)
530
+ });
531
+ }
532
+ onContextChanged(value, previous) {
533
+ if (value !== previous && value.selected !== previous.selected) {
534
+ const parent = value.parent;
535
+ if (value.selected) {
536
+ cacheSelectedElements(this.board, [value.element]);
537
+ }
538
+ const selectElements = getSelectedElements(this.board);
539
+ const associatedNodes = getAssociatedNodesById(value.element.id, parent);
540
+ associatedNodes.forEach(node => {
541
+ const nodeGenerator = getNodeGenerator(node);
542
+ nodeGenerator.destroy();
543
+ nodeGenerator.processDrawing(node, this.getElementG(), {
544
+ isActive: selectElements?.[0]?.id === node.id,
545
+ isFirstDepth: selectElements.length > 0 && isFirstDepthNode(node.id, selectElements[0].id, parent)
546
+ });
547
+ });
548
+ const associatedEdges = getEdgesInSourceOrTarget(value.element.id, parent);
549
+ associatedEdges.forEach(edge => {
550
+ const edgeGenerator = getEdgeGenerator(edge);
551
+ edgeGenerator.destroy();
552
+ edgeGenerator.processDrawing(edge, PlaitBoard.getElementLowerHost(this.board), getEdgeGeneratorData(edge, this.board));
553
+ });
554
+ }
555
+ }
556
+ updateText(previousElement, currentElement) { }
557
+ destroy() {
558
+ super.destroy();
559
+ }
560
+ }
561
+
562
+ class ForceAtlasEdgeFlavour extends CommonElementFlavour {
563
+ constructor() {
564
+ super();
565
+ }
566
+ initializeGenerator() {
567
+ this.edgeGenerator = new ForceAtlasEdgeGenerator(this.board);
568
+ this.getRef().addGenerator(ForceAtlasEdgeGenerator.key, this.edgeGenerator);
569
+ }
570
+ initialize() {
571
+ super.initialize();
572
+ this.initializeGenerator();
573
+ this.edgeGenerator.processDrawing(this.element, PlaitBoard.getElementLowerHost(this.board), getEdgeGeneratorData(this.element, this.board));
574
+ }
575
+ onContextChanged(value, previous) { }
576
+ updateText(previousElement, currentElement) { }
577
+ destroy() {
578
+ super.destroy();
579
+ }
580
+ }
581
+
582
+ const withForceAtlas = (board) => {
583
+ const { drawElement, getRectangle, isRectangleHit, isHit, isInsidePoint, isMovable, isAlign, getRelatedFragment } = board;
584
+ board.drawElement = (context) => {
585
+ if (ForceAtlasElement.isForceAtlas(context.element)) {
586
+ return ForceAtlasFlavour;
587
+ }
588
+ else if (ForceAtlasElement.isForceAtlasNodeElement(context.element)) {
589
+ return ForceAtlasNodeFlavour;
590
+ }
591
+ else if (ForceAtlasElement.isForceAtlasEdgeElement(context.element)) {
592
+ return ForceAtlasEdgeFlavour;
593
+ }
594
+ return drawElement(context);
595
+ };
596
+ board.getRectangle = (element) => {
597
+ if (element.type === 'force-atlas') {
598
+ return {
599
+ width: 0,
600
+ height: 0,
601
+ x: 0,
602
+ y: 0
603
+ };
604
+ }
605
+ else if (ForceAtlasElement.isForceAtlasNodeElement(element)) {
606
+ return RectangleClient.getRectangleByPoints(element.points || []);
607
+ }
608
+ else if (ForceAtlasElement.isForceAtlasEdgeElement(element)) {
609
+ return {
610
+ width: 0,
611
+ height: 0,
612
+ x: 0,
613
+ y: 0
614
+ };
615
+ }
616
+ return getRectangle(element);
617
+ };
618
+ board.isRectangleHit = (element, selection) => {
619
+ return isRectangleHit(element, selection);
620
+ };
621
+ board.isRectangleHit = (element, range) => {
622
+ if (ForceAtlasElement.isForceAtlasNodeElement(element)) {
623
+ return isHitNode(element, [range.anchor, range.focus]);
624
+ }
625
+ return isRectangleHit(element, range);
626
+ };
627
+ board.isHit = (element, point) => {
628
+ if (ForceAtlasElement.isForceAtlasNodeElement(element)) {
629
+ return isHitNode(element, [point, point]);
630
+ }
631
+ return isHit(element, point);
632
+ };
633
+ board.isInsidePoint = (element, point) => {
634
+ return isInsidePoint(element, point);
635
+ };
636
+ board.isMovable = element => {
637
+ if (ForceAtlasElement.isForceAtlasNodeElement(element)) {
638
+ return true;
639
+ }
640
+ return isMovable(element);
641
+ };
642
+ board.setPluginOptions(PlaitPluginKey.withSelection, { isMultiple: false });
643
+ return board;
644
+ };
645
+
646
+ /*
647
+ * Public API Surface of utils
648
+ */
649
+
650
+ /**
651
+ * Generated bundle index. Do not edit.
652
+ */
653
+
654
+ export { ACTIVE_BACKGROUND_NODE_ALPHA, DEFAULT_ACTIVE_NODE_SIZE_MULTIPLIER, DEFAULT_ACTIVE_WAVE_NODE_SIZE_MULTIPLIER, DEFAULT_EDGE_STYLES, DEFAULT_LINE_STYLES, DEFAULT_NODE_LABEL_FONT_SIZE, DEFAULT_NODE_LABEL_MARGIN_TOP, DEFAULT_NODE_SCALING_RATIO, DEFAULT_NODE_SIZE, DEFAULT_NODE_STYLES, EdgeDirection, ForceAtlasElement, SECOND_DEPTH_LINE_ALPHA, SECOND_DEPTH_NODE_ALPHA, withForceAtlas };
655
+ //# sourceMappingURL=plait-graph-viz.mjs.map