@flowgram.ai/free-lines-plugin 0.4.3 → 0.4.5

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/index.js CHANGED
@@ -41,7 +41,6 @@ __export(src_exports, {
41
41
  LINE_OFFSET: () => LINE_OFFSET,
42
42
  LINE_PADDING: () => LINE_PADDING,
43
43
  LinesLayer: () => WorkflowLinesLayer,
44
- WorkflowArkLineContribution: () => WorkflowArkLineContribution,
45
44
  WorkflowBezierLineContribution: () => WorkflowBezierLineContribution,
46
45
  WorkflowFoldLineContribution: () => WorkflowFoldLineContribution,
47
46
  WorkflowLinesLayer: () => WorkflowLinesLayer,
@@ -300,7 +299,7 @@ var LINE_OFFSET = 6;
300
299
  var LINE_PADDING = 12;
301
300
 
302
301
  // src/create-free-lines-plugin.ts
303
- var import_free_layout_core10 = require("@flowgram.ai/free-layout-core");
302
+ var import_free_layout_core8 = require("@flowgram.ai/free-layout-core");
304
303
  var import_core3 = require("@flowgram.ai/core");
305
304
 
306
305
  // src/layer/workflow-lines-layer.tsx
@@ -375,12 +374,12 @@ var LineSVG = (props) => {
375
374
  const { position, reverse, hideArrow, vertical } = line;
376
375
  const renderData = line.getData(import_free_layout_core3.WorkflowLineRenderData);
377
376
  const { bounds, path: bezierPath } = renderData;
378
- const toRelative = (p) => ({
377
+ const toRelative2 = (p) => ({
379
378
  x: p.x - bounds.x + PADDING,
380
379
  y: p.y - bounds.y + PADDING
381
380
  });
382
- const fromPos = toRelative(position.from);
383
- const toPos = toRelative(position.to);
381
+ const fromPos = toRelative2(position.from);
382
+ const toPos = toRelative2(position.to);
384
383
  const arrowToPos = position.to.location === "top" ? { x: toPos.x, y: toPos.y - import_free_layout_core2.POINT_RADIUS } : { x: toPos.x - import_free_layout_core2.POINT_RADIUS, y: toPos.y };
385
384
  const arrowFromPos = position.from.location === "bottom" ? { x: fromPos.x, y: fromPos.y + import_free_layout_core2.POINT_RADIUS + LINE_OFFSET } : { x: fromPos.x + import_free_layout_core2.POINT_RADIUS + LINE_OFFSET, y: fromPos.y };
386
385
  const strokeWidth = selected ? line.uiState.strokeWidthSelected ?? STROKE_WIDTH_SLECTED : line.uiState.strokeWidth ?? STROKE_WIDTH;
@@ -589,7 +588,23 @@ var import_bezier_js = require("bezier-js");
589
588
  var import_utils2 = require("@flowgram.ai/utils");
590
589
  var import_free_layout_core5 = require("@flowgram.ai/free-layout-core");
591
590
 
591
+ // src/contributions/utils.ts
592
+ function toRelative(p, bbox) {
593
+ return {
594
+ x: p.x - bbox.x + LINE_PADDING,
595
+ y: p.y - bbox.y + LINE_PADDING
596
+ };
597
+ }
598
+
592
599
  // src/contributions/bezier/bezier-controls.ts
600
+ function getBezierEdgeCenter(fromPos, toPos, fromControl, toControl) {
601
+ const x = fromPos.x * 0.125 + fromControl.x * 0.375 + toControl.x * 0.375 + toPos.x * 0.125;
602
+ const y = fromPos.y * 0.125 + fromControl.y * 0.375 + toControl.y * 0.375 + toPos.y * 0.125;
603
+ return {
604
+ x,
605
+ y
606
+ };
607
+ }
593
608
  function getControlOffset(distance, curvature) {
594
609
  if (distance >= 0) {
595
610
  return 0.5 * distance;
@@ -644,7 +659,11 @@ function getBezierControlPoints(fromPos, toPos, curvature = 0.25) {
644
659
  y2: fromPos.y,
645
660
  curvature
646
661
  });
647
- return [fromControl, toControl];
662
+ const center = getBezierEdgeCenter(fromPos, toPos, fromControl, toControl);
663
+ return {
664
+ controls: [fromControl, toControl],
665
+ center
666
+ };
648
667
  }
649
668
 
650
669
  // src/contributions/bezier/index.ts
@@ -663,15 +682,22 @@ var WorkflowBezierLineContribution = class {
663
682
  }
664
683
  get bounds() {
665
684
  if (!this.data) {
666
- return new import_utils2.Rectangle();
685
+ return import_utils2.Rectangle.EMPTY;
667
686
  }
668
687
  return this.data.bbox;
669
688
  }
689
+ get center() {
690
+ return this.data?.center;
691
+ }
670
692
  update(params) {
671
693
  this.data = this.calcBezier(params.fromPos, params.toPos);
672
694
  }
673
695
  calcBezier(fromPos, toPos) {
674
- const controls = getBezierControlPoints(fromPos, toPos, this.entity.uiState.curvature);
696
+ const { controls, center } = getBezierControlPoints(
697
+ fromPos,
698
+ toPos,
699
+ this.entity.uiState.curvature
700
+ );
675
701
  const bezier = new import_bezier_js.Bezier([fromPos, ...controls, toPos]);
676
702
  const bbox = bezier.bbox();
677
703
  const bboxBounds = new import_utils2.Rectangle(
@@ -680,6 +706,7 @@ var WorkflowBezierLineContribution = class {
680
706
  bbox.x.max - bbox.x.min,
681
707
  bbox.y.max - bbox.y.min
682
708
  );
709
+ const centerPoint = toRelative(center, bboxBounds);
683
710
  const path = this.getPath({ bbox: bboxBounds, fromPos, toPos, controls });
684
711
  this.data = {
685
712
  fromPos,
@@ -687,19 +714,20 @@ var WorkflowBezierLineContribution = class {
687
714
  bezier,
688
715
  bbox: bboxBounds,
689
716
  controls,
690
- path
717
+ path,
718
+ center: {
719
+ ...center,
720
+ labelX: centerPoint.x,
721
+ labelY: centerPoint.y
722
+ }
691
723
  };
692
724
  return this.data;
693
725
  }
694
726
  getPath(params) {
695
727
  const { bbox } = params;
696
- const toRelative = (p) => ({
697
- x: p.x - bbox.x + LINE_PADDING,
698
- y: p.y - bbox.y + LINE_PADDING
699
- });
700
- const fromPos = toRelative(params.fromPos);
701
- const toPos = toRelative(params.toPos);
702
- const controls = params.controls.map((c) => toRelative(c));
728
+ const fromPos = toRelative(params.fromPos, bbox);
729
+ const toPos = toRelative(params.toPos, bbox);
730
+ const controls = params.controls.map((c) => toRelative(c, bbox));
703
731
  const shrink = this.entity.uiState.shrink;
704
732
  const renderFromPos = params.fromPos.location === "bottom" ? { x: fromPos.x, y: fromPos.y + shrink } : { x: fromPos.x + shrink, y: fromPos.y };
705
733
  const renderToPos = params.toPos.location === "top" ? { x: toPos.x, y: toPos.y - shrink } : { x: toPos.x - shrink, y: toPos.y };
@@ -710,12 +738,11 @@ var WorkflowBezierLineContribution = class {
710
738
  WorkflowBezierLineContribution.type = import_free_layout_core5.LineType.BEZIER;
711
739
 
712
740
  // src/contributions/fold/index.ts
713
- var import_utils4 = require("@flowgram.ai/utils");
741
+ var import_utils5 = require("@flowgram.ai/utils");
714
742
  var import_free_layout_core6 = require("@flowgram.ai/free-layout-core");
715
- var import_free_layout_core7 = require("@flowgram.ai/free-layout-core");
716
743
 
717
744
  // src/contributions/fold/fold-line.ts
718
- var import_utils3 = require("@flowgram.ai/utils");
745
+ var import_utils4 = require("@flowgram.ai/utils");
719
746
  var getPointToSegmentDistance = (point, segStart, segEnd) => {
720
747
  const { x: px, y: py } = point;
721
748
  const { x: x1, y: y1 } = segStart;
@@ -766,10 +793,7 @@ var FoldLine;
766
793
  top: { x: 0, y: -1 },
767
794
  bottom: { x: 0, y: 1 }
768
795
  };
769
- function getPoints({
770
- source,
771
- target
772
- }) {
796
+ function getPoints({ source, target }) {
773
797
  const sourceDir = handleDirections[source.location];
774
798
  const targetDir = handleDirections[target.location];
775
799
  const sourceGapped = {
@@ -851,13 +875,19 @@ var FoldLine;
851
875
  { x: targetGapped.x, y: targetGapped.y },
852
876
  target
853
877
  ];
854
- return pathPoints;
878
+ return {
879
+ points: pathPoints,
880
+ center: {
881
+ x: centerX,
882
+ y: centerY
883
+ }
884
+ };
855
885
  }
856
886
  FoldLine2.getPoints = getPoints;
857
887
  function getBend(a, b, c) {
858
888
  const bendSize = Math.min(
859
- import_utils3.Point.getDistance(a, b) / 2,
860
- import_utils3.Point.getDistance(b, c) / 2,
889
+ import_utils4.Point.getDistance(a, b) / 2,
890
+ import_utils4.Point.getDistance(b, c) / 2,
861
891
  EDGE_RADIUS
862
892
  );
863
893
  const { x, y } = b;
@@ -894,7 +924,7 @@ var FoldLine;
894
924
  const right = Math.max(...xList);
895
925
  const top = Math.min(...yList);
896
926
  const bottom = Math.max(...yList);
897
- return import_utils3.Rectangle.createRectangleWithTwoPoints(
927
+ return import_utils4.Rectangle.createRectangleWithTwoPoints(
898
928
  {
899
929
  x: left,
900
930
  y: top
@@ -911,7 +941,7 @@ var FoldLine;
911
941
  return Infinity;
912
942
  }
913
943
  if (points.length === 1) {
914
- return import_utils3.Point.getDistance(points[0], pos);
944
+ return import_utils4.Point.getDistance(points[0], pos);
915
945
  }
916
946
  const lines = [];
917
947
  for (let i = 0; i < points.length - 1; i++) {
@@ -941,21 +971,25 @@ var WorkflowFoldLineContribution = class {
941
971
  }
942
972
  get bounds() {
943
973
  if (!this.data) {
944
- return new import_utils4.Rectangle();
974
+ return new import_utils5.Rectangle();
945
975
  }
946
976
  return this.data.bbox;
947
977
  }
978
+ get center() {
979
+ return this.data?.center;
980
+ }
948
981
  update(params) {
949
982
  const { fromPos, toPos } = params;
983
+ const shrink = this.entity.uiState.shrink;
950
984
  const sourceOffset = {
951
- x: fromPos.location === "bottom" ? 0 : import_free_layout_core6.POINT_RADIUS,
952
- y: fromPos.location === "bottom" ? import_free_layout_core6.POINT_RADIUS : 0
985
+ x: fromPos.location === "bottom" ? 0 : shrink,
986
+ y: fromPos.location === "bottom" ? shrink : 0
953
987
  };
954
988
  const targetOffset = {
955
- x: toPos.location === "top" ? 0 : -import_free_layout_core6.POINT_RADIUS,
956
- y: toPos.location === "top" ? -import_free_layout_core6.POINT_RADIUS : 0
989
+ x: toPos.location === "top" ? 0 : -shrink,
990
+ y: toPos.location === "top" ? -shrink : 0
957
991
  };
958
- const points = FoldLine.getPoints({
992
+ const { points, center } = FoldLine.getPoints({
959
993
  source: {
960
994
  x: fromPos.x + sourceOffset.x,
961
995
  y: fromPos.y + sourceOffset.y,
@@ -968,23 +1002,27 @@ var WorkflowFoldLineContribution = class {
968
1002
  }
969
1003
  });
970
1004
  const bbox = FoldLine.getBounds(points);
971
- const adjustedPoints = points.map((p) => ({
972
- x: p.x - bbox.x + LINE_PADDING,
973
- y: p.y - bbox.y + LINE_PADDING
974
- }));
1005
+ const adjustedPoints = points.map((p) => toRelative(p, bbox));
975
1006
  const path = FoldLine.getSmoothStepPath(adjustedPoints);
1007
+ const relativeCenter = toRelative(center, bbox);
976
1008
  this.data = {
977
1009
  points,
978
1010
  path,
979
- bbox
1011
+ bbox,
1012
+ center: {
1013
+ x: center.x,
1014
+ y: center.y,
1015
+ labelX: relativeCenter.x,
1016
+ labelY: relativeCenter.y
1017
+ }
980
1018
  };
981
1019
  }
982
1020
  };
983
- WorkflowFoldLineContribution.type = import_free_layout_core7.LineType.LINE_CHART;
1021
+ WorkflowFoldLineContribution.type = import_free_layout_core6.LineType.LINE_CHART;
984
1022
 
985
1023
  // src/contributions/straight/index.ts
986
- var import_utils5 = require("@flowgram.ai/utils");
987
- var import_free_layout_core8 = require("@flowgram.ai/free-layout-core");
1024
+ var import_utils7 = require("@flowgram.ai/utils");
1025
+ var import_free_layout_core7 = require("@flowgram.ai/free-layout-core");
988
1026
 
989
1027
  // src/contributions/straight/point-on-line.ts
990
1028
  function projectPointOnLine(point, lineStart, lineEnd) {
@@ -1017,23 +1055,27 @@ var WorkflowStraightLineContribution = class {
1017
1055
  return Number.MAX_SAFE_INTEGER;
1018
1056
  }
1019
1057
  const [start, end] = this.data.points;
1020
- return import_utils5.Point.getDistance(pos, projectPointOnLine(pos, start, end));
1058
+ return import_utils7.Point.getDistance(pos, projectPointOnLine(pos, start, end));
1021
1059
  }
1022
1060
  get bounds() {
1023
1061
  if (!this.data) {
1024
- return new import_utils5.Rectangle();
1062
+ return new import_utils7.Rectangle();
1025
1063
  }
1026
1064
  return this.data.bbox;
1027
1065
  }
1066
+ get center() {
1067
+ return this.data?.center;
1068
+ }
1028
1069
  update(params) {
1029
1070
  const { fromPos, toPos } = params;
1071
+ const shrink = this.entity.uiState.shrink;
1030
1072
  const sourceOffset = {
1031
- x: fromPos.location === "bottom" ? 0 : import_free_layout_core8.POINT_RADIUS,
1032
- y: fromPos.location === "bottom" ? import_free_layout_core8.POINT_RADIUS : 0
1073
+ x: fromPos.location === "bottom" ? 0 : shrink,
1074
+ y: fromPos.location === "bottom" ? shrink : 0
1033
1075
  };
1034
1076
  const targetOffset = {
1035
- x: toPos.location === "top" ? 0 : -import_free_layout_core8.POINT_RADIUS,
1036
- y: toPos.location === "top" ? -import_free_layout_core8.POINT_RADIUS : 0
1077
+ x: toPos.location === "top" ? 0 : -shrink,
1078
+ y: toPos.location === "top" ? -shrink : 0
1037
1079
  };
1038
1080
  const points = [
1039
1081
  {
@@ -1045,7 +1087,7 @@ var WorkflowStraightLineContribution = class {
1045
1087
  y: toPos.y + targetOffset.y
1046
1088
  }
1047
1089
  ];
1048
- const bbox = import_utils5.Rectangle.createRectangleWithTwoPoints(points[0], points[1]);
1090
+ const bbox = import_utils7.Rectangle.createRectangleWithTwoPoints(points[0], points[1]);
1049
1091
  const adjustedPoints = points.map((p) => ({
1050
1092
  x: p.x - bbox.x + LINE_PADDING,
1051
1093
  y: p.y - bbox.y + LINE_PADDING
@@ -1054,98 +1096,12 @@ var WorkflowStraightLineContribution = class {
1054
1096
  this.data = {
1055
1097
  points,
1056
1098
  path,
1057
- bbox
1058
- };
1059
- }
1060
- };
1061
- WorkflowStraightLineContribution.type = "WorkflowStraightLineContribution";
1062
-
1063
- // src/contributions/arc/index.ts
1064
- var import_utils6 = require("@flowgram.ai/utils");
1065
- var import_free_layout_core9 = require("@flowgram.ai/free-layout-core");
1066
- var WorkflowArkLineContribution = class {
1067
- constructor(entity) {
1068
- this.entity = entity;
1069
- }
1070
- get path() {
1071
- return this.data?.path ?? "";
1072
- }
1073
- calcDistance(pos) {
1074
- if (!this.data) {
1075
- return Number.MAX_SAFE_INTEGER;
1076
- }
1077
- const { fromPos, toPos, bbox } = this.data;
1078
- if (!bbox.contains(pos.x, pos.y)) {
1079
- const dx = Math.max(bbox.x - pos.x, 0, pos.x - (bbox.x + bbox.width));
1080
- const dy = Math.max(bbox.y - pos.y, 0, pos.y - (bbox.y + bbox.height));
1081
- return Math.sqrt(dx * dx + dy * dy);
1082
- }
1083
- const center = {
1084
- x: (fromPos.x + toPos.x) / 2,
1085
- y: (fromPos.y + toPos.y) / 2
1086
- };
1087
- const radius = import_utils6.Point.getDistance(fromPos, center);
1088
- const distanceToCenter = import_utils6.Point.getDistance(pos, center);
1089
- return Math.abs(distanceToCenter - radius);
1090
- }
1091
- get bounds() {
1092
- if (!this.data) {
1093
- return new import_utils6.Rectangle();
1094
- }
1095
- return this.data.bbox;
1096
- }
1097
- update(params) {
1098
- const { fromPos, toPos } = params;
1099
- const { vertical } = this.entity;
1100
- const sourceOffset = {
1101
- x: vertical ? 0 : import_free_layout_core9.POINT_RADIUS,
1102
- y: vertical ? import_free_layout_core9.POINT_RADIUS : 0
1103
- };
1104
- const targetOffset = {
1105
- x: vertical ? 0 : -import_free_layout_core9.POINT_RADIUS,
1106
- y: vertical ? -import_free_layout_core9.POINT_RADIUS : 0
1107
- };
1108
- const start = {
1109
- x: fromPos.x + sourceOffset.x,
1110
- y: fromPos.y + sourceOffset.y
1111
- };
1112
- const end = {
1113
- x: toPos.x + targetOffset.x,
1114
- y: toPos.y + targetOffset.y
1115
- };
1116
- const bbox = this.calculateArcBBox(start, end);
1117
- const path = this.getArcPath(start, end, bbox);
1118
- this.data = {
1119
- fromPos: start,
1120
- toPos: end,
1121
- path,
1122
- bbox
1123
- };
1124
- }
1125
- calculateArcBBox(start, end) {
1126
- const dx = end.x - start.x;
1127
- const dy = end.y - start.y;
1128
- const radius = Math.sqrt(dx * dx + dy * dy) / 2;
1129
- const centerX = (start.x + end.x) / 2;
1130
- const centerY = (start.y + end.y) / 2;
1131
- return new import_utils6.Rectangle(centerX - radius, centerY - radius, radius * 2, radius * 2);
1132
- }
1133
- getArcPath(start, end, bbox) {
1134
- const dx = end.x - start.x;
1135
- const dy = end.y - start.y;
1136
- const distance = Math.sqrt(dx * dx + dy * dy);
1137
- const startRel = {
1138
- x: start.x - bbox.x + LINE_PADDING,
1139
- y: start.y - bbox.y + LINE_PADDING
1140
- };
1141
- const endRel = {
1142
- x: end.x - bbox.x + LINE_PADDING,
1143
- y: end.y - bbox.y + LINE_PADDING
1099
+ bbox,
1100
+ center: (0, import_free_layout_core7.getLineCenter)(fromPos, toPos, bbox, LINE_PADDING)
1144
1101
  };
1145
- return `M ${startRel.x} ${startRel.y} A ${distance / 2} ${distance / 2} 0 0 1 ${endRel.x} ${endRel.y}`;
1146
1102
  }
1147
1103
  };
1148
- WorkflowArkLineContribution.type = "WorkflowArkLineContribution";
1104
+ WorkflowStraightLineContribution.type = import_free_layout_core7.LineType.STRAIGHT;
1149
1105
 
1150
1106
  // src/create-free-lines-plugin.ts
1151
1107
  var createFreeLinesPlugin = (0, import_core3.definePluginCreator)({
@@ -1156,8 +1112,8 @@ var createFreeLinesPlugin = (0, import_core3.definePluginCreator)({
1156
1112
  });
1157
1113
  },
1158
1114
  onReady: (ctx, opts) => {
1159
- const linesManager = ctx.container.get(import_free_layout_core10.WorkflowLinesManager);
1160
- linesManager.registerContribution(WorkflowBezierLineContribution).registerContribution(WorkflowFoldLineContribution);
1115
+ const linesManager = ctx.container.get(import_free_layout_core8.WorkflowLinesManager);
1116
+ linesManager.registerContribution(WorkflowBezierLineContribution).registerContribution(WorkflowFoldLineContribution).registerContribution(WorkflowStraightLineContribution);
1161
1117
  if (opts.contributions) {
1162
1118
  opts.contributions.forEach((contribution) => {
1163
1119
  linesManager.registerContribution(contribution);
@@ -1173,7 +1129,6 @@ var createFreeLinesPlugin = (0, import_core3.definePluginCreator)({
1173
1129
  LINE_OFFSET,
1174
1130
  LINE_PADDING,
1175
1131
  LinesLayer,
1176
- WorkflowArkLineContribution,
1177
1132
  WorkflowBezierLineContribution,
1178
1133
  WorkflowFoldLineContribution,
1179
1134
  WorkflowLinesLayer,