Skip to content

Commit 124199c

Browse files
author
bikathi
committed
FIx duplicated file changes
1 parent 44b98e9 commit 124199c

File tree

1 file changed

+0
-74
lines changed

1 file changed

+0
-74
lines changed

examples/typing-speed-tester/src/file_input.rs

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -71,77 +71,3 @@ impl Component for FileInput {
7171
}
7272
}
7373
}
74-
75-
use gloo::file::callbacks::FileReader;
76-
use gloo::file::File;
77-
use wasm_bindgen::JsCast;
78-
use web_sys::{Event, HtmlInputElement};
79-
use yew::prelude::*;
80-
81-
pub(crate) enum ComponentMsg {
82-
FileSelected(Event),
83-
FileLoaded(String, String), // (filename, content)
84-
}
85-
86-
pub(crate) struct FileInput {
87-
reader: Option<FileReader>,
88-
}
89-
90-
impl Component for FileInput {
91-
type Message = ComponentMsg;
92-
type Properties = crate::app::FileInputProps;
93-
94-
fn create(_ctx: &Context<Self>) -> Self {
95-
Self { reader: None }
96-
}
97-
98-
fn update(&mut self, ctx: &Context<Self>, msg: Self::Message) -> bool {
99-
match msg {
100-
ComponentMsg::FileSelected(e) => {
101-
let input: HtmlInputElement = e.target().unwrap().dyn_into().unwrap();
102-
103-
if let Some(file) = input.files().and_then(|files| files.get(0)) {
104-
let file = File::from(file);
105-
let filename = file.name();
106-
107-
let link = ctx.link().clone();
108-
let reader = gloo::file::callbacks::read_as_text(&file, move |res| {
109-
if let Ok(content) = res {
110-
link.send_message(ComponentMsg::FileLoaded(filename.clone(), content));
111-
}
112-
});
113-
114-
self.reader = Some(reader);
115-
}
116-
false
117-
}
118-
ComponentMsg::FileLoaded(filename, content) => {
119-
// modify the content to replace all whitespace characters (e.g tabs and newline)
120-
// with a simple space
121-
let modified_content = regex::Regex::new(r"\s+")
122-
.unwrap()
123-
.replace_all(&content, " ");
124-
ctx.props()
125-
.on_file_contents_load
126-
.emit((filename, modified_content.to_string()));
127-
false
128-
}
129-
}
130-
}
131-
132-
fn view(&self, ctx: &Context<Self>) -> Html {
133-
let onchange = ctx.link().callback(ComponentMsg::FileSelected);
134-
135-
html! {
136-
<label
137-
for={"dropzone-file"}
138-
class="flex flex-col items-center justify-center h-20 w-64 border-2 border-accent hover:border-accent/80 hover:bg-accent-content/80 border-dashed rounded-lg cursor-pointer bg-accent-content">
139-
<div class="flex flex-col items-center justify-center pt-5 pb-6">
140-
<p class="mb-2 text-sm text-gray-500"><span class="font-semibold">{"Upload Custom Text"}</span></p>
141-
<p class="text-xs text-gray-500">{"Only .txt files supported"}</p>
142-
</div>
143-
<input id="dropzone-file" type="file" class="hidden" {onchange} accept=".txt" />
144-
</label>
145-
}
146-
}
147-
}

0 commit comments

Comments
 (0)