import { collapsedTitle, PROPERTIES } from '../../nested'; import type { NodeDef } from '../../NodeRegistry'; import { OBJECT_BEHAVIOR_SETTING, OBJECT_TYPE_SETTING, propertyPort, propertyTargetOf, } from '../../propertyPorts'; import { objectsOf, readLive, writePort } from '../../liveObjects'; import { asEntity } from '../../values'; import { exec, obj } from '../shared'; export const Properties: NodeDef = { type: PROPERTIES, label: 'Properties', category: 'data', description: 'Reads properties (width, color, a variable, …) of the object it is handed, at any time. Those ' + 'switched to Set are written together when the flow arrives.', inputs: [obj('object')], outputs: [], params: [OBJECT_TYPE_SETTING, OBJECT_BEHAVIOR_SETTING], hiddenParams: (params) => (propertyTargetOf(params) === 'behavior' ? [] : [OBJECT_BEHAVIOR_SETTING.key]), declaresPorts: 'both', declaredPort: (port, node) => propertyPort(port, node.params), derivePorts: ({ node }) => { const ports = node.ports ?? []; const shape = ports.some((p) => p.write) ? { inputs: [exec()], outputs: [exec()] } : {}; return node.collapsed ? { ...shape, label: collapsedTitle(ports, 'properties') } : shape; }, read: (ctx, _params, input, def) => { const target = asEntity(input('object')); return target ? readLive(target.props, def.outputs, objectsOf(ctx, target)) : {}; }, run: (_ctx, inputs, _params, def) => { const target = asEntity(inputs.object); if (target) for (const port of def.inputs) if (port.type !== 'exec' && port.key !== 'object') writePort(target.props, port, inputs[port.key]); return { next: 'exec' }; }, };