@@ -8,15 +8,70 @@ use super::{mode::Selection, viewport::Viewport};
88/// 但涉及多步不变量的操作应优先通过方法完成,例如删除当前行列。
99pub struct TableContext {
1010 pub theme : Theme ,
11- pub path : String ,
12- pub wb : Workbook ,
1311 pub viewport : Viewport ,
14- pub selection : Option < Selection > ,
12+ path : String ,
13+ wb : Workbook ,
14+ selection : Option < Selection > ,
1515 /// 模式需要切换屏幕时写入,随后由 `ModeHost` 取走。
16- pub pending_command : Option < ScreenCommand > ,
16+ pending_command : Option < ScreenCommand > ,
1717}
1818
1919impl TableContext {
20+ /// 创建编辑器共享状态。
21+ pub fn new ( theme : Theme , path : String , wb : Workbook ) -> Self {
22+ Self {
23+ theme,
24+ path,
25+ wb,
26+ viewport : Viewport :: new ( ) ,
27+ selection : None ,
28+ pending_command : None ,
29+ }
30+ }
31+
32+ /// 当前工作簿。
33+ pub fn workbook ( & self ) -> & Workbook {
34+ & self . wb
35+ }
36+
37+ /// 当前工作簿名。
38+ pub fn workbook_name ( & self ) -> & str {
39+ & self . wb . name
40+ }
41+
42+ /// 当前工作簿行数。
43+ pub fn row_count ( & self ) -> usize {
44+ self . wb . rows
45+ }
46+
47+ /// 当前工作簿列数。
48+ pub fn column_count ( & self ) -> usize {
49+ self . wb . columns
50+ }
51+
52+ /// 当前选区。
53+ pub fn selection ( & self ) -> Option < & Selection > {
54+ self . selection . as_ref ( )
55+ }
56+
57+ /// 设置当前选区。
58+ pub fn set_selection ( & mut self , selection : Selection ) {
59+ self . selection = Some ( selection) ;
60+ }
61+
62+ /// 清除当前选区。
63+ pub fn clear_selection ( & mut self ) {
64+ self . selection = None ;
65+ }
66+
67+ /// 光标所在单元格的原始文本。
68+ pub fn current_cell_raw ( & self ) -> String {
69+ self . wb
70+ . get_cell ( self . viewport . cursor ( ) )
71+ . map ( |cell| cell. raw . clone ( ) )
72+ . unwrap_or_default ( )
73+ }
74+
2075 /// 保存当前工作簿。
2176 ///
2277 /// 保存失败会被忽略;当前界面还没有错误提示位置。
@@ -34,6 +89,24 @@ impl TableContext {
3489 self . pending_command . take ( )
3590 }
3691
92+ /// 写入光标所在单元格的文本。
93+ pub fn set_current_cell_text ( & mut self , raw : String ) {
94+ self . wb . set_text ( self . viewport . cursor ( ) , raw) ;
95+ }
96+
97+ /// 清除光标所在单元格。
98+ pub fn clear_current_cell ( & mut self ) {
99+ self . wb . clear_cell ( self . viewport . cursor ( ) ) ;
100+ }
101+
102+ /// 清空当前选区,并退出选区状态。
103+ pub fn clear_selection_cells ( & mut self ) {
104+ if let Some ( spec) = self . selection_clear_spec ( ) {
105+ self . wb . clear_region ( spec) ;
106+ self . selection = None ;
107+ }
108+ }
109+
37110 /// 删除光标所在行,并同步裁剪光标和滚动位置。
38111 pub fn delete_current_row ( & mut self ) {
39112 self . wb . delete_row ( self . viewport . cursor_row ( ) ) ;
@@ -47,4 +120,19 @@ impl TableContext {
47120 self . viewport . clamp_cursor_col ( self . wb . columns ) ;
48121 self . viewport . scroll_into_view ( ) ;
49122 }
123+
124+ fn selection_clear_spec ( & self ) -> Option < crate :: model:: workbook:: ClearSpec > {
125+ use crate :: model:: workbook:: ClearSpec ;
126+
127+ self . selection . as_ref ( ) . map ( |selection| match * selection {
128+ Selection :: Row ( r) => ClearSpec :: Row ( r) ,
129+ Selection :: Column ( c) => ClearSpec :: Column ( c) ,
130+ Selection :: Range { anchor, cursor } => ClearSpec :: Rect {
131+ c1 : anchor. col . min ( cursor. col ) ,
132+ r1 : anchor. row . min ( cursor. row ) ,
133+ c2 : anchor. col . max ( cursor. col ) ,
134+ r2 : anchor. row . max ( cursor. row ) ,
135+ } ,
136+ } )
137+ }
50138}
0 commit comments