Skip to content

Commit 4be8d12

Browse files
committed
refactor(core): 重构项目中 basic shape 组件,修复功能 bug、完善类型定义
- rename formateAnchorConnectValidateData - formatAnchorConnectValidateData - rename setHoverON -> setHoverOn, setHoverOFF -> setHoverOff - 修复 Text 文本节点,文本不在节点中间的显示 bug,通过设置 dominantBaseline: central 解决 - TextNode getBackground 中 rectAttr 设置 y: y-1, 移除该设置
1 parent d267e70 commit 4be8d12

File tree

26 files changed

+470
-279
lines changed

26 files changed

+470
-279
lines changed

examples/engine-browser-examples/src/index.css

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,7 @@ form[action$='destroy'] button {
173173

174174
#sidebar .nav-title {
175175
font-weight: bold;
176+
padding: 0 0.5rem;
176177
}
177178

178179
#search-spinner {
@@ -233,7 +234,7 @@ i {
233234
gap: 1rem;
234235
align-items: center;
235236
justify-content: space-between;
236-
padding: 0.5rem 0 0.5rem 0.2rem;
237+
padding: 0.5rem;
237238
overflow: hidden;
238239
color: inherit;
239240

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
outline: none;
1010
}
1111

12+
.dnd-item {
13+
cursor: grab;
14+
user-select: none;
15+
}
16+
1217
.rect {
1318
width: 50px;
1419
height: 50px;

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

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { animation, connection } from './edges'
1010
import GraphConfigData = LogicFlow.GraphConfigData
1111
import './index.less'
1212

13-
const config: Partial<LogicFlow.Options> = {
13+
const defaultConfig: Partial<LogicFlow.Options> = {
1414
isSilentMode: false,
1515
stopScrollGraph: true,
1616
stopZoomGraph: true,
@@ -38,7 +38,7 @@ const config: Partial<LogicFlow.Options> = {
3838
},
3939
text: {
4040
color: '#b85450',
41-
fontSize: 12,
41+
fontSize: 14,
4242
},
4343
},
4444
}
@@ -131,7 +131,7 @@ export default function BasicNode() {
131131
useEffect(() => {
132132
if (!lfRef.current) {
133133
const lf = new LogicFlow({
134-
...config,
134+
...defaultConfig,
135135
container: containerRef.current as HTMLElement,
136136
// hideAnchors: true,
137137
// width: 1200,
@@ -175,15 +175,18 @@ export default function BasicNode() {
175175
background: {
176176
color: '#FFFFFF',
177177
},
178-
grid: true,
178+
// grid: true,
179+
grid: {
180+
size: 20,
181+
},
179182
edgeTextDraggable: true,
180183
edgeType: 'bezier',
181-
style: {
182-
inputText: {
183-
background: 'black',
184-
color: 'white',
185-
},
186-
},
184+
// style: {
185+
// inputText: {
186+
// background: 'black',
187+
// color: 'white',
188+
// },
189+
// },
187190
// 全局自定义id
188191
// edgeGenerator: (sourceNode, targetNode, currentEdge) => {
189192
// // 起始节点类型 rect 时使用 自定义的边 custom-edge
@@ -306,19 +309,19 @@ export default function BasicNode() {
306309
const handleDragRect = () => {
307310
lfRef?.current?.dnd.startDrag({
308311
type: 'rect',
309-
text: 'xxxxx',
312+
text: 'rect',
310313
})
311314
}
312315
const handleDragCircle = () => {
313316
lfRef?.current?.dnd.startDrag({
314317
type: 'circle',
315-
r: 25,
318+
text: 'circle',
316319
})
317320
}
318321
const handleDragText = () => {
319322
lfRef?.current?.dnd.startDrag({
320323
type: 'text',
321-
text: '文本',
324+
text: '文本节点',
322325
})
323326
}
324327

@@ -458,9 +461,9 @@ export default function BasicNode() {
458461
节点面板
459462
</Divider>
460463
<Flex wrap="wrap" gap="small" justify="center" align="center">
461-
<div className="rect" onMouseDown={handleDragRect} />
462-
<div className="circle" onMouseDown={handleDragCircle} />
463-
<div className="text" onMouseDown={handleDragText}>
464+
<div className="rect dnd-item" onMouseDown={handleDragRect} />
465+
<div className="circle dnd-item" onMouseDown={handleDragCircle} />
466+
<div className="text dnd-item" onMouseDown={handleDragText}>
464467
文本
465468
</div>
466469
</Flex>

examples/engine-browser-examples/src/routes/root.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@ export default function Root() {
44
return (
55
<>
66
<div id="sidebar">
7-
<h1>LogicFlow & Engine Demos</h1>
7+
<h1>LogicFlow Demos</h1>
88
<div>
99
{/* TODO: 完成路由检索功能 */}
1010
<form id="search-form" role="search">
1111
<input
1212
id="q"
1313
aria-label="Search contacts"
14-
placeholder="Press Enter for Search"
14+
placeholder="Press Enter to Search"
1515
type="search"
1616
name="q"
1717
/>
@@ -21,17 +21,17 @@ export default function Root() {
2121
</div>
2222
<nav>
2323
<ul>
24-
<div className="nav-title">Graph</div>
24+
<div className="nav-title">LogicFlow</div>
2525
<li>
26-
<a href={`/graph/get-started`}>Get Started</a>
26+
<a href={`/graph/get-started`}>Graph</a>
2727
</li>
2828
<div className="nav-title">Extension</div>
2929
<li>
3030
<a href={`/extension/bpmn`}>BPMN</a>
3131
</li>
3232
<div className="nav-title">Engine</div>
3333
<li>
34-
<a href={`/engine/get-started`}>Start Engine</a>
34+
<a href={`/engine/get-started`}>Get Started</a>
3535
<a href={`/engine/recorder`}>Recorder</a>
3636
</li>
3737
</ul>

packages/core/src/LogicFlow.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,8 @@ export namespace LogicFlow {
15661566
radius?: number
15671567
rx?: number
15681568
ry?: number
1569+
width?: number
1570+
height?: number
15691571
path?: string
15701572
[key: string]: unknown
15711573
}

packages/core/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ export * from './options'
1616
export * from './keyboard'
1717
export { ElementState, ModelType, ElementType, EventType } from './constant'
1818

19-
export { formateAnchorConnectValidateData } from './util/node'
19+
export { formatAnchorConnectValidateData } from './util/node'
2020

2121
export default LogicFlow

packages/core/src/model/node/CircleNodeModel.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ export class CircleNodeModel extends BaseNodeModel {
5757
const { style: customStyle } = this.properties
5858
return {
5959
...style,
60-
...(customStyle ?? {}),
6160
...cloneDeep(circle),
61+
...(customStyle ?? {}),
6262
}
6363
}
6464

packages/core/src/util/edge.ts

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { pick } from 'lodash-es'
1+
import { forEach, pick } from 'lodash-es'
22
import { getNodeBBox, isInNode, distance, sampleCubic } from '.'
33
import LogicFlow from '../LogicFlow'
44
import { Options } from '../options'
@@ -1032,3 +1032,28 @@ export function createEdgeGenerator(
10321032
return Object.assign({ type: result }, currentEdge)
10331033
}
10341034
}
1035+
1036+
// 获取 Svg 标签文案高度,自动换行
1037+
export type IGetSvgTextSizeParams = {
1038+
rows: string[]
1039+
rowsLength: number
1040+
fontSize: number
1041+
}
1042+
export const getSvgTextSize = ({
1043+
rows,
1044+
rowsLength,
1045+
fontSize,
1046+
}: IGetSvgTextSizeParams): LogicFlow.RectSize => {
1047+
let longestBytes = 0
1048+
forEach(rows, (row) => {
1049+
const rowBytesLength = getBytesLength(row)
1050+
longestBytes = rowBytesLength > longestBytes ? rowBytesLength : longestBytes
1051+
})
1052+
1053+
// 背景框宽度,最长一行字节数/2 * fontsize + 2
1054+
// 背景框宽度, 行数 * fontsize + 2
1055+
return {
1056+
width: Math.ceil(longestBytes / 2) * fontSize + fontSize / 4,
1057+
height: rowsLength * (fontSize + 2) + fontSize / 4,
1058+
}
1059+
}

packages/core/src/util/node.ts

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import { pick } from 'lodash-es'
2+
import { createElement as h } from 'preact/compat'
23
import { getBytesLength } from './edge'
34
import LogicFlow from '../LogicFlow'
45
import {
@@ -444,26 +445,45 @@ export const getNodeAnchorPosition = (
444445
}
445446
}
446447

447-
// 获取文案高度,自动换行,利用dom计算高度
448+
/*********************************************************
449+
* Text 节点文本相关工具函数
450+
********************************************************/
451+
// Text 相关节点工具函数
452+
// TODO: 获取文案高度,设置自动换行,利用 dom 计算高度
453+
// function getTextHeight(text: string, font: string): number {
454+
// const span = document.createElement('span');
455+
// span.textContent = text;
456+
// span.style.font = font;
457+
458+
// const range = document.createRange();
459+
// range.selectNodeContents(span);
460+
461+
// const rect = range.getBoundingClientRect();
462+
// const height = rect.height;
463+
464+
// return height;
465+
// }
466+
467+
// 获取文案高度,自动换行,利用 dom 计算高度
448468
export const getHtmlTextHeight = ({
449469
rows,
450470
style,
451471
rowsLength,
452472
className,
453473
}: {
454474
rows: string[]
455-
style: any // TODO: 完善类型
475+
style: h.JSX.CSSProperties
456476
rowsLength: number
457477
className: string
458478
}) => {
459479
const dom = document.createElement('div')
460-
dom.style.fontSize = style.fontSize
461-
dom.style.width = style.width
462480
dom.className = className
463-
dom.style.lineHeight = style.lineHeight
464-
dom.style.padding = style.padding
481+
dom.style.fontSize = `${style.fontSize}`
482+
dom.style.width = `${style.width}`
483+
dom.style.lineHeight = `${style.lineHeight}`
484+
dom.style.padding = `${style.padding}`
465485
if (style.fontFamily) {
466-
dom.style.fontFamily = style.fontFamily
486+
dom.style.fontFamily = `${style.fontFamily}`
467487
}
468488
if (rowsLength > 1) {
469489
rows.forEach((row) => {
@@ -472,13 +492,14 @@ export const getHtmlTextHeight = ({
472492
dom.appendChild(rowDom)
473493
})
474494
} else {
475-
dom.textContent = rows[0]
495+
dom.textContent = `${rows[0]}`
476496
}
477497
document.body.appendChild(dom)
478498
const height = dom.clientHeight
479499
document.body.removeChild(dom)
480500
return height
481501
}
502+
482503
// 获取文案高度,自动换行,利用dom计算高度
483504
export const getSvgTextWidthHeight = ({
484505
rows,
@@ -506,7 +527,7 @@ export const getSvgTextWidthHeight = ({
506527
/**
507528
* @description 格式化边校验信息
508529
*/
509-
export const formateAnchorConnectValidateData = (
530+
export const formatAnchorConnectValidateData = (
510531
data: Model.ConnectRuleResult,
511532
) => {
512533
if (typeof data !== 'object') {

packages/core/src/view/Anchor.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { ElementState, EventType } from '../constant'
66
import { GraphModel, BaseNodeModel, Model } from '../model'
77
import {
88
StepDrag,
9-
formateAnchorConnectValidateData,
9+
formatAnchorConnectValidateData,
1010
targetNodeInfo,
1111
distance,
1212
cancelRaf,
@@ -29,7 +29,7 @@ interface IProps {
2929
anchorIndex: number
3030
graphModel: GraphModel
3131
nodeModel: BaseNodeModel
32-
setHoverOFF: (e: MouseEvent) => void
32+
setHoverOff: (e: MouseEvent) => void
3333
}
3434

3535
interface IState {
@@ -326,11 +326,11 @@ class Anchor extends Component<IProps, IState> {
326326
)
327327
this.sourceRuleResults.set(
328328
targetInfoId,
329-
formateAnchorConnectValidateData(sourceRuleResult),
329+
formatAnchorConnectValidateData(sourceRuleResult),
330330
)
331331
this.targetRuleResults.set(
332332
targetInfoId,
333-
formateAnchorConnectValidateData(targetRuleResult),
333+
formatAnchorConnectValidateData(targetRuleResult),
334334
)
335335
}
336336
const { isAllPass: isSourcePass } =

packages/core/src/view/Control.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ export class ResizeControl extends Component<
304304

305305
// 由于将拖拽放大缩小改成丝滑模式,这个时候需要再拖拽结束的时候,将节点的位置更新到 grid 上。
306306
onDragEnd = () => {
307+
// TODO: 确认下面该代码是否还需要(应该是默认让节点拖拽以 gridSize 为步长移动)
307308
// const { gridSize = 1 } = this.graphModel
308309
// const x = gridSize * Math.round(this.nodeModel.x / gridSize)
309310
// const y = gridSize * Math.round(this.nodeModel.y / gridSize)
@@ -319,14 +320,16 @@ export class ResizeControl extends Component<
319320

320321
render(): h.JSX.Element {
321322
const { x, y, direction, model } = this.props
322-
const style = model.getResizeControlStyle()
323+
const { width, height, ...restStyle } = model.getResizeControlStyle()
323324
return (
324325
<g className={`lf-resize-control lf-resize-control-${direction}`}>
325326
<Rect
326327
className="lf-resize-control-content"
327328
x={x}
328329
y={y}
329-
{...style}
330+
width={width ?? 7}
331+
height={height ?? 7}
332+
{...restStyle}
330333
onMouseDown={this.dragHandler.handleMouseDown}
331334
/>
332335
</g>

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { GraphModel, BaseNodeModel, BaseEdgeModel, Model } from '../../model'
44
import {
55
IDragParams,
66
StepDrag,
7-
formateAnchorConnectValidateData,
7+
formatAnchorConnectValidateData,
88
targetNodeInfo,
99
NodeContaint,
1010
} from '../../util'
@@ -394,11 +394,11 @@ export class AdjustPoint extends Component<IProps, IState> {
394394
)
395395
this.sourceRuleResults.set(
396396
targetInfoId,
397-
formateAnchorConnectValidateData(sourceRuleResult),
397+
formatAnchorConnectValidateData(sourceRuleResult),
398398
)
399399
this.targetRuleResults.set(
400400
targetInfoId,
401-
formateAnchorConnectValidateData(targetRuleResult),
401+
formatAnchorConnectValidateData(targetRuleResult),
402402
)
403403
}
404404
const { isAllPass: isSourcePass, msg: sourceMsg } =

0 commit comments

Comments
 (0)