94 lines
4.1 KiB
JavaScript
94 lines
4.1 KiB
JavaScript
// Rôle : fournit l'outil tableau avec cellules libres, formules simples et échange TSV.
|
|
import { useMemo } from "react";
|
|
import { ModuleTextExchangeConfiguration } from "./ModuleConfiguration.jsx";
|
|
import { columnIndexToName, evaluateTable } from "./tableFormulaEngine.js";
|
|
import { TableGrid } from "./TableGrid.jsx";
|
|
import { TableToolbar } from "./TableToolbar.jsx";
|
|
import {
|
|
DEFAULT_COLUMNS,
|
|
DEFAULT_ROWS,
|
|
cleanCells,
|
|
cleanLabels,
|
|
getRowHeaderWidth,
|
|
getColumnWidths
|
|
} from "./tableUtils.js";
|
|
import { useTableInteractions } from "./useTableInteractions.js";
|
|
|
|
export function TableModule({ toolboxId, moduleId, context, configurationTarget }) {
|
|
const rawData = context.getModuleData(toolboxId, moduleId, { rows: DEFAULT_ROWS, columns: DEFAULT_COLUMNS, cells: {} });
|
|
const data = useMemo(() => context.normalizeTableData(rawData), [context, rawData]);
|
|
const textContent = context.moduleText?.table || {};
|
|
const evaluatedCells = useMemo(() => evaluateTable(data.cells, data), [data]);
|
|
const columnWidths = useMemo(() => getColumnWidths(data, evaluatedCells), [data, evaluatedCells]);
|
|
const rowHeaderWidth = useMemo(() => getRowHeaderWidth(data), [data]);
|
|
|
|
function save(nextData) {
|
|
context.setModuleData(toolboxId, moduleId, nextData, "table");
|
|
}
|
|
|
|
function resize(rows, columns) {
|
|
save({
|
|
rows,
|
|
columns,
|
|
cells: cleanCells(data.cells, rows, columns),
|
|
rowLabels: cleanLabels(data.rowLabels, rows, (rowIndex) => rowIndex + 1),
|
|
columnLabels: cleanLabels(data.columnLabels, columns, columnIndexToName)
|
|
});
|
|
}
|
|
|
|
function setColumnLabel(columnIndex, value) {
|
|
const nextLabels = { ...data.columnLabels };
|
|
const nextValue = String(value || "").slice(0, 80);
|
|
if (nextValue.trim() && nextValue !== columnIndexToName(columnIndex)) {
|
|
nextLabels[columnIndex] = nextValue;
|
|
} else {
|
|
delete nextLabels[columnIndex];
|
|
}
|
|
save({ ...data, columnLabels: nextLabels });
|
|
}
|
|
|
|
function setRowLabel(rowIndex, value) {
|
|
const nextLabels = { ...data.rowLabels };
|
|
const nextValue = String(value || "").slice(0, 80);
|
|
if (nextValue.trim() && nextValue !== String(rowIndex + 1)) {
|
|
nextLabels[rowIndex] = nextValue;
|
|
} else {
|
|
delete nextLabels[rowIndex];
|
|
}
|
|
save({ ...data, rowLabels: nextLabels });
|
|
}
|
|
const tableInteractions = useTableInteractions({ data, context, textContent, evaluatedCells, save });
|
|
|
|
return (
|
|
<div className="table-module">
|
|
<ModuleTextExchangeConfiguration target={configurationTarget} title={context.moduleConfigurationText?.dataTitle} type="table" data={data} context={context} textContent={textContent} exportLabel="Exporter TSV" onImport={(nextData) => context.setModuleData(toolboxId, moduleId, nextData, "table")} />
|
|
<TableToolbar rows={data.rows} columns={data.columns} textContent={textContent} onResize={resize} onCopy={tableInteractions.copyTsv} />
|
|
<TableGrid
|
|
data={data}
|
|
textContent={textContent}
|
|
evaluatedCells={evaluatedCells}
|
|
editingCell={tableInteractions.editingCell}
|
|
highlightedCell={tableInteractions.highlightedCell}
|
|
selection={tableInteractions.selection}
|
|
movePreview={tableInteractions.movePreview}
|
|
columnWidths={columnWidths}
|
|
rowHeaderWidth={rowHeaderWidth}
|
|
draggingSelectionRef={tableInteractions.draggingSelectionRef}
|
|
onColumnLabelChange={setColumnLabel}
|
|
onRowLabelChange={setRowLabel}
|
|
onCellChange={tableInteractions.setCellValue}
|
|
onFormulaSelectionStart={tableInteractions.startFormulaSelection}
|
|
onFormulaSelectionExtend={tableInteractions.extendFormulaSelection}
|
|
onStartMoveSelection={tableInteractions.startMovingSelection}
|
|
onMoveSelection={tableInteractions.updateMovePreview}
|
|
onSelectCell={tableInteractions.selectCell}
|
|
onExtendSelection={tableInteractions.extendSelection}
|
|
onPasteSelection={tableInteractions.pasteSelection}
|
|
onCellKeyDown={tableInteractions.handleCellKeyDown}
|
|
onStartEditing={tableInteractions.startEditing}
|
|
onEditingCellClear={tableInteractions.clearEditingCell}
|
|
registerInput={tableInteractions.registerInput}
|
|
/>
|
|
</div>
|
|
);
|
|
}
|