wip: ui: esc cancells editing a cell

This commit is contained in:
Jeremy Wall 2024-12-01 09:54:49 -05:00
parent efeb5a22d4
commit 919d84b44c
2 changed files with 26 additions and 5 deletions

View File

@ -202,7 +202,8 @@ impl<'ws> Workspace<'ws> {
], ],
Modality::CellEdit => vec![ Modality::CellEdit => vec![
"Edit Mode:".to_string(), "Edit Mode:".to_string(),
"* ESC, ENTER/RETURN: Exit edit mode".to_string(), "* ENTER/RETURN: Exit edit mode and save changes".to_string(),
"* ESC: Exit edit mode and discard changes".to_string(),
"Otherwise edit as normal".to_string(), "Otherwise edit as normal".to_string(),
], ],
Modality::Command => vec![ Modality::Command => vec![
@ -252,7 +253,8 @@ impl<'ws> Workspace<'ws> {
self.enter_dialog_mode(self.render_help_text()); self.enter_dialog_mode(self.render_help_text());
return Ok(None); return Ok(None);
} }
KeyCode::Esc | KeyCode::Enter => self.exit_edit_mode()?, KeyCode::Enter => self.exit_edit_mode(true)?,
KeyCode::Esc => self.exit_edit_mode(false)?,
_ => { _ => {
// NOOP // NOOP
} }
@ -495,15 +497,17 @@ impl<'ws> Workspace<'ws> {
Ok(()) Ok(())
} }
fn exit_edit_mode(&mut self) -> Result<()> { fn exit_edit_mode(&mut self, keep: bool) -> Result<()> {
self.text_area.set_cursor_line_style(Style::default()); self.text_area.set_cursor_line_style(Style::default());
self.text_area.set_cursor_style(Style::default()); self.text_area.set_cursor_style(Style::default());
let contents = self.text_area.lines().join("\n"); let contents = self.text_area.lines().join("\n");
if self.state.dirty { if self.state.dirty && keep{
self.book.edit_current_cell(contents)?; self.book.edit_current_cell(contents)?;
self.book.evaluate(); self.book.evaluate();
self.state.dirty = false; } else {
self.text_area = reset_text_area(self.book.get_current_cell_contents()?);
} }
self.state.dirty = false;
self.enter_navigation_mode(); self.enter_navigation_mode();
Ok(()) Ok(())
} }

View File

@ -298,3 +298,20 @@ fn test_command_mode_help_keycode() {
assert_eq!(Some(&Modality::Dialog), ws.state.modality_stack.last()); assert_eq!(Some(&Modality::Dialog), ws.state.modality_stack.last());
assert_eq!(edit_help, ws.state.popup); assert_eq!(edit_help, ws.state.popup);
} }
#[test]
fn test_edit_mode_esc_keycode() {
let mut ws =
Workspace::new_empty("en", "America/New_York").expect("Failed to get empty workbook");
assert_eq!(Some(&Modality::Navigate), ws.state.modality_stack.last());
ws.handle_input(construct_key_event(KeyCode::Char('i')))
.expect("Failed to handle 'i' key");
assert_eq!(Some(&Modality::CellEdit), ws.state.modality_stack.last());
ws.handle_input(construct_key_event(KeyCode::Char('a')))
.expect("Failed to handle 'a' key event");
ws.handle_input(construct_key_event(KeyCode::Esc))
.expect("Failed to handle 'esc' key event");
assert_eq!("", ws.book.get_current_cell_contents().expect("Failed to get current cell contents"));
assert_eq!("", ws.text_area.lines().join("\n"));
}