feat: add interactive MCQ and code-sort exercise widgets to tutorial lessons

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-23 01:52:20 -06:00
parent 12cf247e20
commit 6331391792
10 changed files with 568 additions and 53 deletions

View File

@@ -2,6 +2,7 @@ import { DIFFICULTY } from "utils/constants"
import { getACRate } from "utils/functions"
import http from "utils/http"
import {
Exercise,
Problem,
Submission,
SubmissionListPayload,
@@ -420,3 +421,8 @@ export function getProblemSetUserProgress(
) {
return http.get(`problemset/${problemSetId}/users_progress`, { params })
}
export async function getExercises(tutorialId: number): Promise<Exercise[]> {
const res = await http.get("exercises", { params: { tutorial_id: tutorialId } })
return res.data
}

View File

@@ -0,0 +1,78 @@
<script setup lang="ts">
import { Exercise, ExerciseMcqData } from "utils/types"
const props = defineProps<{ exercise: Exercise }>()
const data = computed(() => props.exercise.data as ExerciseMcqData)
const selected = ref<number | null>(null)
const submitted = ref(false)
function select(idx: number) {
if (!submitted.value) selected.value = idx
}
function submit() {
if (selected.value === null) return
submitted.value = true
}
function reset() {
selected.value = null
submitted.value = false
}
function optionType(idx: number): "default" | "success" | "error" {
if (!submitted.value) return "default"
if (idx === data.value.answer) return "success"
if (idx === selected.value) return "error"
return "default"
}
</script>
<template>
<n-card size="small" style="margin: 16px 0; border: 1.5px solid var(--n-border-color)">
<template #header>
<n-space align="center" :size="8">
<n-tag type="success" size="small" :bordered="false">练一练 · 选择题</n-tag>
</n-space>
</template>
<p style="font-weight: 500; margin-bottom: 12px">{{ data.question }}</p>
<n-space vertical :size="8">
<n-button
v-for="(opt, idx) in data.options"
:key="idx"
:type="optionType(idx)"
:secondary="optionType(idx) !== 'default'"
:tertiary="optionType(idx) === 'default'"
:style="{ justifyContent: 'flex-start', width: '100%', textAlign: 'left' }"
@click="select(idx)"
>
<template #icon>
<span style="font-weight: 700">{{ String.fromCharCode(65 + idx) }}</span>
</template>
{{ opt }}
</n-button>
</n-space>
<n-alert
v-if="submitted"
:type="selected === data.answer ? 'success' : 'error'"
:title="selected === data.answer ? '正确!' : '不对,请看正确答案(绿色)'"
style="margin-top: 12px"
/>
<n-space style="margin-top: 12px" :size="8">
<n-button
type="primary"
size="small"
:disabled="selected === null || submitted"
@click="submit"
>
提交
</n-button>
<n-button size="small" @click="reset">重置</n-button>
</n-space>
</n-card>
</template>

View File

@@ -0,0 +1,121 @@
<script setup lang="ts">
import { Exercise, ExerciseSortData } from "utils/types"
const props = defineProps<{ exercise: Exercise }>()
const data = computed(() => props.exercise.data as ExerciseSortData)
type LineItem = { originalIdx: number; text: string }
const lines = ref<LineItem[]>([])
const submitted = ref(false)
function shuffle(arr: LineItem[]): LineItem[] {
const a = [...arr]
for (let i = a.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1))
;[a[i], a[j]] = [a[j], a[i]]
}
const isCorrect = a.every((item, i) => item.originalIdx === i)
return isCorrect && a.length > 1 ? shuffle(arr) : a
}
function init() {
lines.value = shuffle(data.value.lines.map((text, idx) => ({ originalIdx: idx, text })))
submitted.value = false
}
onMounted(init)
watch(() => props.exercise.id, init)
const dragIdx = ref<number | null>(null)
function onDragStart(idx: number) {
dragIdx.value = idx
}
function onDrop(targetIdx: number) {
if (dragIdx.value === null || dragIdx.value === targetIdx) return
const newLines = [...lines.value]
const [moved] = newLines.splice(dragIdx.value, 1)
newLines.splice(targetIdx, 0, moved)
lines.value = newLines
dragIdx.value = null
}
function lineStatus(idx: number): "correct" | "wrong" | "default" {
if (!submitted.value) return "default"
return lines.value[idx].originalIdx === idx ? "correct" : "wrong"
}
const allCorrect = computed(() => lines.value.every((item, i) => item.originalIdx === i))
function submit() {
submitted.value = true
}
function reset() {
init()
}
</script>
<template>
<n-card size="small" style="margin: 16px 0; border: 1.5px solid var(--n-border-color)">
<template #header>
<n-tag type="info" size="small" :bordered="false">练一练 · 代码排序</n-tag>
</template>
<p style="font-weight: 500; margin-bottom: 12px">{{ data.question }}</p>
<n-space vertical :size="6">
<div
v-for="(line, idx) in lines"
:key="line.originalIdx"
draggable="true"
:style="{
display: 'flex',
alignItems: 'center',
gap: '10px',
padding: '8px 12px',
borderRadius: '6px',
border: `1.5px ${submitted ? 'solid' : 'dashed'} ${
lineStatus(idx) === 'correct'
? '#18a058'
: lineStatus(idx) === 'wrong'
? '#d03050'
: 'var(--n-border-color)'
}`,
background:
lineStatus(idx) === 'correct'
? 'rgba(24,160,88,0.08)'
: lineStatus(idx) === 'wrong'
? 'rgba(208,48,80,0.07)'
: 'transparent',
cursor: 'grab',
fontFamily: 'monospace',
fontSize: '13px',
userSelect: 'none',
}"
@dragstart="onDragStart(idx)"
@dragover.prevent
@drop="onDrop(idx)"
>
<span style="color: #bbb; cursor: grab"></span>
<span>{{ line.text }}</span>
</div>
</n-space>
<n-alert
v-if="submitted"
:type="allCorrect ? 'success' : 'error'"
:title="allCorrect ? '顺序正确!' : '顺序有误,红色行需要调整'"
style="margin-top: 12px"
/>
<n-space style="margin-top: 12px" :size="8">
<n-button type="info" size="small" :disabled="submitted" @click="submit">
提交
</n-button>
<n-button size="small" @click="reset">重置</n-button>
</n-space>
</n-card>
</template>

