@logicflow/core 2.1.1 → 2.1.3

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.
@@ -0,0 +1,121 @@
1
+ import LogicFlow from '../LogicFlow'
2
+ import { TransformModel } from './TransformModel'
3
+ import { Options as LFOptions } from '../options'
4
+ import EventEmitter from '../event/eventEmitter'
5
+ type PointTuple = LogicFlow.PointTuple
6
+
7
+ export class NestedTransformModel extends TransformModel {
8
+ parentTransform?: TransformModel
9
+
10
+ constructor(eventCenter: EventEmitter, options: LFOptions.Common) {
11
+ super(eventCenter, options)
12
+ this.parentTransform = options.parentTransform
13
+ }
14
+
15
+ /**
16
+ * 设置父级变换
17
+ * @param parentTransform 父级变换模型
18
+ */
19
+ setParentTransform(parentTransform: TransformModel) {
20
+ this.parentTransform = parentTransform
21
+ }
22
+
23
+ /**
24
+ * 获取累积的缩放值
25
+ * 计算包括所有父级的累积缩放
26
+ */
27
+ private getCumulativeScale(): { scaleX: number; scaleY: number } {
28
+ let scaleX = this.SCALE_X
29
+ let scaleY = this.SCALE_Y
30
+
31
+ if (this.parentTransform) {
32
+ if (this.parentTransform instanceof NestedTransformModel) {
33
+ const parentScale = this.parentTransform.getCumulativeScale()
34
+ scaleX *= parentScale.scaleX
35
+ scaleY *= parentScale.scaleY
36
+ } else {
37
+ scaleX *= this.parentTransform.SCALE_X
38
+ scaleY *= this.parentTransform.SCALE_Y
39
+ }
40
+ }
41
+
42
+ return { scaleX, scaleY }
43
+ }
44
+
45
+ /**
46
+ * 获取累积的平移值
47
+ * 计算包括所有父级的累积平移
48
+ */
49
+ private getCumulativeTranslate(): { translateX: number; translateY: number } {
50
+ let translateX = this.TRANSLATE_X
51
+ let translateY = this.TRANSLATE_Y
52
+
53
+ if (
54
+ this.parentTransform &&
55
+ this.parentTransform instanceof NestedTransformModel
56
+ ) {
57
+ const { scaleX, scaleY } = this.parentTransform.getCumulativeScale()
58
+ translateX = scaleX * translateX
59
+ translateY = scaleY * translateY
60
+ }
61
+
62
+ return { translateX, translateY }
63
+ }
64
+
65
+ /**
66
+ * 将最外层graph上的点基于缩放转换为canvasOverlay层上的点。
67
+ * 重写以支持嵌套变换
68
+ * @param point HTML点
69
+ */
70
+ HtmlPointToCanvasPoint(point: PointTuple): PointTuple {
71
+ const [x, y] = point
72
+ const { scaleX, scaleY } = this.getCumulativeScale()
73
+ const { translateX, translateY } = this.getCumulativeTranslate()
74
+
75
+ return [(x - translateX) / scaleX, (y - translateY) / scaleY]
76
+ }
77
+
78
+ /**
79
+ * 将最外层canvasOverlay层上的点基于缩放转换为graph上的点。
80
+ * 重写以支持嵌套变换
81
+ * @param point Canvas点
82
+ */
83
+ CanvasPointToHtmlPoint(point: PointTuple): PointTuple {
84
+ const [x, y] = point
85
+ const { scaleX, scaleY } = this.getCumulativeScale()
86
+ const { translateX, translateY } = this.getCumulativeTranslate()
87
+
88
+ return [x * scaleX + translateX, y * scaleY + translateY]
89
+ }
90
+
91
+ /**
92
+ * 将一个在canvas上的点,向x轴方向移动directionX距离,向y轴方向移动directionY距离。
93
+ * 重写以支持嵌套变换
94
+ * @param point 点
95
+ * @param directionX x轴距离
96
+ * @param directionY y轴距离
97
+ */
98
+ moveCanvasPointByHtml(
99
+ point: PointTuple,
100
+ directionX: number,
101
+ directionY: number,
102
+ ): PointTuple {
103
+ const [x, y] = point
104
+ const { scaleX, scaleY } = this.getCumulativeScale()
105
+
106
+ return [x + directionX / scaleX, y + directionY / scaleY]
107
+ }
108
+
109
+ /**
110
+ * 根据缩放情况,获取缩放后的delta距离
111
+ * 重写以支持嵌套变换
112
+ * @param deltaX x轴距离变化
113
+ * @param deltaY y轴距离变化
114
+ */
115
+ fixDeltaXY(deltaX: number, deltaY: number): PointTuple {
116
+ const { scaleX, scaleY } = this.getCumulativeScale()
117
+ return [deltaX / scaleX, deltaY / scaleY]
118
+ }
119
+ }
120
+
121
+ export default NestedTransformModel
@@ -4,5 +4,6 @@ export * from './node'
4
4
  export * from './BaseModel'
5
5
  export * from './EditConfigModel'
6
6
  export * from './GraphModel'
7
+ export * from './NestedTransformModel'
7
8
  export * from './SnaplineModel'
8
9
  export * from './TransformModel'
package/src/options.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import type { TransformModel } from './model'
2
+
1
3
  import { assign } from 'lodash-es'
2
4
  import { createElement as h } from 'preact/compat'
3
5
  import LogicFlow from './LogicFlow'
@@ -104,6 +106,9 @@ export namespace Options {
104
106
 
105
107
  customTrajectory?: (props: CustomAnchorLineProps) => h.JSX.Element
106
108
  themeMode?: 'radius' | 'dark' | 'colorful' // 主题模式
109
+
110
+ parentTransform?: TransformModel // 父级变换模型,用于嵌套变换
111
+
107
112
  [key: string]: unknown
108
113
  }
109
114
 
@@ -124,14 +124,23 @@ export abstract class BaseEdge<P extends IProps> extends Component<
124
124
  const { refY = 0, refX = 2 } = model.getArrowStyle()
125
125
  const [start, end] = this.getLastTwoPoints()
126
126
  let theta: string | number = 'auto'
127
- if (start !== null && end !== null) {
128
- theta = degrees(
129
- getThetaOfVector({
130
- x: end.x - start.x,
131
- y: end.y - start.y,
132
- z: 0,
133
- }),
134
- )
127
+ // 防止无效点和零长度向量以避免 NaN 方向
128
+ if (start && end) {
129
+ const dx = end.x - start.x
130
+ const dy = end.y - start.y
131
+ // 仅在有实际方向时才计算
132
+ if (dx !== 0 || dy !== 0) {
133
+ const computed = degrees(
134
+ getThetaOfVector({
135
+ x: dx,
136
+ y: dy,
137
+ z: 0,
138
+ }),
139
+ )
140
+ if (Number.isFinite(computed) && !Number.isNaN(computed)) {
141
+ theta = computed
142
+ }
143
+ }
135
144
  }
136
145
  return (
137
146
  <g>