Skip to content

Commit babbe9d

Browse files
committed
feat: 增加用户设置画布 100% ,在调整视窗时,画布大小同步更新的逻辑,优化体验
- 优化初始化时设置画布大小的逻辑 - 优化 DEMO 代码,解决设置 theme 未生效的 bug( 代码问题。在初始化时 style 覆盖了 defaultConfig 中的 style)
1 parent 9a1f0f6 commit babbe9d

File tree

6 files changed

+166
-78
lines changed

6 files changed

+166
-78
lines changed

examples/engine-browser-examples/src/pages/graph/index.less

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,20 @@
1010
}
1111

1212
.dnd-item {
13+
display: flex;
14+
align-items: center;
15+
justify-content: center;
1316
cursor: grab;
1417
user-select: none;
1518
}
1619

20+
.wrapper {
21+
width: 80px;
22+
height: 50px;
23+
background: #fff;
24+
border: 2px solid #000;
25+
}
26+
1727
.rect {
1828
width: 50px;
1929
height: 50px;

examples/engine-browser-examples/src/pages/graph/index.tsx

Lines changed: 123 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import '@logicflow/core/es/index.css'
44

55
import { Button, Card, Divider, Flex } from 'antd'
66
import { useEffect, useRef } from 'react'
7-
import { combine, square, star, uml, user, reactNode } from './nodes'
7+
import { combine, square, star, uml, user } from './nodes'
88
import { animation, connection } from './edges'
99

10-
import GraphConfigData = LogicFlow.GraphConfigData
10+
import GraphData = LogicFlow.GraphData
1111
import './index.less'
1212

13-
const defaultConfig: Partial<LogicFlow.Options> = {
13+
import OnDragNodeConfig = LogicFlow.OnDragNodeConfig
14+
15+
const config: Partial<LogicFlow.Options> = {
1416
isSilentMode: false,
1517
stopScrollGraph: true,
1618
stopZoomGraph: true,
@@ -38,7 +40,11 @@ const defaultConfig: Partial<LogicFlow.Options> = {
3840
},
3941
text: {
4042
color: '#b85450',
41-
fontSize: 14,
43+
fontSize: 12,
44+
},
45+
inputText: {
46+
background: 'black',
47+
color: 'white',
4248
},
4349
},
4450
}
@@ -57,7 +63,7 @@ const customTheme: Partial<LogicFlow.Theme> = {
5763
lineHeight: 1.5,
5864
fontSize: 13,
5965
textWidth: 100,
60-
}, // 确认 textWidth 是否必传
66+
}, // 确认 textWidth 是否必传
6167
polyline: {
6268
stroke: 'red',
6369
},
@@ -70,41 +76,40 @@ const customTheme: Partial<LogicFlow.Theme> = {
7076
verticalLength: 2, // 箭头垂直于边的距离
7177
},
7278
}
73-
const data: GraphConfigData = {
79+
const data = {
7480
nodes: [
7581
{
7682
id: 'custom-node-1',
7783
rotate: 1.1722738811284763,
7884
text: {
7985
x: 600,
8086
y: 200,
81-
value: 'xxxxx',
87+
value: 'node-1',
8288
},
8389
type: 'rect',
8490
x: 600,
8591
y: 200,
92+
properties: {
93+
width: 80,
94+
height: 120,
95+
},
8696
},
8797
{
88-
id: 'custom-react-node-1',
89-
text: {
90-
x: 200,
91-
y: 500,
92-
value: 'custom-react-node',
93-
},
94-
type: 'custom-react-node',
95-
x: 200,
96-
y: 500,
98+
id: 'custom-node-2',
99+
text: 'node-2',
100+
type: 'polygon',
101+
x: 90,
102+
y: 94,
97103
},
98104
],
99-
edges: [],
100105
}
101106

102107
export default function BasicNode() {
103108
const lfRef = useRef<LogicFlow>()
104109
const containerRef = useRef<HTMLDivElement>(null)
105110

106111
const registerElements = (lf: LogicFlow) => {
107-
const elements = [
112+
const elements: LogicFlow.RegisterConfig[] = [
108113
// edges
109114
animation,
110115
connection,
@@ -114,7 +119,6 @@ export default function BasicNode() {
114119
star,
115120
uml,
116121
user,
117-
reactNode,
118122
]
119123

120124
map(elements, (customElement) => {
@@ -131,10 +135,10 @@ export default function BasicNode() {
131135
useEffect(() => {
132136
if (!lfRef.current) {
133137
const lf = new LogicFlow({
134-
...defaultConfig,
138+
...config,
135139
container: containerRef.current as HTMLElement,
136140
// hideAnchors: true,
137-
// width: 1200,
141+
// width: 800,
138142
// height: 400,
139143
// adjustNodePosition: false,
140144
// isSilentMode: true,
@@ -150,6 +154,7 @@ export default function BasicNode() {
150154
adjustEdgeStartAndEnd: true,
151155
// adjustEdge: false,
152156
allowRotate: true,
157+
// allowResize: true,
153158
edgeTextEdit: true,
154159
keyboard: {
155160
enabled: true,
@@ -175,18 +180,12 @@ export default function BasicNode() {
175180
background: {
176181
color: '#FFFFFF',
177182
},
178-
// grid: true,
179-
grid: {
180-
size: 20,
181-
},
183+
grid: true,
184+
// grid: {
185+
// size: 1,
186+
// },
182187
edgeTextDraggable: true,
183188
edgeType: 'bezier',
184-
// style: {
185-
// inputText: {
186-
// background: 'black',
187-
// color: 'white',
188-
// },
189-
// },
190189
// 全局自定义id
191190
// edgeGenerator: (sourceNode, targetNode, currentEdge) => {
192191
// // 起始节点类型 rect 时使用 自定义的边 custom-edge
@@ -291,38 +290,23 @@ export default function BasicNode() {
291290

292291
const handleTurnAnimationOn = () => {
293292
if (lfRef.current) {
294-
const { edges } = lfRef.current.getGraphRawData()
293+
const { edges } = lfRef.current.getGraphData() as GraphData
295294
forEach(edges, (edge) => {
296295
lfRef.current?.openEdgeAnimation(edge.id)
297296
})
298297
}
299298
}
300299
const handleTurnAnimationOff = () => {
301300
if (lfRef.current) {
302-
const { edges } = lfRef.current.getGraphRawData()
301+
const { edges } = lfRef.current.getGraphData() as GraphData
303302
forEach(edges, (edge) => {
304303
lfRef.current?.closeEdgeAnimation(edge.id)
305304
})
306305
}
307306
}
308307

309-
const handleDragRect = () => {
310-
lfRef?.current?.dnd.startDrag({
311-
type: 'rect',
312-
text: 'rect',
313-
})
314-
}
315-
const handleDragCircle = () => {
316-
lfRef?.current?.dnd.startDrag({
317-
type: 'circle',
318-
text: 'circle',
319-
})
320-
}
321-
const handleDragText = () => {
322-
lfRef?.current?.dnd.startDrag({
323-
type: 'text',
324-
text: '文本节点',
325-
})
308+
const handleDragItem = (node: OnDragNodeConfig) => {
309+
lfRef?.current?.dnd.startDrag(node)
326310
}
327311

328312
return (
@@ -461,9 +445,95 @@ export default function BasicNode() {
461445
节点面板
462446
</Divider>
463447
<Flex wrap="wrap" gap="small" justify="center" align="center">
464-
<div className="rect dnd-item" onMouseDown={handleDragRect} />
465-
<div className="circle dnd-item" onMouseDown={handleDragCircle} />
466-
<div className="text dnd-item" onMouseDown={handleDragText}>
448+
<div
449+
className="dnd-item wrapper"
450+
onMouseDown={() =>
451+
handleDragItem({
452+
type: 'rect',
453+
text: 'rect',
454+
})
455+
}
456+
>
457+
rect
458+
</div>
459+
<div
460+
className="dnd-item wrapper"
461+
onMouseDown={() => {
462+
handleDragItem({
463+
type: 'circle',
464+
text: 'circle',
465+
})
466+
}}
467+
>
468+
circle
469+
</div>
470+
<div
471+
className="dnd-item wrapper"
472+
onMouseDown={() => {
473+
handleDragItem({
474+
type: 'diamond',
475+
text: 'diamond',
476+
})
477+
}}
478+
>
479+
diamond
480+
</div>
481+
<div
482+
className="dnd-item wrapper"
483+
onMouseDown={() => {
484+
handleDragItem({
485+
type: 'ellipse',
486+
text: 'ellipse',
487+
properties: {
488+
rx: 40,
489+
ry: 80,
490+
},
491+
})
492+
}}
493+
>
494+
ellipse
495+
</div>
496+
<div
497+
className="dnd-item wrapper"
498+
onMouseDown={() => {
499+
handleDragItem({
500+
type: 'html',
501+
text: 'html',
502+
})
503+
}}
504+
>
505+
html
506+
</div>
507+
<div
508+
className="dnd-item wrapper"
509+
onMouseDown={() => {
510+
handleDragItem({
511+
type: 'polygon',
512+
text: 'polygon',
513+
properties: {
514+
width: 110,
515+
height: 100,
516+
style: {
517+
fill: '#ffd591',
518+
stroke: '#ffa940',
519+
strokeWidth: 2,
520+
fillRule: 'evenodd',
521+
},
522+
},
523+
})
524+
}}
525+
>
526+
polygon
527+
</div>
528+
<div
529+
className="dnd-item text"
530+
onMouseDown={() => {
531+
handleDragItem({
532+
type: 'text',
533+
text: '文本',
534+
})
535+
}}
536+
>
467537
文本
468538
</div>
469539
</Flex>

examples/feature-examples/src/pages/graph/index.tsx

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ const config: Partial<LogicFlow.Options> = {
4242
color: '#b85450',
4343
fontSize: 12,
4444
},
45+
// 下面的 style 移动到此处,不然会覆盖上面设置的各图形的主题样式
46+
inputText: {
47+
background: 'black',
48+
color: 'white',
49+
},
4550
},
4651
}
4752

@@ -137,8 +142,8 @@ export default function BasicNode() {
137142
...config,
138143
container: containerRef.current as HTMLElement,
139144
// hideAnchors: true,
140-
// width: 1200,
141-
// height: 400,
145+
width: 800,
146+
height: 400,
142147
// adjustNodePosition: false,
143148
// isSilentMode: true,
144149
// overlapMode: 1,
@@ -185,12 +190,6 @@ export default function BasicNode() {
185190
// },
186191
edgeTextDraggable: true,
187192
edgeType: 'bezier',
188-
style: {
189-
inputText: {
190-
background: 'black',
191-
color: 'white',
192-
},
193-
},
194193
// 全局自定义id
195194
// edgeGenerator: (sourceNode, targetNode, currentEdge) => {
196195
// // 起始节点类型 rect 时使用 自定义的边 custom-edge

packages/core/src/LogicFlow.tsx

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,13 +87,17 @@ export class LogicFlow {
8787
// 支持插件在 LogicFlow 实例上增加自定义方法
8888
[propName: string]: any
8989

90-
private initContainer(container: HTMLElement | HTMLDivElement) {
90+
private initContainer(
91+
container: HTMLElement | HTMLDivElement,
92+
width?: number,
93+
height?: number,
94+
) {
9195
// TODO: 确认是否需要,后续是否只要返回 container 即可(下面方法是为了解决事件绑定问题的)
9296
// fix: destroy keyboard events while destroy LogicFlow.(#1110)
9397
const lfContainer = document.createElement('div')
9498
lfContainer.style.position = 'relative'
95-
lfContainer.style.width = '100%'
96-
lfContainer.style.height = '100%'
99+
lfContainer.style.width = width ? `${width}px` : '100%'
100+
lfContainer.style.height = height ? `${height}px` : '100%'
97101
container.innerHTML = ''
98102
container.appendChild(lfContainer)
99103
return lfContainer
@@ -105,8 +109,9 @@ export class LogicFlow {
105109

106110
constructor(options: LFOptions.Common) {
107111
const initOptions = LFOptions.get(options)
112+
const { container, width, height } = initOptions
108113
this.options = initOptions
109-
this.container = this.initContainer(initOptions.container)
114+
this.container = this.initContainer(container, width, height)
110115
this.graphModel = new GraphModel({
111116
...initOptions,
112117
container: this.container, // TODO:测试该部分是否会有问题

0 commit comments

Comments
 (0)