Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: Documents editor example #846

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
chore: Simplify fs
  • Loading branch information
marc2332 committed Sep 3, 2024
commit d211c301b4bd3baab59efc0307b2196c10349e5d
15 changes: 6 additions & 9 deletions examples/documents_editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,13 +242,13 @@ fn Home() -> Element {

#[component]
fn DocumentView(path: ReadOnlySignal<String>) -> Element {
let content = use_resource(move || async move { fs::read(&*path.read()) });
let content = use_resource(move || async move { fs::read_to_string(&*path.read()) });
let content = content.read();

if let Some(Ok(bytes)) = &*content {
if let Some(Ok(text)) = &*content {
rsx!(
label {
"{str::from_utf8(bytes).unwrap()}"
"{text}"
}
)
} else if content.is_none() {
Expand All @@ -273,12 +273,9 @@ fn DocumentEdit(path: String) -> Element {
let documents: Signal<Vec<Document>> = use_context();
let editable = use_editable(
|| {
if let Ok(file) = fs::read(&path) {
let file_content = str::from_utf8(&file).unwrap().to_string();
EditableConfig::new(file_content)
} else {
EditableConfig::new(LOREM_IPSUM.to_string())
}
EditableConfig::new(
fs::read_to_string(&path).unwrap_or_else(|_| LOREM_IPSUM.to_string()),
)
.with_allow_tabs(false)
},
EditableMode::MultipleLinesSingleEditor,
Expand Down