Skip to content

Commit

Permalink
Implement note & directory move functionality (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
panarch authored Jan 6, 2025
1 parent 4c6e210 commit fdb275c
Show file tree
Hide file tree
Showing 14 changed files with 388 additions and 110 deletions.
5 changes: 3 additions & 2 deletions core/src/db/directory.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,9 @@ impl Db {
) -> Result<()> {
table("Directory")
.update()
.filter(col("directory_id").eq(uuid(directory_id)))
.set("parent_id", parent_id)
.filter(col("id").eq(uuid(directory_id)))
.set("parent_id", uuid(parent_id))
.set("updated_at", now())
.execute(&mut self.storage)
.await?;

Expand Down
9 changes: 5 additions & 4 deletions core/src/db/note.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,14 +101,15 @@ impl Db {
self.sync()
}

pub async fn move_note(&mut self, note_id: NoteId, directory_id: DirectoryId) {
pub async fn move_note(&mut self, note_id: NoteId, directory_id: DirectoryId) -> Result<()> {
table("Note")
.update()
.filter(col("id").eq(uuid(note_id)))
.set("directory_id", directory_id)
.set("directory_id", uuid(directory_id))
.set("updated_at", now())
.execute(&mut self.storage)
.await
.unwrap();
.await?;

self.sync()
}
}
4 changes: 4 additions & 0 deletions core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ pub enum NotebookEvent {
AddNote(String),
AddDirectory(String),

MoveNote(DirectoryId),
MoveDirectory(DirectoryId),

OpenNote,
EditNote,
ViewNote,
Expand Down Expand Up @@ -111,6 +114,7 @@ pub enum KeyEvent {
Right,
Up,
Down,
Space,
Enter,
Tab,
Tilde,
Expand Down
36 changes: 31 additions & 5 deletions core/src/state/notebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use {
crate::{
data::{Directory, Note},
state::GetInner,
types::DirectoryId,
types::{DirectoryId, Id},
Error, Event, Glues, NotebookTransition, Result,
},
consume::{directory, note, tabs},
Expand Down Expand Up @@ -95,6 +95,15 @@ impl NotebookState {
NoteTreeNumber(n) => {
format!("Steps: '{n}' selected")
}
MoveMode => match &self.selected {
SelectedItem::Note(Note { name, .. }) => {
format!("Note move mode: '{name}'")
}
SelectedItem::Directory(Directory { name, .. }) => {
format!("Directory move mode: '{name}'")
}
_ => "Move mode".to_owned(),
},
EditingNormalMode(VimNormalState::Idle) => {
let name = &self.get_selected_note()?.name;

Expand Down Expand Up @@ -242,10 +251,11 @@ impl NotebookState {
match &self.inner_state {
NoteSelected => {
let mut shortcuts = vec![
"[l] Open note".to_owned(),
"[l] Open".to_owned(),
"[h] Close parent".to_owned(),
"[j|k] Down | Up".to_owned(),
"[1-9] Set steps".to_owned(),
"[1-9] Steps".to_owned(),
"[Space] Move".to_owned(),
"[m] More actions".to_owned(),
];

Expand All @@ -261,9 +271,9 @@ impl NotebookState {
"[l] Toggle".to_owned(),
"[h] Close parent".to_owned(),
"[j|k] Down | Up".to_owned(),
"[1-9] Set steps".to_owned(),
"[1-9] Steps".to_owned(),
"[Space] Move".to_owned(),
"[m] More actions".to_owned(),
"[Esc] Quit".to_owned(),
];

if !self.tabs.is_empty() {
Expand All @@ -281,6 +291,14 @@ impl NotebookState {
"[Esc] Cancel".to_owned(),
]
}
MoveMode => {
vec![
"[j] Down".to_owned(),
"[k] Up".to_owned(),
"[Enter] Move".to_owned(),
"[Esc] Cancel".to_owned(),
]
}
EditingNormalMode(VimNormalState::Idle) => {
/*
h j k l w e b [1-9] o O 0 $
Expand Down Expand Up @@ -464,6 +482,14 @@ impl NotebookState {
}
}

pub fn get_selected_id(&self) -> Result<&Id> {
match &self.selected {
SelectedItem::Note(ref note) => Ok(&note.id),
SelectedItem::Directory(ref directory) => Ok(&directory.id),
_ => Err(Error::Wip("selected item not found".to_owned())),
}
}

pub fn get_editing(&self) -> Result<&Note> {
let i = self
.tab_index
Expand Down
3 changes: 3 additions & 0 deletions core/src/state/notebook/inner_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ mod directory_selected;
mod editing_insert_mode;
mod editing_normal_mode;
mod editing_visual_mode;
mod move_mode;
mod note_more_actions;
mod note_selected;
mod note_tree_number;
Expand All @@ -22,6 +23,7 @@ pub enum InnerState {
DirectorySelected,
DirectoryMoreActions,
NoteTreeNumber(usize),
MoveMode,
EditingNormalMode(VimNormalState),
EditingVisualMode(VimVisualState),
EditingInsertMode,
Expand All @@ -44,6 +46,7 @@ pub async fn consume(
NoteMoreActions => note_more_actions::consume(db, state, event).await,
DirectoryMoreActions => directory_more_actions::consume(db, state, event).await,
NoteTreeNumber(n) => note_tree_number::consume(db, state, *n, event).await,
MoveMode => move_mode::consume(db, state, event).await,
EditingNormalMode(vim_state) => {
editing_normal_mode::consume(db, state, *vim_state, event).await
}
Expand Down
6 changes: 6 additions & 0 deletions core/src/state/notebook/inner_state/directory_selected.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
db::Db,
state::notebook::{directory, note, tabs, InnerState, NotebookState},
transition::MoveModeTransition,
Error, Event, KeyEvent, NotebookEvent, NotebookTransition, Result,
};

Expand Down Expand Up @@ -58,6 +59,11 @@ pub async fn consume(

directory::show_actions_dialog(state, directory)
}
Key(KeyEvent::Space) => {
state.inner_state = InnerState::MoveMode;

Ok(NotebookTransition::MoveMode(MoveModeTransition::Enter))
}
Notebook(SelectNote(note)) => note::select(state, note),
Notebook(SelectDirectory(directory)) => directory::select(state, directory),
Key(KeyEvent::Num(n)) => {
Expand Down
68 changes: 68 additions & 0 deletions core/src/state/notebook/inner_state/move_mode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
use crate::{
db::Db,
state::notebook::{directory, InnerState, NotebookState, SelectedItem},
transition::MoveModeTransition,
Error, Event, KeyEvent, NotebookEvent, NotebookTransition, Result,
};

pub async fn consume(
db: &mut Db,
state: &mut NotebookState,
event: Event,
) -> Result<NotebookTransition> {
use Event::*;

match event {
Key(KeyEvent::J | KeyEvent::Down) => MoveModeTransition::SelectNext.into(),
Key(KeyEvent::K | KeyEvent::Up) => MoveModeTransition::SelectPrev.into(),
Key(KeyEvent::Esc) => {
match state.selected {
SelectedItem::Directory(_) => {
state.inner_state = InnerState::DirectorySelected;
}
SelectedItem::Note(_) => {
state.inner_state = InnerState::NoteSelected;
}
SelectedItem::None => {}
};

MoveModeTransition::Cancel.into()
}
Key(KeyEvent::Enter) => MoveModeTransition::RequestCommit.into(),
Notebook(NotebookEvent::MoveNote(directory_id)) => {
let note = state.get_selected_note()?.clone();

db.move_note(note.id.clone(), directory_id.clone()).await?;
directory::close(state, state.root.directory.clone())?;
directory::open_all(db, state, directory_id).await?;

state.selected = SelectedItem::Note(note);
state.inner_state = InnerState::NoteSelected;
MoveModeTransition::Commit.into()
}
Notebook(NotebookEvent::MoveDirectory(target_directory_id)) => {
let directory = state.get_selected_directory()?.clone();
if directory.id == target_directory_id {
state.inner_state = InnerState::DirectorySelected;
return MoveModeTransition::Cancel.into();
}

db.move_directory(directory.id.clone(), target_directory_id.clone())
.await?;
directory::close(state, state.root.directory.clone())?;
directory::open_all(db, state, target_directory_id).await?;

state.selected = SelectedItem::Directory(directory);
state.inner_state = InnerState::DirectorySelected;
MoveModeTransition::Commit.into()
}
event @ Key(_) => Ok(NotebookTransition::Inedible(event)),
_ => Err(Error::Wip("todo: Notebook::consume".to_owned())),
}
}

impl From<MoveModeTransition> for Result<NotebookTransition> {
fn from(transition: MoveModeTransition) -> Self {
Ok(NotebookTransition::MoveMode(transition))
}
}
6 changes: 6 additions & 0 deletions core/src/state/notebook/inner_state/note_selected.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::{
db::Db,
state::notebook::{directory, note, tabs, InnerState, NotebookState},
transition::MoveModeTransition,
Error, Event, KeyEvent, NotebookEvent, NotebookTransition, Result,
};

Expand Down Expand Up @@ -42,6 +43,11 @@ pub async fn consume(

note::show_actions_dialog(state, note)
}
Key(KeyEvent::Space) => {
state.inner_state = InnerState::MoveMode;

Ok(NotebookTransition::MoveMode(MoveModeTransition::Enter))
}
Notebook(SelectNote(note)) => note::select(state, note),
Notebook(SelectDirectory(directory)) => directory::select(state, directory),
Key(KeyEvent::L | KeyEvent::Enter) | Notebook(OpenNote) => {
Expand Down
11 changes: 11 additions & 0 deletions core/src/transition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub enum NotebookTransition {
ShowNoteActionsDialog(Note),
ShowDirectoryActionsDialog(Directory),

MoveMode(MoveModeTransition),

OpenNote {
note: Note,
content: String,
Expand All @@ -81,6 +83,15 @@ pub enum NotebookTransition {
ShowVimKeymap(VimKeymapKind),
}

pub enum MoveModeTransition {
Enter,
SelectNext,
SelectPrev,
RequestCommit,
Commit,
Cancel,
}

#[derive(Clone, Copy, Display)]
pub enum VimKeymapKind {
NormalIdle,
Expand Down
1 change: 1 addition & 0 deletions tui/src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ fn to_event(input: Input) -> Option<KeyEvent> {
KeyCode::Char('~') => KeyEvent::Tilde,
KeyCode::Char('.') => KeyEvent::Dot,
KeyCode::Char('-') => KeyEvent::Dash,
KeyCode::Char(' ') => KeyEvent::Space,
KeyCode::Left => KeyEvent::Left,
KeyCode::Right => KeyEvent::Right,
KeyCode::Up => KeyEvent::Up,
Expand Down
Loading

0 comments on commit fdb275c

Please sign in to comment.