io-actions/app/blocks/variables/set.js
2025-08-01 16:22:34 -04:00

70 lines
1.4 KiB
JavaScript

/** @type {import('#types').BlockDefinitionRaw} */
export default {
type: 'io_variables_set',
bytecodeKey: "setVariable",
name: "Set Variable",
inputsInline: true,
colour: 240,
description: "Set a variable to a value",
connections: {
mode: 'statement',
output: "expression",
next: "expression",
},
template: "Set variable %VAR = %VALUE",
inputs: {
VALUE: {
check: "expression",
shadow: "io_text",
}
},
fields: {
VAR: {
type: 'field_variable'
}
},
generators: {
json: (block, generator) => {
const
variableName = block.getField('VAR').getText(),
value = generator.valueToCode(block, 'VALUE', 0)
const
defaultedValue = value
? JSON.parse(value)
// TODO: is this !== 0 check superfluous?
: (value !== 0 && value !== null) && null,
blockPayload = JSON.stringify({
setVariable: {
name: variableName,
value: defaultedValue
}
})
return blockPayload
}
},
regenerators: {
json: (blockObject, helpers) => {
const
{ name, value } = blockObject.setVariable,
id = helpers.registerVariable(name)
return {
type: "io_variables_set",
fields: {
VAR: { id }
},
inputs: {
VALUE: helpers.expressionToBlock(value, { shadow: "io_text" })
}
}
}
}
}