fix(frontend): Fix loops pickable properties (#441)
This commit is contained in:
@@ -23,6 +23,7 @@
|
||||
import { flowStateStore, type FlowModuleSchema, type FlowState } from './flows/flowState'
|
||||
import { stepOpened } from './flows/stepOpenedStore'
|
||||
import { buildExtraLib, objectToTsType, schemaToObject, schemaToTsType } from '$lib/utils'
|
||||
import { rotateRight } from 'svelte-awesome/icons'
|
||||
|
||||
export let indexes: number[]
|
||||
export let mod: FlowModule
|
||||
@@ -37,29 +38,50 @@
|
||||
let pickableProperties: Object | undefined = undefined
|
||||
let bigEditor = false
|
||||
|
||||
const i = indexes[0]
|
||||
|
||||
type PickableProperties = {
|
||||
flow_input?: Object
|
||||
previous_result?: Object
|
||||
step?: Object[]
|
||||
}
|
||||
|
||||
const i = indexes[0]
|
||||
function hasElements(arr: any) {
|
||||
return Array.isArray(arr) && arr.length > 0
|
||||
}
|
||||
|
||||
function getPickableProperties(flow: Flow): PickableProperties {
|
||||
function getLast<T>(arr: T[]): T {
|
||||
return arr[arr.length - 1]
|
||||
}
|
||||
|
||||
function getPickableProperties(flow: Flow, flowState: FlowState): PickableProperties {
|
||||
const flowInputAsObject = schemaToObject(flow.schema, args)
|
||||
|
||||
if (indexes.length > 1) {
|
||||
const prev = $flowStateStore[i - 1].previewResults
|
||||
const prevValues = prev[prev.length - 1]
|
||||
const [parentIndex] = indexes
|
||||
|
||||
const stepBeforeLoop = flowState[parentIndex - 1]
|
||||
|
||||
flowInputAsObject['iter'] = {
|
||||
value: prevValues?.[prevValues.length - 1] ?? "iteration's value",
|
||||
index: `iteration's index (0 to ${prevValues ? prevValues.length - 1 : '..'})`
|
||||
value: "iteration's value",
|
||||
index: "iteration's index"
|
||||
}
|
||||
|
||||
if (hasElements(stepBeforeLoop.previewResults)) {
|
||||
const lastResults = getLast(stepBeforeLoop.previewResults)
|
||||
|
||||
if (hasElements(lastResults)) {
|
||||
flowInputAsObject['iter'] = {
|
||||
value: getLast(lastResults),
|
||||
index: `iteration's index (0 to ${lastResults.length - 1})`
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const hasResults = previousStepPreviewResults.length > 0
|
||||
const hasResults = hasElements(previousStepPreviewResults)
|
||||
const last = hasResults ? getLast(previousStepPreviewResults) : {}
|
||||
|
||||
const last = hasResults ? previousStepPreviewResults[previousStepPreviewResults.length - 1] : {}
|
||||
return {
|
||||
flow_input: flowInputAsObject,
|
||||
previous_result: last,
|
||||
@@ -68,7 +90,7 @@
|
||||
}
|
||||
|
||||
$: shouldPick = 'path' in mod.value && mod.value.path === '' && !('language' in mod.value)
|
||||
$: pickableProperties = getPickableProperties($flowStore)
|
||||
$: pickableProperties = getPickableProperties($flowStore, $flowStateStore)
|
||||
$: extraLib = buildExtraLib(
|
||||
schemaToTsType($flowStore?.schema),
|
||||
i === 0 ? schemaToTsType($flowStore?.schema) : objectToTsType(previousStepPreviewResults)
|
||||
@@ -140,8 +162,8 @@
|
||||
{#if opened}
|
||||
{#if shouldPick}
|
||||
<FlowInputs
|
||||
shouldDisableTriggerScripts={i != 0}
|
||||
shouldDisableLoopCreation={indexes.length > 1 || i == 0}
|
||||
shouldDisableTriggerScripts={indexes[0] > 0}
|
||||
shouldDisableLoopCreation={indexes.length > 1 || indexes[0] == 0}
|
||||
on:pick={(e) => apply(pickScript, e.detail.path)}
|
||||
on:new={(e) =>
|
||||
apply(createInlineScriptModule, {
|
||||
|
||||
Reference in New Issue
Block a user