@affino/diagram-core 0.10.1 → 0.10.2

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 CHANGED
@@ -109,7 +109,7 @@ Holding Shift during a resize preserves the original aspect ratio. The modifier
109
109
 
110
110
  Selected edge endpoints use `beginEdgeEndpointHandle(id, endpoint, event)`. The controller publishes an `edgeEndpointPreview` on scheduled frames and `getPreviewGeometry(id)` returns the previewed route without changing the scene. Pointer up commits one undoable `moveEdgeEndpoint`; cancel discards the preview.
111
111
 
112
- Holding Shift while dragging an endpoint constrains it to the dominant axis measured from its original position (horizontal wins an exact tie). After the pointer moves 4 screen pixels, the axis stays latched until Shift is released; pressing Shift again chooses a new axis from the current gesture direction. Port, alignment, and grid snapping preserve that axis. The same latch behavior applies to selection drags. Preview and final command use the same snapped geometry.
112
+ Holding Shift while dragging an endpoint projects the vector from the stationary opposite endpoint to the nearest cardinal angle (0°, 90°, 180°, or 270°), preserving its length. After the pointer moves 4 screen pixels, the angle stays latched until Shift is released; pressing Shift again chooses a new angle from the current pointer position relative to the stationary endpoint. Port, alignment, and grid snapping preserve the cardinal angle. Orthogonal routes remain orthogonal as the endpoint-adjacent waypoint adjusts. Preview and final command use the same geometry. Selection drags retain their dominant-axis latch behavior.
113
113
 
114
114
  Selection drags snap by default to ports, alignments, and equal spacing. Configure grid spacing and screen-space tolerance when creating the controller; the tolerance is converted to world units using the viewport zoom. The `snapGuides` snapshot field contains world-space line segments for active alignment and equal-spacing matches. Set `enabled: false` to disable drag snapping. Moving a selected edge translates free endpoints and route points while preserving node/port endpoint attachments.
115
115
 
