1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
use iced::widget::{operation, row, scrollable, text_editor};
use iced::{Element, Fill, Font, Theme, highlighter};
use iced_selection::markdown;
fn main() -> iced::Result {
iced::application(State::new, State::update, State::view)
.theme(Theme::CatppuccinMocha)
.run()
}
#[derive(Default)]
struct State {
content: markdown::Content,
raw: text_editor::Content,
}
#[derive(Debug, Clone)]
enum Message {
Edit(text_editor::Action),
LinkClicked(markdown::Uri),
}
impl State {
fn new() -> (Self, iced::Task<Message>) {
const INITIAL_CONTENT: &str = include_str!("../overview.md");
(
Self {
content: markdown::Content::parse(INITIAL_CONTENT),
raw: text_editor::Content::with_text(INITIAL_CONTENT),
},
operation::focus_next(),
)
}
fn update(&mut self, message: Message) {
match message {
Message::Edit(action) => {
let is_edit = action.is_edit();
self.raw.perform(action);
if is_edit {
self.content = markdown::Content::parse(&self.raw.text());
}
}
Message::LinkClicked(link) => {
let _ = open::that_in_background(link);
}
}
}
fn view(&self) -> Element<'_, Message> {
let editor = text_editor(&self.raw)
.placeholder("Type your Markdown here...")
.on_action(Message::Edit)
.height(Fill)
.padding(10)
.font(Font::MONOSPACE)
.highlight("markdown", highlighter::Theme::Base16Ocean);
let preview =
markdown::view(self.content.items(), &Theme::CatppuccinMocha)
.map(Message::LinkClicked);
row![
editor,
scrollable(preview).spacing(10).width(Fill).height(Fill)
]
.spacing(10)
.padding(10)
.into()
}
}
|