Skip to content

Commit

Permalink
feat: allow hiding morphology info
Browse files Browse the repository at this point in the history
  • Loading branch information
BrewingWeasel committed Nov 16, 2023
1 parent 4d6de1c commit 58f5cd8
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 11 deletions.
2 changes: 1 addition & 1 deletion shared/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub enum Dictionary {
File(String, DictFileType),
Url(String),
Command(String),
Wiktionary(String),
Wiktionary(String, bool),
}

#[derive(Serialize, Deserialize, PartialEq, Eq, Clone, Debug)]
Expand Down
14 changes: 12 additions & 2 deletions src-tauri/src/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ fn to_title(language: &str) -> String {
}
}

async fn get_def_wiktionary(lemma: &str, language: &str) -> Result<String, Box<dyn Error>> {
async fn get_def_wiktionary(
lemma: &str,
language: &str,
ignore_morph: bool,
) -> Result<String, Box<dyn Error>> {
let language = to_title(language);
let text = reqwest::get(format!("https://wiktionary.org/wiki/{lemma}"))
.await?
Expand All @@ -85,6 +89,10 @@ async fn get_def_wiktionary(lemma: &str, language: &str) -> Result<String, Box<d
.next()
.is_none()
{
if cur_node.is(Class("NavFrame")) && ignore_morph {
node = cur_node;
continue;
}
def.push_str(&cur_node.html());
}
node = cur_node;
Expand Down Expand Up @@ -117,7 +125,9 @@ async fn get_def(dict: &Dictionary, lemma: &str) -> Result<String, Box<dyn Error
Dictionary::File(f, dict_type) => get_def_from_file(lemma, f, dict_type),
Dictionary::Url(url) => get_def_url(lemma, url).await,
Dictionary::Command(cmd) => get_def_command(lemma, cmd).await,
Dictionary::Wiktionary(lang) => get_def_wiktionary(lemma, lang).await,
Dictionary::Wiktionary(lang, ignore_morph) => {
get_def_wiktionary(lemma, lang, *ignore_morph).await
}
}
}

Expand Down
45 changes: 37 additions & 8 deletions src-ui/src/settings/dictionary_settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ fn DictionaryRepresentation(
}
}
"wiktionary" => {
if !matches!(rdict(), Dictionary::Wiktionary(_)) {
wdict(Dictionary::Wiktionary(String::new()));
if !matches!(rdict(), Dictionary::Wiktionary(_, _)) {
wdict(Dictionary::Wiktionary(String::new(), false));
}
}
_ => unreachable!(),
Expand All @@ -93,7 +93,7 @@ fn DictionaryRepresentation(
<option value="command" selected=matches!(rdict(), Dictionary::Command(_))>
From command
</option>
<option value="wiktionary" selected=matches!(rdict(), Dictionary::Wiktionary(_))>
<option value="wiktionary" selected=matches!(rdict(), Dictionary::Wiktionary(_, _))>
From wiktionary
</option>
</select>
Expand Down Expand Up @@ -143,23 +143,28 @@ fn DictionaryRepresentation(
}
.into_view()
}
Dictionary::Wiktionary(url) => {
let (read_sig, write_sig) = create_signal(url);
Dictionary::Wiktionary(url, _) => {
let (read_url, write_url) = create_signal(url);
view! {
<div class="labeledinput">
<label for="wiktionary">Language</label>
<input
id="wiktionary"
type="text"
on:input=move |ev| {
write_sig(event_target_value(&ev));
write_url(event_target_value(&ev));
}

on:change=move |_| {
wdict.update(|v| { *v = Dictionary::Wiktionary(read_sig()) })
wdict
.update(|v| {
if let Dictionary::Wiktionary(_, hide_morph) = v {
*v = Dictionary::Wiktionary(read_url(), *hide_morph);
}
})
}

prop:value=read_sig
prop:value=read_url
/>
</div>
}
Expand All @@ -178,6 +183,30 @@ fn DictionaryRepresentation(
}
}

// <div class="labeledcheckbox">
// <label for="hidemorph">Hide morphology</label>
// <input
// id="hidemorph"
// type="checkbox"
// on:change=move |ev| {
// // write_showmorph(event_target_checked(&ev));
//
// // FIX: writing breaks it
// if let Dictionary::Wiktionary(lang, _) = rdict() {
// console_log("got here");
// wdict(Dictionary::Wiktionary(lang, event_target_checked(&ev)));
// console_log("anndd got here");
// } else {
// console_log("didnt got here");
// }
// // write_showmorph(event_target_checked(&ev));
// // wdict.update(move |v| if let Dictionary::Wiktionary(lang, _) = v { *v = Dictionary::Wiktionary(lang.to_owned(), read_showmorph()) });
// }
//
// prop:value=move || if let Dictionary::Wiktionary(_, hide_morph) = rdict() { hide_morph } else { false }
// />
// </div>

#[component]
fn file_dictionary_representation(
filename: String,
Expand Down

0 comments on commit 58f5cd8

Please sign in to comment.