-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathNode.tsx
More file actions
168 lines (158 loc) · 4.75 KB
/
Node.tsx
File metadata and controls
168 lines (158 loc) · 4.75 KB
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
import * as React from 'react'
import styled, { css } from 'styled-components'
import { ClassicScheme, RenderEmit } from '../types'
import { $nodecolor, $nodecolorselected, $nodewidth, $socketmargin, $socketsize } from '../vars'
import { RefControl } from './refs/RefControl'
import { RefSocket } from './refs/RefSocket'
type NodeExtraData = { width?: number, height?: number }
export const NodeStyles = styled.div<NodeExtraData & { selected: boolean, styles?: (props: any) => any }>`
background: ${$nodecolor};
border: 2px solid #4e58bf;
border-radius: 10px;
cursor: pointer;
box-sizing: border-box;
width: ${props => Number.isFinite(props.width)
? `${props.width}px`
: `${$nodewidth}px`};
height: ${props => Number.isFinite(props.height)
? `${props.height}px`
: 'auto'};
padding-bottom: 6px;
position: relative;
user-select: none;
line-height: initial;
font-family: Arial;
&:hover {
background: lighten(${$nodecolor},4%);
}
${props => props.selected && css`
background: ${$nodecolorselected};
border-color: #e3c000;
`}
.title {
color: white;
font-family: sans-serif;
font-size: 18px;
padding: 8px;
}
.output {
text-align: right;
}
.input {
text-align: left;
}
.output-socket {
text-align: right;
margin-right: -${$socketsize / 2 + $socketmargin}px;
display: inline-block;
}
.input-socket {
text-align: left;
margin-left: -${$socketsize / 2 + $socketmargin}px;
display: inline-block;
}
.input-title,.output-title {
vertical-align: middle;
color: white;
display: inline-block;
font-family: sans-serif;
font-size: 14px;
margin: ${$socketmargin}px;
line-height: ${$socketsize}px;
}
.input-control {
z-index: 1;
width: calc(100% - ${$socketsize + 2 * $socketmargin}px);
vertical-align: middle;
display: inline-block;
}
.control {
display: block;
padding: ${$socketmargin}px ${$socketsize / 2 + $socketmargin}px;
}
${props => props.styles?.(props)}
`
function sortByIndex<T extends [string, undefined | { index?: number }][]>(entries: T) {
entries.sort((a, b) => {
const ai = a[1]?.index || 0
const bi = b[1]?.index || 0
return ai - bi
})
}
type Props<S extends ClassicScheme> = {
data: S['Node'] & NodeExtraData
styles?: () => any
emit: RenderEmit<S>
}
export type NodeComponent<Scheme extends ClassicScheme> = (props: Props<Scheme>) => JSX.Element
export function Node<Scheme extends ClassicScheme>(props: Props<Scheme>) {
const inputs = Object.entries(props.data.inputs)
const outputs = Object.entries(props.data.outputs)
const controls = Object.entries(props.data.controls)
const selected = props.data.selected || false
const { id, label, width, height } = props.data
sortByIndex(inputs)
sortByIndex(outputs)
sortByIndex(controls)
return (
<NodeStyles
selected={selected}
width={width}
height={height}
styles={props.styles}
data-testid="node"
>
<div className="title" data-testid="title">{label}</div>
{/* Outputs */}
{outputs.map(([key, output]) => output && <div className="output" key={key} data-testid={`output-${key}`}>
<div className="output-title" data-testid="output-title">{output.label}</div>
<RefSocket
name="output-socket"
side="output"
socketKey={key}
nodeId={id}
emit={props.emit}
payload={output.socket}
data-testid="output-socket"
/>
</div>)}
{/* Controls */}
{controls.map(([key, control]) => {
return control
? <RefControl
key={key}
name="control"
emit={props.emit}
payload={control}
data-testid={`control-${key}`}
/>
: null
})}
{/* Inputs */}
{inputs.map(([key, input]) => input && <div className="input" key={key} data-testid={`input-${key}`}>
<RefSocket
name="input-socket"
side="input"
socketKey={key}
nodeId={id}
emit={props.emit}
payload={input.socket}
data-testid="input-socket"
/>
{input && (!input.control || !input.showControl)
&& <div className="input-title" data-testid="input-title">{input.label}</div>
}
{input.control && input.showControl && (
<RefControl
key={key}
name="input-control"
emit={props.emit}
payload={input.control}
data-testid="input-control"
/>
)
}
</div>)}
</NodeStyles>
)
}