@logicflow/extension 2.0.0-beta.5 → 2.0.0-beta.7
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/.turbo/turbo-build.log +750 -34
- package/dist/index.css +63 -0
- package/dist/index.min.js +32 -2
- package/es/NodeResize/control/Control.js +3 -2
- package/es/NodeResize/control/Control.js.map +1 -1
- package/es/bpmn-elements/presets/Pool/Pool.d.ts +21 -1
- package/es/components/mini-map/index.js +2 -2
- package/es/components/mini-map/index.js.map +1 -1
- package/es/components/selection-select/index.js.map +1 -1
- package/es/index.css +63 -0
- package/es/index.d.ts +1 -0
- package/es/index.js +1 -0
- package/es/index.js.map +1 -1
- package/es/materials/group/GroupNode.d.ts +6 -6
- package/es/materials/group/GroupNode.js +7 -6
- package/es/materials/group/GroupNode.js.map +1 -1
- package/es/materials/group/index.js +20 -25
- package/es/materials/group/index.js.map +1 -1
- package/es/style/index.css +63 -0
- package/es/style/index.less +73 -0
- package/es/style/raw.d.ts +1 -1
- package/es/style/raw.js +1 -1
- package/es/style/raw.js.map +1 -1
- package/es/tools/flow-path/index.js +0 -1
- package/es/tools/flow-path/index.js.map +1 -1
- package/es/tools/label/Label.d.ts +30 -0
- package/es/tools/label/Label.js +241 -0
- package/es/tools/label/Label.js.map +1 -0
- package/es/tools/label/LabelModel.d.ts +26 -0
- package/es/tools/label/LabelModel.js +86 -0
- package/es/tools/label/LabelModel.js.map +1 -0
- package/es/tools/label/LabelOverlay.d.ts +28 -0
- package/es/tools/label/LabelOverlay.js +161 -0
- package/es/tools/label/LabelOverlay.js.map +1 -0
- package/es/tools/label/algorithm.d.ts +16 -0
- package/es/tools/label/algorithm.js +27 -0
- package/es/tools/label/algorithm.js.map +1 -0
- package/es/tools/label/index.d.ts +59 -0
- package/es/tools/label/index.js +292 -0
- package/es/tools/label/index.js.map +1 -0
- package/es/tools/label/mediumEditor.d.ts +16 -0
- package/es/tools/label/mediumEditor.js +91 -0
- package/es/tools/label/mediumEditor.js.map +1 -0
- package/es/tools/label/utils.d.ts +64 -0
- package/es/tools/label/utils.js +336 -0
- package/es/tools/label/utils.js.map +1 -0
- package/es/tools/snapshot/index.js +0 -2
- package/es/tools/snapshot/index.js.map +1 -1
- package/lib/NodeResize/control/Control.js +3 -2
- package/lib/NodeResize/control/Control.js.map +1 -1
- package/lib/bpmn-elements/presets/Pool/Pool.d.ts +21 -1
- package/lib/components/mini-map/index.js +2 -2
- package/lib/components/mini-map/index.js.map +1 -1
- package/lib/components/selection-select/index.js.map +1 -1
- package/lib/index.css +63 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/index.js.map +1 -1
- package/lib/materials/group/GroupNode.d.ts +6 -6
- package/lib/materials/group/GroupNode.js +7 -6
- package/lib/materials/group/GroupNode.js.map +1 -1
- package/lib/materials/group/index.js +19 -24
- package/lib/materials/group/index.js.map +1 -1
- package/lib/style/index.css +63 -0
- package/lib/style/index.less +73 -0
- package/lib/style/raw.d.ts +1 -1
- package/lib/style/raw.js +1 -1
- package/lib/style/raw.js.map +1 -1
- package/lib/tools/flow-path/index.js +0 -1
- package/lib/tools/flow-path/index.js.map +1 -1
- package/lib/tools/label/Label.d.ts +30 -0
- package/lib/tools/label/Label.js +247 -0
- package/lib/tools/label/Label.js.map +1 -0
- package/lib/tools/label/LabelModel.d.ts +26 -0
- package/lib/tools/label/LabelModel.js +89 -0
- package/lib/tools/label/LabelModel.js.map +1 -0
- package/lib/tools/label/LabelOverlay.d.ts +28 -0
- package/lib/tools/label/LabelOverlay.js +167 -0
- package/lib/tools/label/LabelOverlay.js.map +1 -0
- package/lib/tools/label/algorithm.d.ts +16 -0
- package/lib/tools/label/algorithm.js +32 -0
- package/lib/tools/label/algorithm.js.map +1 -0
- package/lib/tools/label/index.d.ts +59 -0
- package/lib/tools/label/index.js +298 -0
- package/lib/tools/label/index.js.map +1 -0
- package/lib/tools/label/mediumEditor.d.ts +16 -0
- package/lib/tools/label/mediumEditor.js +97 -0
- package/lib/tools/label/mediumEditor.js.map +1 -0
- package/lib/tools/label/utils.d.ts +64 -0
- package/lib/tools/label/utils.js +349 -0
- package/lib/tools/label/utils.js.map +1 -0
- package/lib/tools/snapshot/index.js +0 -2
- package/lib/tools/snapshot/index.js.map +1 -1
- package/package.json +9 -9
- package/src/NodeResize/control/Control.tsx +3 -2
- package/src/components/mini-map/index.ts +2 -2
- package/src/components/selection-select/index.ts +5 -1
- package/src/index.ts +1 -0
- package/src/materials/group/GroupNode.ts +11 -8
- package/src/materials/group/index.ts +33 -38
- package/src/style/index.less +73 -0
- package/src/style/raw.ts +64 -1
- package/src/tools/flow-path/index.ts +0 -1
- package/src/tools/label/Label.tsx +297 -0
- package/src/tools/label/LabelModel.ts +82 -0
- package/src/tools/label/LabelOverlay.tsx +159 -0
- package/src/tools/label/algorithm.ts +42 -0
- package/src/tools/label/index.ts +401 -0
- package/src/tools/label/mediumEditor.ts +93 -0
- package/src/tools/label/utils.ts +395 -0
- package/src/tools/snapshot/README.md +27 -16
- package/src/tools/snapshot/index.ts +0 -2
|
@@ -0,0 +1,395 @@
|
|
|
1
|
+
import LogicFlow, {
|
|
2
|
+
BaseEdgeModel,
|
|
3
|
+
BaseNodeModel,
|
|
4
|
+
getNodeOutline,
|
|
5
|
+
getEdgeOutline,
|
|
6
|
+
Model,
|
|
7
|
+
ModelType,
|
|
8
|
+
BezierEdgeModel,
|
|
9
|
+
isInSegment,
|
|
10
|
+
getBezierPoints,
|
|
11
|
+
points2PointsList,
|
|
12
|
+
getBBoxOfPoints,
|
|
13
|
+
} from '@logicflow/core'
|
|
14
|
+
import { head, isEmpty, last, min } from 'lodash-es'
|
|
15
|
+
import { calcTwoPointsDistance, getPointOnBezier } from './algorithm'
|
|
16
|
+
|
|
17
|
+
import Point = LogicFlow.Point
|
|
18
|
+
import LabelConfig = LogicFlow.LabelConfig
|
|
19
|
+
import OutlineInfo = Model.OutlineInfo
|
|
20
|
+
|
|
21
|
+
export type BBoxInfo = {
|
|
22
|
+
x: number
|
|
23
|
+
y: number
|
|
24
|
+
width: number
|
|
25
|
+
height: number
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
// 工具函数:计算「缩放」后 某坐标点 相对中心位置比例不变的 新坐标点
|
|
29
|
+
// 前提条件: 当缩放一个矩形时,如果你希望矩形中的某个点的位置相对于矩形保持不变
|
|
30
|
+
//
|
|
31
|
+
// 1. 原始矩形的左上角坐标为 (x1, y1),宽度为 w1,高度为 h1。
|
|
32
|
+
// 2. 缩放后的矩形的左上角坐标为 (x2, y2),宽度为 w2,高度为 h2。
|
|
33
|
+
// 3. 矩形中的某个点在原始矩形中的坐标为 (px1, py1)。
|
|
34
|
+
//
|
|
35
|
+
// 目标
|
|
36
|
+
// 计算该点在缩放后矩形中的新坐标 (px2, py2)。
|
|
37
|
+
//
|
|
38
|
+
// 步骤
|
|
39
|
+
// 1. 计算相对位置:首先计算点 (px1, py1) 在原始矩形中的相对位置。
|
|
40
|
+
// relativeX = (px1 - x1) / w1
|
|
41
|
+
// relativeY = (py1 - y1) / h1
|
|
42
|
+
//
|
|
43
|
+
// 2. 计算新坐标:然后,使用相对位置计算该点在缩放后矩形中的新坐标。
|
|
44
|
+
// px2 = x2 + relativeX * w2
|
|
45
|
+
// py2 = y2 + relativeY * h2
|
|
46
|
+
export function calcPointAfterResize(
|
|
47
|
+
origin: BBoxInfo,
|
|
48
|
+
scaled: BBoxInfo,
|
|
49
|
+
point: Point,
|
|
50
|
+
): Point {
|
|
51
|
+
const { x: x1, y: y1, width: w1, height: h1 } = origin
|
|
52
|
+
const { x: x2, y: y2, width: w2, height: h2 } = scaled
|
|
53
|
+
const { x: px1, y: py1 } = point
|
|
54
|
+
|
|
55
|
+
// 计算点在原始矩形中的相对位置
|
|
56
|
+
const relativeX = (px1 - x1) / w1
|
|
57
|
+
const relativeY = (py1 - y1) / h1
|
|
58
|
+
|
|
59
|
+
// 计算点在缩放后矩形中的新坐标
|
|
60
|
+
const px2 = x2 + relativeX * w2
|
|
61
|
+
const py2 = y2 + relativeY * h2
|
|
62
|
+
|
|
63
|
+
return { x: px2, y: py2 }
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
// 工具函数:计算「旋转」后 某坐标点 相对中心位置比例不变的 新坐标点
|
|
67
|
+
// 要计算以点 x1 = (x1, y1) 为中心,点 x2 = (x2, y2) 旋转 θ 度后的坐标位置,可以使用旋转矩阵进行计算。
|
|
68
|
+
//
|
|
69
|
+
// 旋转公式如下:
|
|
70
|
+
// 1. 首先将点 x2 平移到以 x1 为原点的坐标系:
|
|
71
|
+
// x' = x2 - x1
|
|
72
|
+
// y' = y2 - y1
|
|
73
|
+
// 2. 然后应用旋转矩阵进行旋转:
|
|
74
|
+
// x'' = x' * cos(θ) - y' * sin(θ)
|
|
75
|
+
// y'' = x' * sin(θ) + y' * cos(θ)
|
|
76
|
+
// 3. 最后将结果平移回原来的坐标系:
|
|
77
|
+
// x_new = x'' + x1
|
|
78
|
+
// y_new = y'' + y1
|
|
79
|
+
//
|
|
80
|
+
// 综合起来,旋转后的新坐标 (x_new, y_new) 计算公式如下:
|
|
81
|
+
//
|
|
82
|
+
// x_new = (x2 - x1) * cos(θ) - (y2 - y1) * sin(θ) + x1
|
|
83
|
+
// y_new = (x2 - x1) * sin(θ) + (y2 - y1) * cos(θ) + y1
|
|
84
|
+
//
|
|
85
|
+
// 其中,θ 需要用弧度表示,如果你有的是角度,可以用以下公式转换为弧度:
|
|
86
|
+
//
|
|
87
|
+
// rad = deg * π / 180
|
|
88
|
+
export function rotatePointAroundCenter(
|
|
89
|
+
target: Point,
|
|
90
|
+
center: Point,
|
|
91
|
+
radian: number,
|
|
92
|
+
): Point {
|
|
93
|
+
// Rotate point (x2, y2) around point (x1, y1) by theta degrees.
|
|
94
|
+
//
|
|
95
|
+
// Parameters:
|
|
96
|
+
// x1, y1: Coordinates of the center point.
|
|
97
|
+
// x2, y2: Coordinates of the point to rotate.
|
|
98
|
+
// theta_degrees: Angle in degrees to rotate the point.
|
|
99
|
+
//
|
|
100
|
+
// Returns:
|
|
101
|
+
// Tuple of new coordinates (x_new, y_new) after rotation.
|
|
102
|
+
|
|
103
|
+
const { x: x1, y: y1 } = center
|
|
104
|
+
const { x: x2, y: y2 } = target
|
|
105
|
+
|
|
106
|
+
// Translate point to origin
|
|
107
|
+
const xPrime = x2 - x1
|
|
108
|
+
const yPrime = y2 - y1
|
|
109
|
+
|
|
110
|
+
// Rotate point
|
|
111
|
+
const xDoublePrime = xPrime * Math.cos(radian) - yPrime * Math.sin(radian)
|
|
112
|
+
const yDoublePrime = xPrime * Math.sin(radian) + yPrime * Math.cos(radian)
|
|
113
|
+
|
|
114
|
+
// Translate point back
|
|
115
|
+
const xNew = xDoublePrime + x1
|
|
116
|
+
const yNew = yDoublePrime + y1
|
|
117
|
+
|
|
118
|
+
return {
|
|
119
|
+
x: xNew,
|
|
120
|
+
y: yNew,
|
|
121
|
+
}
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Edge 相关工具方法 */
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* 获取某点在一个矩形图形(节点 or 边的 outline)内的偏移量
|
|
128
|
+
* @param point 目标点(此处即 Label 的坐标信息)
|
|
129
|
+
* @param element 目标元素
|
|
130
|
+
*/
|
|
131
|
+
export const getPointOffsetOfElementOutline = (
|
|
132
|
+
point: Point,
|
|
133
|
+
element: BaseNodeModel | BaseEdgeModel,
|
|
134
|
+
) => {
|
|
135
|
+
const baseType = element.BaseType
|
|
136
|
+
const bboxInfo: OutlineInfo | undefined =
|
|
137
|
+
baseType === 'node' ? getNodeOutline(element) : getEdgeOutline(element)
|
|
138
|
+
|
|
139
|
+
if (bboxInfo) {
|
|
140
|
+
const { x, y } = point
|
|
141
|
+
const { x: minX, y: minY, x1: maxX, y1: maxY } = bboxInfo
|
|
142
|
+
let xDeltaPercent: number = 0.5
|
|
143
|
+
let yDeltaPercent: number = 0.5
|
|
144
|
+
let xDeltaDistance: number = x - maxX
|
|
145
|
+
let yDeltaDistance: number = y - maxY
|
|
146
|
+
/**
|
|
147
|
+
* 文本在由路径点组成的凸包内,就记录偏移比例
|
|
148
|
+
* 文本在凸包外,记录绝对距离
|
|
149
|
+
* 用于边路径变化时计算文本新位置
|
|
150
|
+
*/
|
|
151
|
+
if (minX && maxX && minX < x && x < maxX) {
|
|
152
|
+
xDeltaPercent = min([(x - minX) / (maxX - minX), 1]) as number
|
|
153
|
+
} else if (x <= minX) {
|
|
154
|
+
xDeltaDistance = x - minX
|
|
155
|
+
} else {
|
|
156
|
+
xDeltaDistance = x - maxX
|
|
157
|
+
}
|
|
158
|
+
if (minY && maxY && minY < y && y < maxY) {
|
|
159
|
+
yDeltaPercent = min([(y - minY) / (maxY - minY), 1]) as number
|
|
160
|
+
} else if (y <= minY) {
|
|
161
|
+
yDeltaDistance = y - minY
|
|
162
|
+
} else {
|
|
163
|
+
yDeltaDistance = y - maxY
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
xDeltaPercent,
|
|
167
|
+
yDeltaPercent,
|
|
168
|
+
xDeltaDistance,
|
|
169
|
+
yDeltaDistance,
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/**
|
|
175
|
+
* 判断节点是否在折线上
|
|
176
|
+
* @param point 目标点坐标
|
|
177
|
+
* @param points 折线上的点坐标
|
|
178
|
+
*/
|
|
179
|
+
const isPointOnPolyline = (point: Point, points: Point[]): boolean => {
|
|
180
|
+
for (let i = 0; i < points.length - 1; i++) {
|
|
181
|
+
const start = points[i]
|
|
182
|
+
const end = points[i + 1]
|
|
183
|
+
if (isInSegment(point, start, end)) {
|
|
184
|
+
return true
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
return false
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
/**
|
|
192
|
+
* 给定一个点 P = (x_0, y_0) 和线段的两个端点 A = (x_1, y_1) 和 B = (x_2, y_2) ,可以使用以下步骤计算点到线段的距离:
|
|
193
|
+
* 1. 计算向量 AB 和 AP 。
|
|
194
|
+
* 2. 计算 AB 的平方长度。
|
|
195
|
+
* 3. 计算点 P 在直线 AB 上的投影点 Q 。
|
|
196
|
+
* 4. 判断 Q 是否在线段 AB 上。
|
|
197
|
+
* 5. 根据 Q 是否在线段上,计算点到线段的距离。
|
|
198
|
+
*
|
|
199
|
+
* 计算点到线段质检的距离
|
|
200
|
+
* @param point
|
|
201
|
+
* @param start
|
|
202
|
+
* @param end
|
|
203
|
+
*/
|
|
204
|
+
export const pointToSegmentDistance = (
|
|
205
|
+
point: Point,
|
|
206
|
+
start: Point,
|
|
207
|
+
end: Point,
|
|
208
|
+
): number => {
|
|
209
|
+
const { x: px, y: py } = point
|
|
210
|
+
const { x: sx, y: sy } = start
|
|
211
|
+
const { x: ex, y: ey } = end
|
|
212
|
+
|
|
213
|
+
const SEx = ex - sx
|
|
214
|
+
const SEy = ey - sy
|
|
215
|
+
const SPx = px - sx
|
|
216
|
+
const SPy = py - sy
|
|
217
|
+
|
|
218
|
+
const SE_SE = SEx ** 2 + SEy ** 2
|
|
219
|
+
const SP_SE = SPx * SEx + SPy * SEy
|
|
220
|
+
let t = SP_SE / SE_SE
|
|
221
|
+
if (t < 0) t = 0
|
|
222
|
+
if (t > 1) t = 1
|
|
223
|
+
|
|
224
|
+
const qx = sx + t * SEx
|
|
225
|
+
const qy = sy + t * SEy
|
|
226
|
+
return Math.sqrt((px - qx) ** 2 + (py - qy) ** 2)
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export const calcPolylineTotalLength = (points: Point[]) => {
|
|
230
|
+
let length = 0
|
|
231
|
+
for (let i = 0; i < points.length - 1; i++) {
|
|
232
|
+
const start = points[i]
|
|
233
|
+
const end = points[i + 1]
|
|
234
|
+
length += calcTwoPointsDistance(start, end)
|
|
235
|
+
}
|
|
236
|
+
return length
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* TODO: 确认该函数的意义,写完还是没看懂什么意思
|
|
241
|
+
* @param point
|
|
242
|
+
* @param points
|
|
243
|
+
*/
|
|
244
|
+
export const pointPositionRatio = (point: Point, points: Point[]): number => {
|
|
245
|
+
let length = 0
|
|
246
|
+
for (let i = 0; i < points.length - 1; i++) {
|
|
247
|
+
const start = points[i]
|
|
248
|
+
const end = points[i + 1]
|
|
249
|
+
const segmentLength = calcTwoPointsDistance(start, end)
|
|
250
|
+
|
|
251
|
+
if (pointToSegmentDistance(point, start, end) <= 20) {
|
|
252
|
+
const d1 = calcTwoPointsDistance(point, start)
|
|
253
|
+
length += d1
|
|
254
|
+
const totalLength = calcPolylineTotalLength(points)
|
|
255
|
+
// 小数点后保留一位(四舍五入)
|
|
256
|
+
return Math.round((length / totalLength) * 10) / 10
|
|
257
|
+
} else {
|
|
258
|
+
length += segmentLength
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
return 0
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
/**
|
|
265
|
+
* 计算一个坐标在贝塞尔曲线上最近的一个点
|
|
266
|
+
* @param point
|
|
267
|
+
* @param edge
|
|
268
|
+
* @param step
|
|
269
|
+
*/
|
|
270
|
+
export const calcClosestPointOnBezierEdge = (
|
|
271
|
+
point: Point,
|
|
272
|
+
edge: BezierEdgeModel,
|
|
273
|
+
step: number = 5,
|
|
274
|
+
): Point => {
|
|
275
|
+
let minDistance = Infinity
|
|
276
|
+
let closestPoint: Point = point
|
|
277
|
+
|
|
278
|
+
const pointsList = getBezierPoints(edge.path)
|
|
279
|
+
if (isEmpty(pointsList)) return closestPoint
|
|
280
|
+
|
|
281
|
+
const [start, sNext, ePre, end] = pointsList
|
|
282
|
+
for (let i = 0; i <= step; i++) {
|
|
283
|
+
const t = i / step
|
|
284
|
+
const bezierPoint = getPointOnBezier(t, start, sNext, ePre, end)
|
|
285
|
+
const distance = calcTwoPointsDistance(point, bezierPoint)
|
|
286
|
+
if (distance < minDistance) {
|
|
287
|
+
minDistance = distance
|
|
288
|
+
closestPoint = bezierPoint
|
|
289
|
+
}
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
return closestPoint
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
export const getNewPointAtDistance = (
|
|
296
|
+
points: Point[],
|
|
297
|
+
ratio: number,
|
|
298
|
+
): Point | undefined => {
|
|
299
|
+
const totalLength = calcPolylineTotalLength(points)
|
|
300
|
+
const targetLength = totalLength * ratio
|
|
301
|
+
|
|
302
|
+
let length = 0
|
|
303
|
+
for (let i = 0; i < points.length - 1; i++) {
|
|
304
|
+
const start = points[i]
|
|
305
|
+
const end = points[i + 1]
|
|
306
|
+
const segmentLength = calcTwoPointsDistance(start, end)
|
|
307
|
+
if (length + segmentLength >= targetLength) {
|
|
308
|
+
const ratio = (targetLength - length) / segmentLength
|
|
309
|
+
return {
|
|
310
|
+
x: start.x + (end.x - start.x) * ratio,
|
|
311
|
+
y: start.y + (end.y - start.y) * ratio,
|
|
312
|
+
}
|
|
313
|
+
} else {
|
|
314
|
+
length += segmentLength
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
return last(points)
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
/**
|
|
321
|
+
* 计算一个坐标离折线(包括 PolylineEdge 和 LineEdge 直线)最近的一个点
|
|
322
|
+
* @param point
|
|
323
|
+
* @param edge
|
|
324
|
+
*/
|
|
325
|
+
export const calcLabelPositionOnPolyline = (
|
|
326
|
+
point: Point,
|
|
327
|
+
edge: BaseEdgeModel,
|
|
328
|
+
): Point => {
|
|
329
|
+
let points = points2PointsList(edge.points)
|
|
330
|
+
if (points.length === 0) {
|
|
331
|
+
points = [edge.startPoint, edge.endPoint]
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
const { xDeltaPercent, yDeltaPercent, yDeltaDistance, xDeltaDistance } =
|
|
335
|
+
getPointOffsetOfElementOutline(point, edge) ?? {}
|
|
336
|
+
const isPointOnEdge = isPointOnPolyline(point, points)
|
|
337
|
+
const ratio = pointPositionRatio(point, points)
|
|
338
|
+
|
|
339
|
+
const start = head(points)
|
|
340
|
+
const end = last(points)
|
|
341
|
+
|
|
342
|
+
// 分别取路径中,x轴 和 y轴上的最大最小坐标值组合成一个矩形
|
|
343
|
+
const { minX, minY, maxX, maxY } = getBBoxOfPoints(points, 10)
|
|
344
|
+
|
|
345
|
+
if (!start || !end) return point
|
|
346
|
+
|
|
347
|
+
if (xDeltaPercent && yDeltaPercent) {
|
|
348
|
+
const positByPercent = {
|
|
349
|
+
x: minX + (maxX - minX) * xDeltaPercent,
|
|
350
|
+
y: minY + (maxY - minY) * yDeltaPercent,
|
|
351
|
+
}
|
|
352
|
+
return isPointOnEdge
|
|
353
|
+
? getNewPointAtDistance(points, ratio) ?? point // 函数什么意思
|
|
354
|
+
: positByPercent
|
|
355
|
+
}
|
|
356
|
+
// 如果文本在凸包的上方或者下方
|
|
357
|
+
if (xDeltaPercent && yDeltaDistance) {
|
|
358
|
+
return {
|
|
359
|
+
x: minX + (maxX - minX) * xDeltaPercent,
|
|
360
|
+
y: yDeltaDistance < 0 ? minY + yDeltaDistance : maxY + yDeltaDistance,
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
// 如果文本在凸包的左边或者右边
|
|
364
|
+
if (yDeltaPercent && xDeltaDistance) {
|
|
365
|
+
return {
|
|
366
|
+
x: xDeltaDistance < 0 ? minX + xDeltaDistance : maxX + xDeltaDistance,
|
|
367
|
+
y: minY + (maxY - minY) * yDeltaPercent,
|
|
368
|
+
}
|
|
369
|
+
}
|
|
370
|
+
// 如果文本在凸包左上/左下/右上/右下
|
|
371
|
+
if (xDeltaDistance && yDeltaDistance) {
|
|
372
|
+
return {
|
|
373
|
+
x: xDeltaDistance < 0 ? minX + xDeltaDistance : maxX + xDeltaDistance,
|
|
374
|
+
y: yDeltaDistance < 0 ? minY + yDeltaDistance : maxY + yDeltaDistance,
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
// 兜底
|
|
378
|
+
return point
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* 计算 Label 离边最近的点的坐标,用于更新为 Label 的坐标
|
|
383
|
+
* @param label LabelConfig -> 当前 Label 的配置项
|
|
384
|
+
* @param edge
|
|
385
|
+
*/
|
|
386
|
+
export const getLabelPositionOfLine = (
|
|
387
|
+
label: LabelConfig,
|
|
388
|
+
edge: BaseEdgeModel,
|
|
389
|
+
): Point => {
|
|
390
|
+
const { x, y } = label
|
|
391
|
+
if (edge.modelType === ModelType.BEZIER_EDGE) {
|
|
392
|
+
return calcClosestPointOnBezierEdge({ x, y }, edge as BezierEdgeModel)
|
|
393
|
+
}
|
|
394
|
+
return calcLabelPositionOnPolyline({ x, y }, edge)
|
|
395
|
+
}
|
|
@@ -45,7 +45,9 @@ document.getElementById("button").addEventListener("click", () => {
|
|
|
45
45
|
|
|
46
46
|
当自定义元素在导出图片时需要额外添加 css 样式时,可以用如下方式实现:
|
|
47
47
|
|
|
48
|
-
为了保持流程图生成的图片与画布上效果一致,`snapshot`插件默认会将当前页面所有的 `css` 规则都加载到导出图片中,
|
|
48
|
+
为了保持流程图生成的图片与画布上效果一致,`snapshot`插件默认会将当前页面所有的 `css` 规则都加载到导出图片中,
|
|
49
|
+
但是可能会因为 css 文件跨域引起报错,参考 issue575。可以修改useGlobalRules来禁止加载所有 css
|
|
50
|
+
规则,然后通过`customCssRules`属性来自定义增加css样式。
|
|
49
51
|
|
|
50
52
|
```tsx
|
|
51
53
|
|
|
@@ -59,7 +61,7 @@ lf.extension.snapshot.customCssRules = `
|
|
|
59
61
|
color: blue;
|
|
60
62
|
}
|
|
61
63
|
`
|
|
62
|
-
|
|
64
|
+
|
|
63
65
|
```
|
|
64
66
|
|
|
65
67
|
## API
|
|
@@ -70,41 +72,50 @@ lf.extension.snapshot.customCssRules = `
|
|
|
70
72
|
|
|
71
73
|
```ts
|
|
72
74
|
|
|
73
|
-
getSnapshot(fileName?: string, toImageOptions?: ToImageOptions)
|
|
75
|
+
const getSnapshot = (fileName?: string, toImageOptions?: ToImageOptions): Promise<void> => {}
|
|
74
76
|
|
|
75
77
|
```
|
|
76
78
|
|
|
77
79
|
`fileName` 为文件名称,不填为默认为`logic-flow.当前时间戳`,`ToImageOptions` 描述如下:
|
|
78
80
|
|
|
79
|
-
| 属性名
|
|
80
|
-
|
|
81
|
-
| fileType
|
|
82
|
-
| width
|
|
83
|
-
| height
|
|
84
|
-
| backgroundColor | string
|
|
85
|
-
| quality
|
|
86
|
-
| padding
|
|
87
|
-
| partial
|
|
81
|
+
| 属性名 | 类型 | 默认值 | 必填 | 描述 |
|
|
82
|
+
|-----------------|---------|-----|----|----------------------------------------------------------------------------------------|
|
|
83
|
+
| fileType | string | png | | 图片类型: 默认不填是png 还可以设置有webp、gif、jpeg、svg |
|
|
84
|
+
| width | number | - | | 自定义导出图片的宽度,不设置即可,设置可能会拉伸图形 |
|
|
85
|
+
| height | number | - | | 自定义导出图片的宽度,不设置即可,设置可能会拉伸图形 |
|
|
86
|
+
| backgroundColor | string | - | | 图片背景,不设置背景默认透明 |
|
|
87
|
+
| quality | number | - | | 图片质量,在指定图片格式为 jpeg 或 webp 的情况下,可以从 0 到 1 的区间内选择图片的质量。如果超出取值范围,将会使用默认值 0.92。其他不合法参数会被忽略 |
|
|
88
|
+
| padding | number | 40 | | 图片内边距: 元素内容所在区之外空白空间,不设置默认有40的内边距 |
|
|
89
|
+
| partial | boolean | - | | 导出时是否开启局部渲染,false:将导出画布上所有的元素,true:只导出画面区域内的可见元素,不设置默认为lf实例身上partial值 |
|
|
88
90
|
|
|
89
91
|
注意:
|
|
90
|
-
|
|
92
|
+
|
|
93
|
+
- `svg`目前暂不支持`width`,`height`, `backgroundColor`, `padding` 属性。
|
|
91
94
|
- 自定义宽高后,可能会拉伸图形,这时候`padding`也会被拉伸导致不准确。
|
|
92
95
|
|
|
93
96
|
### lf.getSnapshotBlob(...)
|
|
94
97
|
|
|
95
|
-
`snapshot`
|
|
98
|
+
`snapshot`
|
|
99
|
+
除了支持图片类型导出,还支持下载<a href="https://developer.mozilla.org/zh-CN/docs/Web/API/Blob" target="_blank">
|
|
100
|
+
Blob文件对象 </a>
|
|
101
|
+
和 <a href="https://developer.mozilla.org/zh-CN/docs/Glossary/Base64" target="_blank">
|
|
102
|
+
Base64文本编码 </a>
|
|
96
103
|
|
|
97
104
|
获取`Blob`对象。
|
|
98
105
|
|
|
99
106
|
```ts
|
|
100
107
|
|
|
101
|
-
|
|
108
|
+
const getSnapshotBlob = async (
|
|
109
|
+
backgroundColor?: string,
|
|
110
|
+
fileType?: string
|
|
111
|
+
): Promise<SnapshotResponse> => {}
|
|
102
112
|
|
|
103
113
|
// example
|
|
104
|
-
const { data
|
|
114
|
+
const { data: blob } = await lf.getSnapshotBlob()
|
|
105
115
|
console.log(blob)
|
|
106
116
|
|
|
107
117
|
```
|
|
118
|
+
|
|
108
119
|
`backgroundColor`: 背景,不填默认为透明。
|
|
109
120
|
|
|
110
121
|
`fileType`: 文件类型,不填默认为png。
|
|
@@ -394,8 +394,6 @@ export class Snapshot {
|
|
|
394
394
|
width && height ? copyCanvas(canvas, width, height) : canvas,
|
|
395
395
|
)
|
|
396
396
|
}
|
|
397
|
-
// 如果局部渲染本来是开启的,继续开启
|
|
398
|
-
// partial && this.lf.graphModel.setPartial(true)
|
|
399
397
|
} catch (e) {
|
|
400
398
|
ctx?.drawImage(img, padding / dpr, padding / dpr)
|
|
401
399
|
resolve(width && height ? copyCanvas(canvas, width, height) : canvas)
|