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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use std::cell::RefCell;
use std::rc::Rc;
use virtual_dom_rs::prelude::*;
use crate::store::Store;
use crate::views::MdView;
#[cfg(target_arch = "wasm32")]
use crate::state::Msg;
pub struct FileView {
store: Rc<RefCell<Store>>,
}
impl FileView {
pub fn new(store: Rc<RefCell<Store>>) -> FileView {
FileView { store }
}
fn markdown(&self, file_contents: &str) -> VirtualNode {
let state = self.store.borrow();
let (other_view_type, view) = if *state.render_md() {
("Source", MdView::new(Rc::clone(&self.store)).render())
} else {
("Markdown", self.source_code(file_contents))
};
#[cfg(target_arch = "wasm32")]
let store = Rc::clone(&self.store);
html! {
<div>
<button
onclick=move |_event: web_sys::Event| {
store
.borrow_mut()
.msg(&Msg::ToggleRenderMd)
}
>
{ other_view_type } View
</button>
{ view }
</div>
}
}
fn source_code(&self, file_contents: &str) -> VirtualNode {
html! {
<pre>
<code>{ file_contents.to_string() }</code>
</pre>
}
}
}
impl View for FileView {
fn render(&self) -> VirtualNode {
let state = self.store.borrow();
let content = {
match state.file_contents() {
Some(file_contents) => {
match state.entry_path().extension() {
Some(extension) => {
if extension == "md" || extension == "mdown" || extension == "markdown"
{
self.markdown(file_contents)
} else {
self.source_code(file_contents)
}
}
None => self.source_code(file_contents),
}
}
None => {
let file_name = state.entry_path().file_name().unwrap().to_str().unwrap();
html! {
<span>
Could not find <strong>{ file_name }</strong>
</span>
}
}
}
};
html! {
<div id="content">
{ content }
</div>
}
}
}