Skip to content

Commit

Permalink
More clickable stuff.
Browse files Browse the repository at this point in the history
  • Loading branch information
FS-Frost committed Nov 28, 2023
1 parent c7a9bb9 commit 5444d68
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 17 deletions.
29 changes: 29 additions & 0 deletions src/Dictionary.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
LANG_ES,
} from "./DictionaryClient";
import Word from "./Word.svelte";
import { cleanWord } from "./strings";
let response: ClientResponse;
let isLoading: boolean = false;
Expand All @@ -29,6 +30,9 @@
? "Ups, I hit an error. Try again."
: "Vaya, me tropecé con una piedra. Intenta de nuevo.";
$: searchingMessage = isLangEnglish ? "Searching..." : "Buscando...";
$: proTip = isLangEnglish
? "Pro tip: click a word to search it."
: "Pro tip: clic en una palabra para buscarla.";
onMount(() => {
const url = new URL(window.location.href);
Expand Down Expand Up @@ -116,6 +120,26 @@
</select>
</div>

<p>
<i>
{#each proTip.split(" ") as subword}
<span
class="searchable"
title={isLangEnglish
? `Click to search "${cleanWord(subword)}"`
: `Clic para buscar "${cleanWord(subword)}"`}
on:click={() => {
word = cleanWord(subword);
searchWord();
}}
>
{subword}
</span>
{" "}
{/each}
</i>
</p>

{#if !isLoading && response && response.json}
{#each response.json as defWord}
<Word
Expand Down Expand Up @@ -150,4 +174,9 @@
.text-center {
text-align: center;
}
.searchable:hover {
text-decoration: underline;
cursor: pointer;
}
</style>
54 changes: 37 additions & 17 deletions src/Word.svelte
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { createEventDispatcher } from "svelte";
import { LANG_EN, Word } from "./DictionaryClient";
import { cleanWord } from "./strings";
export let language: string;
export let word: Word;
Expand Down Expand Up @@ -28,22 +29,13 @@
function searchWord(subword: string): void {
dispatch("search", subword);
}
function cleanWord(subword: string): string {
const re = /(\w+)/;
const result = re.exec(subword);
if (!result || result.length == 0) {
return subword;
}
return result[0];
}
</script>

<div class="card mb-3">
<div class="card-body">
<h5 class="card-title">
{word.word}{#if word.phonetic != null}
<span>{word.word}</span>
{#if word.phonetic != null}
<i>({word.phonetic})</i>
{/if}
</h5>
Expand All @@ -56,10 +48,38 @@
{#each word.meanings as meaning}
{#if meaning.partOfSpeech != null}
<h6>
<b
>{getPartOfSpeechPrefix(meaning.partOfSpeech)}
{meaning.partOfSpeech}</b
>
<b>
{#each getPartOfSpeechPrefix(meaning.partOfSpeech).split(" ") as subword}
<span
class="searchable"
title={isLangEnglish
? `Click to search "${cleanWord(subword)}"`
: `Clic para buscar "${cleanWord(
subword
)}"`}
on:click={() => searchWord(cleanWord(subword))}
>
{subword}
</span>
{" "}
{/each}
<span
class="searchable"
title={isLangEnglish
? `Click to search "${cleanWord(
meaning.partOfSpeech
)}"`
: `Clic para buscar "${cleanWord(
meaning.partOfSpeech
)}"`}
on:click={() =>
searchWord(
cleanWord(meaning.partOfSpeech ?? "")
)}
>
{meaning.partOfSpeech}
</span>
</b>
</h6>
{/if}

Expand All @@ -68,7 +88,7 @@
<i>
{#each definition.definition.split(" ") as subword}
<span
class="word"
class="searchable"
title={isLangEnglish
? `Click to search "${cleanWord(subword)}"`
: `Clic para buscar "${cleanWord(
Expand All @@ -88,7 +108,7 @@
</div>

<style>
.word:hover {
.searchable:hover {
text-decoration: underline;
cursor: pointer;
}
Expand Down
9 changes: 9 additions & 0 deletions src/strings.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
export function cleanWord(subword: string): string {
const re = /(\w+)/;
const result = re.exec(subword);
if (!result || result.length == 0) {
return subword;
}

return result[0];
}

0 comments on commit 5444d68

Please sign in to comment.