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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
use crate::{class::Class, icon::SvgIcon};
use dioxus::prelude::*;
use dioxus_free_icons::icons::{bs_icons::*, fa_solid_icons::FaUpload};
use std::{
fs,
path::{Path, PathBuf},
};
use zino_core::{file::NamedFile, SharedString};
/// A custom file upload input.
pub fn FileUpload(props: FileUploadProps) -> Element {
let mut file_names = use_signal(Vec::new);
let has_name = props.children.is_some() || !file_names().is_empty();
rsx! {
div {
class: props.class,
class: if !props.color.is_empty() { "is-{props.color}" },
class: if !props.size.is_empty() { "is-{props.size}" },
class: if props.fullwidth { "is-fullwidth" },
class: if has_name { "has-name" },
label {
class: props.label_class.clone(),
input {
class: props.input_class,
r#type: "file",
..props.attributes,
onchange: move |event| async move {
if let Some(handler) = props.on_change.as_ref() {
if let Some(file_engine) = event.files() {
let mut files = Vec::new();
file_names.write().clear();
for file in file_engine.files() {
if let Some(bytes) = file_engine.read_file(&file).await {
let file_path = Path::new(&file);
if let Some(file_name) = file_path
.file_name()
.map(|f| f.to_string_lossy())
{
let mut file = NamedFile::new(file_name);
if let Some(file_name) = file.file_name() {
file_names.write().push(file_name.to_owned());
}
file.set_bytes(bytes);
files.push(file);
}
}
}
handler.call(files);
}
}
}
}
div {
class: "file-cta",
span {
class: "file-icon",
if props.icon.is_some() {
{ props.icon }
} else {
SvgIcon {
shape: FaUpload,
width: 16,
}
}
}
span {
class: props.label_class,
{ props.label }
}
}
if props.children.is_some() {
span {
class: "file-name",
{ props.children }
}
} else if !file_names().is_empty() {
span {
class: "file-name",
{ file_names().join(", ") }
}
}
}
}
}
}
/// The [`FileUpload`] properties struct for the configuration of the component.
#[derive(Clone, PartialEq, Props)]
pub struct FileUploadProps {
/// The class attribute for the component.
#[props(into, default = "file".into())]
pub class: Class,
/// A class to apply to the `label` element.
#[props(into, default = "file-label".into())]
pub label_class: Class,
/// A class to apply to the `label` element.
#[props(into, default = "file-input".into())]
pub input_class: Class,
/// A flag to determine whether the control is fullwidth or not.
/// The color of the button: `primary` | `link` | `info` | `success` | `warning` | `danger`.
#[props(into, default)]
pub color: SharedString,
/// The size of the button: `small` | `normal` | `medium` | `large`.
#[props(into, default)]
pub size: SharedString,
#[props(default)]
pub fullwidth: bool,
/// The label content.
#[props(into)]
pub label: SharedString,
/// An optional upload icon.
pub icon: Option<VNode>,
/// An event handler to be called when the files are selected.
pub on_change: Option<EventHandler<Vec<NamedFile>>>,
/// Spreading the props of the `input` element.
#[props(extends = input)]
attributes: Vec<Attribute>,
/// The children to render within the component.
children: Option<VNode>,
}
/// A list of files and folders in a hierarchical tree structure.
pub fn FileTree(props: FileTreeProps) -> Element {
let mut opened = use_signal(|| props.opened);
let tree_id = props.tree_id;
let icon_size = props.icon_size;
let show_file_icons = props.show_file_icons;
let on_click = props.on_click;
let current_dir = props.current_dir.as_ref()?;
let current_dir_name = current_dir.file_name().and_then(|s| s.to_str())?;
let mut folders = Vec::new();
let mut files = Vec::new();
if opened() {
let entries = fs::read_dir(current_dir).ok()?;
for entry in entries.flatten() {
if let Ok(metadata) = entry.metadata() {
let path = entry.path();
if metadata.is_dir() {
folders.push(path);
} else if metadata.is_file() {
if let Some(name) = path
.file_name()
.and_then(|name| name.to_str())
.map(|s| s.to_owned())
{
let extension = path
.extension()
.and_then(|ext| ext.to_str())
.map(|s| s.to_ascii_lowercase());
files.push((name, extension, path));
}
}
}
}
}
rsx! {
div {
key: "{tree_id}-{current_dir_name}",
class: "{props.class}",
cursor: "pointer",
div {
onclick: move |_event| {
opened.set(!opened());
},
if opened() {
SvgIcon {
shape: BsChevronDown,
width: icon_size,
}
} else {
SvgIcon {
shape: BsChevronRight,
width: icon_size,
}
}
span { "{current_dir_name}" }
}
for path in folders {
FileTree {
class: props.class.clone(),
file_class: props.file_class.clone(),
current_dir: Some(path),
tree_id: tree_id.clone(),
icon_size: icon_size,
show_file_icons: show_file_icons,
opened: false,
on_click: on_click,
}
}
for (name, extension, path) in files {
div {
class: "{props.file_class}",
onclick: move |_event| {
if let Some(handler) = on_click.as_ref() {
handler.call(path.clone());
}
},
if show_file_icons {
FileIcon {
extension: extension,
icon_size: icon_size,
}
}
span { "{name}" }
}
}
}
}
}
/// The [`FileTree`] properties struct for the configuration of the component.
#[derive(Clone, PartialEq, Props)]
pub struct FileTreeProps {
/// The class attribute for the component.
#[props(into, default = "file-tree".into())]
pub class: Class,
/// The class attribute for files in the current directory.
#[props(into, default = "file-node".into())]
pub file_class: Class,
/// The current directory.
pub current_dir: Option<PathBuf>,
/// An identifer of the tree.
pub tree_id: String,
/// The size of the icon.
#[props(default = 16)]
pub icon_size: u32,
/// A flag to indicate whether the file icons are shown.
#[props(default)]
pub show_file_icons: bool,
/// A flag to indicate whether the directory is opened or not.
#[props(default)]
pub opened: bool,
/// An event handler to be called when a file is clicked.
pub on_click: Option<EventHandler<PathBuf>>,
}
/// An icon for different file extensions.
pub fn FileIcon(props: FileIconProps) -> Element {
let icon_size = props.icon_size;
let extension = props.extension.unwrap_or_default();
match extension.as_str() {
"md" => rsx!(SvgIcon {
shape: BsFileEarmarkText,
width: icon_size,
}),
"ipynb" | "wino" => rsx!(SvgIcon {
shape: BsFileEarmarkRichtext,
width: icon_size,
}),
"js" | "py" | "sql" => rsx!(SvgIcon {
shape: BsFileEarmarkCode,
width: icon_size,
}),
"xls" | "xlsx" => rsx!(SvgIcon {
shape: BsFileExcel,
width: icon_size,
}),
"avif" | "ico" | "gif" | "jpg" | "jpeg" | "png" | "svg" | "tiff" | "webp" => {
rsx!(SvgIcon {
shape: BsFileImage,
width: icon_size,
})
}
"aac" | "flac" | "m4a" | "mp3" | "oga" | "opus" | "wav" => rsx!(SvgIcon {
shape: BsFileMusic,
width: icon_size,
}),
"pdf" => rsx!(SvgIcon {
shape: BsFilePdf,
width: icon_size,
}),
"avi" | "mkv" | "mov" | "mp4" | "mpeg" | "mpg" | "ogg" | "webm" => rsx!(SvgIcon {
shape: BsFilePlay,
width: icon_size,
}),
"ppt" | "pptx" => rsx!(SvgIcon {
shape: BsFilePpt,
width: icon_size,
}),
"doc" | "docx" => rsx!(SvgIcon {
shape: BsFileWord,
width: icon_size,
}),
"7z" | "gz" | "rar" | "tar" | "zip" => rsx!(SvgIcon {
shape: BsFileZip,
width: icon_size,
}),
_ => rsx!(SvgIcon {
shape: BsFileText,
width: icon_size,
}),
}
}
/// The [`FileIcon`] properties struct for the configuration of the component.
#[derive(Clone, PartialEq, Props)]
pub struct FileIconProps {
/// The file extension.
pub extension: Option<String>,
/// The size of the icon.
#[props(default = 16)]
pub icon_size: u32,
}