use ratatui::{
layout::{Alignment, Constraint, Direction, Layout},
style::Style,
widgets::{Block, Borders, Paragraph},
};
use ratatui_textarea::TextArea;
#[derive(Clone, Copy, PartialEq)]
pub enum EditorMode {
Create,
Edit,
}
pub fn render_editor(
frame: &mut ratatui::Frame,
mode: EditorMode,
textarea: &TextArea<'_>,
footer_text: &str,
) {
let [header, body, footer] = {
let chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(3),
Constraint::Fill(1),
Constraint::Length(1),
])
.split(frame.area());
[chunks[0], chunks[1], chunks[2]]
};
let title = match mode {
EditorMode::Create => "New Todo",
EditorMode::Edit => "Edit Note",
};
let header_text = Paragraph::new(title).block(
Block::default()
.title(format!(" {} ", title))
.title_alignment(Alignment::Left)
.borders(Borders::ALL)
.border_style(Style::new().fg(ratatui::style::Color::White)),
);
frame.render_widget(header_text, header);
let editor_block = Block::default()
.borders(Borders::ALL)
.border_style(Style::new().fg(ratatui::style::Color::White));
let mut styled_textarea = textarea.clone();
styled_textarea.set_block(editor_block);
frame.render_widget(&styled_textarea, body);
frame.render_widget(
Paragraph::new(footer_text).alignment(Alignment::Left),
footer,
);
}