diff --git a/web/src/components/RealtimeTable/Cell/EditCell/EditCell.jsx b/web/src/components/RealtimeTable/Cell/EditCell/EditCell.jsx index 5a0687c..5cc4386 100644 --- a/web/src/components/RealtimeTable/Cell/EditCell/EditCell.jsx +++ b/web/src/components/RealtimeTable/Cell/EditCell/EditCell.jsx @@ -2,15 +2,18 @@ import { useEffect, useRef, useLayoutEffect, useState } from 'react'; import { isFormula, extractFormula, calculateFormula, formatNumber } from '../../utils/formulaUtils'; import { saveToStorage, loadFromStorage } from '../../utils/localStorageUtils'; +import { useRealtime } from '../../contexts/RealtimeContext'; + +const EditCell = ({ refCell, onChange, disabled, value, tableId, cellId, table }) => { + const { endEditing: contextEndEditing } = useRealtime(); -const EditCell = ({ refCell, onChange, disabled, value, tableId, cellId }) => { const [curValue, setCurValue] = useState(value); const [displayValue, setDisplayValue] = useState(value); const [isFormulaMode, setIsFormulaMode] = useState(false); - + // Ключ для хранения формул в localStorage const storageKey = `formula_${tableId}_${cellId}`; - + const trFrag = refCell.current.offsetParent.offsetParent; const tdFrag = refCell.current.offsetParent; const left = tdFrag.offsetLeft; @@ -69,7 +72,7 @@ const EditCell = ({ refCell, onChange, disabled, value, tableId, cellId }) => { if (isFormula(curValue)) { // Сохраняем формулу в localStorage saveToStorage(storageKey, curValue); - + // Вычисляем и сохраняем результат try { const formula = extractFormula(curValue); @@ -108,6 +111,32 @@ const EditCell = ({ refCell, onChange, disabled, value, tableId, cellId }) => { return displayValue; }; + const finishEditing = () => { + if (!disabled) { + handleSave(); + } + const editingCell = table.getState().editingCell || null; + if (editingCell) { + contextEndEditing?.(editingCell.row, editingCell.column); + } + }; + + useEffect(() => { + const handleClickOutside = (event) => { + // Проверяем, был ли клик внутри элемента редактирования + const editElement = ref.current; + if (editElement && !editElement.contains(event.target)) { + finishEditing(); + } + }; + + document.addEventListener('mousedown', handleClickOutside); + + return () => { + document.removeEventListener('mousedown', handleClickOutside); + }; + }, [ref, finishEditing]); + return (