подстветка ячеек

This commit is contained in:
2025-07-22 05:35:04 +03:00
parent 555505e09e
commit faad68cdc0
8 changed files with 394 additions and 46 deletions

View File

@@ -11,12 +11,17 @@ import {
CachedCellData,
COL_COUNT,
DualSpreadsheetProps,
HighlightedCell,
ROW_COUNT,
SHEET_NAMES,
SHEET_TYPES,
SheetType,
} from './types'
import { getColumnLabel } from './utils'
import {
convertReferencesToHighlights,
extractCellReferences,
getColumnLabel,
} from './utils'
const DualSpreadsheet: FC<DualSpreadsheetProps> = ({
templateData = {},
@@ -66,6 +71,9 @@ const DualSpreadsheet: FC<DualSpreadsheetProps> = ({
const [baseFileData, setBaseFileData] = useState<
Record<string, Record<string, any>>
>({}) // Базовые данные файла
const [highlightedCells, setHighlightedCells] = useState<HighlightedCell[]>(
[]
) // Подсвеченные ячейки для формул
// Мемоизируем начальное состояние ширин колонок
const initialColumnWidths = useMemo(
@@ -120,7 +128,7 @@ const DualSpreadsheet: FC<DualSpreadsheetProps> = ({
const rowHeaderRefs =
useRef<Record<SheetType, HTMLDivElement | null>>(initialRowHeaderRefs)
const activeCellInputRef = useRef<HTMLInputElement>(null)
const activeCellInputRef = useRef<HTMLInputElement | null>(null)
const formulaBarRef = useRef<HTMLInputElement | null>(null)
const initialTemplateValues = useRef<Record<string, string>>({})
@@ -294,6 +302,38 @@ const DualSpreadsheet: FC<DualSpreadsheetProps> = ({
[revision, multiSheetEngine, initialTemplateValues, columnWidths]
)
// Функция для обновления подсветки ячеек при вводе формулы
const updateCellHighlights = useCallback(
(formula: string) => {
if (!formula.startsWith('=')) {
setHighlightedCells([])
return
}
try {
// Извлекаем ссылки на ячейки из формулы
const references = extractCellReferences(formula)
// Конвертируем ссылки в подсвеченные ячейки
const highlights = convertReferencesToHighlights(
references,
activeSheet
)
setHighlightedCells(highlights)
console.log('🎨 Подсветка ячеек для формулы:', formula, {
references,
highlights,
})
} catch (error) {
console.error('Ошибка при обновлении подсветки ячеек:', error)
setHighlightedCells([])
}
},
[activeSheet]
)
const formulaBarValue = useMemo(() => {
if (isEditing) {
return editingValue
@@ -332,6 +372,16 @@ const DualSpreadsheet: FC<DualSpreadsheetProps> = ({
}
}, [editingValue, isEditing])
// Обновление подсветки ячеек при вводе формулы
useEffect(() => {
if (isEditing) {
updateCellHighlights(editingValue)
} else {
// Очищаем подсветку когда не редактируем
setHighlightedCells([])
}
}, [isEditing, editingValue, updateCellHighlights])
const startEditing = useCallback(() => {
if (!selectedCell) return
const sheetName = SHEET_NAMES[activeSheet]
@@ -642,10 +692,7 @@ const DualSpreadsheet: FC<DualSpreadsheetProps> = ({
try {
const latestFile = await getLatestFileForTemplate(templateId)
if (latestFile) {
console.log(
'📁 Найден последний файл для шаблона:',
latestFile.filename
)
console.log('📁 Найден последний файл для шаблона:', latestFile.name)
const fileData = await getFileData(latestFile.id)
// TODO: Пока getFileData возвращает пустые данные, используем templateData
@@ -813,6 +860,7 @@ const DualSpreadsheet: FC<DualSpreadsheetProps> = ({
activeSheet={activeSheet}
isEditing={isEditing}
editingValue={editingValue}
highlightedCells={highlightedCells}
handleCellClick={handleCellClick}
handleCellDoubleClick={handleCellDoubleClick}
handleCellChange={handleCellChange}