import React, { forwardRef, useCallback, useEffect, useImperativeHandle, useMemo, useRef, useState } from 'react' import CodeEditorHeader from './EditorHeader' import CodeEditor from '../CodeEditor/CodeEditor' import ActionVariable from './ActionVariable' import ActionFunction from './ActionFunction' import FunctionExplain from './FunctionExplain' import { FunctionType, IWithSelectCodemirrorProps, ScriptEditorRef } from '../CodeEditor/interface' import { Diagnostic } from '@codemirror/lint'; import { functionsToCompletions, variablesToCompletions } from '../CodeEditor/utils'; import { Icon } from 'antd'; const AntIcon = Icon as any; interface IWithSelectComProps extends IWithSelectCodemirrorProps { insetDocValue: (value: any)=>void, insetFuncValue: (value: any)=>void, currentHoverFunc: FunctionType|undefined, setCurrentHoverFunc: (value: FunctionType|undefined)=>void, } interface IWithSelectComRef { editorRef: any; } function WithSelectCom(props:IWithSelectComProps,ref:any) { const { actionBoxHeight = "200px", renderHeader ,showHeader = true, placeholderThemeFiled='', insetDocValue, insetFuncValue, currentHoverFunc, setCurrentHoverFunc } = props; const editorRef = useRef(null); const [ errorInfo, setErrorInfo ] = useState(); const [selectValue, setSelectValue] = useState(); const { completions = [], variables, functions } = props; const handleFunctions = useMemo( () => functionsToCompletions(functions), [functions] ); const handleCompletions = useMemo( () => [ ...completions, ...variablesToCompletions(variables, placeholderThemeFiled), ...handleFunctions, ], [completions, variables] ); useImperativeHandle(ref, () => editorRef.current); const isHadError = errorInfo?.message // hover const currentHoverRef = useRef(null); const currentDetail = useRef(null); const handleMouseOver = useCallback((e:any, detail:any) => { currentDetail.current = detail currentHoverRef.current = e.currentTarget; }, []); const handleMouseOut = useCallback((e:any) => { if (!currentHoverRef.current?.contains(e.relatedTarget)) { if (currentDetail.current !== 'functionDetail') { if (!(e.relatedTarget?.classList.contains('in-wrapper')|| e.relatedTarget?.classList.contains('item-explain'))) { setCurrentHoverFunc(selectValue); } } else { setCurrentHoverFunc(selectValue); } currentDetail.current = null; currentHoverRef.current = null; } }, [currentDetail.current, selectValue]); return (
{showHeader && } { setErrorInfo(error) }} ref={editorRef} {...props} functions={handleFunctions} completions={handleCompletions} />
{isHadError&&} {isHadError}
{variables && variables.length && (
)} {functions && functions.length && (
)} {functions && functions.length && (
)}
) } export default forwardRef(WithSelectCom);