refactor(advanced_table): Use table_core.handleEditingKeyboard (DRY)
- Remove 160 lines of duplicated keyboard handling code - Use shared table_core.handleEditingKeyboard() for edit buffer operations - Export NavigateDirection from table_core for external use - Keep AdvancedTable-specific navigation logic (cell movement, auto-edit)
This commit is contained in:
parent
91969cb728
commit
60c3f9d456
1 changed files with 77 additions and 134 deletions
|
|
@ -17,6 +17,7 @@ const Context = @import("../../core/context.zig").Context;
|
||||||
const Command = @import("../../core/command.zig");
|
const Command = @import("../../core/command.zig");
|
||||||
const Layout = @import("../../core/layout.zig");
|
const Layout = @import("../../core/layout.zig");
|
||||||
const Style = @import("../../core/style.zig");
|
const Style = @import("../../core/style.zig");
|
||||||
|
const table_core = @import("../table_core.zig");
|
||||||
|
|
||||||
// Re-export types
|
// Re-export types
|
||||||
pub const types = @import("types.zig");
|
pub const types = @import("types.zig");
|
||||||
|
|
@ -46,6 +47,9 @@ pub const state = @import("state.zig");
|
||||||
pub const AdvancedTableState = state.AdvancedTableState;
|
pub const AdvancedTableState = state.AdvancedTableState;
|
||||||
pub const AdvancedTableResult = state.AdvancedTableResult;
|
pub const AdvancedTableResult = state.AdvancedTableResult;
|
||||||
|
|
||||||
|
// Re-export table_core types
|
||||||
|
pub const NavigateDirection = table_core.NavigateDirection;
|
||||||
|
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
// Public API
|
// Public API
|
||||||
// =============================================================================
|
// =============================================================================
|
||||||
|
|
@ -919,47 +923,48 @@ fn handleEditingKeyboard(
|
||||||
) void {
|
) void {
|
||||||
const config = table_schema.config;
|
const config = table_schema.config;
|
||||||
|
|
||||||
// Escape: cancel editing (1st = revert, 2nd = exit without save)
|
// Obtener texto original para revert
|
||||||
if (ctx.input.keyPressed(.escape)) {
|
var orig_format_buf: [128]u8 = undefined;
|
||||||
table_state.escape_count += 1;
|
const original_text: ?[]const u8 = if (table_state.original_value) |orig|
|
||||||
if (table_state.escape_count >= 2 or table_state.original_value == null) {
|
orig.format(&orig_format_buf)
|
||||||
// Exit without saving
|
else
|
||||||
table_state.stopEditing();
|
null;
|
||||||
result.edit_ended = true;
|
|
||||||
} else {
|
|
||||||
// Revert to original value
|
|
||||||
if (table_state.original_value) |orig| {
|
|
||||||
var format_buf: [128]u8 = undefined;
|
|
||||||
const text = orig.format(&format_buf);
|
|
||||||
table_state.startEditing(text);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Reset escape count on any other key
|
// Usar table_core para procesamiento de teclado (DRY)
|
||||||
table_state.escape_count = 0;
|
const kb_result = table_core.handleEditingKeyboard(
|
||||||
|
ctx,
|
||||||
|
&table_state.edit_buffer,
|
||||||
|
&table_state.edit_len,
|
||||||
|
&table_state.edit_cursor,
|
||||||
|
&table_state.escape_count,
|
||||||
|
original_text,
|
||||||
|
);
|
||||||
|
|
||||||
// Enter: confirm editing
|
// Si no se procesó ningún evento, salir
|
||||||
if (ctx.input.keyPressed(.enter)) {
|
if (!kb_result.handled) return;
|
||||||
commitEdit(table_state, table_schema, result);
|
|
||||||
|
// Escape canceló la edición
|
||||||
|
if (kb_result.cancelled) {
|
||||||
table_state.stopEditing();
|
table_state.stopEditing();
|
||||||
result.edit_ended = true;
|
result.edit_ended = true;
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Tab: confirm and move to next cell
|
// Commit (Enter, Tab, flechas) y navegación
|
||||||
if (ctx.input.keyPressed(.tab) and config.handle_tab) {
|
if (kb_result.committed) {
|
||||||
commitEdit(table_state, table_schema, result);
|
commitEdit(table_state, table_schema, result);
|
||||||
table_state.stopEditing();
|
table_state.stopEditing();
|
||||||
result.edit_ended = true;
|
result.edit_ended = true;
|
||||||
|
|
||||||
// Move to next/prev cell
|
// Procesar navegación después de commit
|
||||||
const shift = ctx.input.modifiers.shift;
|
switch (kb_result.navigate) {
|
||||||
|
.next_cell, .prev_cell => {
|
||||||
|
if (!config.handle_tab) return;
|
||||||
|
|
||||||
const col_count = table_schema.columns.len;
|
const col_count = table_schema.columns.len;
|
||||||
const row_count = table_state.getRowCount();
|
const row_count = table_state.getRowCount();
|
||||||
|
|
||||||
if (shift) {
|
if (kb_result.navigate == .prev_cell) {
|
||||||
// Shift+Tab: move left
|
// Shift+Tab: move left
|
||||||
if (table_state.selected_col > 0) {
|
if (table_state.selected_col > 0) {
|
||||||
table_state.selectCell(
|
table_state.selectCell(
|
||||||
|
|
@ -1000,74 +1005,12 @@ fn handleEditingKeyboard(
|
||||||
}
|
}
|
||||||
|
|
||||||
result.selection_changed = true;
|
result.selection_changed = true;
|
||||||
return;
|
},
|
||||||
}
|
.next_row, .prev_row => {
|
||||||
|
// Enter o flechas arriba/abajo: solo commit, sin navegación adicional aquí
|
||||||
// Cursor movement within edit buffer
|
// (La navegación entre filas se maneja en otro lugar si es necesario)
|
||||||
if (ctx.input.keyPressed(.left)) {
|
},
|
||||||
if (table_state.edit_cursor > 0) {
|
.none => {},
|
||||||
table_state.edit_cursor -= 1;
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (ctx.input.keyPressed(.right)) {
|
|
||||||
if (table_state.edit_cursor < table_state.edit_len) {
|
|
||||||
table_state.edit_cursor += 1;
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (ctx.input.keyPressed(.home)) {
|
|
||||||
table_state.edit_cursor = 0;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
if (ctx.input.keyPressed(.end)) {
|
|
||||||
table_state.edit_cursor = table_state.edit_len;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Backspace: delete char before cursor
|
|
||||||
if (ctx.input.keyPressed(.backspace)) {
|
|
||||||
if (table_state.edit_cursor > 0) {
|
|
||||||
// Shift characters left
|
|
||||||
var i: usize = table_state.edit_cursor - 1;
|
|
||||||
while (i < table_state.edit_len - 1) : (i += 1) {
|
|
||||||
table_state.edit_buffer[i] = table_state.edit_buffer[i + 1];
|
|
||||||
}
|
|
||||||
table_state.edit_len -= 1;
|
|
||||||
table_state.edit_cursor -= 1;
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Delete: delete char at cursor
|
|
||||||
if (ctx.input.keyPressed(.delete)) {
|
|
||||||
if (table_state.edit_cursor < table_state.edit_len) {
|
|
||||||
// Shift characters left
|
|
||||||
var i: usize = table_state.edit_cursor;
|
|
||||||
while (i < table_state.edit_len - 1) : (i += 1) {
|
|
||||||
table_state.edit_buffer[i] = table_state.edit_buffer[i + 1];
|
|
||||||
}
|
|
||||||
table_state.edit_len -= 1;
|
|
||||||
}
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
// Character input
|
|
||||||
if (ctx.input.text_input_len > 0) {
|
|
||||||
const text = ctx.input.text_input[0..ctx.input.text_input_len];
|
|
||||||
for (text) |ch| {
|
|
||||||
if (ch >= 32 and ch < 127) {
|
|
||||||
if (table_state.edit_len < types.MAX_EDIT_BUFFER - 1) {
|
|
||||||
// Shift characters right
|
|
||||||
var i: usize = table_state.edit_len;
|
|
||||||
while (i > table_state.edit_cursor) : (i -= 1) {
|
|
||||||
table_state.edit_buffer[i] = table_state.edit_buffer[i - 1];
|
|
||||||
}
|
|
||||||
table_state.edit_buffer[table_state.edit_cursor] = ch;
|
|
||||||
table_state.edit_len += 1;
|
|
||||||
table_state.edit_cursor += 1;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue