diff --git a/frontend/src/lib/components/InputTransformForm.svelte b/frontend/src/lib/components/InputTransformForm.svelte index b37c53d90a..09983b069b 100644 --- a/frontend/src/lib/components/InputTransformForm.svelte +++ b/frontend/src/lib/components/InputTransformForm.svelte @@ -14,7 +14,7 @@ import ToggleButtonGroup from '$lib/components/common/toggleButton-v2/ToggleButtonGroup.svelte' import ToggleButton from '$lib/components/common/toggleButton-v2/ToggleButton.svelte' import { fade } from 'svelte/transition' - + import { tick } from 'svelte' import type VariableEditor from './VariableEditor.svelte' import type ItemPicker from './ItemPicker.svelte' import type { InputTransform } from '$lib/gen' @@ -133,6 +133,48 @@ } } + let codeInjectionDetected = false + + function checkCodeInjection(rawValue: string) { + if (!arg) { + return + } + + const dynamicTemplateRegex = new RegExp( + /^(flow_input\.|results\.|flo$|flow$|flow_$|flow_i$|flow_in$|flow_inp$|flow_inpu$|flow_input$|res$|resu$|resul$|result$|results$).*/ + ) + + codeInjectionDetected = dynamicTemplateRegex.test(rawValue) + } + + async function setJavaScriptExpr(rawValue: string) { + arg = { + type: 'javascript', + expr: rawValue + } + propertyType = 'javascript' + monaco?.setCode('') + monaco?.insertAtCursor(rawValue) + await tick() + monaco?.focus() + await tick() + monaco?.setCursorToEnd() + } + + function handleKeyUp(e: KeyboardEvent) { + if ( + e.key === 'Tab' && + isStaticTemplate(inputCat) && + propertyType == 'static' && + !noDynamicToggle && + codeInjectionDetected + ) { + setJavaScriptExpr(arg.value) + } else { + stepInputGen?.onKeyUp?.(e) + } + } + function isStaticTemplate(inputCat: InputCat) { return inputCat === 'string' || inputCat === 'sql' || inputCat == 'yaml' } @@ -170,6 +212,8 @@ $: isStaticTemplate(inputCat) && propertyType == 'static' && setPropertyType(arg?.value) + $: isStaticTemplate(inputCat) && propertyType == 'static' && checkCodeInjection(arg?.value) + function setDefaultCode() { if (!arg?.value) { monacoTemplate?.setCode(schema.properties?.[argName].default) @@ -358,7 +402,7 @@
-
+