From 90936b137717ffeff02bfbe3307682e7edeb3846 Mon Sep 17 00:00:00 2001 From: Taehoon Moon Date: Tue, 12 Nov 2024 19:15:35 +0900 Subject: [PATCH] Fix note tree numbering reset on j,k and other key inputs --- .../notebook/inner_state/note_tree_number.rs | 38 ++++++++++++------- tui/src/context/notebook.rs | 12 +----- 2 files changed, 26 insertions(+), 24 deletions(-) diff --git a/core/src/state/notebook/inner_state/note_tree_number.rs b/core/src/state/notebook/inner_state/note_tree_number.rs index 6cb9032..7d0c8f0 100644 --- a/core/src/state/notebook/inner_state/note_tree_number.rs +++ b/core/src/state/notebook/inner_state/note_tree_number.rs @@ -13,6 +13,18 @@ pub async fn consume( use Event::*; use NotebookEvent::*; + let reset_state = |state: &mut NotebookState| { + match state.selected { + SelectedItem::Note { .. } => { + state.inner_state = InnerState::NoteSelected; + } + SelectedItem::Directory { .. } => { + state.inner_state = InnerState::DirectorySelected; + } + SelectedItem::None => {} + }; + }; + match event { Notebook(SelectNote(note)) => note::select(state, note), Notebook(SelectDirectory(directory)) => directory::select(state, directory), @@ -23,21 +35,21 @@ pub async fn consume( Ok(NotebookTransition::None) } Key(KeyEvent::Esc) => { - match state.selected { - SelectedItem::Note { .. } => { - state.inner_state = InnerState::NoteSelected; - } - SelectedItem::Directory { .. } => { - state.inner_state = InnerState::DirectorySelected; - } - SelectedItem::None => {} - }; - + reset_state(state); Ok(NotebookTransition::None) } - Key(KeyEvent::J) => Ok(NotebookTransition::SelectNext(n)), - Key(KeyEvent::K) => Ok(NotebookTransition::SelectPrev(n)), - event @ Key(_) => Ok(NotebookTransition::Inedible(event)), + Key(KeyEvent::J) => { + reset_state(state); + Ok(NotebookTransition::SelectNext(n)) + } + Key(KeyEvent::K) => { + reset_state(state); + Ok(NotebookTransition::SelectPrev(n)) + } + event @ Key(_) => { + reset_state(state); + Ok(NotebookTransition::Inedible(event)) + } _ => Err(Error::Wip("todo: Notebook::consume".to_owned())), } } diff --git a/tui/src/context/notebook.rs b/tui/src/context/notebook.rs index 9accb2c..93c067a 100644 --- a/tui/src/context/notebook.rs +++ b/tui/src/context/notebook.rs @@ -150,7 +150,7 @@ impl NotebookContext { match self.state { ContextState::NoteTreeBrowsing => self.consume_on_note_tree_browsing(code), - ContextState::NoteTreeNumbering => self.consume_on_note_tree_numbering(code), + ContextState::NoteTreeNumbering => Action::PassThrough, ContextState::EditorNormalMode { idle } => self.consume_on_editor_normal(input, idle), ContextState::EditorVisualMode => Action::PassThrough, ContextState::EditorInsertMode => self.consume_on_editor_insert(input), @@ -236,16 +236,6 @@ impl NotebookContext { } } - fn consume_on_note_tree_numbering(&mut self, code: KeyCode) -> Action { - match code { - KeyCode::Char('0' | '1' | '2' | '3' | '4' | '5' | '6' | '7' | '8' | '9') => { - Action::PassThrough - } - KeyCode::Char('j') | KeyCode::Char('k') | KeyCode::Esc => Action::PassThrough, - _ => Action::None, - } - } - fn consume_on_editor_normal(&mut self, input: &Input, idle: bool) -> Action { let code = match input { Input::Key(key) => key.code,