Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement breadcrumb support in tui editor #97

Merged
merged 2 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions tui/src/context/notebook.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ pub struct EditorTab {
pub note: Note,
pub editor: TextArea<'static>,
pub dirty: bool,
pub breadcrumb: Vec<String>,
}

impl Default for NotebookContext {
Expand Down Expand Up @@ -257,6 +258,43 @@ impl NotebookContext {
.log_expect("[NotebookContext::selected] selected must not be empty")
}

pub fn breadcrumb(&self, note: &Note) -> Vec<String> {
let mut breadcrumb = vec![note.name.clone()];
let (i, mut depth) = self
.tree_items
.iter()
.enumerate()
.find_map(|(i, item)| (item.id() == &note.id).then_some((i, item.depth)))
.log_expect("[NotebookContext::open_note] note not found");

self.tree_items[0..i].iter().rev().for_each(|item| {
if item.depth < depth {
depth = item.depth;

breadcrumb.push(item.name().clone());
}
});
breadcrumb.reverse();
breadcrumb
}

pub fn refresh_breadcrumbs(&mut self) {
let mut breadcrumbs = self
.tabs
.iter()
.map(|tab| self.breadcrumb(&tab.note))
.collect::<Vec<_>>();

breadcrumbs
.iter_mut()
.enumerate()
.for_each(|(i, breadcrumb)| {
if let Some(tab) = self.tabs.get_mut(i) {
tab.breadcrumb = breadcrumb.clone();
}
});
}

pub fn open_note(&mut self, note: Note, content: String) {
let i = self.tabs.iter().enumerate().find_map(|(i, tab)| {
if tab.note.id == note.id {
Expand All @@ -269,10 +307,12 @@ impl NotebookContext {
if let Some(i) = i {
self.tab_index = Some(i);
} else {
let breadcrumb = self.breadcrumb(&note);
let tab = EditorTab {
note,
editor: TextArea::from(content.lines()),
dirty: false,
breadcrumb,
};
self.tabs.push(tab);
self.tab_index = Some(self.tabs.len() - 1);
Expand Down
12 changes: 11 additions & 1 deletion tui/src/transitions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ impl App {
}
NotebookTransition::RenameDirectory(_) => {
self.context.notebook.update_items(root);
self.context.notebook.refresh_breadcrumbs();
}
NotebookTransition::RenameNote(note) => {
self.context.notebook.update_items(root);
Expand All @@ -149,6 +150,7 @@ impl App {
tab.note.name = note.name.clone();
}
});
self.context.notebook.refresh_breadcrumbs();
}
NotebookTransition::AddNote(Note {
id,
Expand Down Expand Up @@ -268,7 +270,15 @@ impl App {
let transition = self.glues.dispatch(event).await.log_unwrap();
self.handle_transition(transition).await;
}
Commit | Cancel => {
Commit => {
let state: &NotebookState = self.glues.state.get_inner().log_unwrap();
let id = state.get_selected_id().log_unwrap();

self.context.notebook.update_items(&state.root);
self.context.notebook.select_item(id);
self.context.notebook.refresh_breadcrumbs();
}
Cancel => {
let state: &NotebookState = self.glues.state.get_inner().log_unwrap();
let id = state.get_selected_id().log_unwrap();

Expand Down
15 changes: 11 additions & 4 deletions tui/src/views/body/notebook/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use {
pub fn draw(frame: &mut Frame, area: Rect, context: &mut Context) {
context.notebook.editor_height = area.height - 2;

let title = if let Some(tab_index) = context.notebook.tab_index {
let (title, breadcrumb) = if let Some(tab_index) = context.notebook.tab_index {
let mut title = vec!["[".into()];
for (i, tab) in context.notebook.tabs.iter().enumerate() {
let name = tab.note.name.clone();
Expand All @@ -35,12 +35,19 @@ pub fn draw(frame: &mut Frame, area: Rect, context: &mut Context) {
}
title.push("]".into());

Line::from(title)
let title = Line::from(title);
let breadcrumb = Line::from(format!(
" {} ",
context.notebook.tabs[tab_index].breadcrumb.join("/")
))
.black()
.on_green();
(title, breadcrumb)
} else {
Line::from("[Editor]".dark_gray())
(Line::from("[Editor]".dark_gray()), Line::default())
};

let block = Block::bordered().title(title);
let block = Block::bordered().title(title).title_bottom(breadcrumb);
let block = match (
context.last_log.as_ref(),
context.notebook.tabs.iter().any(|tab| tab.dirty),
Expand Down
Loading