@html-graph/html-graph 8.19.1 → 8.21.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.
@@ -1810,58 +1810,6 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
1810
1810
  this.midpoint = absPoints[3];
1811
1811
  }
1812
1812
  }
1813
- class DirectEdgePath {
1814
- constructor(params) {
1815
- __publicField(this, "path");
1816
- __publicField(this, "midpoint");
1817
- __publicField(this, "diagonalDistance");
1818
- const {
1819
- from,
1820
- to,
1821
- sourceOffset,
1822
- targetOffset,
1823
- hasSourceArrow,
1824
- hasTargetArrow,
1825
- arrowLength
1826
- } = params;
1827
- this.midpoint = { x: (from.x + to.x) / 2, y: (from.y + to.y) / 2 };
1828
- const width = to.x - from.x;
1829
- const height = to.y - from.y;
1830
- this.diagonalDistance = Math.sqrt(width * width + height * height);
1831
- if (this.diagonalDistance === 0) {
1832
- this.path = "";
1833
- return;
1834
- }
1835
- const dir = { x: width, y: height };
1836
- const source = this.createDirectLinePoint({
1837
- offset: sourceOffset,
1838
- hasArrow: hasSourceArrow,
1839
- flip: 1,
1840
- shift: from,
1841
- arrowLength,
1842
- dir
1843
- });
1844
- const target = this.createDirectLinePoint({
1845
- offset: targetOffset,
1846
- hasArrow: hasTargetArrow,
1847
- flip: -1,
1848
- shift: to,
1849
- arrowLength,
1850
- dir
1851
- });
1852
- this.path = `M ${source.x} ${source.y} L ${target.x} ${target.y}`;
1853
- }
1854
- createDirectLinePoint(params) {
1855
- const arrowOffset = params.hasArrow ? params.arrowLength : 0;
1856
- const totalOffset = params.offset + arrowOffset;
1857
- const targetRatio = params.flip * totalOffset / this.diagonalDistance;
1858
- const { dir, shift } = params;
1859
- return {
1860
- x: dir.x * targetRatio + shift.x,
1861
- y: dir.y * targetRatio + shift.y
1862
- };
1863
- }
1864
- }
1865
1813
  const calculateDotourY = (from, to, detourDistance) => {
1866
1814
  const max = Math.max(from.y, to.y);
1867
1815
  const min = Math.min(from.y, to.y);
@@ -2009,7 +1957,7 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2009
1957
  smallCycleRadius: 15,
2010
1958
  curvature: 90,
2011
1959
  interactiveWidth: 10,
2012
- preOffset: 0
1960
+ portOffset: 0
2013
1961
  });
2014
1962
  const createDirectionVector = (dir) => {
2015
1963
  return { x: Math.cos(dir), y: Math.sin(dir) };
@@ -2550,6 +2498,27 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2550
2498
  });
2551
2499
  }
2552
2500
  }
