feat: ImageLayoutEditor, arrow-key nav, multi-select bold, wider columns
Some checks failed
CI / go-lint (push) Has been skipped
CI / python-lint (push) Has been skipped
CI / nodejs-lint (push) Has been skipped
CI / test-go-school (push) Successful in 32s
CI / test-go-edu-search (push) Successful in 25s
CI / test-python-klausur (push) Failing after 1m52s
CI / test-python-agent-core (push) Successful in 15s
CI / test-nodejs-website (push) Successful in 18s
Some checks failed
CI / go-lint (push) Has been skipped
CI / python-lint (push) Has been skipped
CI / nodejs-lint (push) Has been skipped
CI / test-go-school (push) Successful in 32s
CI / test-go-edu-search (push) Successful in 25s
CI / test-python-klausur (push) Failing after 1m52s
CI / test-python-agent-core (push) Successful in 15s
CI / test-nodejs-website (push) Successful in 18s
- New ImageLayoutEditor: SVG overlay on original scan with draggable column dividers, horizontal guidelines (margins/header/footer), double-click to add columns, x-button to delete - GridTable: MIN_COL_WIDTH 40→80px for better readability - Arrow up/down keys navigate between rows in the grid editor - Ctrl+Click for multi-cell selection, Ctrl+B to toggle bold on selection - getAdjacentCell works for cells that don't exist yet (new rows/cols) - deleteColumn now merges x-boundaries correctly - Session restore fix: grid_editor_result/structure_result in session GET - Footer row 3-state cycle, auto-create cells for empty footer rows - Grid save/build/GT-mark now advance current_step=11 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
import { useCallback, useRef, useState } from 'react'
|
||||
import type { StructuredGrid, GridZone } from './types'
|
||||
import type { StructuredGrid, GridZone, LayoutDividers } from './types'
|
||||
|
||||
const KLAUSUR_API = '/klausur-api'
|
||||
const MAX_UNDO = 50
|
||||
@@ -134,12 +134,40 @@ export function useGridEditor(sessionId: string | null) {
|
||||
if (!prev) return prev
|
||||
return {
|
||||
...prev,
|
||||
zones: prev.zones.map((zone) => ({
|
||||
...zone,
|
||||
cells: zone.cells.map((cell) =>
|
||||
cell.cell_id === cellId ? { ...cell, text: newText } : cell,
|
||||
),
|
||||
})),
|
||||
zones: prev.zones.map((zone) => {
|
||||
// Check if cell exists
|
||||
const existing = zone.cells.find((c) => c.cell_id === cellId)
|
||||
if (existing) {
|
||||
return {
|
||||
...zone,
|
||||
cells: zone.cells.map((cell) =>
|
||||
cell.cell_id === cellId ? { ...cell, text: newText } : cell,
|
||||
),
|
||||
}
|
||||
}
|
||||
// Cell doesn't exist — create it if the cellId belongs to this zone
|
||||
// cellId format: Z{zone}_R{row}_C{col}
|
||||
const match = cellId.match(/^Z(\d+)_R(\d+)_C(\d+)$/)
|
||||
if (!match || parseInt(match[1]) !== zone.zone_index) return zone
|
||||
const rowIndex = parseInt(match[2])
|
||||
const colIndex = parseInt(match[3])
|
||||
const col = zone.columns.find((c) => c.index === colIndex)
|
||||
const newCell = {
|
||||
cell_id: cellId,
|
||||
zone_index: zone.zone_index,
|
||||
row_index: rowIndex,
|
||||
col_index: colIndex,
|
||||
col_type: col?.label ?? '',
|
||||
text: newText,
|
||||
confidence: 0,
|
||||
bbox_px: { x: 0, y: 0, w: 0, h: 0 },
|
||||
bbox_pct: { x: 0, y: 0, w: 0, h: 0 },
|
||||
word_boxes: [],
|
||||
ocr_engine: 'manual',
|
||||
is_bold: false,
|
||||
}
|
||||
return { ...zone, cells: [...zone.cells, newCell] }
|
||||
}),
|
||||
}
|
||||
})
|
||||
setDirty(true)
|
||||
@@ -192,6 +220,7 @@ export function useGridEditor(sessionId: string | null) {
|
||||
if (!grid) return
|
||||
pushUndo(grid.zones)
|
||||
|
||||
// Cycle: normal → header → footer → normal
|
||||
setGrid((prev) => {
|
||||
if (!prev) return prev
|
||||
return {
|
||||
@@ -200,9 +229,16 @@ export function useGridEditor(sessionId: string | null) {
|
||||
if (zone.zone_index !== zoneIndex) return zone
|
||||
return {
|
||||
...zone,
|
||||
rows: zone.rows.map((r) =>
|
||||
r.index === rowIndex ? { ...r, is_header: !r.is_header } : r,
|
||||
),
|
||||
rows: zone.rows.map((r) => {
|
||||
if (r.index !== rowIndex) return r
|
||||
if (!r.is_header && !r.is_footer) {
|
||||
return { ...r, is_header: true, is_footer: false }
|
||||
} else if (r.is_header) {
|
||||
return { ...r, is_header: false, is_footer: true }
|
||||
} else {
|
||||
return { ...r, is_header: false, is_footer: false }
|
||||
}
|
||||
}),
|
||||
}
|
||||
}),
|
||||
}
|
||||
@@ -229,9 +265,23 @@ export function useGridEditor(sessionId: string | null) {
|
||||
...prev,
|
||||
zones: prev.zones.map((z) => {
|
||||
if (z.zone_index !== zoneIndex) return z
|
||||
const deletedCol = z.columns.find((c) => c.index === colIndex)
|
||||
const newColumns = z.columns
|
||||
.filter((c) => c.index !== colIndex)
|
||||
.map((c, i) => ({ ...c, index: i, label: `column_${i + 1}` }))
|
||||
.map((c, i) => {
|
||||
const result = { ...c, index: i, label: `column_${i + 1}` }
|
||||
// Merge x-boundary: previous column absorbs deleted column's space
|
||||
if (deletedCol) {
|
||||
if (c.index === colIndex - 1) {
|
||||
result.x_max_pct = deletedCol.x_max_pct
|
||||
result.x_max_px = deletedCol.x_max_px
|
||||
} else if (colIndex === 0 && c.index === 1) {
|
||||
result.x_min_pct = deletedCol.x_min_pct
|
||||
result.x_min_px = deletedCol.x_min_px
|
||||
}
|
||||
}
|
||||
return result
|
||||
})
|
||||
const newCells = z.cells
|
||||
.filter((c) => c.col_index !== colIndex)
|
||||
.map((c) => {
|
||||
@@ -337,6 +387,323 @@ export function useGridEditor(sessionId: string | null) {
|
||||
[grid, pushUndo],
|
||||
)
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Row management
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
const deleteRow = useCallback(
|
||||
(zoneIndex: number, rowIndex: number) => {
|
||||
if (!grid) return
|
||||
const zone = grid.zones.find((z) => z.zone_index === zoneIndex)
|
||||
if (!zone || zone.rows.length <= 1) return // keep at least 1 row
|
||||
pushUndo(grid.zones)
|
||||
|
||||
setGrid((prev) => {
|
||||
if (!prev) return prev
|
||||
return {
|
||||
...prev,
|
||||
zones: prev.zones.map((z) => {
|
||||
if (z.zone_index !== zoneIndex) return z
|
||||
const newRows = z.rows
|
||||
.filter((r) => r.index !== rowIndex)
|
||||
.map((r, i) => ({ ...r, index: i }))
|
||||
const newCells = z.cells
|
||||
.filter((c) => c.row_index !== rowIndex)
|
||||
.map((c) => {
|
||||
const newRI = c.row_index > rowIndex ? c.row_index - 1 : c.row_index
|
||||
return {
|
||||
...c,
|
||||
row_index: newRI,
|
||||
cell_id: `Z${zoneIndex}_R${String(newRI).padStart(2, '0')}_C${c.col_index}`,
|
||||
}
|
||||
})
|
||||
return { ...z, rows: newRows, cells: newCells }
|
||||
}),
|
||||
summary: {
|
||||
...prev.summary,
|
||||
total_rows: prev.summary.total_rows - 1,
|
||||
total_cells: prev.zones.reduce(
|
||||
(sum, z) =>
|
||||
sum +
|
||||
(z.zone_index === zoneIndex
|
||||
? z.cells.filter((c) => c.row_index !== rowIndex).length
|
||||
: z.cells.length),
|
||||
0,
|
||||
),
|
||||
},
|
||||
}
|
||||
})
|
||||
setDirty(true)
|
||||
},
|
||||
[grid, pushUndo],
|
||||
)
|
||||
|
||||
const addRow = useCallback(
|
||||
(zoneIndex: number, afterRowIndex: number) => {
|
||||
if (!grid) return
|
||||
const zone = grid.zones.find((z) => z.zone_index === zoneIndex)
|
||||
if (!zone) return
|
||||
pushUndo(grid.zones)
|
||||
|
||||
const newRowIndex = afterRowIndex + 1
|
||||
|
||||
setGrid((prev) => {
|
||||
if (!prev) return prev
|
||||
return {
|
||||
...prev,
|
||||
zones: prev.zones.map((z) => {
|
||||
if (z.zone_index !== zoneIndex) return z
|
||||
// Shift existing rows
|
||||
const shiftedRows = z.rows.map((r) =>
|
||||
r.index > afterRowIndex ? { ...r, index: r.index + 1 } : r,
|
||||
)
|
||||
// Insert new row
|
||||
const refRow = z.rows.find((r) => r.index === afterRowIndex) || z.rows[z.rows.length - 1]
|
||||
const newRow = {
|
||||
index: newRowIndex,
|
||||
y_min_px: refRow.y_max_px,
|
||||
y_max_px: refRow.y_max_px + (refRow.y_max_px - refRow.y_min_px),
|
||||
y_min_pct: refRow.y_max_pct,
|
||||
y_max_pct: Math.min(100, refRow.y_max_pct + (refRow.y_max_pct - refRow.y_min_pct)),
|
||||
is_header: false,
|
||||
is_footer: false,
|
||||
}
|
||||
const allRows = [...shiftedRows, newRow].sort((a, b) => a.index - b.index)
|
||||
|
||||
// Shift existing cells
|
||||
const shiftedCells = z.cells.map((c) => {
|
||||
if (c.row_index > afterRowIndex) {
|
||||
const newRI = c.row_index + 1
|
||||
return {
|
||||
...c,
|
||||
row_index: newRI,
|
||||
cell_id: `Z${zoneIndex}_R${String(newRI).padStart(2, '0')}_C${c.col_index}`,
|
||||
}
|
||||
}
|
||||
return c
|
||||
})
|
||||
// Create empty cells for each column
|
||||
const newCells = z.columns.map((col) => ({
|
||||
cell_id: `Z${zoneIndex}_R${String(newRowIndex).padStart(2, '0')}_C${col.index}`,
|
||||
zone_index: zoneIndex,
|
||||
row_index: newRowIndex,
|
||||
col_index: col.index,
|
||||
col_type: col.label,
|
||||
text: '',
|
||||
confidence: 0,
|
||||
bbox_px: { x: 0, y: 0, w: 0, h: 0 },
|
||||
bbox_pct: { x: 0, y: 0, w: 0, h: 0 },
|
||||
word_boxes: [],
|
||||
ocr_engine: 'manual',
|
||||
is_bold: false,
|
||||
}))
|
||||
|
||||
return { ...z, rows: allRows, cells: [...shiftedCells, ...newCells] }
|
||||
}),
|
||||
summary: {
|
||||
...prev.summary,
|
||||
total_rows: prev.summary.total_rows + 1,
|
||||
total_cells: prev.summary.total_cells + (zone?.columns.length ?? 0),
|
||||
},
|
||||
}
|
||||
})
|
||||
setDirty(true)
|
||||
},
|
||||
[grid, pushUndo],
|
||||
)
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Layout editing (image overlay)
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
/** Capture current state for undo — call once at drag start. */
|
||||
const commitUndoPoint = useCallback(() => {
|
||||
if (!grid) return
|
||||
pushUndo(grid.zones)
|
||||
}, [grid, pushUndo])
|
||||
|
||||
/** Move a column boundary. boundaryIndex 0 = left edge of col 0, etc. */
|
||||
const updateColumnDivider = useCallback(
|
||||
(zoneIndex: number, boundaryIndex: number, newXPct: number) => {
|
||||
if (!grid) return
|
||||
setGrid((prev) => {
|
||||
if (!prev) return prev
|
||||
const imgW = prev.image_width || 1
|
||||
const newPx = Math.round((newXPct / 100) * imgW)
|
||||
return {
|
||||
...prev,
|
||||
zones: prev.zones.map((z) => {
|
||||
if (z.zone_index !== zoneIndex) return z
|
||||
return {
|
||||
...z,
|
||||
columns: z.columns.map((col) => {
|
||||
// Right edge of the column before this boundary
|
||||
if (col.index === boundaryIndex - 1) {
|
||||
return { ...col, x_max_pct: newXPct, x_max_px: newPx }
|
||||
}
|
||||
// Left edge of the column at this boundary
|
||||
if (col.index === boundaryIndex) {
|
||||
return { ...col, x_min_pct: newXPct, x_min_px: newPx }
|
||||
}
|
||||
return col
|
||||
}),
|
||||
}
|
||||
}),
|
||||
}
|
||||
})
|
||||
setDirty(true)
|
||||
},
|
||||
[grid],
|
||||
)
|
||||
|
||||
/** Update horizontal layout guidelines (margins, header, footer). */
|
||||
const updateLayoutHorizontals = useCallback(
|
||||
(horizontals: LayoutDividers['horizontals']) => {
|
||||
if (!grid) return
|
||||
setGrid((prev) => {
|
||||
if (!prev) return prev
|
||||
return {
|
||||
...prev,
|
||||
layout_dividers: {
|
||||
...(prev.layout_dividers || { horizontals: {} }),
|
||||
horizontals,
|
||||
},
|
||||
}
|
||||
})
|
||||
setDirty(true)
|
||||
},
|
||||
[grid],
|
||||
)
|
||||
|
||||
/** Split a column at a given x percentage, creating a new column. */
|
||||
const splitColumnAt = useCallback(
|
||||
(zoneIndex: number, xPct: number) => {
|
||||
if (!grid) return
|
||||
const zone = grid.zones.find((z) => z.zone_index === zoneIndex)
|
||||
if (!zone) return
|
||||
|
||||
const sorted = [...zone.columns].sort((a, b) => a.index - b.index)
|
||||
const targetCol = sorted.find((c) => c.x_min_pct <= xPct && c.x_max_pct >= xPct)
|
||||
if (!targetCol) return
|
||||
|
||||
pushUndo(grid.zones)
|
||||
const newColIndex = targetCol.index + 1
|
||||
const imgW = grid.image_width || 1
|
||||
|
||||
setGrid((prev) => {
|
||||
if (!prev) return prev
|
||||
return {
|
||||
...prev,
|
||||
zones: prev.zones.map((z) => {
|
||||
if (z.zone_index !== zoneIndex) return z
|
||||
const leftCol = {
|
||||
...targetCol,
|
||||
x_max_pct: xPct,
|
||||
x_max_px: Math.round((xPct / 100) * imgW),
|
||||
}
|
||||
const rightCol = {
|
||||
index: newColIndex,
|
||||
label: `column_${newColIndex + 1}`,
|
||||
x_min_pct: xPct,
|
||||
x_max_pct: targetCol.x_max_pct,
|
||||
x_min_px: Math.round((xPct / 100) * imgW),
|
||||
x_max_px: targetCol.x_max_px,
|
||||
bold: false,
|
||||
}
|
||||
const updatedCols = z.columns.map((c) => {
|
||||
if (c.index === targetCol.index) return leftCol
|
||||
if (c.index > targetCol.index) return { ...c, index: c.index + 1, label: `column_${c.index + 2}` }
|
||||
return c
|
||||
})
|
||||
const allCols = [...updatedCols, rightCol].sort((a, b) => a.index - b.index)
|
||||
const shiftedCells = z.cells.map((c) => {
|
||||
if (c.col_index > targetCol.index) {
|
||||
const newCI = c.col_index + 1
|
||||
return {
|
||||
...c,
|
||||
col_index: newCI,
|
||||
cell_id: `Z${zoneIndex}_R${String(c.row_index).padStart(2, '0')}_C${newCI}`,
|
||||
}
|
||||
}
|
||||
return c
|
||||
})
|
||||
const newCells = z.rows.map((row) => ({
|
||||
cell_id: `Z${zoneIndex}_R${String(row.index).padStart(2, '0')}_C${newColIndex}`,
|
||||
zone_index: zoneIndex,
|
||||
row_index: row.index,
|
||||
col_index: newColIndex,
|
||||
col_type: `column_${newColIndex + 1}`,
|
||||
text: '',
|
||||
confidence: 0,
|
||||
bbox_px: { x: 0, y: 0, w: 0, h: 0 },
|
||||
bbox_pct: { x: 0, y: 0, w: 0, h: 0 },
|
||||
word_boxes: [],
|
||||
ocr_engine: 'manual',
|
||||
is_bold: false,
|
||||
}))
|
||||
return { ...z, columns: allCols, cells: [...shiftedCells, ...newCells] }
|
||||
}),
|
||||
summary: {
|
||||
...prev.summary,
|
||||
total_columns: prev.summary.total_columns + 1,
|
||||
total_cells: prev.summary.total_cells + (zone.rows.length),
|
||||
},
|
||||
}
|
||||
})
|
||||
setDirty(true)
|
||||
},
|
||||
[grid, pushUndo],
|
||||
)
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Multi-select & bulk formatting
|
||||
// ------------------------------------------------------------------
|
||||
|
||||
const [selectedCells, setSelectedCells] = useState<Set<string>>(new Set())
|
||||
|
||||
const toggleCellSelection = useCallback(
|
||||
(cellId: string) => {
|
||||
setSelectedCells((prev) => {
|
||||
const next = new Set(prev)
|
||||
if (next.has(cellId)) next.delete(cellId)
|
||||
else next.add(cellId)
|
||||
return next
|
||||
})
|
||||
},
|
||||
[],
|
||||
)
|
||||
|
||||
const clearCellSelection = useCallback(() => {
|
||||
setSelectedCells(new Set())
|
||||
}, [])
|
||||
|
||||
/** Toggle bold on all selected cells (and their columns). */
|
||||
const toggleSelectedBold = useCallback(() => {
|
||||
if (!grid || selectedCells.size === 0) return
|
||||
pushUndo(grid.zones)
|
||||
|
||||
// Determine if we're turning bold on or off (majority rule)
|
||||
const cells = grid.zones.flatMap((z) => z.cells)
|
||||
const selectedArr = cells.filter((c) => selectedCells.has(c.cell_id))
|
||||
const boldCount = selectedArr.filter((c) => c.is_bold).length
|
||||
const newBold = boldCount < selectedArr.length / 2
|
||||
|
||||
setGrid((prev) => {
|
||||
if (!prev) return prev
|
||||
return {
|
||||
...prev,
|
||||
zones: prev.zones.map((zone) => ({
|
||||
...zone,
|
||||
cells: zone.cells.map((cell) =>
|
||||
selectedCells.has(cell.cell_id) ? { ...cell, is_bold: newBold } : cell,
|
||||
),
|
||||
})),
|
||||
}
|
||||
})
|
||||
setDirty(true)
|
||||
setSelectedCells(new Set())
|
||||
}, [grid, selectedCells, pushUndo])
|
||||
|
||||
// ------------------------------------------------------------------
|
||||
// Undo / Redo
|
||||
// ------------------------------------------------------------------
|
||||
@@ -368,20 +735,37 @@ export function useGridEditor(sessionId: string | null) {
|
||||
(cellId: string, direction: 'up' | 'down' | 'left' | 'right'): string | null => {
|
||||
if (!grid) return null
|
||||
for (const zone of grid.zones) {
|
||||
// Find the cell or derive row/col from cellId pattern
|
||||
const cell = zone.cells.find((c) => c.cell_id === cellId)
|
||||
if (!cell) continue
|
||||
let currentRow: number, currentCol: number
|
||||
if (cell) {
|
||||
currentRow = cell.row_index
|
||||
currentCol = cell.col_index
|
||||
} else {
|
||||
// Try to parse from cellId: Z{zone}_R{row}_C{col}
|
||||
const match = cellId.match(/^Z(\d+)_R(\d+)_C(\d+)$/)
|
||||
if (!match || parseInt(match[1]) !== zone.zone_index) continue
|
||||
currentRow = parseInt(match[2])
|
||||
currentCol = parseInt(match[3])
|
||||
}
|
||||
|
||||
let targetRow = cell.row_index
|
||||
let targetCol = cell.col_index
|
||||
let targetRow = currentRow
|
||||
let targetCol = currentCol
|
||||
if (direction === 'up') targetRow--
|
||||
if (direction === 'down') targetRow++
|
||||
if (direction === 'left') targetCol--
|
||||
if (direction === 'right') targetCol++
|
||||
|
||||
// Check bounds
|
||||
const hasRow = zone.rows.some((r) => r.index === targetRow)
|
||||
const hasCol = zone.columns.some((c) => c.index === targetCol)
|
||||
if (!hasRow || !hasCol) return null
|
||||
|
||||
// Return existing cell ID or construct one
|
||||
const target = zone.cells.find(
|
||||
(c) => c.row_index === targetRow && c.col_index === targetCol,
|
||||
)
|
||||
return target?.cell_id ?? null
|
||||
return target?.cell_id ?? `Z${zone.zone_index}_R${String(targetRow).padStart(2, '0')}_C${targetCol}`
|
||||
}
|
||||
return null
|
||||
},
|
||||
@@ -396,6 +780,7 @@ export function useGridEditor(sessionId: string | null) {
|
||||
dirty,
|
||||
selectedCell,
|
||||
selectedZone,
|
||||
selectedCells,
|
||||
setSelectedCell,
|
||||
setSelectedZone,
|
||||
buildGrid,
|
||||
@@ -411,5 +796,14 @@ export function useGridEditor(sessionId: string | null) {
|
||||
getAdjacentCell,
|
||||
deleteColumn,
|
||||
addColumn,
|
||||
deleteRow,
|
||||
addRow,
|
||||
commitUndoPoint,
|
||||
updateColumnDivider,
|
||||
updateLayoutHorizontals,
|
||||
splitColumnAt,
|
||||
toggleCellSelection,
|
||||
clearCellSelection,
|
||||
toggleSelectedBold,
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user