/
PlaygroundContainer.tsx
167 lines (154 loc) · 4.54 KB
/
PlaygroundContainer.tsx
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
import { css } from '@emotion/react';
import cloneDeep from 'lodash.clonedeep';
import React, { MutableRefObject } from 'react';
import {
Canvas,
CanvasRef,
Edge,
EdgeProps,
hasLink,
NodeProps,
} from 'reaflow';
import { EdgeData } from 'reaflow/dist/types';
import BaseNodeData from '../types/BaseNodeData';
import NodeRouter from './NodeRouter';
type Props = {
canvasRef: MutableRefObject<CanvasRef | null>;
blocksContainerWidth: string;
nodes: BaseNodeData[];
setNodes: (nodes: BaseNodeData[]) => void;
edges: EdgeData[];
setEdges: (edges: EdgeData[]) => void;
isDraggedNodeClose: boolean;
isDroppable: boolean;
setDroppable: (isDroppable: boolean) => void;
enteredNode: BaseNodeData | undefined;
setEnteredNode: (node: BaseNodeData | undefined) => void;
}
/**
* @see https://github.com/reaviz/reaflow
*/
const PlaygroundContainer: React.FunctionComponent<Props> = (props): JSX.Element | null => {
const {
canvasRef,
blocksContainerWidth,
nodes,
edges,
setNodes,
setEdges,
isDraggedNodeClose,
isDroppable,
setDroppable,
enteredNode,
setEnteredNode,
} = props;
console.log('playground nodes', nodes);
return (
<div
className={'playground-container'}
css={css`
position: absolute;
top: 0;
bottom: 0;
left: ${blocksContainerWidth};
right: 0;
.background {
position: absolute;
width: 100%;
height: 100%;
background-color: #f5f5f5;
background-size: 50px 50px;
background-image: linear-gradient(90deg, #eaeaea 1px, transparent 0), linear-gradient(180deg, #eaeaea 1px, transparent 0);
background-position: right -109px bottom -39px;
transform: scale(1);
z-index: -10000;
}
.reaflow-canvas {
.edge {
stroke: #b1b1b7;
stroke-dasharray: 5;
animation: dashdraw .5s linear infinite;
stroke-width: 1;
}
@keyframes dashdraw {
0% {
stroke-dashoffset: 10;
}
}
}
`}
>
<div className={'background'} />
<Canvas
ref={canvasRef}
className={'reaflow-canvas'}
direction={'RIGHT'}
nodes={nodes}
edges={edges}
node={(node: NodeProps) => {
const id = node.id;
const updateCurrentNode = (nodeData: Partial<BaseNodeData>): void => {
console.log('Updating current node with', nodeData);
const nodeToUpdateIndex = nodes.findIndex((node: BaseNodeData) => node.id === id);
console.log('updateCurrentNode nodeToUpdateIndex', nodeToUpdateIndex);
const nodeToUpdate = {
...nodes[nodeToUpdateIndex],
...nodeData,
id, // Force keep same id to avoid edge cases
};
console.log('updateCurrentNode updated node', nodeToUpdate);
const newNodes = cloneDeep(nodes);
newNodes[nodeToUpdateIndex] = nodeToUpdate;
console.log('updateCurrentNode new nodes', newNodes);
setNodes(newNodes);
};
console.log('node in canvas', node, nodes);
return (
<NodeRouter
node={node}
updateCurrentNode={updateCurrentNode}
isDroppable={isDroppable}
isDraggedNodeClose={isDraggedNodeClose}
enteredNode={enteredNode}
setEnteredNode={setEnteredNode}
/>
);
}}
edge={(edge: EdgeProps) => (
<Edge
{...edge}
className={'edge'}
/>
)}
onLayoutChange={layout => console.log('Layout', layout)}
onMouseEnter={() => {
if (!isDroppable) {
console.log('setDroppable', true);
setDroppable(true);
}
}}
onMouseLeave={() => {
// console.log('setDroppable', false);
// setDroppable(false);
}}
onNodeLinkCheck={(from: BaseNodeData, to: BaseNodeData) => {
console.log('onNodeLinkCheck', 'will link?', !hasLink(edges, from, to));
return !hasLink(edges, from, to);
}}
onNodeLink={(from, to) => {
const id = `${from.id}-${to.id}`;
console.log('onNodeLink', id);
setEdges([
...edges,
{
id,
from: from.id,
to: to.id,
},
]);
}}
/>
</div>
);
};
export default PlaygroundContainer;