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

@@ -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>