Refactor large toolbox modules
All checks were successful
Deploy Sokko G / deploy (push) Successful in 16s
All checks were successful
Deploy Sokko G / deploy (push) Successful in 16s
This commit is contained in:
parent
c2e37ad3f2
commit
6dd64f87c3
19 changed files with 2042 additions and 1659 deletions
317
website/src/features/toolboxes/modules/useTableInteractions.js
Normal file
317
website/src/features/toolboxes/modules/useTableInteractions.js
Normal file
|
|
@ -0,0 +1,317 @@
|
|||
// Rôle : centralise les interactions du tableau entre sélection, édition, formules et presse-papiers.
|
||||
import { useEffect, useRef, useState } from "react";
|
||||
import { cellAddress, parseCellAddress } from "./tableFormulaEngine.js";
|
||||
import {
|
||||
clampCellPosition,
|
||||
getBoundsFromStartAndSize,
|
||||
getFormulaRangeText,
|
||||
getSelectionBounds,
|
||||
isCellSelected,
|
||||
isSameBounds,
|
||||
moveSelectionCells
|
||||
} from "./tableUtils.js";
|
||||
import { useTableClipboard } from "./useTableClipboard.js";
|
||||
|
||||
export function useTableInteractions({ data, context, textContent, evaluatedCells, save }) {
|
||||
const [editingCell, setEditingCell] = useState("");
|
||||
const [highlightedCell, setHighlightedCell] = useState("");
|
||||
const [selection, setSelection] = useState(null);
|
||||
const [movePreview, setMovePreview] = useState(null);
|
||||
const draggingSelectionRef = useRef(false);
|
||||
const movingSelectionRef = useRef(null);
|
||||
const formulaSelectionRef = useRef(null);
|
||||
const inputRefs = useRef(new Map());
|
||||
const highlightTimeoutRef = useRef(0);
|
||||
const clipboard = useTableClipboard({ data, context, textContent, evaluatedCells, selection, setSelection, save, focusCell });
|
||||
|
||||
function setCellValue(address, value) {
|
||||
const nextCells = { ...data.cells };
|
||||
if (String(value || "").trim()) {
|
||||
nextCells[address] = value;
|
||||
} else {
|
||||
delete nextCells[address];
|
||||
}
|
||||
save({ ...data, cells: nextCells });
|
||||
}
|
||||
|
||||
function clearSelection(rowIndex, columnIndex) {
|
||||
const nextCells = { ...data.cells };
|
||||
const bounds = getSelectionBounds(selection) || {
|
||||
startRow: rowIndex,
|
||||
endRow: rowIndex,
|
||||
startColumn: columnIndex,
|
||||
endColumn: columnIndex
|
||||
};
|
||||
|
||||
for (let selectedRowIndex = bounds.startRow; selectedRowIndex <= bounds.endRow; selectedRowIndex += 1) {
|
||||
for (let selectedColumnIndex = bounds.startColumn; selectedColumnIndex <= bounds.endColumn; selectedColumnIndex += 1) {
|
||||
delete nextCells[cellAddress(selectedRowIndex, selectedColumnIndex)];
|
||||
}
|
||||
}
|
||||
|
||||
save({ ...data, cells: nextCells });
|
||||
const focusedAddress = cellAddress(rowIndex, columnIndex);
|
||||
window.requestAnimationFrame(() => inputRefs.current.get(focusedAddress)?.focus());
|
||||
}
|
||||
|
||||
function startMovingSelection(rowIndex, columnIndex) {
|
||||
const sourceBounds = getSelectionBounds(selection);
|
||||
if (!sourceBounds || !isCellSelected(selection, rowIndex, columnIndex)) return false;
|
||||
const height = sourceBounds.endRow - sourceBounds.startRow + 1;
|
||||
const width = sourceBounds.endColumn - sourceBounds.startColumn + 1;
|
||||
const targetBounds = getBoundsFromStartAndSize(sourceBounds.startRow, sourceBounds.startColumn, height, width);
|
||||
movingSelectionRef.current = { sourceBounds, targetBounds, height, width };
|
||||
setMovePreview(targetBounds);
|
||||
return true;
|
||||
}
|
||||
|
||||
function updateMovePreview(rowIndex, columnIndex) {
|
||||
const movingSelection = movingSelectionRef.current;
|
||||
if (!movingSelection) return false;
|
||||
const targetRow = Math.min(data.rows - movingSelection.height, Math.max(0, rowIndex));
|
||||
const targetColumn = Math.min(data.columns - movingSelection.width, Math.max(0, columnIndex));
|
||||
const targetBounds = getBoundsFromStartAndSize(targetRow, targetColumn, movingSelection.height, movingSelection.width);
|
||||
movingSelectionRef.current = { ...movingSelection, targetBounds };
|
||||
setMovePreview(targetBounds);
|
||||
return true;
|
||||
}
|
||||
|
||||
function finishMovingSelection() {
|
||||
const movingSelection = movingSelectionRef.current;
|
||||
movingSelectionRef.current = null;
|
||||
draggingSelectionRef.current = false;
|
||||
setMovePreview(null);
|
||||
if (!movingSelection || isSameBounds(movingSelection.sourceBounds, movingSelection.targetBounds)) return;
|
||||
const nextCells = moveSelectionCells(data.cells, movingSelection.sourceBounds, movingSelection.targetBounds);
|
||||
save({ ...data, cells: nextCells });
|
||||
const anchor = {
|
||||
rowIndex: movingSelection.targetBounds.startRow,
|
||||
columnIndex: movingSelection.targetBounds.startColumn
|
||||
};
|
||||
const focus = {
|
||||
rowIndex: movingSelection.targetBounds.endRow,
|
||||
columnIndex: movingSelection.targetBounds.endColumn
|
||||
};
|
||||
setSelection({ anchor, focus });
|
||||
focusCell(anchor.rowIndex, anchor.columnIndex);
|
||||
}
|
||||
|
||||
function registerInput(address, element) {
|
||||
if (element) {
|
||||
inputRefs.current.set(address, element);
|
||||
} else {
|
||||
inputRefs.current.delete(address);
|
||||
}
|
||||
}
|
||||
|
||||
function getActiveFormulaInput() {
|
||||
if (!editingCell) return null;
|
||||
const input = inputRefs.current.get(editingCell);
|
||||
const value = data.cells[editingCell] || "";
|
||||
return input && value.startsWith("=") ? input : null;
|
||||
}
|
||||
|
||||
function focusCell(rowIndex, columnIndex, options = {}) {
|
||||
const position = clampCellPosition(rowIndex, columnIndex, data.rows, data.columns);
|
||||
const address = cellAddress(position.rowIndex, position.columnIndex);
|
||||
window.requestAnimationFrame(() => {
|
||||
const input = inputRefs.current.get(address);
|
||||
input?.focus();
|
||||
if (options.select) {
|
||||
input?.select();
|
||||
} else {
|
||||
input?.setSelectionRange(input.value.length, input.value.length);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function startEditing(address, initialValue = null, options = {}) {
|
||||
const nextValue = initialValue == null ? data.cells[address] || "" : initialValue;
|
||||
if (initialValue != null) setCellValue(address, initialValue);
|
||||
setEditingCell(address);
|
||||
window.requestAnimationFrame(() => {
|
||||
const input = inputRefs.current.get(address);
|
||||
input?.focus();
|
||||
if (options.select) {
|
||||
input?.select();
|
||||
} else {
|
||||
const position = options.caret ?? nextValue.length;
|
||||
input?.setSelectionRange(position, position);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function insertFormulaText(text, options = {}) {
|
||||
const input = getActiveFormulaInput();
|
||||
if (!input || !text) return false;
|
||||
|
||||
const currentValue = data.cells[editingCell] || "";
|
||||
const start = options.start ?? input.selectionStart ?? currentValue.length;
|
||||
const end = options.end ?? input.selectionEnd ?? start;
|
||||
const nextValue = `${currentValue.slice(0, start)}${text}${currentValue.slice(end)}`;
|
||||
setCellValue(editingCell, nextValue);
|
||||
setHighlightedCell(options.highlightedCell || text);
|
||||
window.clearTimeout(highlightTimeoutRef.current);
|
||||
highlightTimeoutRef.current = window.setTimeout(() => setHighlightedCell(""), 700);
|
||||
window.requestAnimationFrame(() => {
|
||||
input.focus();
|
||||
input.setSelectionRange(start + text.length, start + text.length);
|
||||
});
|
||||
return true;
|
||||
}
|
||||
|
||||
function insertFormulaReferenceSelection(formulaSelection) {
|
||||
const text = getFormulaRangeText({ anchor: formulaSelection.anchor, focus: formulaSelection.focus });
|
||||
insertFormulaText(text, {
|
||||
start: formulaSelection.start,
|
||||
end: formulaSelection.end,
|
||||
highlightedCell: cellAddress(formulaSelection.focus.rowIndex, formulaSelection.focus.columnIndex)
|
||||
});
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
function stopSelectionDrag() {
|
||||
if (movingSelectionRef.current) {
|
||||
finishMovingSelection();
|
||||
return;
|
||||
}
|
||||
if (formulaSelectionRef.current) {
|
||||
const formulaSelection = formulaSelectionRef.current;
|
||||
formulaSelectionRef.current = null;
|
||||
draggingSelectionRef.current = false;
|
||||
insertFormulaReferenceSelection(formulaSelection);
|
||||
return;
|
||||
}
|
||||
draggingSelectionRef.current = false;
|
||||
}
|
||||
|
||||
window.addEventListener("pointerup", stopSelectionDrag);
|
||||
return () => window.removeEventListener("pointerup", stopSelectionDrag);
|
||||
});
|
||||
|
||||
function selectCell(rowIndex, columnIndex, extend = false) {
|
||||
const cell = { rowIndex, columnIndex };
|
||||
setSelection((currentSelection) => extend && currentSelection?.anchor
|
||||
? { anchor: currentSelection.anchor, focus: cell }
|
||||
: { anchor: cell, focus: cell });
|
||||
}
|
||||
|
||||
function extendSelection(rowIndex, columnIndex) {
|
||||
const cell = { rowIndex, columnIndex };
|
||||
setSelection((currentSelection) => currentSelection?.anchor
|
||||
? { anchor: currentSelection.anchor, focus: cell }
|
||||
: { anchor: cell, focus: cell });
|
||||
}
|
||||
|
||||
function startFormulaSelection(rowIndex, columnIndex) {
|
||||
const input = getActiveFormulaInput();
|
||||
const formulaCell = parseCellAddress(editingCell);
|
||||
if (!input || (formulaCell?.rowIndex === rowIndex && formulaCell?.columnIndex === columnIndex)) return false;
|
||||
const cell = { rowIndex, columnIndex };
|
||||
formulaSelectionRef.current = {
|
||||
anchor: cell,
|
||||
focus: cell,
|
||||
start: input.selectionStart ?? (data.cells[editingCell] || "").length,
|
||||
end: input.selectionEnd ?? input.selectionStart ?? (data.cells[editingCell] || "").length
|
||||
};
|
||||
setSelection({ anchor: cell, focus: cell });
|
||||
draggingSelectionRef.current = true;
|
||||
return true;
|
||||
}
|
||||
|
||||
function extendFormulaSelection(rowIndex, columnIndex) {
|
||||
if (!formulaSelectionRef.current) return false;
|
||||
const focus = { rowIndex, columnIndex };
|
||||
formulaSelectionRef.current = { ...formulaSelectionRef.current, focus };
|
||||
setSelection({ anchor: formulaSelectionRef.current.anchor, focus });
|
||||
return true;
|
||||
}
|
||||
|
||||
function handleCellKeyDown(event, rowIndex, columnIndex, address, isEditing) {
|
||||
if (!isEditing && (event.ctrlKey || event.metaKey) && event.key.toLowerCase() === "c") {
|
||||
clipboard.copySelection(event);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isEditing && event.key === "Delete") {
|
||||
event.preventDefault();
|
||||
clearSelection(rowIndex, columnIndex);
|
||||
return;
|
||||
}
|
||||
|
||||
if (event.key === "Escape") {
|
||||
event.preventDefault();
|
||||
formulaSelectionRef.current = null;
|
||||
draggingSelectionRef.current = false;
|
||||
setEditingCell("");
|
||||
setSelection({ anchor: { rowIndex, columnIndex }, focus: { rowIndex, columnIndex } });
|
||||
focusCell(rowIndex, columnIndex);
|
||||
return;
|
||||
}
|
||||
|
||||
if (!isEditing && event.key === "Enter") {
|
||||
event.preventDefault();
|
||||
startEditing(address, null, { select: true });
|
||||
return;
|
||||
}
|
||||
|
||||
const deltas = {
|
||||
ArrowUp: [-1, 0],
|
||||
ArrowDown: [1, 0],
|
||||
ArrowLeft: [0, -1],
|
||||
ArrowRight: [0, 1],
|
||||
Enter: [event.shiftKey ? -1 : 1, 0],
|
||||
Tab: [0, event.shiftKey ? -1 : 1]
|
||||
};
|
||||
const delta = deltas[event.key];
|
||||
if (!delta) {
|
||||
if (!isEditing && event.key.length === 1 && !event.ctrlKey && !event.metaKey && !event.altKey) {
|
||||
event.preventDefault();
|
||||
startEditing(address, event.key, { caret: event.key.length });
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
const input = event.currentTarget;
|
||||
const valueLength = input.value.length;
|
||||
const caretStart = input.selectionStart ?? valueLength;
|
||||
const caretEnd = input.selectionEnd ?? caretStart;
|
||||
const hasTextSelection = caretStart !== caretEnd;
|
||||
const isHorizontalArrow = event.key === "ArrowLeft" || event.key === "ArrowRight";
|
||||
const canMoveHorizontal = event.key === "ArrowLeft" ? caretStart === 0 : caretEnd === valueLength;
|
||||
const shouldNavigate = !isEditing || event.key === "Tab" || event.key === "Enter" || event.shiftKey || !isHorizontalArrow || (!hasTextSelection && canMoveHorizontal);
|
||||
|
||||
if (!shouldNavigate) return;
|
||||
|
||||
event.preventDefault();
|
||||
setEditingCell("");
|
||||
const nextCell = clampCellPosition(rowIndex + delta[0], columnIndex + delta[1], data.rows, data.columns);
|
||||
if (event.shiftKey && event.key.startsWith("Arrow")) {
|
||||
extendSelection(nextCell.rowIndex, nextCell.columnIndex);
|
||||
} else {
|
||||
selectCell(nextCell.rowIndex, nextCell.columnIndex);
|
||||
}
|
||||
focusCell(nextCell.rowIndex, nextCell.columnIndex);
|
||||
}
|
||||
|
||||
return {
|
||||
editingCell,
|
||||
highlightedCell,
|
||||
selection,
|
||||
movePreview,
|
||||
draggingSelectionRef,
|
||||
copyTsv: clipboard.copyTsv,
|
||||
setCellValue,
|
||||
startFormulaSelection,
|
||||
extendFormulaSelection,
|
||||
startMovingSelection,
|
||||
updateMovePreview,
|
||||
selectCell,
|
||||
extendSelection,
|
||||
pasteSelection: clipboard.pasteSelection,
|
||||
handleCellKeyDown,
|
||||
startEditing,
|
||||
clearEditingCell: () => setEditingCell(""),
|
||||
registerInput
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue