Skip to content

Commit

Permalink
flip card button
Browse files Browse the repository at this point in the history
  • Loading branch information
TBS1996 committed Dec 28, 2024
1 parent 26c38f3 commit 7238fd6
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 14 deletions.
28 changes: 27 additions & 1 deletion 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 @@ -73,4 +73,4 @@ hyper-tls = "0.5"
petgraph = "0.6.5"
once_cell = "1.20.2"
strum = { version = "0.24", features = ["derive"] }
regex = "1.11.1"
fancy-regex = "0.14.0"
2 changes: 1 addition & 1 deletion speki-web/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ petgraph.workspace = true
once_cell.workspace = true
speki-provider = { workspace = true, features = ["dexie", "browserfs"] }
strum.workspace = true
regex.workspace = true
fancy-regex.workspace = true


speki-dto = { path = "../speki-dto" }
Expand Down
53 changes: 42 additions & 11 deletions speki-web/src/overlays/uploader.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use dioxus::prelude::*;
use dioxus_elements::FileEngine;
use regex::Regex;
use fancy_regex::Regex;
use serde::{Deserialize, Serialize};
use std::sync::Arc;
use strum::{EnumIter, IntoEnumIterator};
use tracing::info;

use crate::{
components::{CardRef, CardTy, DropDownMenu, Komponent},
Expand Down Expand Up @@ -52,19 +51,28 @@ struct QA {

impl QA {
fn extract(re: String, content: String) -> Vec<Self> {
// Compile the regex using `fancy-regex`
let Ok(re) = Regex::new(&re) else {
info!("invalid regex: {re:?}");
eprintln!("Invalid regex: {re:?}");
return vec![];
};

re.captures_iter(&content)
.map(|cap| Self {
q: cap
.get(1)
.map_or("".to_string(), |m| m.as_str().to_string()),
a: cap
.get(2)
.map_or("".to_string(), |m| m.as_str().to_string()),
// Split the content by lines and apply the regex to each line
content
.lines()
.filter_map(|line| {
// Use `re.captures` which returns `Result`
match re.captures(line) {
Ok(Some(cap)) => Some(Self {
a: cap
.get(1)
.map_or("".to_string(), |m| m.as_str().to_string()), // Capture the answer first
q: cap
.get(2)
.map_or("".to_string(), |m| m.as_str().to_string()), // Capture the question second
}),
_ => None,
}
})
.collect()
}
Expand All @@ -81,6 +89,19 @@ pub struct Uploader {
}

impl Uploader {
pub fn flip_qa(&self) {
let mut qa = self.cards.cloned();
for x in &mut qa {
let q = x.q.clone();
let a = x.a.clone();

x.q = a;
x.a = q;
}

self.cards.clone().set(qa);
}

pub fn new() -> Self {
let regex: Signal<String> =
Signal::new_in_scope(Extraction::Tabs.regex().unwrap().to_string(), ScopeId(3));
Expand Down Expand Up @@ -136,6 +157,7 @@ impl Komponent for Uploader {
let dropdown = self.dropdown.clone();
let concept = self.concept.clone();
let concept2 = self.concept.clone();
let selv = self.clone();

rsx! {
div {
Expand Down Expand Up @@ -204,6 +226,15 @@ impl Komponent for Uploader {
}

{dropdown.render()}
div {
button {
onclick: move |_| {
let selv = selv.clone();
selv.flip_qa();
},
"flip qa"
}
}
}
}

Expand Down

0 comments on commit 7238fd6

Please sign in to comment.