(options: PrepareBlockStateOptions)
| 112 | * Generates subBlocks and outputs from the block registry. |
| 113 | */ |
| 114 | export function prepareBlockState(options: PrepareBlockStateOptions): BlockState { |
| 115 | const { id, type, name, position, data, parentId, extent, triggerMode = false } = options |
| 116 | |
| 117 | const blockConfig = getBlock(type) |
| 118 | |
| 119 | const blockData: Record<string, unknown> = { ...(data || {}) } |
| 120 | if (parentId) blockData.parentId = parentId |
| 121 | if (extent) blockData.extent = extent |
| 122 | |
| 123 | if (!blockConfig) { |
| 124 | return { |
| 125 | id, |
| 126 | type, |
| 127 | name, |
| 128 | position, |
| 129 | data: blockData, |
| 130 | subBlocks: {}, |
| 131 | outputs: {}, |
| 132 | enabled: true, |
| 133 | horizontalHandles: true, |
| 134 | advancedMode: false, |
| 135 | triggerMode, |
| 136 | height: 0, |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | const subBlocks: Record<string, SubBlockState> = {} |
| 141 | |
| 142 | if (blockConfig.subBlocks) { |
| 143 | blockConfig.subBlocks.forEach((subBlock) => { |
| 144 | let initialValue: unknown = null |
| 145 | |
| 146 | if (typeof subBlock.value === 'function') { |
| 147 | try { |
| 148 | initialValue = subBlock.value({}) |
| 149 | } catch { |
| 150 | initialValue = null |
| 151 | } |
| 152 | } else if (subBlock.defaultValue !== undefined) { |
| 153 | initialValue = subBlock.defaultValue |
| 154 | } else if (subBlock.type === 'input-format' || subBlock.type === 'response-format') { |
| 155 | initialValue = [createDefaultInputFormatField()] |
| 156 | } else if (subBlock.type === 'table') { |
| 157 | initialValue = [] |
| 158 | } |
| 159 | |
| 160 | subBlocks[subBlock.id] = { |
| 161 | id: subBlock.id, |
| 162 | type: subBlock.type, |
| 163 | value: initialValue as SubBlockState['value'], |
| 164 | } |
| 165 | }) |
| 166 | } |
| 167 | |
| 168 | const isTriggerCapable = hasTriggerCapability(blockConfig) |
| 169 | const effectiveTriggerMode = Boolean(triggerMode && isTriggerCapable) |
| 170 | const outputs = getEffectiveBlockOutputs(type, subBlocks, { |
| 171 | triggerMode: effectiveTriggerMode, |
no test coverage detected