-
Notifications
You must be signed in to change notification settings - Fork 309
/
actions.ts
182 lines (161 loc) · 5.35 KB
/
actions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import { v4 } from 'uuid'
import {
IChart, IOnCanvasClick, IOnCanvasDrop, IOnDeleteKey, IOnDragCanvas, IOnDragNode, IOnLinkCancel,
IOnLinkComplete, IOnLinkMouseEnter, IOnLinkMouseLeave, IOnLinkMove, IOnLinkStart, IOnNodeClick,
IOnNodeSizeChange, IOnPortPositionChange,
} from '../'
import { rotate } from './utils/rotate'
/**
* This file contains actions for updating state after each of the required callbacks
*/
export const onDragNode: IOnDragNode = ({ config, event, data, id }) => (chart: IChart) => {
const nodechart = chart.nodes[id]
if (nodechart) {
chart.nodes[id] = {
...nodechart,
position: config && config.snapToGrid ? { x: Math.round(data.x / 20) * 20, y: Math.round(data.y / 20) * 20 } : data,
}
}
return chart
}
export const onDragCanvas: IOnDragCanvas = ({ config, event, data }) => (chart: IChart): IChart => {
chart.offset = config && config.snapToGrid ? { x: Math.round(data.x / 20) * 20, y: Math.round(data.y / 20) * 20 } : data
return chart
}
export const onLinkStart: IOnLinkStart = ({ linkId, fromNodeId, fromPortId }) => (chart: IChart): IChart => {
chart.links[linkId] = {
id: linkId,
from: {
nodeId: fromNodeId,
portId: fromPortId,
},
to: {},
}
return chart
}
export const onLinkMove: IOnLinkMove = ({ linkId, toPosition }) => (chart: IChart): IChart => {
const link = chart.links[linkId]
link.to.position = toPosition
chart.links[linkId] = { ...link }
return chart
}
export const onLinkComplete: IOnLinkComplete = (props) => {
const { linkId, fromNodeId, fromPortId, toNodeId, toPortId, config = {} } = props
return (chart: IChart): IChart => {
if ((config.validateLink ? config.validateLink({ ...props, chart }) : true) && [fromNodeId, fromPortId].join() !== [toNodeId, toPortId].join()) {
chart.links[linkId].to = {
nodeId: toNodeId,
portId: toPortId,
}
} else {
delete chart.links[linkId]
}
return chart
}
}
export const onLinkCancel: IOnLinkCancel = ({ linkId }) => (chart: IChart) => {
delete chart.links[linkId]
return chart
}
export const onLinkMouseEnter: IOnLinkMouseEnter = ({ linkId }) => (chart: IChart) => {
// Set the link to hover
const link = chart.links[linkId]
// Set the connected ports to hover
if (link.to.nodeId && link.to.portId) {
if (chart.hovered.type !== 'link' || chart.hovered.id !== linkId) {
chart.hovered = {
type: 'link',
id: linkId,
}
}
}
return chart
}
export const onLinkMouseLeave: IOnLinkMouseLeave = ({ linkId }) => (chart: IChart) => {
const link = chart.links[linkId]
// Set the connected ports to hover
if (link.to.nodeId && link.to.portId) {
chart.hovered = {}
}
return chart
}
export const onLinkClick: IOnLinkMouseLeave = ({ linkId }) => (chart: IChart) => {
if (chart.selected.id !== linkId || chart.selected.type !== 'link') {
chart.selected = {
type: 'link',
id: linkId,
}
}
return chart
}
export const onCanvasClick: IOnCanvasClick = () => (chart: IChart) => {
if (chart.selected.id) {
chart.selected = {}
}
return chart
}
export const onDeleteKey: IOnDeleteKey = () => (chart: IChart) => {
if (chart.selected.type === 'node' && chart.selected.id) {
const node = chart.nodes[chart.selected.id]
// Delete the connected links
Object.keys(chart.links).forEach((linkId) => {
const link = chart.links[linkId]
if (link.from.nodeId === node.id || link.to.nodeId === node.id) {
delete chart.links[link.id]
}
})
// Delete the node
delete chart.nodes[chart.selected.id]
} else if (chart.selected.type === 'link' && chart.selected.id) {
delete chart.links[chart.selected.id]
}
if (chart.selected) {
chart.selected = {}
}
return chart
}
export const onNodeClick: IOnNodeClick = ({ nodeId }) => (chart: IChart) => {
if (chart.selected.id !== nodeId || chart.selected.type !== 'node') {
chart.selected = {
type: 'node',
id: nodeId,
}
}
return chart
}
export const onNodeSizeChange: IOnNodeSizeChange = ({ nodeId, size }) => (chart: IChart) => {
chart.nodes[nodeId] = {
...chart.nodes[nodeId],
size,
}
return chart
}
export const onPortPositionChange: IOnPortPositionChange = ({ node: nodeToUpdate, port, el, nodesEl }) =>
(chart: IChart): IChart => {
if (nodeToUpdate.size) {
// rotate the port's position based on the node's orientation prop (angle)
const center = { x: nodeToUpdate.size.width / 2, y: nodeToUpdate.size.height / 2 }
const current = { x: el.offsetLeft + nodesEl.offsetLeft + el.offsetWidth / 2, y: el.offsetTop + nodesEl.offsetTop + el.offsetHeight / 2 }
const angle = nodeToUpdate.orientation || 0
const position = rotate(center, current, angle)
const node = chart.nodes[nodeToUpdate.id]
node.ports[port.id].position = {
x: position.x,
y: position.y,
}
chart.nodes[nodeToUpdate.id] = { ...node }
}
return chart
}
export const onCanvasDrop: IOnCanvasDrop = ({ config, data, position }) => (chart: IChart): IChart => {
const id = v4()
chart.nodes[id] = {
id,
position: config && config.snapToGrid ? { x: Math.round(position.x / 20) * 20, y: Math.round(position.y / 20) * 20 } : position,
orientation: data.orientation || 0,
type: data.type,
ports: data.ports,
properties: data.properties,
}
return chart
}