Skip to content

Commit

Permalink
Rename example
Browse files Browse the repository at this point in the history
  • Loading branch information
flosse committed Dec 9, 2024
1 parent d253ff3 commit 294e733
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 49 deletions.
26 changes: 13 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ members = [
"xilem_web/web_examples/spawn_tasks",
"xilem_web/web_examples/svgtoy",
"xilem_web/web_examples/svgdraw",
"xilem_web/web_examples/up_and_download",
"xilem_web/web_examples/open_and_save_file",
"tree_arena",
]

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "up_and_download"
name = "open_and_save_file"
version = "0.1.0"
publish = false
license.workspace = true
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright 2023 the Xilem Authors
// Copyright 2024 the Xilem Authors
// SPDX-License-Identifier: Apache-2.0

//! This example demonstrates how to download or upload a text file
//! This example demonstrates how to open or save a text file
//! within a client side rendered web application without a server.
use std::{cell::RefCell, rc::Rc};
Expand All @@ -15,88 +15,88 @@ use xilem_web::{

struct AppState {
text: String,
upload_file: Option<File>,
start_upload: bool,
file_to_open: Option<File>,
start_opening: bool,
raw_file_input_el: Rc<RefCell<Option<web_sys::HtmlInputElement>>>,
raw_download_link: Rc<RefCell<Option<web_sys::HtmlAnchorElement>>>,
raw_save_link: Rc<RefCell<Option<web_sys::HtmlAnchorElement>>>,
}

impl Default for AppState {
fn default() -> Self {
AppState {
text: "Hello from Xilem Web :)".to_string(),
upload_file: None,
start_upload: false,
file_to_open: None,
start_opening: false,
raw_file_input_el: Rc::new(RefCell::new(None)),
raw_download_link: Rc::new(RefCell::new(None)),
raw_save_link: Rc::new(RefCell::new(None)),
}
}
}

fn app_logic(app_state: &mut AppState) -> impl Element<AppState> {
let upload_action = app_state
.start_upload
let open_action = app_state
.start_opening
.then(|| {
app_state.upload_file.take().map(|file| {
app_state.file_to_open.take().map(|file| {
reset_file_input(app_state);
app_state.start_upload = false;
app_state.start_opening = false;
memoized_await(
file,
|file| gloo_file::futures::read_as_text(file),
handle_upload_result,
handle_open_result,
)
})
})
.flatten();

html::div((
html::h1("Up- and download example"),
html::h1("Open and save file example"),
html::textarea(app_state.text.clone()),
html::h2("Download"),
html::button("download text").on_click(|state: &mut AppState, _| {
let el_ref = state.raw_download_link.borrow_mut();
html::h2("Save"),
html::button("save text").on_click(|state: &mut AppState, _| {
let el_ref = state.raw_save_link.borrow_mut();
let blob = Blob::new(&*state.text);
let url = ObjectUrl::from(blob);
let el = el_ref.as_ref().unwrap();
el.set_href(&url);
el.click();
}),
hidden_download_link(app_state),
html::h2("Upload"),
hidden_save_link(app_state),
html::h2("Open"),
html::div((
upload_file_input(app_state),
open_file_input(app_state),
html::button("x").on_click(|state: &mut AppState, _| {
reset_file_input(state);
}),
)),
fork(
html::button("upload").on_click(|state: &mut AppState, _| {
state.start_upload = true;
html::button("open").on_click(|state: &mut AppState, _| {
state.start_opening = true;
}),
upload_action,
open_action,
),
))
}

fn reset_file_input(state: &mut AppState) {
state.upload_file = None;
state.file_to_open = None;
if let Some(el) = &*state.raw_file_input_el.borrow_mut() {
el.set_value("");
}
}

fn handle_upload_result(state: &mut AppState, result: Result<String, FileReadError>) {
fn handle_open_result(state: &mut AppState, result: Result<String, FileReadError>) {
match result {
Ok(txt) => {
state.text = txt;
}
Err(err) => {
log::error!("Unable to upload file: {err}");
log::error!("Unable to open file: {err}");
}
}
}

fn upload_file_input(app_state: &mut AppState) -> impl Element<AppState> {
fn open_file_input(app_state: &mut AppState) -> impl Element<AppState> {
html::input(())
.attr("type", "file")
.attr("accept", "text/plain")
Expand All @@ -119,26 +119,26 @@ fn upload_file_input(app_state: &mut AppState) -> impl Element<AppState> {
.unwrap()
.unchecked_into::<web_sys::HtmlInputElement>();
let Some(files) = input.files() else {
state.upload_file = None;
state.file_to_open = None;
return;
};
state.upload_file = files.get(0).map(File::from);
state.file_to_open = files.get(0).map(File::from);
})
}

fn hidden_download_link(state: &mut AppState) -> impl Element<AppState> {
html::a("Download example text")
fn hidden_save_link(state: &mut AppState) -> impl Element<AppState> {
html::a("Save example text")
.style(style("display", "none"))
.attr("download", "example.txt")
.attr("save", "example.txt")
.after_build({
let el_ref = Rc::clone(&state.raw_download_link);
let el_ref = Rc::clone(&state.raw_save_link);
move |el| {
*el_ref.borrow_mut() =
Some(el.dyn_ref::<web_sys::HtmlAnchorElement>().unwrap().clone());
}
})
.before_teardown({
let el_ref = Rc::clone(&state.raw_download_link);
let el_ref = Rc::clone(&state.raw_save_link);
move |_| {
*el_ref.borrow_mut() = None;
}
Expand Down

0 comments on commit 294e733

Please sign in to comment.