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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
|
import { createContext, FunctionComponent, ComponentChildren } from 'preact';
import { useContext } from 'preact/hooks';
import { batch, Signal } from '@preact/signals';
import { InputSocket } from './dataflow.ts';
import { cls } from './util.ts';
import styles from './node.module.css';
export type SocketHandler = (nodeId: number, socket: string, event: MouseEvent) => void;
export interface SocketHandlers {
onOutMouseDown?: SocketHandler;
onOutMouseUp?: SocketHandler;
onInMouseDown?: SocketHandler;
onInMouseUp?: SocketHandler;
}
export const SocketHandlers = createContext<SocketHandlers>({});
export const NodeId = createContext<number>(0);
export interface NodeComponentProps<I extends {}> {
id: number;
x: Signal<number>;
y: Signal<number>;
inputs: { [Property in keyof I]: InputSocket<I[Property]> };
}
export type NodeComponent<I extends {}> = FunctionComponent<NodeComponentProps<I>>;
export interface NodeInfo<I extends {}, O extends {}> {
component: NodeComponent<I>;
func: (inputs: I) => O;
inputs: I;
}
interface SocketProps {
name: string;
onMouseDown?: SocketHandler;
onMouseUp?: SocketHandler;
}
const Socket = ({ name, onMouseDown, onMouseUp }: SocketProps) => {
const nodeId = useContext(NodeId);
const wrap = (func?: SocketHandler) => (event: MouseEvent) => func && func(nodeId, name, event);
return (
<svg
width="10"
height="10"
viewBox="0 0 16 16"
onMouseDown={wrap(onMouseDown)}
onMouseUp={wrap(onMouseUp)}
>
<circle cx="8" cy="8" r="7" stroke="black" stroke-width="1" />
</svg>
);
};
const InSocket = ({ name }: { name: string }) => {
const handlers = useContext(SocketHandlers);
return (
<Socket name={name} onMouseDown={handlers.onInMouseDown} onMouseUp={handlers.onInMouseUp} />
);
};
const OutSocket = ({ name }: { name: string }) => {
const handlers = useContext(SocketHandlers);
return (
<Socket name={name} onMouseDown={handlers.onOutMouseDown} onMouseUp={handlers.onOutMouseUp} />
);
};
export interface InputProps<T> {
name: string;
label: string;
value: InputSocket<T>;
}
export const InputAny = ({ name, label }: Omit<InputProps<any>, 'value'>) => {
return (
<li class={cls(styles.in)}>
<InSocket name={name} />
{label}
</li>
);
};
export const InputArray = ({ name, label }: Omit<InputProps<any>, 'value'>) => {
return (
<li class={cls(styles.in, styles.number)}>
<InSocket name={name} />
{label}
</li>
);
};
const InputNum = (parseFunc: (string) => number) => ({ name, label, value }: InputProps<number>) => {
const onInput = (event: InputEvent) => {
value.value = parseFunc((event.target as HTMLInputElement).value);
}
return (
<li class={cls(styles.in, styles.number, value.link.value && styles.linked)}>
<InSocket name={name} />
<span>{label}</span>
<input type="number" value={value.manual.value} onInput={onInput} />
</li>
);
};
export const InputNumber = InputNum(parseFloat);
export const InputInteger = InputNum(parseInt);
export const InputVector = ({ name, label, value }: InputProps<[number, number, number]>) => {
const onInput = (i: 0 | 1 | 2) => (event: InputEvent) => {
const newValue: [number, number, number] = [...value.value];
newValue[i] = parseFloat((event.target as HTMLInputElement).value);
value.value = newValue;
};
return (
<li class={cls(styles.in, styles.vector, value.link.value && styles.linked)}>
<div>
<InSocket name={name} />
{label}
</div>
<input type="number" value={value.value[0]} onInput={onInput(0)} />
<input type="number" value={value.value[1]} onInput={onInput(1)} />
<input type="number" value={value.value[2]} onInput={onInput(2)} />
</li>
);
};
export interface InputSelectProps extends InputProps<string> {
options: string[] | Record<string, string[]>;
}
export const InputSelect = ({ name, label, value, options }: InputSelectProps) => {
const onChange = (event: InputEvent) => {
value.value = (event.target as HTMLSelectElement).value;
}
return (
<li class={cls(styles.in, styles.select, value.link.value && styles.linked)}>
<InSocket name={name} />
<select aria-label={label} onChange={onChange}>
{Array.isArray(options)
? options.map(opt => <option value={opt}>{opt}</option>)
: Object.entries(options).map(([label, group]) => (
<optgroup label={label}>
{group.map(opt => <option value={opt}>{opt}</option>)}
</optgroup>
))
}
</select>
</li>
);
};
export interface OutputProps {
name: string;
label: string;
}
const Output = ({ name, label, type }: OutputProps & { type: string }) => {
return (
<li class={cls(styles.out, styles[type])}>
{label}
<OutSocket name={name} />
</li>
);
};
export const OutputNumber = (props: OutputProps) => <Output {...props} type="number" />;
export const OutputVector = (props: OutputProps) => <Output {...props} type="vector" />;
export interface NodeShellProps {
id: number;
name: string;
x: Signal<number>;
y: Signal<number>;
children: ComponentChildren;
}
export const NodeShell = ({ id, name, x, y, children }: NodeShellProps) => {
const onMouseDown = (event: MouseEvent) => {
event.stopPropagation();
const onMouseMove = (event: MouseEvent) => batch(() => {
x.value += event.movementX;
y.value += event.movementY;
});
const onMouseUp = () => {
window.removeEventListener('mousemove', onMouseMove);
window.removeEventListener('mouseup', onMouseUp);
};
window.addEventListener('mousemove', onMouseMove);
window.addEventListener('mouseup', onMouseUp);
};
return (
<foreignObject x={x} y={y} width="0" height="0" class={styles.foreignObject}>
<details open tabindex={0} class={styles.node} onMouseDown={onMouseDown}>
<summary><span onClick={e => e.stopPropagation()}>{name}</span></summary>
<ul>
<NodeId.Provider value={id}>
{children}
</NodeId.Provider>
</ul>
</details>
</foreignObject>
);
};
|