@affino/diagram-core 0.10.0 → 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,11 +109,11 @@ 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). Port, alignment, and grid snapping preserve that axis. Pressing or releasing Shift during a drag updates both preview and final command.
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
 
116
- Holding Shift during a selection drag constrains the group to the dominant axis from the gesture origin (horizontal wins an exact tie). The constraint applies to the frame-batched preview and the committed delta; snapping stays on that axis, and releasing Shift resumes free movement for the rest of the drag. Shift-click continues to add or toggle selection. Direct `snapTranslation()` callers can set `axisConstraint: "x" | "y"` to apply the same axis-aware port, alignment, equal-spacing, and grid snapping.
116
+ Holding Shift during a selection drag constrains the group to the dominant axis from the gesture origin (horizontal wins an exact tie). After the pointer moves 4 screen pixels, the axis stays latched until Shift is released; pressing Shift again chooses from the current gesture direction. The constraint applies to the frame-batched preview and the committed delta, and snapping stays on that axis. Shift-click continues to add or toggle selection. Direct `snapTranslation()` callers can set `axisConstraint: "x" | "y"` to apply the same axis-aware port, alignment, equal-spacing, and grid snapping.
117
117
 
118
118
  ```ts
119
119
  const interaction = createDiagramInteractionController(engine, {
@@ -13,6 +13,7 @@ export declare class DiagramInteractionController {
13
13
  private startPoint;
14
14
  private latestPoint;
15
15
  private latestShiftKey;
16
+ private shiftAxis;
16
17
  private previewDelta;
17
18
  private marquee;
18
19
  private resizeOwnerId;
@@ -25,7 +26,10 @@ export declare class DiagramInteractionController {
25
26
  private edgeEndpointId;
26
27
  private edgeEndpointKind;
27
28
  private edgeEndpointStart;
29
+ private edgeEndpointOppositeStart;
30
+ private edgeEndpointCardinalAngle;
28
31
  private edgeEndpointPreview;
32
+ private edgeWaypointPreview;
29
33
  private dragSelectionIds;
30
34
  private snapGuides;
31
35
  private framePending;
@@ -55,6 +59,9 @@ export declare class DiagramInteractionController {
55
59
  private createSelectionResizePreviewEntries;
56
60
  private createResizePreview;
57
61
  private resizeGridSize;
62
+ private updateShiftAxis;
63
+ private isPastAxisActivation;
64
+ private createOrthogonalWaypointPreview;
58
65
  private getMarqueeSelectionIds;
59
66
  private clearGesture;
60
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;AAEnD,MAAM,MAAM,yBAAyB,GAAG,QAAQ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AAED,wBAAgB,kCAAkC,CAAC,MAAM,EAAE,aAAa,EAAE,OAAO,GAAE,yBAA8B,GAAG,4BAA4B,CAE/I"}
@@ -1,4 +1,5 @@
1
1
  import { createEntityGeometry, inflateRect, rectFromPoints as boundsFromPoints } from "./geometry.js";
2
+ const AXIS_LOCK_ACTIVATION_TOLERANCE_PX = 4;
2
3
  export class DiagramInteractionController {
3
4
  constructor(engine, options = {}) {
4
5
  var _a, _b, _c;
@@ -38,6 +39,12 @@ export class DiagramInteractionController {
38
39
  writable: true,
39
40
  value: false
40
41
  });
42
+ Object.defineProperty(this, "shiftAxis", {
43
+ enumerable: true,
44
+ configurable: true,
45
+ writable: true,
46
+ value: null
47
+ });
41
48
  Object.defineProperty(this, "previewDelta", {
42
49
  enumerable: true,
43
50
  configurable: true,
@@ -110,12 +117,30 @@ export class DiagramInteractionController {
110
117
  writable: true,
111
118
  value: null
112
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
+ });
113
132
  Object.defineProperty(this, "edgeEndpointPreview", {
114
133
  enumerable: true,
115
134
  configurable: true,
116
135
  writable: true,
117
136
  value: null
118
137
  });
138
+ Object.defineProperty(this, "edgeWaypointPreview", {
139
+ enumerable: true,
140
+ configurable: true,
141
+ writable: true,
142
+ value: null
143
+ });
119
144
  Object.defineProperty(this, "dragSelectionIds", {
120
145
  enumerable: true,
121
146
  configurable: true,
@@ -191,6 +216,7 @@ export class DiagramInteractionController {
191
216
  this.startPoint = event.point;
192
217
  this.latestPoint = event.point;
193
218
  this.latestShiftKey = event.shiftKey === true;
219
+ this.shiftAxis = null;
194
220
  this.previewDelta = null;
195
221
  this.marquee = null;
196
222
  this.snapGuides = Object.freeze([]);
@@ -238,6 +264,7 @@ export class DiagramInteractionController {
238
264
  this.startPoint = event.point;
239
265
  this.latestPoint = event.point;
240
266
  this.latestShiftKey = event.shiftKey === true;
267
+ this.shiftAxis = null;
241
268
  this.resizeOwnerId = ownerId;
242
269
  this.resizeHandle = handle;
243
270
  this.resizeStartBounds = (_a = geometry.unrotatedBounds) !== null && _a !== void 0 ? _a : geometry.bounds;
@@ -250,15 +277,18 @@ export class DiagramInteractionController {
250
277
  if (!geometry || geometry.kind !== "edge" || !((_a = geometry.path) === null || _a === void 0 ? void 0 : _a.length))
251
278
  return false;
252
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];
253
281
  this.cancel();
254
282
  this.tool = "drag-edge-endpoint";
255
283
  this.activePointer = event;
256
284
  this.startPoint = event.point;
257
285
  this.latestPoint = event.point;
258
286
  this.latestShiftKey = event.shiftKey === true;
287
+ this.shiftAxis = null;
259
288
  this.edgeEndpointId = ownerId;
260
289
  this.edgeEndpointKind = endpoint;
261
290
  this.edgeEndpointStart = point;
291
+ this.edgeEndpointOppositeStart = oppositePoint;
262
292
  return true;
263
293
  }
264
294
  beginResizeSelectionHandle(handle, event) {
@@ -279,6 +309,7 @@ export class DiagramInteractionController {
279
309
  this.startPoint = event.point;
280
310
  this.latestPoint = event.point;
281
311
  this.latestShiftKey = event.shiftKey === true;
312
+ this.shiftAxis = null;
282
313
  this.resizeSelectionIds = ids;
283
314
  this.resizeHandle = handle;
284
315
  this.resizeStartBounds = bounds;
@@ -290,7 +321,7 @@ export class DiagramInteractionController {
290
321
  return;
291
322
  }
292
323
  this.latestPoint = event.point;
293
- this.latestShiftKey = event.shiftKey === true;
324
+ this.updateShiftAxis(event.shiftKey === true, event.point);
294
325
  this.pendingMoveCount += 1;
295
326
  if (!this.framePending) {
296
327
  this.framePending = true;
@@ -303,12 +334,12 @@ export class DiagramInteractionController {
303
334
  }
304
335
  }
305
336
  pointerUp(event) {
306
- var _a, _b, _c, _d, _e, _f;
337
+ var _a, _b, _c, _d, _e, _f, _g;
307
338
  if (!this.activePointer || this.activePointer.id !== event.id) {
308
339
  return;
309
340
  }
310
341
  this.latestPoint = event.point;
311
- this.latestShiftKey = event.shiftKey === true;
342
+ this.updateShiftAxis(event.shiftKey === true, event.point);
312
343
  this.flushPointerMove();
313
344
  if (this.tool === "drag-selection" && this.previewDelta) {
314
345
  const selection = this.engine.getScene().selection;
@@ -326,12 +357,26 @@ export class DiagramInteractionController {
326
357
  }
327
358
  if (this.tool === "drag-edge-endpoint" && this.edgeEndpointId && this.edgeEndpointKind && this.edgeEndpointPreview
328
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))) {
329
- 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]);
330
375
  this.commitCount += 1;
331
376
  }
332
377
  if (this.tool === "marquee" && this.marquee) {
333
378
  const ids = this.getMarqueeSelectionIds(this.marquee);
334
- 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" });
335
380
  this.commitCount += 1;
336
381
  }
337
382
  this.clearGesture();
@@ -345,13 +390,14 @@ export class DiagramInteractionController {
345
390
  }
346
391
  /** Returns the renderer-ready geometry for the active move preview. */
347
392
  getPreviewGeometry(id) {
348
- var _a, _b, _c, _d;
393
+ var _a, _b, _c, _d, _e;
349
394
  const geometry = this.engine.getGeometrySnapshot(id);
350
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) {
351
- const path = geometry.path.map((point, index) => {
352
- const isEndpoint = this.edgeEndpointKind === "source" ? index === 0 : index === geometry.path.length - 1;
353
- return isEndpoint ? this.edgeEndpointPreview : point;
354
- });
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
+ ];
355
401
  const bounds = boundsFromPoints(path);
356
402
  return Object.freeze({ ...geometry, path: Object.freeze(path), bounds, hitBounds: inflateRect(bounds, 6) });
357
403
  }
@@ -370,7 +416,7 @@ export class DiagramInteractionController {
370
416
  ? moved.has(endpoint.nodeId)
371
417
  : moved.has((_b = (_a = scene.entities.portsById.get(endpoint.portId)) === null || _a === void 0 ? void 0 : _a.nodeId) !== null && _b !== void 0 ? _b : "");
372
418
  };
373
- 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) => {
374
420
  var _a, _b;
375
421
  const isSource = index === 0;
376
422
  const isTarget = index === ((_b = (_a = geometry.path) === null || _a === void 0 ? void 0 : _a.length) !== null && _b !== void 0 ? _b : 0) - 1;
@@ -393,8 +439,8 @@ export class DiagramInteractionController {
393
439
  bounds: { ...geometry.bounds, x: geometry.bounds.x + this.previewDelta.x, y: geometry.bounds.y + this.previewDelta.y },
394
440
  hitBounds: { ...geometry.hitBounds, x: geometry.hitBounds.x + this.previewDelta.x, y: geometry.hitBounds.y + this.previewDelta.y },
395
441
  unrotatedBounds: geometry.unrotatedBounds ? { ...geometry.unrotatedBounds, x: geometry.unrotatedBounds.x + this.previewDelta.x, y: geometry.unrotatedBounds.y + this.previewDelta.y } : undefined,
396
- corners: (_c = geometry.corners) === null || _c === void 0 ? void 0 : _c.map(move),
397
- 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),
398
444
  point: geometry.point ? move(geometry.point) : undefined,
399
445
  });
400
446
  }
@@ -419,16 +465,14 @@ export class DiagramInteractionController {
419
465
  return this.commitCount;
420
466
  }
421
467
  flushPointerMove() {
422
- var _a, _b, _c, _d, _e, _f;
468
+ var _a, _b, _c, _d, _e, _f, _g, _h;
423
469
  this.framePending = false;
424
470
  if (!this.startPoint || !this.latestPoint) {
425
471
  return;
426
472
  }
427
473
  const delta = { x: this.latestPoint.x - this.startPoint.x, y: this.latestPoint.y - this.startPoint.y };
428
474
  if (this.tool === "drag-selection" || this.tool === "pan") {
429
- const axisConstraint = this.tool === "drag-selection" && this.latestShiftKey
430
- ? Math.abs(delta.x) >= Math.abs(delta.y) ? "x" : "y"
431
- : undefined;
475
+ const axisConstraint = this.tool === "drag-selection" && this.latestShiftKey ? (_a = this.shiftAxis) !== null && _a !== void 0 ? _a : undefined : undefined;
432
476
  const constrainedDelta = axisConstraint === "x"
433
477
  ? { x: delta.x, y: 0 }
434
478
  : axisConstraint === "y"
@@ -438,13 +482,13 @@ export class DiagramInteractionController {
438
482
  const viewport = this.engine.getScene().viewport;
439
483
  const zoom = viewport.zoom > 0 ? viewport.zoom : 1;
440
484
  const selection = this.engine.getScene().selection;
441
- const snapped = this.engine.snapTranslation({ ...selection, ids: this.dragSelectionIds, primaryId: this.dragSelectionIds.includes((_a = selection.primaryId) !== null && _a !== void 0 ? _a : "") ? selection.primaryId : (_b = this.dragSelectionIds[0]) !== null && _b !== void 0 ? _b : null }, constrainedDelta, {
442
- radius: ((_c = this.snapOptions.tolerancePx) !== null && _c !== void 0 ? _c : 6) / zoom,
485
+ const snapped = this.engine.snapTranslation({ ...selection, ids: this.dragSelectionIds, primaryId: this.dragSelectionIds.includes((_b = selection.primaryId) !== null && _b !== void 0 ? _b : "") ? selection.primaryId : (_c = this.dragSelectionIds[0]) !== null && _c !== void 0 ? _c : null }, constrainedDelta, {
486
+ radius: ((_d = this.snapOptions.tolerancePx) !== null && _d !== void 0 ? _d : 6) / zoom,
443
487
  gridSize: this.snapOptions.gridSize,
444
488
  axisConstraint,
445
489
  snapToPorts: this.snapOptions.snapToPorts,
446
490
  snapToAlignment: this.snapOptions.snapToAlignment,
447
- snapToEqualSpacing: (_d = this.snapOptions.snapToEqualSpacing) !== null && _d !== void 0 ? _d : true,
491
+ snapToEqualSpacing: (_e = this.snapOptions.snapToEqualSpacing) !== null && _e !== void 0 ? _e : true,
448
492
  });
449
493
  this.previewDelta = snapped.point;
450
494
  this.snapGuides = snapped.guides;
@@ -457,35 +501,37 @@ export class DiagramInteractionController {
457
501
  }
458
502
  if (this.tool === "resize-selection") {
459
503
  this.resizePreviewEntries = this.createResizePreviewEntries(delta);
460
- this.resizePreview = (_e = this.resizePreviewEntries[0]) !== null && _e !== void 0 ? _e : null;
504
+ this.resizePreview = (_f = this.resizePreviewEntries[0]) !== null && _f !== void 0 ? _f : null;
461
505
  return;
462
506
  }
463
507
  if (this.tool === "drag-edge-endpoint") {
464
508
  if (!this.edgeEndpointStart)
465
509
  return;
466
510
  const origin = this.edgeEndpointStart;
467
- const axisConstraint = this.latestShiftKey
468
- ? Math.abs(delta.x) >= Math.abs(delta.y) ? "x" : "y"
469
- : undefined;
470
- const desiredPoint = {
471
- x: axisConstraint === "y" ? origin.x : this.latestPoint.x,
472
- y: axisConstraint === "x" ? origin.y : this.latestPoint.y,
473
- };
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);
474
516
  if (this.snapOptions.enabled === false) {
475
517
  this.edgeEndpointPreview = desiredPoint;
476
518
  }
477
519
  else {
478
520
  const viewport = this.engine.getScene().viewport;
479
521
  const zoom = viewport.zoom > 0 ? viewport.zoom : 1;
480
- this.edgeEndpointPreview = this.engine.snapPoint(desiredPoint, {
481
- radius: ((_f = this.snapOptions.tolerancePx) !== null && _f !== void 0 ? _f : 6) / zoom,
522
+ const snapped = this.engine.snapPoint(desiredPoint, {
523
+ radius: ((_h = this.snapOptions.tolerancePx) !== null && _h !== void 0 ? _h : 6) / zoom,
482
524
  gridSize: this.snapOptions.gridSize,
483
525
  axisConstraint,
484
526
  snapToPorts: this.snapOptions.snapToPorts,
485
527
  snapToAlignment: this.snapOptions.snapToAlignment,
486
528
  excludeIds: new Set(this.edgeEndpointId ? [this.edgeEndpointId] : []),
487
529
  }).point;
530
+ this.edgeEndpointPreview = angle === null || !this.edgeEndpointOppositeStart
531
+ ? snapped
532
+ : projectToCardinal(this.edgeEndpointOppositeStart, snapped, angle);
488
533
  }
534
+ this.edgeWaypointPreview = this.createOrthogonalWaypointPreview(this.edgeEndpointPreview);
489
535
  return;
490
536
  }
491
537
  if (this.tool === "marquee") {
@@ -547,6 +593,74 @@ export class DiagramInteractionController {
547
593
  resizeGridSize() {
548
594
  return !this.latestShiftKey || this.snapOptions.enabled === false ? undefined : this.snapOptions.gridSize;
549
595
  }
596
+ updateShiftAxis(shiftKey, point) {
597
+ const wasShifted = this.latestShiftKey;
598
+ this.latestShiftKey = shiftKey;
599
+ if (!shiftKey) {
600
+ this.shiftAxis = null;
601
+ this.edgeEndpointCardinalAngle = null;
602
+ return;
603
+ }
604
+ if (!wasShifted) {
605
+ this.shiftAxis = null;
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")
615
+ return;
616
+ const delta = { x: point.x - this.startPoint.x, y: point.y - this.startPoint.y };
617
+ if (!this.isPastAxisActivation(point, this.startPoint))
618
+ return;
619
+ this.shiftAxis = Math.abs(delta.x) >= Math.abs(delta.y) ? "x" : "y";
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
+ }
550
664
  getMarqueeSelectionIds(rect) {
551
665
  const visible = new Set(this.engine.queryVisible(rect));
552
666
  const scene = this.engine.getScene();
@@ -570,6 +684,7 @@ export class DiagramInteractionController {
570
684
  this.startPoint = null;
571
685
  this.latestPoint = null;
572
686
  this.latestShiftKey = false;
687
+ this.shiftAxis = null;
573
688
  this.previewDelta = null;
574
689
  this.marquee = null;
575
690
  this.resizeOwnerId = null;
@@ -584,7 +699,10 @@ export class DiagramInteractionController {
584
699
  this.edgeEndpointId = null;
585
700
  this.edgeEndpointKind = null;
586
701
  this.edgeEndpointStart = null;
702
+ this.edgeEndpointOppositeStart = null;
703
+ this.edgeEndpointCardinalAngle = null;
587
704
  this.edgeEndpointPreview = null;
705
+ this.edgeWaypointPreview = null;
588
706
  this.framePending = false;
589
707
  this.pendingMoveCount = 0;
590
708
  }
@@ -605,6 +723,57 @@ function rotateDelta(delta, rotation) {
605
723
  const sin = Math.sin(angle);
606
724
  return { x: delta.x * cos - delta.y * sin, y: delta.x * sin + delta.y * cos };
607
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
+ }
608
777
  function resizeBoundsByHandle(bounds, handle, delta, minSize, preserveAspectRatio = false, gridSize) {
609
778
  let x = bounds.x;
610
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.0",
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",