feat(workflow): add drag-and-drop support for variable list items for start node (#22150)

This commit is contained in:
Minamiyama
2025-07-11 18:53:29 +08:00
committed by GitHub
parent 76d21743fd
commit 5f9628e027
2 changed files with 53 additions and 15 deletions

View File

@@ -13,8 +13,10 @@ import { Edit03 } from '@/app/components/base/icons/src/vender/solid/general'
import Badge from '@/app/components/base/badge' import Badge from '@/app/components/base/badge'
import ConfigVarModal from '@/app/components/app/configuration/config-var/config-modal' import ConfigVarModal from '@/app/components/app/configuration/config-var/config-modal'
import { noop } from 'lodash-es' import { noop } from 'lodash-es'
import cn from '@/utils/classnames'
type Props = { type Props = {
className?: string
readonly: boolean readonly: boolean
payload: InputVar payload: InputVar
onChange?: (item: InputVar, moreInfo?: MoreInfo) => void onChange?: (item: InputVar, moreInfo?: MoreInfo) => void
@@ -25,6 +27,7 @@ type Props = {
} }
const VarItem: FC<Props> = ({ const VarItem: FC<Props> = ({
className,
readonly, readonly,
payload, payload,
onChange = noop, onChange = noop,
@@ -47,9 +50,9 @@ const VarItem: FC<Props> = ({
hideEditVarModal() hideEditVarModal()
}, [onChange, hideEditVarModal]) }, [onChange, hideEditVarModal])
return ( return (
<div ref={ref} className='flex h-8 cursor-pointer items-center justify-between rounded-lg border border-components-panel-border-subtle bg-components-panel-on-panel-item-bg px-2.5 shadow-xs hover:shadow-md'> <div ref={ref} className={cn('flex h-8 cursor-pointer items-center justify-between rounded-lg border border-components-panel-border-subtle bg-components-panel-on-panel-item-bg px-2.5 shadow-xs hover:shadow-md', className)}>
<div className='flex w-0 grow items-center space-x-1'> <div className='flex w-0 grow items-center space-x-1'>
<Variable02 className='h-3.5 w-3.5 text-text-accent' /> <Variable02 className='h-3.5 w-3.5 text-text-accent group-hover:opacity-0' />
<div title={payload.variable} className='max-w-[130px] shrink-0 truncate text-[13px] font-medium text-text-secondary'>{payload.variable}</div> <div title={payload.variable} className='max-w-[130px] shrink-0 truncate text-[13px] font-medium text-text-secondary'>{payload.variable}</div>
{payload.label && (<><div className='shrink-0 text-xs font-medium text-text-quaternary'>·</div> {payload.label && (<><div className='shrink-0 text-xs font-medium text-text-quaternary'>·</div>
<div title={payload.label as string} className='max-w-[130px] truncate text-[13px] font-medium text-text-tertiary'>{payload.label as string}</div> <div title={payload.label as string} className='max-w-[130px] truncate text-[13px] font-medium text-text-tertiary'>{payload.label as string}</div>

View File

@@ -1,10 +1,15 @@
'use client' 'use client'
import type { FC } from 'react' import type { FC } from 'react'
import React, { useCallback } from 'react' import React, { useCallback, useMemo } from 'react'
import produce from 'immer' import produce from 'immer'
import { useTranslation } from 'react-i18next' import { useTranslation } from 'react-i18next'
import VarItem from './var-item' import VarItem from './var-item'
import { ChangeType, type InputVar, type MoreInfo } from '@/app/components/workflow/types' import { ChangeType, type InputVar, type MoreInfo } from '@/app/components/workflow/types'
import { v4 as uuid4 } from 'uuid'
import { ReactSortable } from 'react-sortablejs'
import { RiDraggable } from '@remixicon/react'
import cn from '@/utils/classnames'
type Props = { type Props = {
readonly: boolean readonly: boolean
list: InputVar[] list: InputVar[]
@@ -44,6 +49,16 @@ const VarList: FC<Props> = ({
} }
}, [list, onChange]) }, [list, onChange])
const listWithIds = useMemo(() => list.map((item) => {
const id = uuid4()
return {
id,
variable: { ...item },
}
}), [list])
const varCount = list.length
if (list.length === 0) { if (list.length === 0) {
return ( return (
<div className='flex h-[42px] items-center justify-center rounded-md bg-components-panel-bg text-xs font-normal leading-[18px] text-text-tertiary'> <div className='flex h-[42px] items-center justify-center rounded-md bg-components-panel-bg text-xs font-normal leading-[18px] text-text-tertiary'>
@@ -53,18 +68,38 @@ const VarList: FC<Props> = ({
} }
return ( return (
<div className='space-y-1'> <ReactSortable
{list.map((item, index) => ( className='space-y-1'
list={listWithIds}
setList={(list) => { onChange(list.map(item => item.variable)) }}
handle='.handle'
ghostClass='opacity-50'
animation={150}
>
{list.map((item, index) => {
const canDrag = (() => {
if (readonly)
return false
return varCount > 1
})()
return (
<div key={index} className='group relative'>
<VarItem <VarItem
key={index} className={cn(canDrag && 'handle')}
readonly={readonly} readonly={readonly}
payload={item} payload={item}
onChange={handleVarChange(index)} onChange={handleVarChange(index)}
onRemove={handleVarRemove(index)} onRemove={handleVarRemove(index)}
varKeys={list.map(item => item.variable)} varKeys={list.map(item => item.variable)}
/> />
))} {canDrag && <RiDraggable className={cn(
'handle absolute left-3 top-2.5 hidden h-3 w-3 cursor-pointer text-text-tertiary',
'group-hover:block',
)} />}
</div> </div>
) )
})}
</ReactSortable>
)
} }
export default React.memo(VarList) export default React.memo(VarList)