excel_cli/app/
edit.rs

1use crate::actions::{ActionCommand, ActionType, CellAction};
2use crate::app::AppState;
3use crate::app::InputMode;
4use anyhow::Result;
5
6impl AppState<'_> {
7    pub fn start_editing(&mut self) {
8        self.input_mode = InputMode::Editing;
9        let content = self.get_cell_content(self.selected_cell.0, self.selected_cell.1);
10        self.input_buffer = content.clone();
11
12        // Set up TextArea for editing
13        self.text_area = ratatui_textarea::TextArea::default();
14        self.text_area.insert_str(&content);
15    }
16
17    pub fn confirm_edit(&mut self) -> Result<()> {
18        if let InputMode::Editing = self.input_mode {
19            // Get content from TextArea
20            let content = self.text_area.lines().join("\n");
21            let (row, col) = self.selected_cell;
22
23            self.workbook.ensure_cell_exists(row, col);
24
25            self.ensure_column_widths();
26
27            let sheet_index = self.workbook.get_current_sheet_index();
28            let sheet_name = self.workbook.get_current_sheet_name();
29
30            let old_cell = self.workbook.get_current_sheet().data[row][col].clone();
31
32            let mut new_cell = old_cell.clone();
33            new_cell.value = content.clone();
34
35            let cell_action = CellAction::new(
36                sheet_index,
37                sheet_name,
38                row,
39                col,
40                old_cell,
41                new_cell,
42                ActionType::Edit,
43            );
44
45            self.undo_history.push(ActionCommand::Cell(cell_action));
46
47            self.workbook.set_cell_value(row, col, content)?;
48            self.input_mode = InputMode::Normal;
49            self.input_buffer = String::new();
50            self.text_area = ratatui_textarea::TextArea::default();
51        }
52        Ok(())
53    }
54
55    pub fn copy_cell(&mut self) {
56        let content = self.get_cell_content_mut(self.selected_cell.0, self.selected_cell.1);
57        self.clipboard = Some(content);
58        self.add_notification("Cell content copied".to_string());
59    }
60
61    pub fn cut_cell(&mut self) -> Result<()> {
62        let (row, col) = self.selected_cell;
63
64        self.workbook.ensure_cell_exists(row, col);
65
66        self.ensure_column_widths();
67
68        let content = self.get_cell_content(row, col);
69        self.clipboard = Some(content);
70
71        let sheet_index = self.workbook.get_current_sheet_index();
72        let sheet_name = self.workbook.get_current_sheet_name();
73
74        let old_cell = self.workbook.get_current_sheet().data[row][col].clone();
75
76        let mut new_cell = old_cell.clone();
77        new_cell.value = String::new();
78
79        let cell_action = CellAction::new(
80            sheet_index,
81            sheet_name,
82            row,
83            col,
84            old_cell,
85            new_cell,
86            ActionType::Cut,
87        );
88
89        self.undo_history.push(ActionCommand::Cell(cell_action));
90        self.workbook.set_cell_value(row, col, String::new())?;
91
92        self.add_notification("Cell content cut".to_string());
93        Ok(())
94    }
95
96    pub fn paste_cell(&mut self) -> Result<()> {
97        if let Some(content) = self.clipboard.clone() {
98            let (row, col) = self.selected_cell;
99
100            self.workbook.ensure_cell_exists(row, col);
101            self.ensure_column_widths();
102
103            let sheet_index = self.workbook.get_current_sheet_index();
104            let sheet_name = self.workbook.get_current_sheet_name();
105
106            let old_cell = self.workbook.get_current_sheet().data[row][col].clone();
107
108            let mut new_cell = old_cell.clone();
109            new_cell.value = content.clone();
110
111            let cell_action = CellAction::new(
112                sheet_index,
113                sheet_name,
114                row,
115                col,
116                old_cell,
117                new_cell,
118                ActionType::Paste,
119            );
120
121            self.undo_history.push(ActionCommand::Cell(cell_action));
122            self.workbook.set_cell_value(row, col, content)?;
123            self.add_notification("Content pasted".to_string());
124        } else {
125            self.add_notification("Clipboard is empty".to_string());
126        }
127        Ok(())
128    }
129}