import { useCallback, useEffect, useMemo, useRef, useState } from 'react'; import Button from '../../components/Common/Button'; import Label from '../../components/Common/Label'; import { useRSForm } from '../../context/RSFormContext'; import { toast } from 'react-toastify'; import RSEditButton from './RSEditButton'; import { CstType, TokenID } from '../../utils/models'; import useCheckExpression from '../../hooks/useCheckExpression'; import ParsingResult from './ParsingResult'; import { Loader } from '../../components/Common/Loader'; import StatusBar from './StatusBar'; import { AxiosResponse } from 'axios'; import { TextWrapper } from '../../utils/textEditing'; interface ExpressionEditorProps { id: string label: string isActive: boolean disabled?: boolean placeholder?: string value: any onChange: (event: React.ChangeEvent) => void toggleEditMode: () => void setTypification: (typificaiton: string) => void setValue: (expression: string) => void } function ExpressionEditor({ id, label, disabled, isActive, placeholder, value, setValue, toggleEditMode, setTypification, onChange }: ExpressionEditorProps) { const { schema, active } = useRSForm(); const [isModified, setIsModified] = useState(false); const { parseData, checkExpression, resetParse, loading } = useCheckExpression({schema: schema}); const expressionCtrl = useRef(null); useEffect(() => { setIsModified(false); resetParse(); }, [active, resetParse]); const handleCheckExpression = useCallback(() => { const prefix = active?.alias + (active?.cstType === CstType.STRUCTURED ? '::=' : ':=='); const expression = prefix + value; checkExpression(expression, (response: AxiosResponse) => { // TODO: update cursor position setIsModified(false); setTypification(response.data['typification']); toast.success('проверка завершена'); }); }, [value, checkExpression, active, setTypification]); const handleEdit = useCallback((id: TokenID) => { if (!expressionCtrl.current) { toast.error('Нет доступа к полю редактирования формального выражения'); return; } let text = new TextWrapper(expressionCtrl.current); text.insertToken(id); text.finalize(); text.focus(); setValue(text.value); }, [setValue]); const handleChange = useCallback((event: React.ChangeEvent) => { onChange(event); setIsModified(true); }, [setIsModified, onChange]); const handleFocusIn = useCallback(() => { toggleEditMode() }, [toggleEditMode]); const EditButtons = useMemo( () => { return (
); }, [handleEdit]) return (