@logicflow/core 2.0.0-beta.0 → 2.0.0-beta.1

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.
@@ -1,33 +1,16 @@
1
1
  import { Component } from 'preact/compat'
2
+ import { cloneDeep, assign } from 'lodash-es'
2
3
  import { observer } from '../..'
3
4
  import { createUuid } from '../../util'
4
5
  import { GraphModel } from '../../model'
5
6
  import { DEFAULT_GRID_SIZE } from '../../constant'
6
7
 
7
- export type GridOptions = {
8
- /**
9
- * 网格格子间距
10
- */
11
- size?: number
12
- /**
13
- * 网格是否可见
14
- */
15
- visible?: boolean
8
+ import GridOptions = Grid.GridOptions
16
9
 
10
+ type IProps = GridOptions & {
17
11
  graphModel: GraphModel
18
- /**
19
- * 网格类型
20
- * 'dot' || 'mesh'
21
- */
22
- type?: string
23
- config?: {
24
- color: string
25
- thickness?: number
26
- }
27
12
  }
28
13
 
29
- type IProps = GridOptions
30
-
31
14
  @observer
32
15
  export class Grid extends Component<IProps> {
33
16
  readonly id = createUuid()
@@ -38,21 +21,16 @@ export class Grid extends Component<IProps> {
38
21
 
39
22
  const { color, thickness = 2 } = config ?? {}
40
23
 
41
- const length = Math.min(Math.max(1.5, thickness), size / 2) // 2 < length < size /2
42
- let opacity = 1
43
- if (!visible) {
44
- opacity = 0
45
- }
46
- /* eslint-disable-next-line */
24
+ // 对于点状网格,点的半径不能大于网格大小的四分之一
25
+ const radius = Math.min(Math.max(2, thickness), size / 4)
26
+ const opacity = visible ? 1 : 0
47
27
  return (
48
- <rect
49
- width={length}
50
- height={length}
51
- rx={length / 2}
52
- ry={length / 2}
53
- fill={color}
54
- opacity={opacity}
55
- />
28
+ <g fill={color} opacity={opacity}>
29
+ <circle cx={0} cy={0} r={radius / 2} />
30
+ <circle cx={0} cy={size} r={radius / 2} />
31
+ <circle cx={size} cy={0} r={radius / 2} />
32
+ <circle cx={size} cy={size} r={radius / 2} />
33
+ </g>
56
34
  )
57
35
  }
58
36
 
@@ -61,22 +39,26 @@ export class Grid extends Component<IProps> {
61
39
  renderMesh() {
62
40
  const { config, size = 1, visible } = this.props
63
41
  const { color, thickness = 1 } = config ?? {}
64
- const strokeWidth = Math.min(Math.max(1, thickness), size / 2) // 1 < strokeWidth < size /2
65
- const d = `M ${size} 0 H0 M0 0 V0 ${size}`
66
- let opacity = 1
67
- if (!visible) {
68
- opacity = 0
69
- }
42
+
43
+ // 对于交叉线网格,线的宽度不能大于网格大小的一半
44
+ const strokeWidth = Math.min(Math.max(1, thickness), size / 2)
45
+ const d = `M 0 0 H ${size} V ${size} H 0 Z`
46
+ const opacity = visible ? 1 : 0
70
47
  return (
71
- <path d={d} stroke={color} strokeWidth={strokeWidth} opacity={opacity} />
48
+ <path
49
+ d={d}
50
+ stroke={color}
51
+ strokeWidth={strokeWidth}
52
+ opacity={opacity}
53
+ fill="transparent"
54
+ />
72
55
  )
73
56
  }
74
57
 
75
58
  render() {
76
- // TODO 生成网格️️️✔、网格支持 options(size)✔
77
59
  const {
78
60
  type,
79
- size,
61
+ size = 1,
80
62
  graphModel: { transformModel },
81
63
  } = this.props
82
64
  const { SCALE_X, SKEW_Y, SKEW_X, SCALE_Y, TRANSLATE_X, TRANSLATE_Y } =
@@ -122,14 +104,54 @@ export class Grid extends Component<IProps> {
122
104
  }
123
105
  }
124
106
 
125
- Grid.defaultProps = {
126
- size: DEFAULT_GRID_SIZE,
127
- visible: true,
128
- type: 'dot',
129
- config: {
130
- color: '#ababab',
131
- thickness: 1,
132
- },
133
- }
107
+ export namespace Grid {
108
+ export type GridOptions = {
109
+ /**
110
+ * 网格格子间距
111
+ */
112
+ size?: number
113
+ /**
114
+ * 网格是否可见
115
+ */
116
+ visible?: boolean
117
+ /**
118
+ * 网格类型
119
+ * - `dot` 点状网格
120
+ * - `mesh` 交叉线网格
121
+ */
122
+ type?: 'dot' | 'mesh'
123
+ config?: {
124
+ /**
125
+ * 网格的颜色
126
+ */
127
+ color: string
128
+ /**
129
+ * 网格的宽度
130
+ * - 对于 `dot` 点状网格,表示点的大小
131
+ * - 对于 `mesh` 交叉线网格,表示线的宽度
132
+ */
133
+ thickness?: number
134
+ }
135
+ }
136
+
137
+ export const defaultProps: GridOptions = {
138
+ size: DEFAULT_GRID_SIZE,
139
+ visible: true,
140
+ type: 'dot',
141
+ config: {
142
+ color: '#ababab',
143
+ thickness: 1,
144
+ },
145
+ }
134
146
 
135
- export default Grid
147
+ export function getGridOptions(options: number | boolean | GridOptions) {
148
+ const defaultOptions = cloneDeep(Grid.defaultProps)
149
+ if (typeof options === 'number') {
150
+ return assign(defaultOptions, { size: options })
151
+ } else if (typeof options === 'boolean') {
152
+ return assign(defaultOptions, { visible: options })
153
+ } else {
154
+ return assign(defaultOptions, options)
155
+ }
156
+ }
157
+ }