282 lines
9.0 KiB
Rust
282 lines
9.0 KiB
Rust
// src/modes/canvas/edit.rs
|
|
use crate::config::binds::config::Config;
|
|
use crate::services::grpc_client::GrpcClient;
|
|
use crate::state::pages::{
|
|
auth::{LoginState, RegisterState},
|
|
canvas_state::CanvasState,
|
|
};
|
|
use crate::state::pages::form::FormState;
|
|
use crate::state::pages::add_table::AddTableState;
|
|
use crate::modes::handlers::event::EventOutcome;
|
|
use crate::functions::modes::edit::{auth_e, form_e};
|
|
use crate::functions::modes::edit::add_table_e;
|
|
use crate::state::app::state::AppState;
|
|
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
|
|
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
|
pub enum EditEventOutcome {
|
|
Message(String), // Return a message, stay in Edit mode
|
|
ExitEditMode, // Signal to exit Edit mode
|
|
}
|
|
|
|
pub async fn handle_edit_event(
|
|
key: KeyEvent,
|
|
config: &Config,
|
|
form_state: &mut FormState,
|
|
login_state: &mut LoginState,
|
|
register_state: &mut RegisterState,
|
|
add_table_state: &mut AddTableState,
|
|
ideal_cursor_column: &mut usize,
|
|
current_position: &mut u64,
|
|
total_count: u64,
|
|
grpc_client: &mut GrpcClient,
|
|
app_state: &AppState,
|
|
) -> Result<EditEventOutcome, Box<dyn std::error::Error>> {
|
|
// Global command mode check (should ideally be handled before calling this function)
|
|
if let Some("enter_command_mode") = config.get_action_for_key_in_mode(
|
|
&config.keybindings.global,
|
|
key.code,
|
|
key.modifiers,
|
|
) {
|
|
return Ok(EditEventOutcome::Message(
|
|
"Command mode entry handled globally.".to_string(),
|
|
));
|
|
}
|
|
|
|
if let Some(action) = config.get_action_for_key_in_mode(
|
|
&config.keybindings.common,
|
|
key.code,
|
|
key.modifiers,
|
|
).as_deref() {
|
|
if matches!(action, "save" | "revert") {
|
|
let message_string: String = if app_state.ui.show_login {
|
|
auth_e::execute_common_action(
|
|
action,
|
|
login_state,
|
|
grpc_client,
|
|
current_position,
|
|
total_count,
|
|
)
|
|
.await?
|
|
} else if app_state.ui.show_register {
|
|
auth_e::execute_common_action(
|
|
action,
|
|
register_state,
|
|
grpc_client,
|
|
current_position,
|
|
total_count,
|
|
)
|
|
.await?
|
|
} else if app_state.ui.show_add_table {
|
|
format!(
|
|
"Action '{}' not fully implemented for Add Table view here.",
|
|
action
|
|
)
|
|
} else {
|
|
let outcome = form_e::execute_common_action(
|
|
action,
|
|
form_state,
|
|
grpc_client,
|
|
current_position,
|
|
total_count,
|
|
)
|
|
.await?;
|
|
match outcome {
|
|
EventOutcome::Ok(msg) => msg,
|
|
EventOutcome::DataSaved(_, msg) => msg,
|
|
_ => format!(
|
|
"Unexpected outcome from common action: {:?}",
|
|
outcome
|
|
),
|
|
}
|
|
};
|
|
return Ok(EditEventOutcome::Message(message_string));
|
|
}
|
|
}
|
|
|
|
// Edit-specific actions
|
|
if let Some(action) =
|
|
config.get_edit_action_for_key(key.code, key.modifiers)
|
|
.as_deref() {
|
|
// Handle enter_decider first
|
|
if action == "enter_decider" {
|
|
let effective_action = if app_state.ui.show_register
|
|
&& register_state.in_suggestion_mode
|
|
&& register_state.current_field() == 4 {
|
|
"select_suggestion"
|
|
} else {
|
|
"next_field"
|
|
};
|
|
|
|
let msg = if app_state.ui.show_login {
|
|
auth_e::execute_edit_action(
|
|
effective_action,
|
|
key,
|
|
login_state,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?
|
|
} else if app_state.ui.show_add_table {
|
|
add_table_e::execute_edit_action(
|
|
effective_action,
|
|
key,
|
|
add_table_state,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?
|
|
} else if app_state.ui.show_register {
|
|
auth_e::execute_edit_action(
|
|
effective_action,
|
|
key,
|
|
register_state,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?
|
|
} else {
|
|
form_e::execute_edit_action(
|
|
effective_action,
|
|
key,
|
|
form_state,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?
|
|
};
|
|
return Ok(EditEventOutcome::Message(msg));
|
|
}
|
|
|
|
if action == "exit" {
|
|
if app_state.ui.show_register && register_state.in_suggestion_mode {
|
|
let msg = auth_e::execute_edit_action(
|
|
"exit_suggestion_mode",
|
|
key,
|
|
register_state,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?;
|
|
return Ok(EditEventOutcome::Message(msg));
|
|
} else {
|
|
return Ok(EditEventOutcome::ExitEditMode);
|
|
}
|
|
}
|
|
|
|
// Special handling for role field suggestions (Register view only)
|
|
if app_state.ui.show_register && register_state.current_field() == 4 {
|
|
if !register_state.in_suggestion_mode
|
|
&& key.code == KeyCode::Tab
|
|
&& key.modifiers == KeyModifiers::NONE
|
|
{
|
|
register_state.update_role_suggestions();
|
|
if !register_state.role_suggestions.is_empty() {
|
|
register_state.in_suggestion_mode = true;
|
|
register_state.selected_suggestion_index = Some(0);
|
|
return Ok(EditEventOutcome::Message(
|
|
"Suggestions shown".to_string(),
|
|
));
|
|
} else {
|
|
return Ok(EditEventOutcome::Message(
|
|
"No suggestions available".to_string(),
|
|
));
|
|
}
|
|
}
|
|
if register_state.in_suggestion_mode
|
|
&& matches!(
|
|
action,
|
|
"suggestion_down" | "suggestion_up"
|
|
)
|
|
{
|
|
let msg = auth_e::execute_edit_action(
|
|
action,
|
|
key,
|
|
register_state,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?;
|
|
return Ok(EditEventOutcome::Message(msg));
|
|
}
|
|
}
|
|
|
|
// Execute other edit actions based on the current view
|
|
let msg = if app_state.ui.show_login {
|
|
auth_e::execute_edit_action(
|
|
action,
|
|
key,
|
|
login_state,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?
|
|
} else if app_state.ui.show_add_table {
|
|
add_table_e::execute_edit_action(
|
|
action,
|
|
key,
|
|
add_table_state,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?
|
|
} else if app_state.ui.show_register {
|
|
auth_e::execute_edit_action(
|
|
action,
|
|
key,
|
|
register_state,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?
|
|
} else {
|
|
form_e::execute_edit_action(
|
|
action,
|
|
key,
|
|
form_state,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?
|
|
};
|
|
return Ok(EditEventOutcome::Message(msg));
|
|
}
|
|
|
|
// --- Character insertion ---
|
|
if app_state.ui.show_register && register_state.in_suggestion_mode {
|
|
register_state.in_suggestion_mode = false;
|
|
register_state.show_role_suggestions = false;
|
|
register_state.selected_suggestion_index = None;
|
|
}
|
|
|
|
let msg = if app_state.ui.show_login {
|
|
auth_e::execute_edit_action(
|
|
"insert_char",
|
|
key,
|
|
login_state,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?
|
|
} else if app_state.ui.show_add_table {
|
|
add_table_e::execute_edit_action(
|
|
"insert_char",
|
|
key,
|
|
add_table_state,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?
|
|
} else if app_state.ui.show_register {
|
|
auth_e::execute_edit_action(
|
|
"insert_char",
|
|
key,
|
|
register_state,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?
|
|
} else {
|
|
form_e::execute_edit_action(
|
|
"insert_char",
|
|
key,
|
|
form_state,
|
|
ideal_cursor_column,
|
|
)
|
|
.await?
|
|
};
|
|
|
|
if app_state.ui.show_register && register_state.current_field() == 4 {
|
|
register_state.update_role_suggestions();
|
|
}
|
|
|
|
return Ok(EditEventOutcome::Message(msg));
|
|
}
|