@selenite/graph-editor
Version:
A graph editor for visual programming, based on rete and svelte.
243 lines (242 loc) • 7.9 kB
JavaScript
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
import { description, Node, registerNode } from '../Node.svelte';
import { InputControlNode } from './common-data-nodes.svelte';
import { DynamicTypeComponent } from '../components/DynamicTypeComponent.svelte';
import { ConnectionTrackerComponent } from '../components';
let ArrayNode = class ArrayNode extends InputControlNode {
constructor(params = {}) {
super({
label: 'Array',
...params,
controlType: 'text',
datastructure: 'array',
socketType: 'any'
});
this.addComponentByClass(DynamicTypeComponent, {
inputs: [],
outputs: ['value']
});
}
};
ArrayNode = __decorate([
registerNode('array.Array'),
__metadata("design:paramtypes", [Object])
], ArrayNode);
export { ArrayNode };
let MergeArraysNode = class MergeArraysNode extends Node {
constructor(params = {}) {
super({
label: 'Merge Arrays',
...params
});
this.addInData('a', {
datastructure: 'array',
type: 'any'
});
this.addInData('b', {
datastructure: 'array',
type: 'any'
});
this.addOutData('value', {
datastructure: 'array',
type: 'any'
});
this.addComponentByClass(DynamicTypeComponent, {
inputs: ['a', 'b'],
outputs: ['value']
});
}
data(inputs) {
const a = this.getData('a', inputs);
const b = this.getData('b', inputs);
return {
value: [...a, ...b]
};
}
};
MergeArraysNode = __decorate([
registerNode('array.MergeArrays'),
__metadata("design:paramtypes", [Object])
], MergeArraysNode);
export { MergeArraysNode };
let GetArrayElementNode = class GetArrayElementNode extends Node {
constructor(params = {}) {
super({
label: 'Get Element',
...params
});
this.addInData('array', {
datastructure: 'array',
type: 'any'
});
this.addInData('index', {
datastructure: 'scalar',
type: 'integer'
});
this.addOutData('value', {
datastructure: 'scalar',
type: 'any'
});
this.addComponentByClass(DynamicTypeComponent, {
inputs: ['array'],
outputs: ['value']
});
}
data(inputs) {
const array = this.getData('array', inputs);
const index = this.getData('index', inputs);
return {
value: array[index]
};
}
};
GetArrayElementNode = __decorate([
registerNode('array.GetArrayElement'),
description('Get an element from an array'),
__metadata("design:paramtypes", [Object])
], GetArrayElementNode);
export { GetArrayElementNode };
let MakeArrayNode = class MakeArrayNode extends Node {
constructor(params = {}) {
super({
label: 'Make Array',
...params
});
const numPins = params.numPins ?? 1;
this.addOutData('array', {
datastructure: 'array',
type: 'any',
showLabel: false
});
for (let i = 0; i < numPins; i++) {
this.addInData(`data-${i}`, {
datastructure: 'scalar',
type: 'any',
label: `${i}`
});
}
this.addComponentByClass(DynamicTypeComponent, {
inputs: Array.from({ length: numPins }, (_, i) => `data-${i}`),
outputs: ['array']
});
}
};
MakeArrayNode = __decorate([
registerNode('array.makeArray'),
__metadata("design:paramtypes", [Object])
], MakeArrayNode);
export { MakeArrayNode };
let JoinNode = class JoinNode extends Node {
constructor(params = {}) {
super({
label: 'Join',
...params
});
this.addInData('array', {
datastructure: 'array',
type: 'any'
});
this.addInData('separator', {
datastructure: 'scalar',
type: 'string',
initial: ', '
});
this.addOutData('value', {
datastructure: 'scalar',
type: 'string',
showLabel: false
});
this.addComponentByClass(DynamicTypeComponent, {
inputs: ['array'],
outputs: []
});
}
data(inputs) {
const array = this.getData('array', inputs);
const separator = this.getData('separator', inputs);
return {
value: array.join(separator).replaceAll('\\n', '\n')
};
}
};
JoinNode = __decorate([
registerNode('array.join'),
description('Join an array of items into a single string'),
__metadata("design:paramtypes", [Object])
], JoinNode);
export { JoinNode };
let BreakArrayNode = class BreakArrayNode extends Node {
currentOutputs = new Set();
constructor(params = {}) {
super({ label: 'Break', ...params });
this.addInData('array', {
datastructure: 'array',
hideLabel: true,
type: params.type ?? 'any'
});
this.addComponentByClass(DynamicTypeComponent, {
inputs: ['array'],
outputs: '*'
});
this.updateOutputs();
}
previousData = {};
data(inputs) {
const array = this.getData('array', inputs);
this.updateOutputs(inputs);
const newData = Object.fromEntries(array.map((value) => [typeof value === 'object' ? JSON.stringify(value) : `${value}`, value]));
const res = { ...this.previousData, ...newData };
this.previousData = newData;
return res;
}
updateOutputs(inputs) {
const array = this.getData('array', inputs);
const keep = new Set();
const type = this.inputs.array?.socket.type;
if (!type)
return;
for (const t of array) {
const str = typeof t === 'object' ? JSON.stringify(t) : `${t}`;
if (str === '')
continue;
keep.add(str);
if (this.currentOutputs.has(str))
continue;
this.addOutData(`${str}`, {
label: str,
datastructure: 'scalar',
type,
description: 'A value extracted from the array.'
});
this.currentOutputs.add(str);
}
for (const t of this.currentOutputs.difference(keep)) {
this.removeOutput(t);
this.currentOutputs.delete(t);
}
setTimeout(() => {
const type = this.inputs.array?.socket.type;
if (!type)
return;
for (const output of Object.values(this.outputs)) {
if (!output)
continue;
output.socket.type = type;
}
});
}
};
BreakArrayNode = __decorate([
registerNode('array.Break'),
description('Break an array into its elements.'),
__metadata("design:paramtypes", [Object])
], BreakArrayNode);
export { BreakArrayNode };