mirror of
https://git.mirrors.martin98.com/https://github.com/langgenius/dify.git
synced 2025-08-15 21:06:03 +08:00
feat: rename var name sync to used jinjia code (#3964)
This commit is contained in:
parent
99292edd46
commit
fa509ce64e
@ -13,6 +13,7 @@ type Props = {
|
|||||||
readonly: boolean
|
readonly: boolean
|
||||||
list: Variable[]
|
list: Variable[]
|
||||||
onChange: (list: Variable[]) => void
|
onChange: (list: Variable[]) => void
|
||||||
|
onVarNameChange?: (oldName: string, newName: string) => void
|
||||||
isSupportConstantValue?: boolean
|
isSupportConstantValue?: boolean
|
||||||
onlyLeafNodeVar?: boolean
|
onlyLeafNodeVar?: boolean
|
||||||
filterVar?: (payload: Var, valueSelector: ValueSelector) => boolean
|
filterVar?: (payload: Var, valueSelector: ValueSelector) => boolean
|
||||||
@ -23,6 +24,7 @@ const VarList: FC<Props> = ({
|
|||||||
readonly,
|
readonly,
|
||||||
list,
|
list,
|
||||||
onChange,
|
onChange,
|
||||||
|
onVarNameChange,
|
||||||
isSupportConstantValue,
|
isSupportConstantValue,
|
||||||
onlyLeafNodeVar,
|
onlyLeafNodeVar,
|
||||||
filterVar,
|
filterVar,
|
||||||
@ -31,12 +33,13 @@ const VarList: FC<Props> = ({
|
|||||||
|
|
||||||
const handleVarNameChange = useCallback((index: number) => {
|
const handleVarNameChange = useCallback((index: number) => {
|
||||||
return (e: React.ChangeEvent<HTMLInputElement>) => {
|
return (e: React.ChangeEvent<HTMLInputElement>) => {
|
||||||
|
onVarNameChange?.(list[index].variable, e.target.value)
|
||||||
const newList = produce(list, (draft) => {
|
const newList = produce(list, (draft) => {
|
||||||
draft[index].variable = e.target.value
|
draft[index].variable = e.target.value
|
||||||
})
|
})
|
||||||
onChange(newList)
|
onChange(newList)
|
||||||
}
|
}
|
||||||
}, [list, onChange])
|
}, [list, onVarNameChange, onChange])
|
||||||
|
|
||||||
const handleVarReferenceChange = useCallback((index: number) => {
|
const handleVarReferenceChange = useCallback((index: number) => {
|
||||||
return (value: ValueSelector | string, varKindType: VarKindType) => {
|
return (value: ValueSelector | string, varKindType: VarKindType) => {
|
||||||
|
@ -27,6 +27,7 @@ const Panel: FC<NodePanelProps<TemplateTransformNodeType>> = ({
|
|||||||
readOnly,
|
readOnly,
|
||||||
inputs,
|
inputs,
|
||||||
handleVarListChange,
|
handleVarListChange,
|
||||||
|
handleVarNameChange,
|
||||||
handleAddVariable,
|
handleAddVariable,
|
||||||
handleAddEmptyVariable,
|
handleAddEmptyVariable,
|
||||||
handleCodeChange,
|
handleCodeChange,
|
||||||
@ -58,6 +59,7 @@ const Panel: FC<NodePanelProps<TemplateTransformNodeType>> = ({
|
|||||||
readonly={readOnly}
|
readonly={readOnly}
|
||||||
list={inputs.variables}
|
list={inputs.variables}
|
||||||
onChange={handleVarListChange}
|
onChange={handleVarListChange}
|
||||||
|
onVarNameChange={handleVarNameChange}
|
||||||
filterVar={filterVar}
|
filterVar={filterVar}
|
||||||
/>
|
/>
|
||||||
</Field>
|
</Field>
|
||||||
|
@ -22,11 +22,18 @@ const useConfig = (id: string, payload: TemplateTransformNodeType) => {
|
|||||||
inputsRef.current = newPayload
|
inputsRef.current = newPayload
|
||||||
}, [doSetInputs])
|
}, [doSetInputs])
|
||||||
|
|
||||||
const { handleVarListChange, handleAddVariable: handleAddEmptyVariable } = useVarList<TemplateTransformNodeType>({
|
const { handleAddVariable: handleAddEmptyVariable } = useVarList<TemplateTransformNodeType>({
|
||||||
inputs,
|
inputs,
|
||||||
setInputs,
|
setInputs,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const handleVarListChange = useCallback((newList: Variable[]) => {
|
||||||
|
const newInputs = produce(inputsRef.current, (draft: any) => {
|
||||||
|
draft.variables = newList
|
||||||
|
})
|
||||||
|
setInputs(newInputs)
|
||||||
|
}, [setInputs])
|
||||||
|
|
||||||
const handleAddVariable = useCallback((payload: Variable) => {
|
const handleAddVariable = useCallback((payload: Variable) => {
|
||||||
const newInputs = produce(inputsRef.current, (draft: any) => {
|
const newInputs = produce(inputsRef.current, (draft: any) => {
|
||||||
draft.variables.push(payload)
|
draft.variables.push(payload)
|
||||||
@ -34,6 +41,14 @@ const useConfig = (id: string, payload: TemplateTransformNodeType) => {
|
|||||||
setInputs(newInputs)
|
setInputs(newInputs)
|
||||||
}, [setInputs])
|
}, [setInputs])
|
||||||
|
|
||||||
|
// rename var in code
|
||||||
|
const handleVarNameChange = useCallback((oldName: string, newName: string) => {
|
||||||
|
const newInputs = produce(inputsRef.current, (draft: any) => {
|
||||||
|
draft.template = draft.template.replaceAll(`{{ ${oldName} }}`, `{{ ${newName} }}`)
|
||||||
|
})
|
||||||
|
setInputs(newInputs)
|
||||||
|
}, [setInputs])
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (inputs.template)
|
if (inputs.template)
|
||||||
return
|
return
|
||||||
@ -94,6 +109,7 @@ const useConfig = (id: string, payload: TemplateTransformNodeType) => {
|
|||||||
readOnly,
|
readOnly,
|
||||||
inputs,
|
inputs,
|
||||||
handleVarListChange,
|
handleVarListChange,
|
||||||
|
handleVarNameChange,
|
||||||
handleAddVariable,
|
handleAddVariable,
|
||||||
handleAddEmptyVariable,
|
handleAddEmptyVariable,
|
||||||
handleCodeChange,
|
handleCodeChange,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user