From 2060123d475153669cf396ba47f3a55ee1db2fca Mon Sep 17 00:00:00 2001 From: Andrea Fazzi Date: Wed, 19 Apr 2023 10:47:06 +0200 Subject: [PATCH] Move quizzes fetching in App.svelte component --- app.go | 27 ++++++++++++++++++++ frontend/src/App.svelte | 16 +++++++++--- frontend/src/QuizCardList.svelte | 41 ++++++++++++++----------------- frontend/wailsjs/go/main/App.d.ts | 2 ++ frontend/wailsjs/go/main/App.js | 4 +++ 5 files changed, 65 insertions(+), 25 deletions(-) diff --git a/app.go b/app.go index 0cc9beb..4c35cb4 100644 --- a/app.go +++ b/app.go @@ -49,3 +49,30 @@ func (a *App) Markdown(quiz Quiz) string { } 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, + } +} diff --git a/frontend/src/App.svelte b/frontend/src/App.svelte index 8c67dd0..32f7113 100644 --- a/frontend/src/App.svelte +++ b/frontend/src/App.svelte @@ -1,10 +1,20 @@
- +
diff --git a/frontend/src/QuizCardList.svelte b/frontend/src/QuizCardList.svelte index 4922e93..bfc6d5d 100644 --- a/frontend/src/QuizCardList.svelte +++ b/frontend/src/QuizCardList.svelte @@ -3,45 +3,42 @@ import { markdown } from "@codemirror/lang-markdown"; import QuizCard from "./QuizCard.svelte"; import { main } from "../wailsjs/go/models"; - import { onMount } from "svelte"; - import { FetchQuizzes, Markdown } from "../wailsjs/go/main/App"; + import { Markdown, ParseMarkdown } from "../wailsjs/go/main/App"; - let quizzes: main.Quiz[] = []; + export let quizzes: main.Quiz[]; - onMount(async () => { - FetchQuizzes().then( + let value: string; + let editingIndex: number; + + function editQuiz(quiz: Quiz, index: number) { + Markdown(quiz).then( (result) => { - console.log(result); - quizzes = result; - }); - }); - - let editingIndex; - - function showEditor(index) { + value = result; + } + ) editingIndex = index; } - function hideEditor() { + function updateQuiz(index: number) { + ParseMarkdown(value).then( + (result) => { + quizzes[index] = result; + } + ) editingIndex = -1; } - function updateItem(index, value) { - quizzes[index] = value; - // hideEditor(); - } -
{#each quizzes as quiz, index} {#if editingIndex === index}
- - + +
{:else} -
+
{/if} diff --git a/frontend/wailsjs/go/main/App.d.ts b/frontend/wailsjs/go/main/App.d.ts index 4d5cff8..e880f88 100755 --- a/frontend/wailsjs/go/main/App.d.ts +++ b/frontend/wailsjs/go/main/App.d.ts @@ -5,3 +5,5 @@ import {main} from '../models'; export function FetchQuizzes():Promise>; export function Markdown(arg1:main.Quiz):Promise; + +export function ParseMarkdown(arg1:string):Promise; diff --git a/frontend/wailsjs/go/main/App.js b/frontend/wailsjs/go/main/App.js index 6e292f2..63e26e3 100755 --- a/frontend/wailsjs/go/main/App.js +++ b/frontend/wailsjs/go/main/App.js @@ -9,3 +9,7 @@ export function FetchQuizzes() { export function Markdown(arg1) { return window['go']['main']['App']['Markdown'](arg1); } + +export function ParseMarkdown(arg1) { + return window['go']['main']['App']['ParseMarkdown'](arg1); +}