Skip to content

Commit fc563ec

Browse files
committed
fix: 修复 GraphModel 中 getAreaElement 方法的 bug 及一些其他类型定义的优化
1 parent 29a3eb0 commit fc563ec

File tree

5 files changed

+38
-49
lines changed

5 files changed

+38
-49
lines changed

packages/core/src/LogicFlow.tsx

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createElement as h, render, Component } from 'preact/compat'
22
import { observer } from 'mobx-preact'
3+
import { cloneDeep, forEach } from 'lodash-es'
34
import { Options as LFOptions } from './options'
45
import {
56
BaseNodeModel,
@@ -741,15 +742,15 @@ export class LogicFlow {
741742
nodes: [],
742743
edges: [],
743744
}
744-
for (let i = 0; i < nodes.length; i++) {
745-
const node = nodes[i]
745+
forEach(nodes, (node) => {
746746
const preId = node.id
747747
const nodeModel = this.addNode(node)
748748
if (!nodeModel) return
749749
if (preId) nodeIdMap[preId] = nodeModel.id
750750
elements.nodes.push(nodeModel)
751-
}
752-
edges.forEach((edge) => {
751+
})
752+
753+
forEach(edges, (edge) => {
753754
let sourceId = edge.sourceNodeId
754755
let targetId = edge.targetNodeId
755756
if (nodeIdMap[sourceId]) sourceId = nodeIdMap[sourceId]
@@ -1246,10 +1247,11 @@ export class LogicFlow {
12461247
* @param graphData 图数据
12471248
*/
12481249
render(graphData: GraphConfigData) {
1250+
let graphRawData = cloneDeep(graphData)
12491251
if (this.adapterIn) {
1250-
graphData = this.adapterIn(graphData)
1252+
graphRawData = this.adapterIn(graphRawData)
12511253
}
1252-
this.renderRawData(graphData)
1254+
this.renderRawData(graphRawData)
12531255
}
12541256

12551257
/**
@@ -1339,8 +1341,8 @@ export namespace LogicFlow {
13391341
}
13401342

13411343
export interface GraphConfigData {
1342-
nodes: NodeConfig[]
1343-
edges: EdgeConfig[]
1344+
nodes?: NodeConfig[]
1345+
edges?: EdgeConfig[]
13441346
}
13451347

13461348
export interface GraphData {
@@ -1383,7 +1385,7 @@ export namespace LogicFlow {
13831385

13841386
export interface EdgeConfig {
13851387
id?: string
1386-
type: string // TODO: 将所有类型选项列出来;LogicFlow 内部默认为 polyline
1388+
type?: string // TODO: 将所有类型选项列出来;LogicFlow 内部默认为 polyline
13871389

13881390
sourceNodeId: string
13891391
sourceAnchorId?: string
@@ -1404,6 +1406,8 @@ export namespace LogicFlow {
14041406
type: string
14051407
text?: TextConfig
14061408

1409+
startPoint: Point
1410+
endPoint: Point
14071411
[key: string]: unknown
14081412
}
14091413

packages/core/src/model/GraphModel.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ export class GraphModel {
275275
wholeEdge,
276276
wholeNode,
277277
)
278-
if (!ignoreHideElement || (element.visible && isElementInArea)) {
278+
if ((!ignoreHideElement || element.visible) && isElementInArea) {
279279
areaElements.push(element)
280280
}
281281
})
@@ -396,7 +396,7 @@ export class GraphModel {
396396
* 注意:将会清除画布上所有已有的节点和边
397397
* @param { object } graphData 图数据
398398
*/
399-
graphDataToModel(graphData: LogicFlow.GraphConfigData) {
399+
graphDataToModel(graphData: Partial<LogicFlow.GraphConfigData>) {
400400
if (!this.width || !this.height) {
401401
this.resize()
402402
}
@@ -414,7 +414,7 @@ export class GraphModel {
414414
}
415415
if (graphData.edges) {
416416
this.edges = map(graphData.edges, (edge) => {
417-
const Model = this.getModel(edge.type)
417+
const Model = this.getModel(edge.type ?? '')
418418
if (!Model) {
419419
throw new Error(`找不到${edge.type}对应的边。`)
420420
}
@@ -875,6 +875,8 @@ export class GraphModel {
875875
},
876876
this,
877877
)
878+
console.log('edgeModel', edgeModel)
879+
878880
const edgeData = edgeModel.getData()
879881
this.edges.push(edgeModel)
880882
this.eventCenter.emit(EventType.EDGE_ADD, { data: edgeData })

packages/core/src/model/edge/BaseEdgeModel.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ export class BaseEdgeModel implements IBaseEdgeModel {
5252
@observable type = ''
5353
@observable sourceNodeId = ''
5454
@observable targetNodeId = ''
55-
@observable startPoint: Point = { x: 0, y: 0 }
56-
@observable endPoint: Point = { x: 0, y: 0 }
55+
@observable startPoint!: Point
56+
@observable endPoint!: Point
5757

5858
@observable text = {
5959
value: '',
@@ -325,6 +325,8 @@ export class BaseEdgeModel implements IBaseEdgeModel {
325325
)
326326
}
327327
targetAnchors.forEach((anchor) => {
328+
if (!this.startPoint) return // 如果此时 this.startPoint 为 undefined,直接返回
329+
328330
const distance = twoPointDistance(anchor, this.startPoint)
329331
if (minDistance === undefined) {
330332
minDistance = distance
@@ -623,8 +625,10 @@ export class BaseEdgeModel implements IBaseEdgeModel {
623625

624626
@action
625627
moveStartPoint(deltaX, deltaY): void {
626-
this.startPoint.x += deltaX
627-
this.startPoint.y += deltaY
628+
if (this.startPoint) {
629+
this.startPoint.x += deltaX
630+
this.startPoint.y += deltaY
631+
}
628632
}
629633

630634
@action
@@ -634,8 +638,10 @@ export class BaseEdgeModel implements IBaseEdgeModel {
634638

635639
@action
636640
moveEndPoint(deltaX, deltaY): void {
637-
this.endPoint.x += deltaX
638-
this.endPoint.y += deltaY
641+
if (this.endPoint) {
642+
this.endPoint.x += deltaX
643+
this.endPoint.y += deltaY
644+
}
639645
}
640646

641647
@action

packages/core/src/view/edge/BaseEdge.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ export class BaseEdge extends Component<IProps> {
104104
const { id } = model
105105
const { refY = 0, refX = 2 } = model.getArrowStyle()
106106
const [start, end] = this.getLastTwoPoints()
107+
console.log('start', start)
108+
console.log('end', end)
107109
let theta: string | number = 'auto'
108110
if (start !== null && end !== null) {
109111
theta = degrees(
@@ -114,6 +116,7 @@ export class BaseEdge extends Component<IProps> {
114116
}),
115117
)
116118
}
119+
console.log('theta', theta)
117120
return (
118121
<g>
119122
<defs>

pnpm-lock.yaml

Lines changed: 5 additions & 31 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)