2501
+ const boxPortOffsetFn = (params) => {
2502
+ const { direction, radius } = params;
2503
+ const { x, y } = direction;
2504
+ const { horizontal, vertical } = radius;
2505
+ const tg = y / x;
2506
+ const horX = Math.abs(vertical / tg);
2507
+ const vertY = Math.abs(horizontal * tg);
2508
+ const minX = Math.min(horizontal, horX);
2509
+ const minY = Math.min(vertical, vertY);
2510
+ return Math.sqrt(minX * minX + minY * minY);
2511
+ };
2512
+ const resolvePortOffsetFn = (offset) => {
2513
+ if (typeof offset === "number") {
2514
+ return () => offset;
2515
+ }
2516
+ if (offset === "box") {
2517
+ return boxPortOffsetFn;
2518
+ }
2519
+ return offset;
2520
+ };
2521
+ const defaultPortOffset = edgeConstants.portOffset;
2553
2522
  class DirectEdgeShape {
2554
2523
  constructor(params) {
2555
2524
  __publicField(this, "svg");
@@ -2563,8 +2532,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2563
2532
  __publicField(this, "color");
2564
2533
  __publicField(this, "width");
2565
2534
  __publicField(this, "arrowLength");
2566
- __publicField(this, "sourceOffset");
2567
- __publicField(this, "targetOffset");
2535
+ __publicField(this, "sourceOffsetFn");
2536
+ __publicField(this, "targetOffsetFn");
2568
2537
  __publicField(this, "onAfterRender");
2569
2538
  __publicField(this, "afterRenderEmitter");
2570
2539
  __publicField(this, "arrowRenderer");
@@ -2573,8 +2542,12 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2573
2542
  this.width = (params == null ? void 0 : params.width) ?? edgeConstants.width;
2574
2543
  this.arrowLength = (params == null ? void 0 : params.arrowLength) ?? edgeConstants.arrowLength;
2575
2544
  this.arrowRenderer = resolveArrowRenderer((params == null ? void 0 : params.arrowRenderer) ?? {});
2576
- this.sourceOffset = (params == null ? void 0 : params.sourceOffset) ?? edgeConstants.preOffset;
2577
- this.targetOffset = (params == null ? void 0 : params.targetOffset) ?? edgeConstants.preOffset;
2545
+ this.sourceOffsetFn = resolvePortOffsetFn(
2546
+ (params == null ? void 0 : params.sourceOffset) ?? defaultPortOffset
2547
+ );
2548
+ this.targetOffsetFn = resolvePortOffsetFn(
2549
+ (params == null ? void 0 : params.targetOffset) ?? defaultPortOffset
2550
+ );
2578
2551
  this.svg = createEdgeSvg(this.color);
2579
2552
  this.svg.appendChild(this.group);
2580
2553
  this.line = createEdgePath(this.width);
@@ -2595,63 +2568,102 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2595
2568
  svgPadding
2596
2569
  );
2597
2570
  setSvgRectangle(this.svg, { x, y, width, height });
2598
- const edgePath = new DirectEdgePath({
2599
- from,
2600
- to,
2601
- sourceOffset: this.sourceOffset,
2602
- targetOffset: this.targetOffset,
2603
- hasSourceArrow: this.sourceArrow !== null,
2604
- hasTargetArrow: this.targetArrow !== null,
2605
- arrowLength: this.arrowLength
2606
- });
2607
- this.line.setAttribute("d", edgePath.path);
2608
- let sourceArrowPath = null;
2609
- let targetArrowPath = null;
2610
- const diagonal = edgePath.diagonalDistance;
2571
+ const dirX = to.x - from.x;
2572
+ const dirY = to.y - from.y;
2573
+ const diagonal = Math.sqrt(dirX * dirX + dirY * dirY);
2611
2574
  if (diagonal === 0) {
2612
- if (this.sourceArrow !== null) {
2613
- sourceArrowPath = "";
2614
- this.sourceArrow.setAttribute("d", sourceArrowPath);
2575
+ this.renderEmpty(from);
2576
+ return;
2577
+ }
2578
+ const direction = { x: dirX / diagonal, y: dirY / diagonal };
2579
+ const sourceOffset = this.sourceOffsetFn({
2580
+ direction: { x: direction.x, y: direction.y },
2581
+ radius: {
2582
+ horizontal: params.from.width / 2,
2583
+ vertical: params.from.height / 2
2615
2584
  }
2616
- if (this.targetArrow !== null) {
2617
- targetArrowPath = "";
2618
- this.targetArrow.setAttribute("d", targetArrowPath);
2585
+ });
2586
+ const targetOffset = this.targetOffsetFn({
2587
+ direction: { x: -direction.x, y: -direction.y },
2588
+ radius: {
2589
+ horizontal: params.to.width / 2,
2590
+ vertical: params.to.height / 2
2619
2591
  }
2620
- } else {
2621
- const direction = {
2622
- x: (to.x - from.x) / diagonal,
2623
- y: (to.y - from.y) / diagonal
2592
+ });
2593
+ const source = {
2594
+ x: from.x + sourceOffset * direction.x,
2595
+ y: from.y + sourceOffset * direction.y
2596
+ };
2597
+ const target = {
2598
+ x: to.x - targetOffset * direction.x,
2599
+ y: to.y - targetOffset * direction.y
2600
+ };
2601
+ const diagonalSource = this.sourceArrow !== null ? this.arrowLength : 0;
2602
+ const sourceLine = {
2603
+ x: source.x + diagonalSource * direction.x,
2604
+ y: source.y + diagonalSource * direction.y
2605
+ };
2606
+ const diagonalTarget = this.targetArrow !== null ? this.arrowLength : 0;
2607
+ const targetLine = {
2608
+ x: target.x - diagonalTarget * direction.x,
2609
+ y: target.y - diagonalTarget * direction.y
2610
+ };
2611
+ const midpoint = {
2612
+ x: (source.x + target.x) / 2,
2613
+ y: (source.y + target.y) / 2
2614
+ };
2615
+ const path = `M ${sourceLine.x} ${sourceLine.y} L ${targetLine.x} ${targetLine.y}`;
2616
+ this.line.setAttribute("d", path);
2617
+ let sourceArrowPath = null;
2618
+ let targetArrowPath = null;
2619
+ if (this.sourceArrow) {
2620
+ const sourceOffsetPoint = {
2621
+ x: direction.x * sourceOffset + from.x,
2622
+ y: direction.y * sourceOffset + from.y
2624
2623
  };
2625
- if (this.sourceArrow) {
2626
- const sourceOffset = {
2627
- x: direction.x * this.sourceOffset + from.x,
2628
- y: direction.y * this.sourceOffset + from.y
2629
- };
2630
- sourceArrowPath = this.arrowRenderer({
2631
- direction,
2632
- shift: sourceOffset,
2633
- arrowLength: this.arrowLength
2634
- });
2635
- this.sourceArrow.setAttribute("d", sourceArrowPath);
2636
- }
2637
- if (this.targetArrow) {
2638
- const targetOffset = {
2639
- x: direction.x * this.targetOffset,
2640
- y: direction.y * this.targetOffset
2641
- };
2642
- targetArrowPath = this.arrowRenderer({
2643
- direction: { x: -direction.x, y: -direction.y },
2644
- shift: {
2645
- x: to.x - targetOffset.x,
2646
- y: to.y - targetOffset.y
2647
- },
2648
- arrowLength: this.arrowLength
2649
- });
2650
- this.targetArrow.setAttribute("d", targetArrowPath);
2651
- }
2624
+ sourceArrowPath = this.arrowRenderer({
2625
+ direction,
2626
+ shift: sourceOffsetPoint,
2627
+ arrowLength: this.arrowLength
2628
+ });
2629
+ this.sourceArrow.setAttribute("d", sourceArrowPath);
2630
+ }
2631
+ if (this.targetArrow) {
2632
+ const targetOffsetPoint = {
2633
+ x: direction.x * targetOffset,
2634
+ y: direction.y * targetOffset
2635
+ };
2636
+ targetArrowPath = this.arrowRenderer({
2637
+ direction: { x: -direction.x, y: -direction.y },
2638
+ shift: {
2639
+ x: to.x - targetOffsetPoint.x,
2640
+ y: to.y - targetOffsetPoint.y
2641
+ },
2642
+ arrowLength: this.arrowLength
2643
+ });
2644
+ this.targetArrow.setAttribute("d", targetArrowPath);
2652
2645
  }
2653
2646
  this.afterRenderEmitter.emit({
2654
- edgePath,
2647
+ edgePath: { path, midpoint },
2648
+ sourceArrowPath,
2649
+ targetArrowPath
2650
+ });
2651
+ }
2652
+ renderEmpty(midpoint) {
2653
+ const emptyPath = "";
2654
+ let sourceArrowPath = null;
2655
+ let targetArrowPath = null;
2656
+ this.line.setAttribute("d", emptyPath);
2657
+ if (this.sourceArrow !== null) {
2658
+ sourceArrowPath = "";
2659
+ this.sourceArrow.setAttribute("d", sourceArrowPath);
2660
+ }
2661
+ if (this.targetArrow !== null) {
2662
+ targetArrowPath = "";
2663
+ this.targetArrow.setAttribute("d", targetArrowPath);
2664
+ }
2665
+ this.afterRenderEmitter.emit({
2666
+ edgePath: { path: emptyPath, midpoint },
2655
2667
  sourceArrowPath,
2656
2668
  targetArrowPath
2657
2669
  });
@@ -7113,5 +7125,6 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
7113
7125
  exports2.MidpointEdgeShape = MidpointEdgeShape;
7114
7126
  exports2.StraightEdgeShape = StraightEdgeShape;
7115
7127
  exports2.VerticalEdgeShape = VerticalEdgeShape;
7128
+ exports2.boxPortOffsetFn = boxPortOffsetFn;
7116
7129
  Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
7117
7130
  });
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@html-graph/html-graph",
3
3
  "author": "Dmitry Marov <d.marov94@gmail.com>",
4
4
  "private": false,
5
- "version": "8.19.1",
5
+ "version": "8.21.0",
6
6
  "type": "module",
7
7
  "main": "dist/html-graph.js",
8
8
  "types": "dist/html-graph.d.ts",