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

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/src/options.ts CHANGED
@@ -1,15 +1,17 @@
1
1
  import { assign } from 'lodash-es'
2
2
  import { createElement as h } from 'preact/compat'
3
3
  import LogicFlow from './LogicFlow'
4
- import { GraphModel } from './model'
5
4
  import { KeyboardDef } from './keyboard'
6
- import { DEFAULT_GRID_SIZE, OverlapMode } from './constant'
5
+ import { OverlapMode } from './constant'
6
+ import { Grid } from './view/overlay'
7
+
8
+ import GridOptions = Grid.GridOptions
7
9
 
8
10
  export namespace Options {
9
11
  import NodeData = LogicFlow.NodeData
10
12
  import EdgeData = LogicFlow.EdgeData
11
- import ExtensionConstructor = LogicFlow.ExtensionConstructor
12
13
  import GraphData = LogicFlow.GraphData
14
+ import ExtensionType = LogicFlow.ExtensionType
13
15
  export type EdgeType = 'line' | 'polyline' | 'bezier' | string
14
16
  export type BackgroundConfig = {
15
17
  // 背景图片地址
@@ -25,19 +27,6 @@ export namespace Options {
25
27
  // TODO: 根据具体情况添加各种自定义样式
26
28
  [key: string]: any
27
29
  }
28
- export type GridOptions = {
29
- // 网格格子间距
30
- size?: number
31
- // 网格是否可见
32
- visible?: boolean
33
- graphModel?: GraphModel
34
- // 网格类型
35
- type?: 'dot' | 'mesh'
36
- config?: {
37
- color: string
38
- thickness?: number
39
- }
40
- }
41
30
 
42
31
  export type AnimationConfig = {
43
32
  node: boolean
@@ -68,6 +57,9 @@ export namespace Options {
68
57
  width?: number
69
58
  height?: number
70
59
  background?: false | BackgroundConfig
60
+ /**
61
+ * Grid 网格配置
62
+ */
71
63
  grid?: number | boolean | GridOptions
72
64
 
73
65
  partial?: boolean
@@ -96,7 +88,7 @@ export namespace Options {
96
88
  guards?: GuardsConfig
97
89
  overlapMode?: OverlapMode
98
90
 
99
- plugins?: ExtensionConstructor[]
91
+ plugins?: ExtensionType[]
100
92
  pluginsOptions?: Record<string, any>
101
93
  disabledPlugins?: string[]
102
94
  disabledTools?: string[]
@@ -117,7 +109,7 @@ export namespace Options {
117
109
 
118
110
  export namespace Options {
119
111
  export function get(options: Partial<Manual>) {
120
- const { grid, ...others } = options
112
+ const { ...others } = options
121
113
  const container = options.container
122
114
  if (container != null) {
123
115
  if (options.width == null) {
@@ -134,29 +126,6 @@ export namespace Options {
134
126
 
135
127
  const result = assign({}, defaults, others) as Options.Definition
136
128
 
137
- const defaultGrid: GridOptions = {
138
- size: DEFAULT_GRID_SIZE,
139
- type: 'dot',
140
- visible: true,
141
- config: {
142
- color: '#ababab',
143
- thickness: 1,
144
- },
145
- }
146
- if (typeof grid === 'number') {
147
- result.grid = {
148
- ...defaultGrid,
149
- size: grid,
150
- }
151
- } else if (typeof grid === 'boolean') {
152
- result.grid = {
153
- ...defaultGrid,
154
- visible: grid,
155
- }
156
- } else {
157
- result.grid = { ...defaultGrid, ...grid }
158
- }
159
-
160
129
  return result
161
130
  }
162
131
  }
@@ -68,7 +68,7 @@ class Graph extends Component<IGraphProps> {
68
68
  if (options.height) {
69
69
  style.height = `${graphModel.height}px`
70
70
  }
71
- const grid = options.grid as any // TODO: fix type
71
+ const grid = options.grid && Grid.getGridOptions(options.grid)
72
72
  const { fakeNode, editConfigModel } = graphModel
73
73
  const { adjustEdge } = editConfigModel
74
74
 
@@ -95,7 +95,7 @@ class Graph extends Component<IGraphProps> {
95
95
  {options.background && (
96
96
  <BackgroundOverlay background={options.background} />
97
97
  )}
98
- {options.grid && <Grid {...grid} graphModel={graphModel} />}
98
+ {grid && <Grid {...grid} graphModel={graphModel} />}
99
99
  </div>
100
100
  )
101
101
  }
@@ -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
+ }