View File

@@ -0,0 +1,13 @@
<script setup lang="ts">
import { Exercise } from "utils/types"
const ExerciseMcq = defineAsyncComponent(() => import("./ExerciseMcq.vue"))
const ExerciseSort = defineAsyncComponent(() => import("./ExerciseSort.vue"))
defineProps<{ exercise: Exercise }>()
</script>
<template>
<ExerciseMcq v-if="exercise.type === 'mcq'" :exercise="exercise" />
<ExerciseSort v-else-if="exercise.type === 'sort'" :exercise="exercise" />
</template>

View File

@@ -0,0 +1,31 @@
import { Exercise } from "utils/types"
type Segment =
| { type: "md"; content: string }
| { type: "exercise"; exercise: Exercise }
export function parseExercises(content: string, exercises: Exercise[]): Segment[] {
const exerciseMap = new Map(exercises.map((e) => [e.id, e]))
const segments: Segment[] = []
const regex = /\[\[exercise:(\d+)\]\]/g
let lastIndex = 0
let match: RegExpExecArray | null
while ((match = regex.exec(content)) !== null) {
if (match.index > lastIndex) {
segments.push({ type: "md", content: content.slice(lastIndex, match.index) })
}
const id = parseInt(match[1])
const exercise = exerciseMap.get(id)
if (exercise) {
segments.push({ type: "exercise", exercise })
}
lastIndex = regex.lastIndex
}
if (lastIndex < content.length) {
segments.push({ type: "md", content: content.slice(lastIndex) })
}
return segments
}

View File

