@selenite/graph-editor
Version:
A graph editor for visual programming, based on rete and svelte.
70 lines (69 loc) • 2.2 kB
JavaScript
import { inputControlSocketType, socketToControl } from '../../socket';
import { Node } from '../Node.svelte';
export class InputControlNode extends Node {
inputControl;
outSocket;
constructor(params) {
super(params);
const { datastructure = 'scalar' } = params;
const socketType = this.state.type ??
params.socketType ??
inputControlSocketType[params.controlType] ??
'any';
const controlType = this.state.controlType ?? params.controlType;
this.inputControl = this.addInputControl('value', {
type: controlType,
socketType,
canChangeType: datastructure !== 'scalar',
datastructure,
initial: params.initial,
props: params.props,
changeType: (type) => this.changeType(type)
});
this.outSocket = this.addOutData('value', {
type: socketType,
datastructure
});
}
changeType(type) {
console.warn('Change type', type);
const controlType = socketToControl[type];
if (!controlType) {
console.error('No control type for', type);
return;
}
this.inputControl.socketType = type;
this.inputControl.type = controlType;
this.outSocket.type = type;
this.state.controlType = controlType;
this.state.type = type;
}
data(inputs) {
return {
value: this.controls.value.value
};
}
}
/**
* Base class for converter nodes.
*
* Converter nodes are used to convert data from one type to another type.
* @template S Source type.
* @template T Target type.
*/
export class ConverterNode extends Node {
convert;
constructor(params = {}) {
const { source = 'any', target = 'any' } = params;
super({ ...params, params: { source, target } });
this.convert = params.convert;
this.addInData('value', { type: source });
this.addOutData('value', { type: target });
}
data(inputs) {
const v = this.getData('value', inputs);
return {
value: this.convert?.(v) ?? v
};
}
}