mirror of
https://github.com/langgenius/dify.git
synced 2026-04-28 03:36:36 +08:00
feat: fin var assigner
This commit is contained in:
parent
31930159b8
commit
dec60fdd4c
@ -4,24 +4,31 @@ import React from 'react'
|
|||||||
import { useBoolean, useClickAway } from 'ahooks'
|
import { useBoolean, useClickAway } from 'ahooks'
|
||||||
import cn from 'classnames'
|
import cn from 'classnames'
|
||||||
import { ChevronSelectorVertical } from '@/app/components/base/icons/src/vender/line/arrows'
|
import { ChevronSelectorVertical } from '@/app/components/base/icons/src/vender/line/arrows'
|
||||||
|
import { Check } from '@/app/components/base/icons/src/vender/line/general'
|
||||||
type Item = {
|
type Item = {
|
||||||
value: string
|
value: string
|
||||||
label: string
|
label: string
|
||||||
}
|
}
|
||||||
type Props = {
|
type Props = {
|
||||||
list: Item[]
|
trigger?: JSX.Element
|
||||||
|
options: Item[]
|
||||||
value: string
|
value: string
|
||||||
onChange: (value: any) => void
|
onChange: (value: any) => void
|
||||||
uppercase?: boolean
|
uppercase?: boolean
|
||||||
popupClassName?: string
|
popupClassName?: string
|
||||||
|
readonly?: boolean
|
||||||
|
showChecked?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
const TypeSelector: FC<Props> = ({
|
const TypeSelector: FC<Props> = ({
|
||||||
list,
|
trigger,
|
||||||
|
options: list,
|
||||||
value,
|
value,
|
||||||
onChange,
|
onChange,
|
||||||
uppercase,
|
uppercase,
|
||||||
popupClassName,
|
popupClassName,
|
||||||
|
readonly,
|
||||||
|
showChecked,
|
||||||
}) => {
|
}) => {
|
||||||
const item = list.find(item => item.value === value)
|
const item = list.find(item => item.value === value)
|
||||||
const [showOption, { setFalse: setHide, toggle: toggleShow }] = useBoolean(false)
|
const [showOption, { setFalse: setHide, toggle: toggleShow }] = useBoolean(false)
|
||||||
@ -31,13 +38,24 @@ const TypeSelector: FC<Props> = ({
|
|||||||
}, ref)
|
}, ref)
|
||||||
return (
|
return (
|
||||||
<div className='relative left-[-8px]' ref={ref}>
|
<div className='relative left-[-8px]' ref={ref}>
|
||||||
<div
|
{trigger
|
||||||
onClick={toggleShow}
|
? (
|
||||||
className={cn(showOption && 'bg-black/5', 'flex items-center h-5 pl-1 pr-0.5 rounded-md text-xs font-semibold text-gray-700 cursor-pointer hover:bg-black/5')}>
|
<div
|
||||||
<div className={cn('text-sm font-semibold', uppercase && 'uppercase')}>{item?.label}</div>
|
onClick={toggleShow}
|
||||||
<ChevronSelectorVertical className='w-3 h-3 ' />
|
>
|
||||||
</div>
|
{trigger}
|
||||||
{showOption && (
|
</div>
|
||||||
|
)
|
||||||
|
: (
|
||||||
|
<div
|
||||||
|
onClick={toggleShow}
|
||||||
|
className={cn(showOption && 'bg-black/5', 'flex items-center h-5 pl-1 pr-0.5 rounded-md text-xs font-semibold text-gray-700 cursor-pointer hover:bg-black/5')}>
|
||||||
|
<div className={cn('text-sm font-semibold', uppercase && 'uppercase')}>{item?.label}</div>
|
||||||
|
<ChevronSelectorVertical className='w-3 h-3 ' />
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
|
|
||||||
|
{(showOption && !readonly) && (
|
||||||
<div className={cn(popupClassName, 'absolute z-10 top-[24px] w-[120px] p-1 border border-gray-200 shadow-lg rounded-lg bg-white')}>
|
<div className={cn(popupClassName, 'absolute z-10 top-[24px] w-[120px] p-1 border border-gray-200 shadow-lg rounded-lg bg-white')}>
|
||||||
{list.map(item => (
|
{list.map(item => (
|
||||||
<div
|
<div
|
||||||
@ -46,8 +64,11 @@ const TypeSelector: FC<Props> = ({
|
|||||||
setHide()
|
setHide()
|
||||||
onChange(item.value)
|
onChange(item.value)
|
||||||
}}
|
}}
|
||||||
className={cn(uppercase && 'uppercase', 'flex items-center h-[30px] min-w-[44px] px-3 rounded-lg cursor-pointer text-[13px] font-medium text-gray-700 hover:bg-gray-50')}
|
className={cn(uppercase && 'uppercase', 'flex items-center h-[30px] justify-between min-w-[44px] px-3 rounded-lg cursor-pointer text-[13px] font-medium text-gray-700 hover:bg-gray-50')}
|
||||||
>{item.label}</div>
|
>
|
||||||
|
<div>{item.label}</div>
|
||||||
|
{showChecked && item.value === value && <Check className='text-primary-600 w-4 h-4' />}
|
||||||
|
</div>
|
||||||
))
|
))
|
||||||
}
|
}
|
||||||
</div>
|
</div>
|
||||||
@ -9,7 +9,7 @@ import AddButton from '@/app/components/base/button/add-button'
|
|||||||
import Field from '@/app/components/workflow/nodes/_base/components/field'
|
import Field from '@/app/components/workflow/nodes/_base/components/field'
|
||||||
import Split from '@/app/components/workflow/nodes/_base/components/split'
|
import Split from '@/app/components/workflow/nodes/_base/components/split'
|
||||||
import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor'
|
import CodeEditor from '@/app/components/workflow/nodes/_base/components/editor/code-editor'
|
||||||
import TypeSelector from '@/app/components/workflow/nodes/_base/components/editor/type-selector'
|
import TypeSelector from '@/app/components/workflow/nodes/_base/components/selector'
|
||||||
const i18nPrefix = 'workflow.nodes.code'
|
const i18nPrefix = 'workflow.nodes.code'
|
||||||
|
|
||||||
const codeLanguages = [
|
const codeLanguages = [
|
||||||
@ -54,7 +54,7 @@ const Panel: FC = () => {
|
|||||||
<CodeEditor
|
<CodeEditor
|
||||||
title={
|
title={
|
||||||
<TypeSelector
|
<TypeSelector
|
||||||
list={codeLanguages}
|
options={codeLanguages}
|
||||||
value={inputs.code_language}
|
value={inputs.code_language}
|
||||||
onChange={handleCodeLanguageChange}
|
onChange={handleCodeLanguageChange}
|
||||||
/>
|
/>
|
||||||
|
|||||||
@ -0,0 +1,60 @@
|
|||||||
|
'use client'
|
||||||
|
import type { FC } from 'react'
|
||||||
|
import React, { useCallback } from 'react'
|
||||||
|
import produce from 'immer'
|
||||||
|
import VarReferencePicker from '@/app/components/workflow/nodes/_base/components/variable/var-reference-picker'
|
||||||
|
import type { ValueSelector } from '@/app/components/workflow/types'
|
||||||
|
import { Trash03 } from '@/app/components/base/icons/src/vender/line/general'
|
||||||
|
|
||||||
|
type Props = {
|
||||||
|
readonly: boolean
|
||||||
|
list: ValueSelector[]
|
||||||
|
onChange: (list: ValueSelector[]) => void
|
||||||
|
}
|
||||||
|
|
||||||
|
const VarList: FC<Props> = ({
|
||||||
|
readonly,
|
||||||
|
list,
|
||||||
|
onChange,
|
||||||
|
}) => {
|
||||||
|
const handleVarReferenceChange = useCallback((index: number) => {
|
||||||
|
return (value: ValueSelector) => {
|
||||||
|
const newList = produce(list, (draft) => {
|
||||||
|
draft[index] = value
|
||||||
|
})
|
||||||
|
onChange(newList)
|
||||||
|
}
|
||||||
|
}, [list, onChange])
|
||||||
|
|
||||||
|
const handleVarRemove = useCallback((index: number) => {
|
||||||
|
return () => {
|
||||||
|
const newList = produce(list, (draft) => {
|
||||||
|
draft.splice(index, 1)
|
||||||
|
})
|
||||||
|
onChange(newList)
|
||||||
|
}
|
||||||
|
}, [list, onChange])
|
||||||
|
|
||||||
|
return (
|
||||||
|
<div className='space-y-2'>
|
||||||
|
{list.map((item, index) => (
|
||||||
|
<div className='flex items-center space-x-1' key={index}>
|
||||||
|
<VarReferencePicker
|
||||||
|
readonly={readonly}
|
||||||
|
isShowNodeName
|
||||||
|
className='grow'
|
||||||
|
value={item}
|
||||||
|
onChange={handleVarReferenceChange(index)}
|
||||||
|
/>
|
||||||
|
<div
|
||||||
|
className='p-2 rounded-lg bg-gray-100 hover:bg-gray-200 cursor-pointer'
|
||||||
|
onClick={handleVarRemove(index)}
|
||||||
|
>
|
||||||
|
<Trash03 className='w-4 h-4 text-gray-500' />
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
export default React.memo(VarList)
|
||||||
@ -1,7 +1,7 @@
|
|||||||
import { useCallback } from 'react'
|
import { useCallback } from 'react'
|
||||||
import produce from 'immer'
|
import produce from 'immer'
|
||||||
import type { VariableAssignerNodeType } from './types'
|
import type { VariableAssignerNodeType } from '../../types'
|
||||||
import type { Variable } from '@/app/components/workflow/types'
|
import type { ValueSelector } from '@/app/components/workflow/types'
|
||||||
|
|
||||||
type Params = {
|
type Params = {
|
||||||
inputs: VariableAssignerNodeType
|
inputs: VariableAssignerNodeType
|
||||||
@ -11,15 +11,15 @@ function useVarList({
|
|||||||
inputs,
|
inputs,
|
||||||
setInputs,
|
setInputs,
|
||||||
}: Params) {
|
}: Params) {
|
||||||
const handleVarListChange = useCallback((newList: Variable[]) => {
|
const handleVarListChange = useCallback((newList: ValueSelector[]) => {
|
||||||
const newInputs = produce(inputs, (draft: any) => {
|
const newInputs = produce(inputs, (draft) => {
|
||||||
draft.variables = newList
|
draft.variables = newList
|
||||||
})
|
})
|
||||||
setInputs(newInputs)
|
setInputs(newInputs)
|
||||||
}, [inputs, setInputs])
|
}, [inputs, setInputs])
|
||||||
|
|
||||||
const handleAddVariable = useCallback(() => {
|
const handleAddVariable = useCallback(() => {
|
||||||
const newInputs = produce(inputs, (draft: any) => {
|
const newInputs = produce(inputs, (draft) => {
|
||||||
draft.variables.push([])
|
draft.variables.push([])
|
||||||
})
|
})
|
||||||
setInputs(newInputs)
|
setInputs(newInputs)
|
||||||
@ -1,8 +1,68 @@
|
|||||||
import type { FC } from 'react'
|
import type { FC } from 'react'
|
||||||
|
import { useTranslation } from 'react-i18next'
|
||||||
|
import useConfig from './use-config'
|
||||||
|
import { mockData } from './mock'
|
||||||
|
import VarList from './components/var-list'
|
||||||
|
import Field from '@/app/components/workflow/nodes/_base/components/field'
|
||||||
|
import Selector from '@/app/components/workflow/nodes/_base/components/selector'
|
||||||
|
import AddButton from '@/app/components/base/button/add-button'
|
||||||
|
import { ChevronDown } from '@/app/components/base/icons/src/vender/line/arrows'
|
||||||
|
|
||||||
|
const i18nPrefix = 'workflow.nodes.variableAssigner'
|
||||||
const Panel: FC = () => {
|
const Panel: FC = () => {
|
||||||
|
const { t } = useTranslation()
|
||||||
|
const readOnly = false
|
||||||
|
|
||||||
|
const {
|
||||||
|
inputs,
|
||||||
|
handleOutputTypeChange,
|
||||||
|
handleVarListChange,
|
||||||
|
handleAddVariable,
|
||||||
|
} = useConfig(mockData)
|
||||||
|
|
||||||
|
const typeOptions = [
|
||||||
|
{ label: t(`${i18nPrefix}.type.string`), value: 'string' },
|
||||||
|
{ label: t(`${i18nPrefix}.type.number`), value: 'number' },
|
||||||
|
{ label: t(`${i18nPrefix}.type.object`), value: 'Object' },
|
||||||
|
{ label: t(`${i18nPrefix}.type.array`), value: 'array' },
|
||||||
|
]
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>start panel inputs</div>
|
<div className='mt-2'>
|
||||||
|
<div className='px-4 pb-4 space-y-4'>
|
||||||
|
<Field
|
||||||
|
title={t(`${i18nPrefix}.outputVarType`)}
|
||||||
|
>
|
||||||
|
<Selector
|
||||||
|
readonly={readOnly}
|
||||||
|
value={inputs.output_type}
|
||||||
|
options={typeOptions}
|
||||||
|
onChange={handleOutputTypeChange}
|
||||||
|
trigger={
|
||||||
|
<div className='flex items-center h-8 justify-between px-2.5 rounded-lg bg-gray-100 capitalize'>
|
||||||
|
<div className='text-[13px] font-normal text-gray-900'>{inputs.output_type}</div>
|
||||||
|
<ChevronDown className='w-3.5 h-3.5 text-gray-700' />
|
||||||
|
</div>
|
||||||
|
}
|
||||||
|
popupClassName='!top-[36px] !w-[387px]'
|
||||||
|
showChecked
|
||||||
|
/>
|
||||||
|
</Field>
|
||||||
|
<Field
|
||||||
|
title={t(`${i18nPrefix}.title`)}
|
||||||
|
operations={
|
||||||
|
<AddButton onClick={handleAddVariable} />
|
||||||
|
}
|
||||||
|
>
|
||||||
|
<VarList
|
||||||
|
readonly={readOnly}
|
||||||
|
list={inputs.variables}
|
||||||
|
onChange={handleVarListChange}
|
||||||
|
/>
|
||||||
|
</Field>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
import { useCallback, useState } from 'react'
|
import { useCallback, useState } from 'react'
|
||||||
import produce from 'immer'
|
import produce from 'immer'
|
||||||
import useVarList from './use-var-list'
|
import useVarList from './components/var-list/use-var-list'
|
||||||
import type { VariableAssignerNodeType } from './types'
|
import type { VariableAssignerNodeType } from './types'
|
||||||
|
|
||||||
const useConfig = (initInputs: VariableAssignerNodeType) => {
|
const useConfig = (initInputs: VariableAssignerNodeType) => {
|
||||||
|
|||||||
@ -90,12 +90,13 @@ const translation = {
|
|||||||
variableAssigner: {
|
variableAssigner: {
|
||||||
title: 'Assign variables',
|
title: 'Assign variables',
|
||||||
outputType: 'Output Type',
|
outputType: 'Output Type',
|
||||||
|
outputVarType: 'Output Variable Type',
|
||||||
varNotSet: 'Variable not set',
|
varNotSet: 'Variable not set',
|
||||||
type: {
|
type: {
|
||||||
string: 'String',
|
string: 'String',
|
||||||
number: 'Number',
|
number: 'Number',
|
||||||
object: 'Object',
|
object: 'Object',
|
||||||
arrayObject: 'Array[Object]',
|
array: 'Array',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
@ -71,8 +71,8 @@ const translation = {
|
|||||||
},
|
},
|
||||||
ifElse: {
|
ifElse: {
|
||||||
conditions: '条件',
|
conditions: '条件',
|
||||||
and: '且',
|
and: 'and',
|
||||||
or: '或',
|
or: 'or',
|
||||||
comparisonOperator: {
|
comparisonOperator: {
|
||||||
'contains': '包含',
|
'contains': '包含',
|
||||||
'not contains': '不包含',
|
'not contains': '不包含',
|
||||||
@ -89,12 +89,13 @@ const translation = {
|
|||||||
variableAssigner: {
|
variableAssigner: {
|
||||||
title: '变量赋值',
|
title: '变量赋值',
|
||||||
outputType: '输出类型',
|
outputType: '输出类型',
|
||||||
|
outputVarType: '输出变量类型',
|
||||||
varNotSet: '未设置变量',
|
varNotSet: '未设置变量',
|
||||||
type: {
|
type: {
|
||||||
string: 'String',
|
string: 'String',
|
||||||
number: 'Number',
|
number: 'Number',
|
||||||
object: 'Object',
|
object: 'Object',
|
||||||
arrayObject: 'Array[Object]',
|
array: 'Array',
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user