@@ -27,11 +27,15 @@
:bordered="false"
size="small"
>
<MdPreview
preview-theme="vuepress"
:theme="isDark ? 'dark' : 'light'"
:model-value="tutorial.content"
/>
<template v-for="(seg, i) in segments" :key="i">
<MdPreview
v-if="seg.type === 'md'"
preview-theme="vuepress"
:theme="isDark ? 'dark' : 'light'"
:model-value="seg.content"
/>
<ExerciseWidget v-else :exercise="seg.exercise" />
</template>
</n-card>
</n-gi>
@@ -63,11 +67,15 @@
</n-tab-pane>
<n-tab-pane name="content" :tab="`第 ${step} 课`">
<MdPreview
preview-theme="vuepress"
:theme="isDark ? 'dark' : 'light'"
:model-value="tutorial.content"
/>
<template v-for="(seg, i) in segments" :key="i">
<MdPreview
v-if="seg.type === 'md'"
preview-theme="vuepress"
:theme="isDark ? 'dark' : 'light'"
:model-value="seg.content"
/>
<ExerciseWidget v-else :exercise="seg.exercise" />
</template>
</n-tab-pane>
<n-tab-pane name="code" tab="示例代码" v-if="tutorial.code">
@@ -78,21 +86,11 @@
<n-divider style="margin: 12px 0" />
<n-flex align="center" justify="space-between">
<n-button
secondary
type="primary"
:disabled="isFirstLesson"
@click="goToPrevLesson"
>
<n-button secondary type="primary" :disabled="isFirstLesson" @click="goToPrevLesson">
上一课
</n-button>
<n-text>{{ step }} / {{ titles.length }}</n-text>
<n-button
secondary
type="primary"
:disabled="isLastLesson"
@click="goToNextLesson"
>
<n-button secondary type="primary" :disabled="isLastLesson" @click="goToNextLesson">
下一课
</n-button>
</n-flex>
@@ -103,56 +101,45 @@
<script setup lang="ts">
import { MdPreview } from "md-editor-v3"
import "md-editor-v3/lib/preview.css"
import { Tutorial } from "utils/types"
import { getTutorial, getTutorials } from "../api"
import { Tutorial, Exercise } from "utils/types"
import { getTutorial, getTutorials, getExercises } from "../api"
import { parseExercises } from "./composables/useExerciseParse"
import { useBreakpoints } from "shared/composables/breakpoints"
const ExerciseWidget = defineAsyncComponent(() => import("./components/ExerciseWidget.vue"))
const CodeEditor = defineAsyncComponent(() => import("shared/components/CodeEditor.vue"))
const isDark = useDark()
const CodeEditor = defineAsyncComponent(
() => import("shared/components/CodeEditor.vue"),
)
const route = useRoute()
const router = useRouter()
const { isDesktop } = useBreakpoints()
const step = computed(() => {
if (!route.params.step || !route.params.step.length) return 1
else {
return parseInt(route.params.step[0])
}
})
const tutorial = ref<Partial<Tutorial>>({
id: 0,
title: "",
content: "",
code: "",
return parseInt(route.params.step[0])
})
const tutorial = ref<Partial<Tutorial>>({ id: 0, title: "", content: "", code: "" })
const titles = ref<{ id: number; title: string }[]>([])
const exercises = ref<Exercise[]>([])
const activeTab = ref("content")
const segments = computed(() =>
parseExercises(tutorial.value.content ?? "", exercises.value),
)
const isFirstLesson = computed(() => step.value === 1)
const isLastLesson = computed(() => step.value === titles.value.length)
function goToLesson(lessonNumber: number) {
activeTab.value = "content"
const dest = lessonNumber.toString().padStart(2, "0")
router.push("/learn/" + dest)
router.push("/learn/" + lessonNumber.toString().padStart(2, "0"))
}
function goToPrevLesson() {
if (step.value > 1) {
goToLesson(step.value - 1)
}
if (step.value > 1) goToLesson(step.value - 1)
}
function goToNextLesson() {
if (step.value < titles.value.length) {
goToLesson(step.value + 1)
}
if (step.value < titles.value.length) goToLesson(step.value + 1)
}
async function init() {
@@ -160,15 +147,15 @@ async function init() {
titles.value = res1.data
if (titles.value.length === 0) return
const id = titles.value[step.value - 1].id
const res2 = await getTutorial(id)
const [res2, exs] = await Promise.all([getTutorial(id), getExercises(id)])
tutorial.value = res2.data
exercises.value = exs
}
watch(
() => route.params.step,
async () => {
if (route.name !== "learn") return
init()
if (route.name === "learn") init()
},
{ immediate: true },
)