diff --git a/core/src/event.rs b/core/src/event.rs index 3692188..57ece59 100644 --- a/core/src/event.rs +++ b/core/src/event.rs @@ -72,15 +72,19 @@ pub enum NotebookEvent { #[derive(Clone, Copy, Debug, Display)] pub enum KeyEvent { + A, B, E, H, + I, J, K, L, M, O, W, + CapA, + CapI, CapO, DollarSign, Num(NumKey), diff --git a/core/src/state/notebook/consume/note.rs b/core/src/state/notebook/consume/note.rs index c0a99c6..6de08ea 100644 --- a/core/src/state/notebook/consume/note.rs +++ b/core/src/state/notebook/consume/note.rs @@ -98,12 +98,6 @@ pub async fn open( Ok(NotebookTransition::OpenNote { note, content }) } -pub async fn edit(state: &mut NotebookState) -> Result { - state.inner_state = InnerState::EditingInsertMode; - - Ok(NotebookTransition::EditMode) -} - pub async fn view(state: &mut NotebookState) -> Result { let note = state.get_editing()?.clone(); diff --git a/core/src/state/notebook/inner_state/editing_normal_mode.rs b/core/src/state/notebook/inner_state/editing_normal_mode.rs index 5cc66bb..a6b3751 100644 --- a/core/src/state/notebook/inner_state/editing_normal_mode.rs +++ b/core/src/state/notebook/inner_state/editing_normal_mode.rs @@ -35,7 +35,6 @@ async fn consume_idle( Notebook(SelectNote(note)) => note::select(state, note), Notebook(SelectDirectory(directory)) => directory::select(state, directory), Notebook(UpdateNoteContent(content)) => note::update_content(db, state, content).await, - Notebook(EditNote) => note::edit(state).await, Notebook(BrowseNoteTree) => note::browse(state).await, Key(KeyEvent::J) => Ok(NotebookTransition::EditingNormalMode( NormalModeTransition::MoveCursorDown(1), @@ -64,6 +63,34 @@ async fn consume_idle( Key(KeyEvent::DollarSign) => Ok(NotebookTransition::EditingNormalMode( NormalModeTransition::MoveCursorLineEnd, )), + Key(KeyEvent::I) => { + state.inner_state = InnerState::EditingInsertMode; + + Ok(NotebookTransition::EditingNormalMode( + NormalModeTransition::InsertAtCursor, + )) + } + Key(KeyEvent::CapI) => { + state.inner_state = InnerState::EditingInsertMode; + + Ok(NotebookTransition::EditingNormalMode( + NormalModeTransition::InsertAtLineStart, + )) + } + Key(KeyEvent::A) => { + state.inner_state = InnerState::EditingInsertMode; + + Ok(NotebookTransition::EditingNormalMode( + NormalModeTransition::InsertAfterCursor, + )) + } + Key(KeyEvent::CapA) => { + state.inner_state = InnerState::EditingInsertMode; + + Ok(NotebookTransition::EditingNormalMode( + NormalModeTransition::InsertAtLineEnd, + )) + } Key(KeyEvent::O) => { state.inner_state = InnerState::EditingInsertMode; diff --git a/core/src/transition.rs b/core/src/transition.rs index 474b6aa..e94ac5c 100644 --- a/core/src/transition.rs +++ b/core/src/transition.rs @@ -61,7 +61,6 @@ pub enum NotebookTransition { note: Note, content: String, }, - EditMode, ViewMode(Note), SelectNote(Note), SelectDirectory(Directory), @@ -92,6 +91,10 @@ pub enum NormalModeTransition { MoveCursorWordBack(usize), MoveCursorLineStart, MoveCursorLineEnd, + InsertAtCursor, + InsertAtLineStart, + InsertAfterCursor, + InsertAtLineEnd, InsertNewLineBelow, InsertNewLineAbove, } diff --git a/tui/src/action.rs b/tui/src/action.rs index 11e0c08..8a398aa 100644 --- a/tui/src/action.rs +++ b/tui/src/action.rs @@ -354,16 +354,20 @@ fn to_event(input: Input) -> Option { }; let event = match code { + KeyCode::Char('a') => KeyEvent::A, KeyCode::Char('b') => KeyEvent::B, KeyCode::Char('e') => KeyEvent::E, KeyCode::Char('h') => KeyEvent::H, + KeyCode::Char('i') => KeyEvent::I, KeyCode::Char('j') => KeyEvent::J, KeyCode::Char('k') => KeyEvent::K, KeyCode::Char('l') => KeyEvent::L, KeyCode::Char('m') => KeyEvent::M, - KeyCode::Char('O') => KeyEvent::CapO, KeyCode::Char('o') => KeyEvent::O, KeyCode::Char('w') => KeyEvent::W, + KeyCode::Char('A') => KeyEvent::CapA, + KeyCode::Char('I') => KeyEvent::CapI, + KeyCode::Char('O') => KeyEvent::CapO, KeyCode::Char('1') => NumKey::One.into(), KeyCode::Char('2') => NumKey::Two.into(), KeyCode::Char('3') => NumKey::Three.into(), diff --git a/tui/src/context/notebook.rs b/tui/src/context/notebook.rs index e6542a4..183d049 100644 --- a/tui/src/context/notebook.rs +++ b/tui/src/context/notebook.rs @@ -259,10 +259,6 @@ impl NotebookContext { Action::Dispatch(NotebookEvent::BrowseNoteTree.into()) } - KeyCode::Char('i') => { - self.state = ContextState::EditorInsertMode; - Action::Dispatch(NotebookEvent::EditNote.into()) - } KeyCode::Char('t') => { self.show_line_number = !self.show_line_number; @@ -274,8 +270,8 @@ impl NotebookContext { } .into(), KeyCode::Char( - 'h' | 'j' | 'k' | 'l' | 'w' | 'e' | 'b' | 'o' | 'O' | '$' | '0' | '1' | '2' | '3' - | '4' | '5' | '6' | '7' | '8' | '9', + 'a' | 'h' | 'i' | 'j' | 'k' | 'l' | 'w' | 'e' | 'b' | 'o' | 'A' | 'I' | 'O' | '$' + | '0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9', ) | KeyCode::Esc => Action::PassThrough, _ => Action::None, diff --git a/tui/src/transitions.rs b/tui/src/transitions.rs index 16fefe8..0ea02bc 100644 --- a/tui/src/transitions.rs +++ b/tui/src/transitions.rs @@ -72,7 +72,6 @@ impl App { self.glues.dispatch(event).await.log_unwrap(); } - NotebookTransition::EditMode => {} NotebookTransition::RemoveNote { selected_directory, .. } @@ -241,6 +240,24 @@ impl App { self.context.notebook.editor.move_cursor(CursorMove::Up); self.context.notebook.state = context::notebook::ContextState::EditorInsertMode; } + NotebookTransition::EditingNormalMode(NormalModeTransition::InsertAtCursor) => { + self.context.notebook.state = context::notebook::ContextState::EditorInsertMode; + } + NotebookTransition::EditingNormalMode(NormalModeTransition::InsertAtLineStart) => { + self.context.notebook.editor.move_cursor(CursorMove::Head); + self.context.notebook.state = context::notebook::ContextState::EditorInsertMode; + } + NotebookTransition::EditingNormalMode(NormalModeTransition::InsertAfterCursor) => { + self.context + .notebook + .editor + .move_cursor(CursorMove::Forward); + self.context.notebook.state = context::notebook::ContextState::EditorInsertMode; + } + NotebookTransition::EditingNormalMode(NormalModeTransition::InsertAtLineEnd) => { + self.context.notebook.editor.move_cursor(CursorMove::End); + self.context.notebook.state = context::notebook::ContextState::EditorInsertMode; + } NotebookTransition::Alert(message) => { log!("[Alert] {message}"); self.context.alert = Some(message);