Move quizzes fetching in App.svelte component
This commit is contained in:
parent
8fa98b6782
commit
2060123d47
5 changed files with 65 additions and 25 deletions
27
app.go
27
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,
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,20 @@
|
|||
<script lang="ts">
|
||||
|
||||
import { onMount } from "svelte";
|
||||
import { FetchQuizzes } from "../wailsjs/go/main/App";
|
||||
import QuizCardList from "./QuizCardList.svelte";
|
||||
|
||||
|
||||
let quizzes: main.Quiz[] = [];
|
||||
|
||||
onMount(async () => {
|
||||
FetchQuizzes().then(
|
||||
(result) => {
|
||||
quizzes = result;
|
||||
});
|
||||
});
|
||||
|
||||
</script>
|
||||
|
||||
<main>
|
||||
<QuizCardList />
|
||||
<QuizCardList {quizzes} />
|
||||
</main>
|
||||
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
|
||||
</script>
|
||||
|
||||
<div>
|
||||
{#each quizzes as quiz, index}
|
||||
{#if editingIndex === index}
|
||||
<div class="editor">
|
||||
<CodeMirror lang={ markdown() } />
|
||||
<button on:click="{ ()=>hideEditor() }">Save</button>
|
||||
<CodeMirror bind:value lang={ markdown() } />
|
||||
<button on:click="{ ()=>updateQuiz(index) }">Save</button>
|
||||
</div>
|
||||
{:else}
|
||||
<div class="item" on:click="{() => showEditor(index)}">
|
||||
<div class="item" on:click="{() => editQuiz(quiz, index)}">
|
||||
<QuizCard {quiz} />
|
||||
</div>
|
||||
{/if}
|
||||
|
|
2
frontend/wailsjs/go/main/App.d.ts
vendored
2
frontend/wailsjs/go/main/App.d.ts
vendored
|
@ -5,3 +5,5 @@ import {main} from '../models';
|
|||
export function FetchQuizzes():Promise<Array<main.Quiz>>;
|
||||
|
||||
export function Markdown(arg1:main.Quiz):Promise<string>;
|
||||
|
||||
export function ParseMarkdown(arg1:string):Promise<main.Quiz>;
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue