1#![doc = include_str!("../readme.md")]
2
3use crate::op::{md_backtab, md_line_break, md_make_header, md_strong, md_surround, md_tab};
4use rat_event::{HandleEvent, Regular, ct_event, flow};
5use rat_focus::HasFocus;
6use rat_text::event::TextOutcome;
7use rat_text::text_area::TextAreaState;
8use ratatui_crossterm::crossterm::event::Event;
9
10pub mod dump;
11mod format;
12mod operations;
13pub mod parser;
14pub mod styles;
15mod util;
16
17pub mod op {
18 pub use crate::format::{md_format, reformat};
19 pub use crate::operations::{
20 md_backtab, md_line_break, md_make_header, md_strong, md_surround, md_tab,
21 };
22}
23
24#[derive(Debug)]
26pub struct MarkDown {
27 text_width: u16,
28}
29
30impl Default for MarkDown {
31 fn default() -> Self {
32 Self { text_width: 65 }
33 }
34}
35
36impl MarkDown {
37 pub fn new(text_width: u16) -> Self {
38 Self { text_width }
39 }
40
41 pub fn text_width(mut self, width: u16) -> Self {
42 self.text_width = width;
43 self
44 }
45}
46
47impl HandleEvent<Event, MarkDown, TextOutcome> for TextAreaState {
48 fn handle(&mut self, event: &Event, _qualifier: MarkDown) -> TextOutcome {
49 if self.is_focused() {
50 flow!(match event {
51 ct_event!(key press ALT-'1') => md_make_header(self, 1),
52 ct_event!(key press ALT-'2') => md_make_header(self, 2),
53 ct_event!(key press ALT-'3') => md_make_header(self, 3),
54 ct_event!(key press ALT-'4') => md_make_header(self, 4),
55 ct_event!(key press ALT-'5') => md_make_header(self, 5),
56 ct_event!(key press ALT-'6') => md_make_header(self, 6),
57
58 ct_event!(key press ANY-'*') => md_strong(self, '*'),
59 ct_event!(key press ANY-'_') => md_strong(self, '_'),
60 ct_event!(key press ANY-'~') => md_strong(self, '~'),
61 ct_event!(key press ALT-'c') => md_surround(self, "```\n", None, "\n```", Some(0)),
62 ct_event!(key press ALT-'i') => md_surround(self, "![", None, "]()", Some(2)),
63 ct_event!(key press ALT-'l') => md_surround(self, "[", None, "]()", Some(2)),
64 ct_event!(key press ALT-'k') => md_surround(self, "[", None, "][]", Some(2)),
65 ct_event!(key press ALT-'r') => md_surround(self, "[", Some(1), "]: ", None),
66 ct_event!(key press ALT-'f') => md_surround(self, "[^1]", Some(4), "", None),
67
68 ct_event!(keycode press Enter) => md_line_break(self),
69 ct_event!(keycode press Tab) => md_tab(self),
70 ct_event!(keycode press SHIFT-BackTab) => md_backtab(self),
71 _ => TextOutcome::Continue,
72 });
73 }
74
75 self.handle(event, Regular)
76 }
77}