feat: code transform node editor support insert var by add slash or left brace (#3946)

Co-authored-by: StyleZhang <jasonapring2015@outlook.com>
This commit is contained in:
Joel
2024-04-28 17:51:58 +08:00
committed by GitHub
parent e7b4d024ee
commit 3e992cb23c
8 changed files with 221 additions and 9 deletions

View File

@@ -1,7 +1,7 @@
import { useCallback, useEffect } from 'react'
import { useCallback, useEffect, useRef } from 'react'
import produce from 'immer'
import useVarList from '../_base/hooks/use-var-list'
import type { Var } from '../../types'
import type { Var, Variable } from '../../types'
import { VarType } from '../../types'
import { useStore } from '../../store'
import type { TemplateTransformNodeType } from './types'
@@ -15,12 +15,25 @@ const useConfig = (id: string, payload: TemplateTransformNodeType) => {
const { nodesReadOnly: readOnly } = useNodesReadOnly()
const defaultConfig = useStore(s => s.nodesDefaultConfigs)[payload.type]
const { inputs, setInputs } = useNodeCrud<TemplateTransformNodeType>(id, payload)
const { handleVarListChange, handleAddVariable } = useVarList<TemplateTransformNodeType>({
const { inputs, setInputs: doSetInputs } = useNodeCrud<TemplateTransformNodeType>(id, payload)
const inputsRef = useRef(inputs)
const setInputs = useCallback((newPayload: TemplateTransformNodeType) => {
doSetInputs(newPayload)
inputsRef.current = newPayload
}, [doSetInputs])
const { handleVarListChange, handleAddVariable: handleAddEmptyVariable } = useVarList<TemplateTransformNodeType>({
inputs,
setInputs,
})
const handleAddVariable = useCallback((payload: Variable) => {
const newInputs = produce(inputsRef.current, (draft: any) => {
draft.variables.push(payload)
})
setInputs(newInputs)
}, [setInputs])
useEffect(() => {
if (inputs.template)
return
@@ -36,11 +49,11 @@ const useConfig = (id: string, payload: TemplateTransformNodeType) => {
}, [defaultConfig])
const handleCodeChange = useCallback((template: string) => {
const newInputs = produce(inputs, (draft: any) => {
const newInputs = produce(inputsRef.current, (draft: any) => {
draft.template = template
})
setInputs(newInputs)
}, [inputs, setInputs])
}, [setInputs])
// single run
const {
@@ -82,6 +95,7 @@ const useConfig = (id: string, payload: TemplateTransformNodeType) => {
inputs,
handleVarListChange,
handleAddVariable,
handleAddEmptyVariable,
handleCodeChange,
filterVar,
// single run