@html-graph/html-graph 8.19.0 → 8.20.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,13 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2550
2498
  });
2551
2499
  }
2552
2500
  }
2501
+ const resolvePortOffsetFn = (offset) => {
2502
+ if (typeof offset === "number") {
2503
+ return () => offset;
2504
+ }
2505
+ return offset;
2506
+ };
2507
+ const defaultPortOffset = edgeConstants.portOffset;
2553
2508
  class DirectEdgeShape {
2554
2509
  constructor(params) {
2555
2510
  __publicField(this, "svg");
@@ -2563,8 +2518,8 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2563
2518
  __publicField(this, "color");
2564
2519
  __publicField(this, "width");
2565
2520
  __publicField(this, "arrowLength");
2566
- __publicField(this, "sourceOffset");
2567
- __publicField(this, "targetOffset");
2521
+ __publicField(this, "sourceOffsetFn");
2522
+ __publicField(this, "targetOffsetFn");
2568
2523
  __publicField(this, "onAfterRender");
2569
2524
  __publicField(this, "afterRenderEmitter");
2570
2525
  __publicField(this, "arrowRenderer");
@@ -2573,8 +2528,12 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2573
2528
  this.width = (params == null ? void 0 : params.width) ?? edgeConstants.width;
2574
2529
  this.arrowLength = (params == null ? void 0 : params.arrowLength) ?? edgeConstants.arrowLength;
2575
2530
  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;
2531
+ this.sourceOffsetFn = resolvePortOffsetFn(
2532
+ (params == null ? void 0 : params.sourceOffset) ?? defaultPortOffset
2533
+ );
2534
+ this.targetOffsetFn = resolvePortOffsetFn(
2535
+ (params == null ? void 0 : params.targetOffset) ?? defaultPortOffset
2536
+ );
2578
2537
  this.svg = createEdgeSvg(this.color);
2579
2538
  this.svg.appendChild(this.group);
2580
2539
  this.line = createEdgePath(this.width);
@@ -2595,68 +2554,118 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
2595
2554
  svgPadding
2596
2555
  );
2597
2556
  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;
2557
+ const dirX = to.x - from.x;
2558
+ const dirY = to.y - from.y;
2559
+ const diagonal = Math.sqrt(dirX * dirX + dirY * dirY);
2611
2560
  if (diagonal === 0) {
2612
- if (this.sourceArrow !== null) {
2613
- sourceArrowPath = "";
2614
- this.sourceArrow.setAttribute("d", sourceArrowPath);
2561
+ this.renderEmpty(from);
2562
+ return;
2563
+ }
2564
+ const direction = { x: dirX / diagonal, y: dirY / diagonal };
2565
+ const sourceOffset = this.sourceOffsetFn({
2566
+ direction: { x: direction.x, y: direction.y },
2567
+ radius: {
2568
+ horizontal: params.from.width / 2,
2569
+ vertical: params.from.height / 2
2615
2570
  }
2616
- if (this.targetArrow !== null) {
2617
- targetArrowPath = "";
2618
- this.targetArrow.setAttribute("d", targetArrowPath);
2571
+ });
2572
+ const targetOffset = this.targetOffsetFn({
2573
+ direction: { x: -direction.x, y: -direction.y },
2574
+ radius: {
2575
+ horizontal: params.to.width / 2,
2576
+ vertical: params.to.height / 2
2619
2577
  }
2620
- } else {
2621
- const direction = {
2622
- x: (to.x - from.x) / diagonal,
2623
- y: (to.y - from.y) / diagonal
2578
+ });
2579
+ const source = {
2580
+ x: from.x + sourceOffset * direction.x,
2581
+ y: from.y + sourceOffset * direction.y
2582
+ };
2583
+ const target = {
2584
+ x: to.x - targetOffset * direction.x,
2585
+ y: to.y - targetOffset * direction.y
2586
+ };
2587
+ const diagonalSource = this.sourceArrow !== null ? this.arrowLength : 0;
2588
+ const sourceLine = {
2589
+ x: source.x + diagonalSource * direction.x,
2590
+ y: source.y + diagonalSource * direction.y
2591
+ };
2592
+ const diagonalTarget = this.targetArrow !== null ? this.arrowLength : 0;
2593
+ const targetLine = {
2594
+ x: target.x - diagonalTarget * direction.x,
2595
+ y: target.y - diagonalTarget * direction.y
2596
+ };
2597
+ const midpoint = {
2598
+ x: (source.x + target.x) / 2,
2599
+ y: (source.y + target.y) / 2
2600
+ };
2601
+ const path = `M ${sourceLine.x} ${sourceLine.y} L ${targetLine.x} ${targetLine.y}`;
2602
+ this.line.setAttribute("d", path);
2603
+ let sourceArrowPath = null;
2604
+ let targetArrowPath = null;
2605
+ if (this.sourceArrow) {
2606
+ const sourceOffsetPoint = {
2607
+ x: direction.x * sourceOffset + from.x,
2608
+ y: direction.y * sourceOffset + from.y
2624
2609
  };
2625
- if (this.sourceArrow) {
2626
- const sourceOffset = {
2627
- x: direction.x * this.sourceOffset,
2628
- y: direction.y * this.sourceOffset
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
- }
2610
+ sourceArrowPath = this.arrowRenderer({
2611
+ direction,
2612
+ shift: sourceOffsetPoint,
2613
+ arrowLength: this.arrowLength
2614
+ });
2615
+ this.sourceArrow.setAttribute("d", sourceArrowPath);
2616
+ }
2617
+ if (this.targetArrow) {
2618
+ const targetOffsetPoint = {
2619
+ x: direction.x * targetOffset,
2620
+ y: direction.y * targetOffset
2621
+ };
2622
+ targetArrowPath = this.arrowRenderer({
2623
+ direction: { x: -direction.x, y: -direction.y },
2624
+ shift: {
2625
+ x: to.x - targetOffsetPoint.x,
2626
+ y: to.y - targetOffsetPoint.y
2627
+ },
2628
+ arrowLength: this.arrowLength
2629
+ });
2630
+ this.targetArrow.setAttribute("d", targetArrowPath);
2652
2631
  }
2653
2632
  this.afterRenderEmitter.emit({
2654
- edgePath,
2633
+ edgePath: { path, midpoint },
2634
+ sourceArrowPath,
2635
+ targetArrowPath
2636
+ });
2637
+ }
2638
+ renderEmpty(midpoint) {
2639
+ const emptyPath = "";
2640
+ let sourceArrowPath = null;
2641
+ let targetArrowPath = null;
2642
+ this.line.setAttribute("d", emptyPath);
2643
+ if (this.sourceArrow !== null) {
2644
+ sourceArrowPath = "";
2645
+ this.sourceArrow.setAttribute("d", sourceArrowPath);
2646
+ }
2647
+ if (this.targetArrow !== null) {
2648
+ targetArrowPath = "";
2649
+ this.targetArrow.setAttribute("d", targetArrowPath);
2650
+ }
2651
+ this.afterRenderEmitter.emit({
2652
+ edgePath: { path: emptyPath, midpoint },
2655
2653
  sourceArrowPath,
2656
2654
  targetArrowPath
2657
2655
  });
2658
2656
  }
2659
2657
  }
2658
+ const boxPortOffsetFn = (params) => {
2659
+ const { direction, radius } = params;
2660
+ const { x, y } = direction;
2661
+ const { horizontal, vertical } = radius;
2662
+ const tg = y / x;
2663
+ const horX = Math.abs(vertical / tg);
2664
+ const vertY = Math.abs(horizontal * tg);
2665
+ const minX = Math.min(horizontal, horX);
2666
+ const minY = Math.min(vertical, vertY);
2667
+ return Math.sqrt(minX * minX + minY * minY);
2668
+ };
2660
2669
  const createEdgeGroup = () => {
2661
2670
  const group = document.createElementNS("http://www.w3.org/2000/svg", "g");
2662
2671
  group.style.pointerEvents = "auto";
@@ -7113,5 +7122,6 @@ var __publicField = (obj, key, value) => __defNormalProp(obj, typeof key !== "sy
7113
7122
  exports2.MidpointEdgeShape = MidpointEdgeShape;
7114
7123
  exports2.StraightEdgeShape = StraightEdgeShape;
7115
7124
  exports2.VerticalEdgeShape = VerticalEdgeShape;
7125
+ exports2.boxPortOffsetFn = boxPortOffsetFn;
7116
7126
  Object.defineProperty(exports2, Symbol.toStringTag, { value: "Module" });
7117
7127
  });
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.0",
5
+ "version": "8.20.0",
6
6
  "type": "module",
7
7
  "main": "dist/html-graph.js",
8
8
  "types": "dist/html-graph.d.ts",