Skip to content

Commit

Permalink
feat: allow embedding pages
Browse files Browse the repository at this point in the history
  • Loading branch information
BrewingWeasel committed Aug 4, 2024
1 parent 7f8823a commit ae39bb4
Show file tree
Hide file tree
Showing 4 changed files with 38 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 @@ -83,7 +83,7 @@ pub struct Dictionary {
#[serde(tag = "t", content = "c")]
pub enum DictionarySpecificSettings {
File(String, DictFileType),
Url(String),
Url(String, bool),
Command(String),
EkalbaBendrines,
EkalbaDabartines,
Expand Down
19 changes: 13 additions & 6 deletions src-tauri/src/dictionary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,19 @@ fn get_def_from_file(
}
}

async fn get_def_url(lemma: &str, url: &str) -> Result<Definition, KalbaError> {
async fn get_def_url(lemma: &str, url: &str, embed: bool) -> Result<Definition, KalbaError> {
let new_url = url.replacen("{word}", lemma, 1);
let client = reqwest::Client::new();
Ok(Definition::Text(
client.get(new_url).send().await?.text().await?,
))
if embed {
Ok(Definition::Text(format!(
"<iframe class=\"w-full\" src=\"{}\"></iframe>",
new_url
)))
} else {
let client = reqwest::Client::new();
Ok(Definition::Text(
client.get(new_url).send().await?.text().await?,
))
}
}

async fn get_def_command(lemma: &str, cmd: &str) -> Result<Definition, KalbaError> {
Expand Down Expand Up @@ -224,7 +231,7 @@ async fn get_def(
) -> Result<Definition, KalbaError> {
match dict {
DictionarySpecificSettings::File(f, dict_type) => get_def_from_file(lemma, f, dict_type),
DictionarySpecificSettings::Url(url) => get_def_url(lemma, url).await,
DictionarySpecificSettings::Url(url, embed) => get_def_url(lemma, url, *embed).await,
DictionarySpecificSettings::Command(cmd) => get_def_command(lemma, cmd).await,
DictionarySpecificSettings::EkalbaBendrines => {
get_ekalba_bendrines(dict_info, lemma, definition_styling).await
Expand Down
21 changes: 18 additions & 3 deletions src/pages/settings/components/IndividualDict.vue
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {
type FileType,
} from "@/types";
import { watch } from "vue";
import { Switch } from "@/components/ui/switch";
import { Label } from "@/components/ui/label";
const dict = defineModel<DictionarySpecificSettings>({ required: true });
Expand Down Expand Up @@ -39,7 +41,10 @@ watch(
dict.value.c = undefined;
break;
}
case DictionaryType.Url:
case DictionaryType.Url: {
dict.value.c = ["", true];
break;
}
case DictionaryType.Command: {
dict.value.c = "";
break;
Expand All @@ -55,6 +60,13 @@ function isWiktionary(
return dictType === "Wiktionary";
}
function isUrl(
dictType: DictionaryType,
_contents: any,
): _contents is [string, boolean] {
return dictType === "Url";
}
function isFile(
dictType: DictionaryType,
_contents: any,
Expand Down Expand Up @@ -109,8 +121,11 @@ function isFile(
<Label for="wordlang">Word Language:</Label>
<Input type="text" v-model="dict.c[1]" class="w-100" id="wordlang" />
</div>
<div v-else-if="dict.t == 'Url' && typeof dict.c === 'string'">
<div v-else-if="isUrl(dict.t, dict.c)">
<Label for="command">Url:</Label>
<Input type="text" id="command" v-model="dict.c" />
<Input type="text" id="command" v-model="dict.c[0]" />

<Label for="embed">Embed page</Label>
<Switch id="embed" v-model:checked="dict.c[1]" />
</div>
</template>
7 changes: 6 additions & 1 deletion src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,12 @@ export interface Dictionary {

export interface DictionarySpecificSettings {
t: DictionaryType;
c: [string, FileType] | [string, string] | string | undefined;
c:
| [string, FileType]
| [string, string]
| [string, boolean]
| string
| undefined;
}

export interface Deck {
Expand Down

0 comments on commit ae39bb4

Please sign in to comment.