@@ -26,7 +26,10 @@ export declare class DiagramInteractionController {
26
26
  private edgeEndpointId;
27
27
  private edgeEndpointKind;
28
28
  private edgeEndpointStart;
29
+ private edgeEndpointOppositeStart;
30
+ private edgeEndpointCardinalAngle;
29
31
  private edgeEndpointPreview;
32
+ private edgeWaypointPreview;
30
33
  private dragSelectionIds;
31
34
  private snapGuides;
32
35
  private framePending;
@@ -57,6 +60,8 @@ export declare class DiagramInteractionController {
57
60
  private createResizePreview;
58
61
  private resizeGridSize;
59
62
  private updateShiftAxis;
63
+ private isPastAxisActivation;
64
+ private createOrthogonalWaypointPreview;
60
65
  private getMarqueeSelectionIds;
61
66
  private clearGesture;
62
67
  }
@@ -1 +1 @@
1
- {"version":3,"file":"interaction.d.ts","sourceRoot":"","sources":["../src/interaction.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,oBAAoB,CAAA;AACvD,OAAO,KAAK,EAAE,SAAS,EAAE,eAAe,EAAE,6BAA6B,EAAE,0BAA0B,EAAE,sBAAsB,EAAE,kBAAkB,EAAE,mBAAmB,EAAiD,mBAAmB,EAAoB,MAAM,YAAY,CAAA;AAE9Q,KAAK,aAAa,GAAG,CAAC,QAAQ,EAAE,MAAM,IAAI,KAAK,IAAI,CAAA;AAGnD,MAAM,MAAM,yBAAyB,GAAG,QAAQ,CAAC;IAC/C,aAAa,CAAC,EAAE,aAAa,CAAA;IAC7B,WAAW,CAAC,EAAE,kBAAkB,CAAA;IAChC,IAAI,CAAC,EAAE,6BAA6B,CAAA;CACrC,CAAC,CAAA;AAEF,qBAAa,4BAA4B;IA+B3B,OAAO,CAAC,QAAQ,CAAC,MAAM;IA9BnC,OAAO,CAAC,IAAI,CAAmC;IAC/C,OAAO,CAAC,aAAa,CAAmC;IACxD,OAAO,CAAC,UAAU,CAA4B;IAC9C,OAAO,CAAC,WAAW,CAA4B;IAC/C,OAAO,CAAC,cAAc,CAAQ;IAC9B,OAAO,CAAC,SAAS,CAAyB;IAC1C,OAAO,CAAC,YAAY,CAA4B;IAChD,OAAO,CAAC,OAAO,CAA2B;IAC1C,OAAO,CAAC,aAAa,CAAyB;IAC9C,OAAO,CAAC,kBAAkB,CAA+B;IACzD,OAAO,CAAC,YAAY,CAAmC;IACvD,OAAO,CAAC,iBAAiB,CAA2B;IACpD,OAAO,CAAC,mBAAmB,C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mB,GAAG,OAAO;IAmB/G,0BAA0B,CAAC,MAAM,EAAE,mBAAmB,EAAE,KAAK,EAAE,mBAAmB,GAAG,OAAO;IA0B5F,WAAW,CAAC,KAAK,EAAE,mBAAmB,GAAG,IAAI;IAiB7C,SAAS,CAAC,KAAK,EAAE,mBAAmB,GAAG,IAAI;IAiD3C,MAAM,IAAI,IAAI;IAKd,uEAAuE;IACvE,kBAAkB,CAAC,EAAE,EAAE,SAAS,GAAG,eAAe,GAAG,IAAI;IAgDzD,WAAW,IAAI,0BAA0B;IAezC,mBAAmB,IAAI,MAAM;IAI7B,cAAc,IAAI,MAAM;IAIxB,OAAO,CAAC,gBAAgB;IAuExB,OAAO,CAAC,0BAA0B;IAQlC,OAAO,CAAC,mCAAmC;IAqB3C,OAAO,CAAC,mBAAmB;IAoB3B,OAAO,CAAC,cAAc;IAItB,OAAO,CAAC,eAAe;IAyBvB,OAAO,CAAC,oBAAoB;IAM5B,OAAO,CAAC,+BAA+B;IA8BvC,OAAO,CAAC,sBAAsB;IAgB9B,OAAO,CAAC,YAAY;CA4BrB;AAED,wBAAgB,kCAAkC,CAAC,MAAM,EAAE,aAAa,EAAE,OAAO,GAAE,yBAA8B,GAAG,4BAA4B,CAE/I"}
@@ -117,12 +117,30 @@ export class DiagramInteractionController {
117
117
  writable: true,
118
118
  value: null
119
119
  });
120
+ Object.defineProperty(this, "edgeEndpointOppositeStart", {
121
+ enumerable: true,
122
+ configurable: true,
123
+ writable: true,
124
+ value: null
125
+ });
126
+ Object.defineProperty(this, "edgeEndpointCardinalAngle", {
127
+ enumerable: true,
128
+ configurable: true,
129
+ writable: true,
130
+ value: null
131
+ });
120
132
  Object.defineProperty(this, "edgeEndpointPreview", {
121
133
  enumerable: true,
122
134
  configurable: true,
123
135
  writable: true,
124
136
  value: null
125
137
  });
138
+ Object.defineProperty(this, "edgeWaypointPreview", {
139
+ enumerable: true,
140
+ configurable: true,
141
+ writable: true,
142
+ value: null
143
+ });
126
144
  Object.defineProperty(this, "dragSelectionIds", {
127
145
  enumerable: true,
128
146
  configurable: true,
@@ -259,6 +277,7 @@ export class DiagramInteractionController {
259
277
  if (!geometry || geometry.kind !== "edge" || !((_a = geometry.path) === null || _a === void 0 ? void 0 : _a.length))
260
278
  return false;
261
279
  const point = endpoint === "source" ? geometry.path[0] : geometry.path[geometry.path.length - 1];
280
+ const oppositePoint = endpoint === "source" ? geometry.path[geometry.path.length - 1] : geometry.path[0];
262
281
  this.cancel();
263
282
  this.tool = "drag-edge-endpoint";
264
283
  this.activePointer = event;
@@ -269,6 +288,7 @@ export class DiagramInteractionController {
269
288
  this.edgeEndpointId = ownerId;
270
289
  this.edgeEndpointKind = endpoint;
271
290
  this.edgeEndpointStart = point;
291
+ this.edgeEndpointOppositeStart = oppositePoint;
272
292
  return true;
273
293
  }
274
294
  beginResizeSelectionHandle(handle, event) {
@@ -314,7 +334,7 @@ export class DiagramInteractionController {
314
334
  }
315
335
  }
316
336
  pointerUp(event) {
317
- var _a, _b, _c, _d, _e, _f;
337
+ var _a, _b, _c, _d, _e, _f, _g;
318
338
  if (!this.activePointer || this.activePointer.id !== event.id) {
319
339
  return;
320
340
  }
@@ -337,12 +357,26 @@ export class DiagramInteractionController {
337
357
  }
338
358
  if (this.tool === "drag-edge-endpoint" && this.edgeEndpointId && this.edgeEndpointKind && this.edgeEndpointPreview
339
359
  && (this.edgeEndpointPreview.x !== ((_d = this.edgeEndpointStart) === null || _d === void 0 ? void 0 : _d.x) || this.edgeEndpointPreview.y !== ((_e = this.edgeEndpointStart) === null || _e === void 0 ? void 0 : _e.y))) {
340
- this.engine.dispatch({ type: "moveEdgeEndpoint", id: this.edgeEndpointId, endpoint: this.edgeEndpointKind, point: this.edgeEndpointPreview });
360
+ const edge = this.engine.getScene().entities.edgesById.get(this.edgeEndpointId);
361
+ const commands = [{ type: "moveEdgeEndpoint", id: this.edgeEndpointId, endpoint: this.edgeEndpointKind, point: this.edgeEndpointPreview }];
362
+ if (edge && this.edgeWaypointPreview) {
363
+ for (let index = 0; index < this.edgeWaypointPreview.length; index += 1) {
364
+ const point = this.edgeWaypointPreview[index];
365
+ const previous = (_f = edge.points) === null || _f === void 0 ? void 0 : _f[index];
366
+ if (!previous || previous.x !== point.x || previous.y !== point.y) {
367
+ commands.push({ type: "moveEdgeWaypoint", id: this.edgeEndpointId, index, point });
368
+ }
369
+ }
370
+ }
371
+ if (commands.length > 1)
372
+ this.engine.dispatchBatch(commands);
373
+ else
374
+ this.engine.dispatch(commands[0]);
341
375
  this.commitCount += 1;
342
376
  }
343
377
  if (this.tool === "marquee" && this.marquee) {
344
378
  const ids = this.getMarqueeSelectionIds(this.marquee);
345
- this.engine.dispatch({ type: "setSelection", selection: { ids, primaryId: (_f = ids[0]) !== null && _f !== void 0 ? _f : null }, mode: this.activePointer.shiftKey ? "add" : "replace" });
379
+ this.engine.dispatch({ type: "setSelection", selection: { ids, primaryId: (_g = ids[0]) !== null && _g !== void 0 ? _g : null }, mode: this.activePointer.shiftKey ? "add" : "replace" });
346
380
  this.commitCount += 1;
347
381
  }
348
382
  this.clearGesture();
@@ -356,13 +390,14 @@ export class DiagramInteractionController {
356
390
  }
357
391
  /** Returns the renderer-ready geometry for the active move preview. */
358
392
  getPreviewGeometry(id) {
359
- var _a, _b, _c, _d;
393
+ var _a, _b, _c, _d, _e;
360
394
  const geometry = this.engine.getGeometrySnapshot(id);
361
395
  if ((geometry === null || geometry === void 0 ? void 0 : geometry.kind) === "edge" && ((_a = geometry.path) === null || _a === void 0 ? void 0 : _a.length) && this.edgeEndpointId === id && this.edgeEndpointKind && this.edgeEndpointPreview) {
362
- const path = geometry.path.map((point, index) => {
363
- const isEndpoint = this.edgeEndpointKind === "source" ? index === 0 : index === geometry.path.length - 1;
364
- return isEndpoint ? this.edgeEndpointPreview : point;
365
- });
396
+ const path = [
397
+ this.edgeEndpointKind === "source" ? this.edgeEndpointPreview : geometry.path[0],
398
+ ...((_b = this.edgeWaypointPreview) !== null && _b !== void 0 ? _b : geometry.path.slice(1, -1)),
399
+ this.edgeEndpointKind === "target" ? this.edgeEndpointPreview : geometry.path[geometry.path.length - 1],
400
+ ];
366
401
  const bounds = boundsFromPoints(path);
367
402
  return Object.freeze({ ...geometry, path: Object.freeze(path), bounds, hitBounds: inflateRect(bounds, 6) });
368
403
  }
@@ -381,7 +416,7 @@ export class DiagramInteractionController {
381
416
  ? moved.has(endpoint.nodeId)
382
417
  : moved.has((_b = (_a = scene.entities.portsById.get(endpoint.portId)) === null || _a === void 0 ? void 0 : _a.nodeId) !== null && _b !== void 0 ? _b : "");
383
418
  };
384
- const path = (_b = geometry.path) === null || _b === void 0 ? void 0 : _b.map((point, index) => {
419
+ const path = (_c = geometry.path) === null || _c === void 0 ? void 0 : _c.map((point, index) => {
385
420
  var _a, _b;
386
421
  const isSource = index === 0;
387
422
  const isTarget = index === ((_b = (_a = geometry.path) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) - 1;
@@ -404,8 +439,8 @@ export class DiagramInteractionController {
404
439
  bounds: { ...geometry.bounds, x: geometry.bounds.x + this.previewDelta.x, y: geometry.bounds.y + this.previewDelta.y },
405
440
  hitBounds: { ...geometry.hitBounds, x: geometry.hitBounds.x + this.previewDelta.x, y: geometry.hitBounds.y + this.previewDelta.y },
406
441
  unrotatedBounds: geometry.unrotatedBounds ? { ...geometry.unrotatedBounds, x: geometry.unrotatedBounds.x + this.previewDelta.x, y: geometry.unrotatedBounds.y + this.previewDelta.y } : undefined,
407
- corners: (_c = geometry.corners) === null || _c === void 0 ? void 0 : _c.map(move),
408
- path: (_d = geometry.path) === null || _d === void 0 ? void 0 : _d.map(move),
442
+ corners: (_d = geometry.corners) === null || _d === void 0 ? void 0 : _d.map(move),
443
+ path: (_e = geometry.path) === null || _e === void 0 ? void 0 : _e.map(move),
409
444
  point: geometry.point ? move(geometry.point) : undefined,
410
445
  });
411
446
  }
@@ -473,18 +508,18 @@ export class DiagramInteractionController {
473
508
  if (!this.edgeEndpointStart)
474
509
  return;
475
510
  const origin = this.edgeEndpointStart;
476
- const axisConstraint = this.latestShiftKey ? (_g = this.shiftAxis) !== null && _g !== void 0 ? _g : undefined : undefined;
477
- const desiredPoint = {
478
- x: axisConstraint === "y" ? origin.x : this.latestPoint.x,
479
- y: axisConstraint === "x" ? origin.y : this.latestPoint.y,
480
- };
511
+ const angle = this.latestShiftKey ? this.edgeEndpointCardinalAngle : null;
512
+ const axisConstraint = angle === null ? undefined : angle === 0 || angle === 180 ? "x" : "y";
513
+ let desiredPoint = angle === null
514
+ ? this.latestPoint
515
+ : projectToCardinal((_g = this.edgeEndpointOppositeStart) !== null && _g !== void 0 ? _g : origin, this.latestPoint, angle);
481
516
  if (this.snapOptions.enabled === false) {
482
517
  this.edgeEndpointPreview = desiredPoint;
483
518
  }
484
519
  else {
485
520
  const viewport = this.engine.getScene().viewport;
486
521
  const zoom = viewport.zoom > 0 ? viewport.zoom : 1;
487
- this.edgeEndpointPreview = this.engine.snapPoint(desiredPoint, {
522
+ const snapped = this.engine.snapPoint(desiredPoint, {
488
523
  radius: ((_h = this.snapOptions.tolerancePx) !== null && _h !== void 0 ? _h : 6) / zoom,
489
524
  gridSize: this.snapOptions.gridSize,
490
525
  axisConstraint,
@@ -492,7 +527,11 @@ export class DiagramInteractionController {
492
527
  snapToAlignment: this.snapOptions.snapToAlignment,
493
528
  excludeIds: new Set(this.edgeEndpointId ? [this.edgeEndpointId] : []),
494
529
  }).point;
530
+ this.edgeEndpointPreview = angle === null || !this.edgeEndpointOppositeStart
531
+ ? snapped
532
+ : projectToCardinal(this.edgeEndpointOppositeStart, snapped, angle);
495
533
  }
534
+ this.edgeWaypointPreview = this.createOrthogonalWaypointPreview(this.edgeEndpointPreview);
496
535
  return;
497
536
  }
498
537
  if (this.tool === "marquee") {
@@ -559,19 +598,69 @@ export class DiagramInteractionController {
559
598
  this.latestShiftKey = shiftKey;
560
599
  if (!shiftKey) {
561
600
  this.shiftAxis = null;
601
+ this.edgeEndpointCardinalAngle = null;
562
602
  return;
563
603
  }
564
- if (!wasShifted)
604
+ if (!wasShifted) {
565
605
  this.shiftAxis = null;
566
- if (this.shiftAxis || !this.startPoint || (this.tool !== "drag-selection" && this.tool !== "drag-edge-endpoint"))
606
+ this.edgeEndpointCardinalAngle = null;
607
+ }
608
+ if (this.tool === "drag-edge-endpoint") {
609
+ if (this.edgeEndpointCardinalAngle === null && this.edgeEndpointOppositeStart && this.startPoint && this.isPastAxisActivation(point, this.startPoint)) {
610
+ this.edgeEndpointCardinalAngle = nearestCardinalAngle(point, this.edgeEndpointOppositeStart);
611
+ }
612
+ return;
613
+ }
614
+ if (this.shiftAxis || !this.startPoint || this.tool !== "drag-selection")
567
615
  return;
568
616
  const delta = { x: point.x - this.startPoint.x, y: point.y - this.startPoint.y };
569
- const viewport = this.engine.getScene().viewport;
570
- const zoom = viewport.zoom > 0 ? viewport.zoom : 1;
571
- if (Math.hypot(delta.x, delta.y) * zoom < AXIS_LOCK_ACTIVATION_TOLERANCE_PX)
617
+ if (!this.isPastAxisActivation(point, this.startPoint))
572
618
  return;
573
619
  this.shiftAxis = Math.abs(delta.x) >= Math.abs(delta.y) ? "x" : "y";
574
620
  }
621
+ isPastAxisActivation(point, origin) {
622
+ const viewport = this.engine.getScene().viewport;
623
+ const zoom = viewport.zoom > 0 ? viewport.zoom : 1;
624
+ return Math.hypot(point.x - origin.x, point.y - origin.y) * zoom >= AXIS_LOCK_ACTIVATION_TOLERANCE_PX;
625
+ }
626
+ createOrthogonalWaypointPreview(endpoint) {
627
+ if (!endpoint || !this.edgeEndpointId || !this.edgeEndpointKind)
628
+ return null;
629
+ const edge = this.engine.getScene().entities.edgesById.get(this.edgeEndpointId);
630
+ const points = edge === null || edge === void 0 ? void 0 : edge.points;
631
+ if (!edge || !(points === null || points === void 0 ? void 0 : points.length))
632
+ return points !== null && points !== void 0 ? points : null;
633
+ const geometry = this.engine.getGeometrySnapshot(this.edgeEndpointId);
634
+ const path = geometry === null || geometry === void 0 ? void 0 : geometry.path;
635
+ if (!(path === null || path === void 0 ? void 0 : path.length) || !isOrthogonalPath(path))
636
+ return points;
637
+ const nextPoints = points.map((point) => ({ x: point.x, y: point.y }));
638
+ if (this.edgeEndpointKind === "source") {
639
+ const waypointIndex = 0;
640
+ const routeIndex = 1;
641
+ const inward = findDistinctPoint(path, routeIndex, 1);
642
+ if (!inward)
643
+ return nextPoints;
644
+ const segment = segmentAxis(path[routeIndex], inward);
645
+ if (segment === "horizontal")
646
+ nextPoints[waypointIndex] = { x: endpoint.x, y: inward.y };
647
+ else if (segment === "vertical")
648
+ nextPoints[waypointIndex] = { x: inward.x, y: endpoint.y };
649
+ }
650
+ else {
651
+ const waypointIndex = points.length - 1;
652
+ const routeIndex = path.length - 2;
653
+ const inward = findDistinctPoint(path, routeIndex, -1);
654
+ if (!inward)
655
+ return nextPoints;
656
+ const segment = segmentAxis(inward, path[routeIndex]);
657
+ if (segment === "horizontal")
658
+ nextPoints[waypointIndex] = { x: endpoint.x, y: inward.y };
659
+ else if (segment === "vertical")
660
+ nextPoints[waypointIndex] = { x: inward.x, y: endpoint.y };
661
+ }
662
+ return nextPoints;
663
+ }
575
664
  getMarqueeSelectionIds(rect) {
576
665
  const visible = new Set(this.engine.queryVisible(rect));
577
666
  const scene = this.engine.getScene();
@@ -610,7 +699,10 @@ export class DiagramInteractionController {
610
699
  this.edgeEndpointId = null;
611
700
  this.edgeEndpointKind = null;
612
701
  this.edgeEndpointStart = null;
702
+ this.edgeEndpointOppositeStart = null;
703
+ this.edgeEndpointCardinalAngle = null;
613
704
  this.edgeEndpointPreview = null;
705
+ this.edgeWaypointPreview = null;
614
706
  this.framePending = false;
615
707
  this.pendingMoveCount = 0;
616
708
  }
@@ -631,6 +723,57 @@ function rotateDelta(delta, rotation) {
631
723
  const sin = Math.sin(angle);
632
724
  return { x: delta.x * cos - delta.y * sin, y: delta.x * sin + delta.y * cos };
633
725
  }
726
+ function nearestCardinalAngle(point, origin) {
727
+ const degrees = (Math.atan2(point.y - origin.y, point.x - origin.x) * 180 / Math.PI + 360) % 360;
728
+ const candidates = [0, 180, 90, 270];
729
+ let nearest = candidates[0];
730
+ let nearestDistance = Number.POSITIVE_INFINITY;
731
+ for (const candidate of candidates) {
732
+ const difference = Math.abs(degrees - candidate);
733
+ const distance = Math.min(difference, 360 - difference);
734
+ if (distance < nearestDistance) {
735
+ nearest = candidate;
736
+ nearestDistance = distance;
737
+ }
738
+ }
739
+ return nearest;
740
+ }
741
+ function projectToCardinal(origin, point, angle) {
742
+ const distance = Math.hypot(point.x - origin.x, point.y - origin.y);
743
+ switch (angle) {
744
+ case 0: return { x: origin.x + distance, y: origin.y };
745
+ case 90: return { x: origin.x, y: origin.y + distance };
746
+ case 180: return { x: origin.x - distance, y: origin.y };
747
+ case 270: return { x: origin.x, y: origin.y - distance };
748
+ }
749
+ }
750
+ function segmentAxis(start, end) {
751
+ if (Math.abs(start.y - end.y) <= 1e-6)
752
+ return "horizontal";
753
+ if (Math.abs(start.x - end.x) <= 1e-6)
754
+ return "vertical";
755
+ return null;
756
+ }
757
+ function isOrthogonalPath(path) {
758
+ for (let index = 1; index < path.length; index += 1) {
759
+ if (!segmentAxis(path[index - 1], path[index]))
760
+ return false;
761
+ }
762
+ return true;
763
+ }
764
+ function findDistinctPoint(path, index, step) {
765
+ const origin = path[index];
766
+ if (!origin)
767
+ return null;
768
+ for (let nextIndex = index + step; nextIndex >= 0 && nextIndex < path.length; nextIndex += step) {
769
+ const next = path[nextIndex];
770
+ if (Math.abs(origin.x - next.x) <= 1e-6 && Math.abs(origin.y - next.y) <= 1e-6)
771
+ continue;
772
+ if (segmentAxis(origin, next))
773
+ return next;
774
+ }
775
+ return null;
776
+ }
634
777
  function resizeBoundsByHandle(bounds, handle, delta, minSize, preserveAspectRatio = false, gridSize) {
635
778
  let x = bounds.x;
636
779
  let y = bounds.y;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@affino/diagram-core",
3
- "version": "0.10.1",
3
+ "version": "0.10.2",
4
4
  "author": "Anton Pavlov <a.pavlov@affino.dev>",
5
5
  "type": "module",
6
6
  "description": "Headless diagram core for Affino UI primitives",