Move quizzes fetching in App.svelte component

This commit is contained in:
Andrea Fazzi 2023-04-19 10:47:06 +02:00
parent 8fa98b6782
commit 2060123d47
5 changed files with 65 additions and 25 deletions

27
app.go
View file

@ -49,3 +49,30 @@ func (a *App) Markdown(quiz Quiz) string {
} }
return question + strings.Join(answers, "\n") return question + strings.Join(answers, "\n")
} }
func (a *App) ParseMarkdown(markdown string) Quiz {
lines := strings.Split(markdown, "\n")
question := ""
answers := []string{}
for _, line := range lines {
trimmedLine := strings.TrimSpace(line)
if trimmedLine == "" {
continue
}
if strings.HasPrefix(trimmedLine, "#") {
question = strings.TrimPrefix(trimmedLine, "#")
question = strings.TrimSpace(question)
} else if strings.HasPrefix(trimmedLine, "-") {
answer := strings.TrimPrefix(trimmedLine, "-")
answer = strings.TrimSpace(answer)
answers = append(answers, answer)
}
}
return Quiz{
Question: question,
Answers: answers,
}
}

View file

@ -1,10 +1,20 @@
<script lang="ts"> <script lang="ts">
import { onMount } from "svelte";
import { FetchQuizzes } from "../wailsjs/go/main/App";
import QuizCardList from "./QuizCardList.svelte"; import QuizCardList from "./QuizCardList.svelte";
let quizzes: main.Quiz[] = [];
onMount(async () => {
FetchQuizzes().then(
(result) => {
quizzes = result;
});
});
</script> </script>
<main> <main>
<QuizCardList /> <QuizCardList {quizzes} />
</main> </main>

View file

@ -3,45 +3,42 @@
import { markdown } from "@codemirror/lang-markdown"; import { markdown } from "@codemirror/lang-markdown";
import QuizCard from "./QuizCard.svelte"; import QuizCard from "./QuizCard.svelte";
import { main } from "../wailsjs/go/models"; import { main } from "../wailsjs/go/models";
import { onMount } from "svelte"; import { Markdown, ParseMarkdown } from "../wailsjs/go/main/App";
import { FetchQuizzes, Markdown } from "../wailsjs/go/main/App";
let quizzes: main.Quiz[] = []; export let quizzes: main.Quiz[];
onMount(async () => { let value: string;
FetchQuizzes().then( let editingIndex: number;
function editQuiz(quiz: Quiz, index: number) {
Markdown(quiz).then(
(result) => { (result) => {
console.log(result); value = result;
quizzes = result; }
}); )
});
let editingIndex;
function showEditor(index) {
editingIndex = index; editingIndex = index;
} }
function hideEditor() { function updateQuiz(index: number) {
ParseMarkdown(value).then(
(result) => {
quizzes[index] = result;
}
)
editingIndex = -1; editingIndex = -1;
} }
function updateItem(index, value) {
quizzes[index] = value;
// hideEditor();
}
</script> </script>
<div> <div>
{#each quizzes as quiz, index} {#each quizzes as quiz, index}
{#if editingIndex === index} {#if editingIndex === index}
<div class="editor"> <div class="editor">
<CodeMirror lang={ markdown() } /> <CodeMirror bind:value lang={ markdown() } />
<button on:click="{ ()=>hideEditor() }">Save</button> <button on:click="{ ()=>updateQuiz(index) }">Save</button>
</div> </div>
{:else} {:else}
<div class="item" on:click="{() => showEditor(index)}"> <div class="item" on:click="{() => editQuiz(quiz, index)}">
<QuizCard {quiz} /> <QuizCard {quiz} />
</div> </div>
{/if} {/if}

View file

@ -5,3 +5,5 @@ import {main} from '../models';
export function FetchQuizzes():Promise<Array<main.Quiz>>; export function FetchQuizzes():Promise<Array<main.Quiz>>;
export function Markdown(arg1:main.Quiz):Promise<string>; export function Markdown(arg1:main.Quiz):Promise<string>;
export function ParseMarkdown(arg1:string):Promise<main.Quiz>;

View file

@ -9,3 +9,7 @@ export function FetchQuizzes() {
export function Markdown(arg1) { export function Markdown(arg1) {
return window['go']['main']['App']['Markdown'](arg1); return window['go']['main']['App']['Markdown'](arg1);
} }
export function ParseMarkdown(arg1) {
return window['go']['main']['App']['ParseMarkdown'](arg1);
}