@flowgram.ai/free-lines-plugin 0.4.2 → 0.4.4

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/dist/esm/index.js CHANGED
@@ -346,12 +346,12 @@ var LineSVG = (props) => {
346
346
  const { position, reverse, hideArrow, vertical } = line;
347
347
  const renderData = line.getData(WorkflowLineRenderData);
348
348
  const { bounds, path: bezierPath } = renderData;
349
- const toRelative = (p) => ({
349
+ const toRelative2 = (p) => ({
350
350
  x: p.x - bounds.x + PADDING,
351
351
  y: p.y - bounds.y + PADDING
352
352
  });
353
- const fromPos = toRelative(position.from);
354
- const toPos = toRelative(position.to);
353
+ const fromPos = toRelative2(position.from);
354
+ const toPos = toRelative2(position.to);
355
355
  const arrowToPos = position.to.location === "top" ? { x: toPos.x, y: toPos.y - POINT_RADIUS } : { x: toPos.x - POINT_RADIUS, y: toPos.y };
356
356
  const arrowFromPos = position.from.location === "bottom" ? { x: fromPos.x, y: fromPos.y + POINT_RADIUS + LINE_OFFSET } : { x: fromPos.x + POINT_RADIUS + LINE_OFFSET, y: fromPos.y };
357
357
  const strokeWidth = selected ? line.uiState.strokeWidthSelected ?? STROKE_WIDTH_SLECTED : line.uiState.strokeWidth ?? STROKE_WIDTH;
@@ -560,7 +560,23 @@ import { Bezier } from "bezier-js";
560
560
  import { Point, Rectangle } from "@flowgram.ai/utils";
561
561
  import { LineType } from "@flowgram.ai/free-layout-core";
562
562
 
563
+ // src/contributions/utils.ts
564
+ function toRelative(p, bbox) {
565
+ return {
566
+ x: p.x - bbox.x + LINE_PADDING,
567
+ y: p.y - bbox.y + LINE_PADDING
568
+ };
569
+ }
570
+
563
571
  // src/contributions/bezier/bezier-controls.ts
572
+ function getBezierEdgeCenter(fromPos, toPos, fromControl, toControl) {
573
+ const x = fromPos.x * 0.125 + fromControl.x * 0.375 + toControl.x * 0.375 + toPos.x * 0.125;
574
+ const y = fromPos.y * 0.125 + fromControl.y * 0.375 + toControl.y * 0.375 + toPos.y * 0.125;
575
+ return {
576
+ x,
577
+ y
578
+ };
579
+ }
564
580
  function getControlOffset(distance, curvature) {
565
581
  if (distance >= 0) {
566
582
  return 0.5 * distance;
@@ -615,7 +631,11 @@ function getBezierControlPoints(fromPos, toPos, curvature = 0.25) {
615
631
  y2: fromPos.y,
616
632
  curvature
617
633
  });
618
- return [fromControl, toControl];
634
+ const center = getBezierEdgeCenter(fromPos, toPos, fromControl, toControl);
635
+ return {
636
+ controls: [fromControl, toControl],
637
+ center
638
+ };
619
639
  }
620
640
 
621
641
  // src/contributions/bezier/index.ts
@@ -634,15 +654,22 @@ var WorkflowBezierLineContribution = class {
634
654
  }
635
655
  get bounds() {
636
656
  if (!this.data) {
637
- return new Rectangle();
657
+ return Rectangle.EMPTY;
638
658
  }
639
659
  return this.data.bbox;
640
660
  }
661
+ get center() {
662
+ return this.data?.center;
663
+ }
641
664
  update(params) {
642
665
  this.data = this.calcBezier(params.fromPos, params.toPos);
643
666
  }
644
667
  calcBezier(fromPos, toPos) {
645
- const controls = getBezierControlPoints(fromPos, toPos, this.entity.uiState.curvature);
668
+ const { controls, center } = getBezierControlPoints(
669
+ fromPos,
670
+ toPos,
671
+ this.entity.uiState.curvature
672
+ );
646
673
  const bezier = new Bezier([fromPos, ...controls, toPos]);
647
674
  const bbox = bezier.bbox();
648
675
  const bboxBounds = new Rectangle(
@@ -651,6 +678,7 @@ var WorkflowBezierLineContribution = class {
651
678
  bbox.x.max - bbox.x.min,
652
679
  bbox.y.max - bbox.y.min
653
680
  );
681
+ const centerPoint = toRelative(center, bboxBounds);
654
682
  const path = this.getPath({ bbox: bboxBounds, fromPos, toPos, controls });
655
683
  this.data = {
656
684
  fromPos,
@@ -658,19 +686,20 @@ var WorkflowBezierLineContribution = class {
658
686
  bezier,
659
687
  bbox: bboxBounds,
660
688
  controls,
661
- path
689
+ path,
690
+ center: {
691
+ ...center,
692
+ labelX: centerPoint.x,
693
+ labelY: centerPoint.y
694
+ }
662
695
  };
663
696
  return this.data;
664
697
  }
665
698
  getPath(params) {
666
699
  const { bbox } = params;
667
- const toRelative = (p) => ({
668
- x: p.x - bbox.x + LINE_PADDING,
669
- y: p.y - bbox.y + LINE_PADDING
670
- });
671
- const fromPos = toRelative(params.fromPos);
672
- const toPos = toRelative(params.toPos);
673
- const controls = params.controls.map((c) => toRelative(c));
700
+ const fromPos = toRelative(params.fromPos, bbox);
701
+ const toPos = toRelative(params.toPos, bbox);
702
+ const controls = params.controls.map((c) => toRelative(c, bbox));
674
703
  const shrink = this.entity.uiState.shrink;
675
704
  const renderFromPos = params.fromPos.location === "bottom" ? { x: fromPos.x, y: fromPos.y + shrink } : { x: fromPos.x + shrink, y: fromPos.y };
676
705
  const renderToPos = params.toPos.location === "top" ? { x: toPos.x, y: toPos.y - shrink } : { x: toPos.x - shrink, y: toPos.y };
@@ -682,9 +711,6 @@ WorkflowBezierLineContribution.type = LineType.BEZIER;
682
711
 
683
712
  // src/contributions/fold/index.ts
684
713
  import { Rectangle as Rectangle3 } from "@flowgram.ai/utils";
685
- import {
686
- POINT_RADIUS as POINT_RADIUS2
687
- } from "@flowgram.ai/free-layout-core";
688
714
  import { LineType as LineType2 } from "@flowgram.ai/free-layout-core";
689
715
 
690
716
  // src/contributions/fold/fold-line.ts
@@ -739,10 +765,7 @@ var FoldLine;
739
765
  top: { x: 0, y: -1 },
740
766
  bottom: { x: 0, y: 1 }
741
767
  };
742
- function getPoints({
743
- source,
744
- target
745
- }) {
768
+ function getPoints({ source, target }) {
746
769
  const sourceDir = handleDirections[source.location];
747
770
  const targetDir = handleDirections[target.location];
748
771
  const sourceGapped = {
@@ -824,7 +847,13 @@ var FoldLine;
824
847
  { x: targetGapped.x, y: targetGapped.y },
825
848
  target
826
849
  ];
827
- return pathPoints;
850
+ return {
851
+ points: pathPoints,
852
+ center: {
853
+ x: centerX,
854
+ y: centerY
855
+ }
856
+ };
828
857
  }
829
858
  FoldLine2.getPoints = getPoints;
830
859
  function getBend(a, b, c) {
@@ -918,17 +947,21 @@ var WorkflowFoldLineContribution = class {
918
947
  }
919
948
  return this.data.bbox;
920
949
  }
950
+ get center() {
951
+ return this.data?.center;
952
+ }
921
953
  update(params) {
922
954
  const { fromPos, toPos } = params;
955
+ const shrink = this.entity.uiState.shrink;
923
956
  const sourceOffset = {
924
- x: fromPos.location === "bottom" ? 0 : POINT_RADIUS2,
925
- y: fromPos.location === "bottom" ? POINT_RADIUS2 : 0
957
+ x: fromPos.location === "bottom" ? 0 : shrink,
958
+ y: fromPos.location === "bottom" ? shrink : 0
926
959
  };
927
960
  const targetOffset = {
928
- x: toPos.location === "top" ? 0 : -POINT_RADIUS2,
929
- y: toPos.location === "top" ? -POINT_RADIUS2 : 0
961
+ x: toPos.location === "top" ? 0 : -shrink,
962
+ y: toPos.location === "top" ? -shrink : 0
930
963
  };
931
- const points = FoldLine.getPoints({
964
+ const { points, center } = FoldLine.getPoints({
932
965
  source: {
933
966
  x: fromPos.x + sourceOffset.x,
934
967
  y: fromPos.y + sourceOffset.y,
@@ -941,15 +974,19 @@ var WorkflowFoldLineContribution = class {
941
974
  }
942
975
  });
943
976
  const bbox = FoldLine.getBounds(points);
944
- const adjustedPoints = points.map((p) => ({
945
- x: p.x - bbox.x + LINE_PADDING,
946
- y: p.y - bbox.y + LINE_PADDING
947
- }));
977
+ const adjustedPoints = points.map((p) => toRelative(p, bbox));
948
978
  const path = FoldLine.getSmoothStepPath(adjustedPoints);
979
+ const relativeCenter = toRelative(center, bbox);
949
980
  this.data = {
950
981
  points,
951
982
  path,
952
- bbox
983
+ bbox,
984
+ center: {
985
+ x: center.x,
986
+ y: center.y,
987
+ labelX: relativeCenter.x,
988
+ labelY: relativeCenter.y
989
+ }
953
990
  };
954
991
  }
955
992
  };
@@ -958,7 +995,8 @@ WorkflowFoldLineContribution.type = LineType2.LINE_CHART;
958
995
  // src/contributions/straight/index.ts
959
996
  import { Point as Point3, Rectangle as Rectangle4 } from "@flowgram.ai/utils";
960
997
  import {
961
- POINT_RADIUS as POINT_RADIUS3
998
+ getLineCenter,
999
+ LineType as LineType3
962
1000
  } from "@flowgram.ai/free-layout-core";
963
1001
 
964
1002
  // src/contributions/straight/point-on-line.ts
@@ -1000,15 +1038,19 @@ var WorkflowStraightLineContribution = class {
1000
1038
  }
1001
1039
  return this.data.bbox;
1002
1040
  }
1041
+ get center() {
1042
+ return this.data?.center;
1043
+ }
1003
1044
  update(params) {
1004
1045
  const { fromPos, toPos } = params;
1046
+ const shrink = this.entity.uiState.shrink;
1005
1047
  const sourceOffset = {
1006
- x: fromPos.location === "bottom" ? 0 : POINT_RADIUS3,
1007
- y: fromPos.location === "bottom" ? POINT_RADIUS3 : 0
1048
+ x: fromPos.location === "bottom" ? 0 : shrink,
1049
+ y: fromPos.location === "bottom" ? shrink : 0
1008
1050
  };
1009
1051
  const targetOffset = {
1010
- x: toPos.location === "top" ? 0 : -POINT_RADIUS3,
1011
- y: toPos.location === "top" ? -POINT_RADIUS3 : 0
1052
+ x: toPos.location === "top" ? 0 : -shrink,
1053
+ y: toPos.location === "top" ? -shrink : 0
1012
1054
  };
1013
1055
  const points = [
1014
1056
  {
@@ -1029,100 +1071,12 @@ var WorkflowStraightLineContribution = class {
1029
1071
  this.data = {
1030
1072
  points,
1031
1073
  path,
1032
- bbox
1033
- };
1034
- }
1035
- };
1036
- WorkflowStraightLineContribution.type = "WorkflowStraightLineContribution";
1037
-
1038
- // src/contributions/arc/index.ts
1039
- import { Point as Point4, Rectangle as Rectangle5 } from "@flowgram.ai/utils";
1040
- import {
1041
- POINT_RADIUS as POINT_RADIUS4
1042
- } from "@flowgram.ai/free-layout-core";
1043
- var WorkflowArkLineContribution = class {
1044
- constructor(entity) {
1045
- this.entity = entity;
1046
- }
1047
- get path() {
1048
- return this.data?.path ?? "";
1049
- }
1050
- calcDistance(pos) {
1051
- if (!this.data) {
1052
- return Number.MAX_SAFE_INTEGER;
1053
- }
1054
- const { fromPos, toPos, bbox } = this.data;
1055
- if (!bbox.contains(pos.x, pos.y)) {
1056
- const dx = Math.max(bbox.x - pos.x, 0, pos.x - (bbox.x + bbox.width));
1057
- const dy = Math.max(bbox.y - pos.y, 0, pos.y - (bbox.y + bbox.height));
1058
- return Math.sqrt(dx * dx + dy * dy);
1059
- }
1060
- const center = {
1061
- x: (fromPos.x + toPos.x) / 2,
1062
- y: (fromPos.y + toPos.y) / 2
1063
- };
1064
- const radius = Point4.getDistance(fromPos, center);
1065
- const distanceToCenter = Point4.getDistance(pos, center);
1066
- return Math.abs(distanceToCenter - radius);
1067
- }
1068
- get bounds() {
1069
- if (!this.data) {
1070
- return new Rectangle5();
1071
- }
1072
- return this.data.bbox;
1073
- }
1074
- update(params) {
1075
- const { fromPos, toPos } = params;
1076
- const { vertical } = this.entity;
1077
- const sourceOffset = {
1078
- x: vertical ? 0 : POINT_RADIUS4,
1079
- y: vertical ? POINT_RADIUS4 : 0
1080
- };
1081
- const targetOffset = {
1082
- x: vertical ? 0 : -POINT_RADIUS4,
1083
- y: vertical ? -POINT_RADIUS4 : 0
1084
- };
1085
- const start = {
1086
- x: fromPos.x + sourceOffset.x,
1087
- y: fromPos.y + sourceOffset.y
1088
- };
1089
- const end = {
1090
- x: toPos.x + targetOffset.x,
1091
- y: toPos.y + targetOffset.y
1092
- };
1093
- const bbox = this.calculateArcBBox(start, end);
1094
- const path = this.getArcPath(start, end, bbox);
1095
- this.data = {
1096
- fromPos: start,
1097
- toPos: end,
1098
- path,
1099
- bbox
1100
- };
1101
- }
1102
- calculateArcBBox(start, end) {
1103
- const dx = end.x - start.x;
1104
- const dy = end.y - start.y;
1105
- const radius = Math.sqrt(dx * dx + dy * dy) / 2;
1106
- const centerX = (start.x + end.x) / 2;
1107
- const centerY = (start.y + end.y) / 2;
1108
- return new Rectangle5(centerX - radius, centerY - radius, radius * 2, radius * 2);
1109
- }
1110
- getArcPath(start, end, bbox) {
1111
- const dx = end.x - start.x;
1112
- const dy = end.y - start.y;
1113
- const distance = Math.sqrt(dx * dx + dy * dy);
1114
- const startRel = {
1115
- x: start.x - bbox.x + LINE_PADDING,
1116
- y: start.y - bbox.y + LINE_PADDING
1117
- };
1118
- const endRel = {
1119
- x: end.x - bbox.x + LINE_PADDING,
1120
- y: end.y - bbox.y + LINE_PADDING
1074
+ bbox,
1075
+ center: getLineCenter(fromPos, toPos, bbox, LINE_PADDING)
1121
1076
  };
1122
- return `M ${startRel.x} ${startRel.y} A ${distance / 2} ${distance / 2} 0 0 1 ${endRel.x} ${endRel.y}`;
1123
1077
  }
1124
1078
  };
1125
- WorkflowArkLineContribution.type = "WorkflowArkLineContribution";
1079
+ WorkflowStraightLineContribution.type = LineType3.STRAIGHT;
1126
1080
 
1127
1081
  // src/create-free-lines-plugin.ts
1128
1082
  var createFreeLinesPlugin = definePluginCreator({
@@ -1134,7 +1088,7 @@ var createFreeLinesPlugin = definePluginCreator({
1134
1088
  },
1135
1089
  onReady: (ctx, opts) => {
1136
1090
  const linesManager = ctx.container.get(WorkflowLinesManager2);
1137
- linesManager.registerContribution(WorkflowBezierLineContribution).registerContribution(WorkflowFoldLineContribution);
1091
+ linesManager.registerContribution(WorkflowBezierLineContribution).registerContribution(WorkflowFoldLineContribution).registerContribution(WorkflowStraightLineContribution);
1138
1092
  if (opts.contributions) {
1139
1093
  opts.contributions.forEach((contribution) => {
1140
1094
  linesManager.registerContribution(contribution);
@@ -1149,7 +1103,6 @@ export {
1149
1103
  LINE_OFFSET,
1150
1104
  LINE_PADDING,
1151
1105
  WorkflowLinesLayer as LinesLayer,
1152
- WorkflowArkLineContribution,
1153
1106
  WorkflowBezierLineContribution,
1154
1107
  WorkflowFoldLineContribution,
1155
1108
  WorkflowLinesLayer,