add fills
This commit is contained in:
@@ -263,7 +263,9 @@ export function setTutorialVisibility(id: number, is_public: boolean) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getAdminExercises(tutorialId: number) {
|
export async function getAdminExercises(tutorialId: number) {
|
||||||
const res = await http.get("admin/exercise", { params: { tutorial_id: tutorialId } })
|
const res = await http.get("admin/exercise", {
|
||||||
|
params: { tutorial_id: tutorialId },
|
||||||
|
})
|
||||||
return res.data as Exercise[]
|
return res.data as Exercise[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,10 @@
|
|||||||
<script setup lang="ts">
|
<script setup lang="ts">
|
||||||
import { Exercise, ExerciseMcqData, ExerciseSortData } from "utils/types"
|
import {
|
||||||
|
Exercise,
|
||||||
|
ExerciseMcqData,
|
||||||
|
ExerciseSortData,
|
||||||
|
ExerciseFillData,
|
||||||
|
} from "utils/types"
|
||||||
import {
|
import {
|
||||||
getAdminExercises,
|
getAdminExercises,
|
||||||
createExercise,
|
createExercise,
|
||||||
@@ -14,7 +19,7 @@ const dialog = useDialog()
|
|||||||
const exercises = ref<Exercise[]>([])
|
const exercises = ref<Exercise[]>([])
|
||||||
const showForm = ref(false)
|
const showForm = ref(false)
|
||||||
const editingId = ref<number | null>(null)
|
const editingId = ref<number | null>(null)
|
||||||
const formType = ref<"mcq" | "sort">("mcq")
|
const formType = ref<"mcq" | "sort" | "fill">("mcq")
|
||||||
const formOrder = ref(0)
|
const formOrder = ref(0)
|
||||||
|
|
||||||
const mcqQuestion = ref("")
|
const mcqQuestion = ref("")
|
||||||
@@ -24,6 +29,9 @@ const mcqAnswer = ref(0)
|
|||||||
const sortQuestion = ref("")
|
const sortQuestion = ref("")
|
||||||
const sortCode = ref("")
|
const sortCode = ref("")
|
||||||
|
|
||||||
|
const fillQuestion = ref("")
|
||||||
|
const fillCode = ref("")
|
||||||
|
|
||||||
async function load() {
|
async function load() {
|
||||||
exercises.value = await getAdminExercises(props.tutorialId)
|
exercises.value = await getAdminExercises(props.tutorialId)
|
||||||
}
|
}
|
||||||
@@ -39,6 +47,8 @@ function openCreate() {
|
|||||||
mcqAnswer.value = 0
|
mcqAnswer.value = 0
|
||||||
sortQuestion.value = ""
|
sortQuestion.value = ""
|
||||||
sortCode.value = ""
|
sortCode.value = ""
|
||||||
|
fillQuestion.value = ""
|
||||||
|
fillCode.value = ""
|
||||||
showForm.value = true
|
showForm.value = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -51,29 +61,51 @@ function openEdit(ex: Exercise) {
|
|||||||
mcqQuestion.value = d.question
|
mcqQuestion.value = d.question
|
||||||
mcqOptions.value = [...d.options]
|
mcqOptions.value = [...d.options]
|
||||||
mcqAnswer.value = d.answer
|
mcqAnswer.value = d.answer
|
||||||
} else {
|
} else if (ex.type === "sort") {
|
||||||
const d = ex.data as ExerciseSortData
|
const d = ex.data as ExerciseSortData
|
||||||
sortQuestion.value = d.question
|
sortQuestion.value = d.question
|
||||||
sortCode.value = d.lines.join("\n")
|
sortCode.value = d.lines.join("\n")
|
||||||
|
} else {
|
||||||
|
const d = ex.data as ExerciseFillData
|
||||||
|
fillQuestion.value = d.question
|
||||||
|
fillCode.value = d.code
|
||||||
}
|
}
|
||||||
showForm.value = true
|
showForm.value = true
|
||||||
}
|
}
|
||||||
|
|
||||||
async function save() {
|
async function save() {
|
||||||
const data =
|
let data: Record<string, unknown>
|
||||||
formType.value === "mcq"
|
if (formType.value === "mcq") {
|
||||||
? { question: mcqQuestion.value, options: mcqOptions.value, answer: mcqAnswer.value }
|
data = {
|
||||||
: {
|
question: mcqQuestion.value,
|
||||||
|
options: mcqOptions.value,
|
||||||
|
answer: mcqAnswer.value,
|
||||||
|
}
|
||||||
|
} else if (formType.value === "sort") {
|
||||||
|
data = {
|
||||||
question: sortQuestion.value || "将下列代码行排列为正确顺序",
|
question: sortQuestion.value || "将下列代码行排列为正确顺序",
|
||||||
lines: sortCode.value.split("\n").filter((l) => l.trim() !== ""),
|
lines: sortCode.value.split("\n").filter((l) => l.trim() !== ""),
|
||||||
}
|
}
|
||||||
|
} else {
|
||||||
|
data = { question: fillQuestion.value, code: fillCode.value }
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (editingId.value) {
|
if (editingId.value) {
|
||||||
await updateExercise({ id: editingId.value, type: formType.value, data, order: formOrder.value })
|
await updateExercise({
|
||||||
|
id: editingId.value,
|
||||||
|
type: formType.value,
|
||||||
|
data,
|
||||||
|
order: formOrder.value,
|
||||||
|
})
|
||||||
message.success("练习题已更新")
|
message.success("练习题已更新")
|
||||||
} else {
|
} else {
|
||||||
await createExercise({ tutorial_id: props.tutorialId, type: formType.value, data, order: formOrder.value })
|
await createExercise({
|
||||||
|
tutorial_id: props.tutorialId,
|
||||||
|
type: formType.value,
|
||||||
|
data,
|
||||||
|
order: formOrder.value,
|
||||||
|
})
|
||||||
message.success("练习题已创建")
|
message.success("练习题已创建")
|
||||||
}
|
}
|
||||||
showForm.value = false
|
showForm.value = false
|
||||||
@@ -102,7 +134,15 @@ function copyPlaceholder(id: number) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function typeName(type: string) {
|
function typeName(type: string) {
|
||||||
return type === "mcq" ? "选择题" : "代码排序"
|
if (type === "mcq") return "选择题"
|
||||||
|
if (type === "sort") return "代码排序"
|
||||||
|
return "代码填空"
|
||||||
|
}
|
||||||
|
|
||||||
|
function typeTagType(type: string): "success" | "info" | "warning" {
|
||||||
|
if (type === "mcq") return "success"
|
||||||
|
if (type === "sort") return "info"
|
||||||
|
return "warning"
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
@@ -110,7 +150,9 @@ function typeName(type: string) {
|
|||||||
<div>
|
<div>
|
||||||
<n-flex justify="space-between" align="center" style="margin-bottom: 16px">
|
<n-flex justify="space-between" align="center" style="margin-bottom: 16px">
|
||||||
<n-text>共 {{ exercises.length }} 道练习题</n-text>
|
<n-text>共 {{ exercises.length }} 道练习题</n-text>
|
||||||
<n-button type="primary" size="small" @click="openCreate">+ 添加练习题</n-button>
|
<n-button type="primary" size="small" @click="openCreate"
|
||||||
|
>+ 添加练习题</n-button
|
||||||
|
>
|
||||||
</n-flex>
|
</n-flex>
|
||||||
|
|
||||||
<n-empty v-if="exercises.length === 0" description="暂无练习题" />
|
<n-empty v-if="exercises.length === 0" description="暂无练习题" />
|
||||||
@@ -119,11 +161,7 @@ function typeName(type: string) {
|
|||||||
<n-list-item v-for="ex in exercises" :key="ex.id">
|
<n-list-item v-for="ex in exercises" :key="ex.id">
|
||||||
<n-flex justify="space-between" align="center">
|
<n-flex justify="space-between" align="center">
|
||||||
<div>
|
<div>
|
||||||
<n-tag
|
<n-tag size="small" :type="typeTagType(ex.type)" :bordered="false">
|
||||||
size="small"
|
|
||||||
:type="ex.type === 'mcq' ? 'success' : 'info'"
|
|
||||||
:bordered="false"
|
|
||||||
>
|
|
||||||
{{ typeName(ex.type) }}
|
{{ typeName(ex.type) }}
|
||||||
</n-tag>
|
</n-tag>
|
||||||
<n-text style="margin-left: 10px">
|
<n-text style="margin-left: 10px">
|
||||||
@@ -133,12 +171,16 @@ function typeName(type: string) {
|
|||||||
<n-space :size="8">
|
<n-space :size="8">
|
||||||
<n-tooltip trigger="hover">
|
<n-tooltip trigger="hover">
|
||||||
<template #trigger>
|
<template #trigger>
|
||||||
<n-button size="small" @click="copyPlaceholder(ex.id)">复制占位符</n-button>
|
<n-button size="small" @click="copyPlaceholder(ex.id)"
|
||||||
|
>复制占位符</n-button
|
||||||
|
>
|
||||||
</template>
|
</template>
|
||||||
将 [[exercise:{{ ex.id }}]] 粘贴到 Markdown 内容中
|
将 [[exercise:{{ ex.id }}]] 粘贴到 Markdown 内容中
|
||||||
</n-tooltip>
|
</n-tooltip>
|
||||||
<n-button size="small" @click="openEdit(ex)">编辑</n-button>
|
<n-button size="small" @click="openEdit(ex)">编辑</n-button>
|
||||||
<n-button size="small" type="error" @click="confirmDelete(ex.id)">删除</n-button>
|
<n-button size="small" type="error" @click="confirmDelete(ex.id)"
|
||||||
|
>删除</n-button
|
||||||
|
>
|
||||||
</n-space>
|
</n-space>
|
||||||
</n-flex>
|
</n-flex>
|
||||||
</n-list-item>
|
</n-list-item>
|
||||||
@@ -155,11 +197,16 @@ function typeName(type: string) {
|
|||||||
<n-radio-group v-model:value="formType" :disabled="!!editingId">
|
<n-radio-group v-model:value="formType" :disabled="!!editingId">
|
||||||
<n-radio value="mcq">选择题</n-radio>
|
<n-radio value="mcq">选择题</n-radio>
|
||||||
<n-radio value="sort">代码排序</n-radio>
|
<n-radio value="sort">代码排序</n-radio>
|
||||||
|
<n-radio value="fill">代码填空</n-radio>
|
||||||
</n-radio-group>
|
</n-radio-group>
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
|
|
||||||
<n-form-item label="顺序">
|
<n-form-item label="顺序">
|
||||||
<n-input-number v-model:value="formOrder" :min="0" style="width: 100px" />
|
<n-input-number
|
||||||
|
v-model:value="formOrder"
|
||||||
|
:min="0"
|
||||||
|
style="width: 100px"
|
||||||
|
/>
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
|
|
||||||
<template v-if="formType === 'mcq'">
|
<template v-if="formType === 'mcq'">
|
||||||
@@ -168,7 +215,12 @@ function typeName(type: string) {
|
|||||||
</n-form-item>
|
</n-form-item>
|
||||||
<n-form-item label="选项(正确答案前选择单选按钮)">
|
<n-form-item label="选项(正确答案前选择单选按钮)">
|
||||||
<n-space vertical style="width: 100%">
|
<n-space vertical style="width: 100%">
|
||||||
<n-flex v-for="(opt, i) in mcqOptions" :key="i" align="center" :size="8">
|
<n-flex
|
||||||
|
v-for="(opt, i) in mcqOptions"
|
||||||
|
:key="i"
|
||||||
|
align="center"
|
||||||
|
:size="8"
|
||||||
|
>
|
||||||
<n-radio
|
<n-radio
|
||||||
:value="i"
|
:value="i"
|
||||||
:checked="mcqAnswer === i"
|
:checked="mcqAnswer === i"
|
||||||
@@ -182,19 +234,32 @@ function typeName(type: string) {
|
|||||||
<n-button
|
<n-button
|
||||||
size="small"
|
size="small"
|
||||||
:disabled="mcqOptions.length <= 2"
|
:disabled="mcqOptions.length <= 2"
|
||||||
@click="() => { mcqOptions.splice(i, 1); if (mcqAnswer >= mcqOptions.length) mcqAnswer = mcqOptions.length - 1 }"
|
@click="
|
||||||
|
() => {
|
||||||
|
mcqOptions.splice(i, 1)
|
||||||
|
if (mcqAnswer >= mcqOptions.length)
|
||||||
|
mcqAnswer = mcqOptions.length - 1
|
||||||
|
}
|
||||||
|
"
|
||||||
>
|
>
|
||||||
✕
|
✕
|
||||||
</n-button>
|
</n-button>
|
||||||
</n-flex>
|
</n-flex>
|
||||||
<n-button size="small" @click="mcqOptions.push('')">+ 添加选项</n-button>
|
<n-button size="small" @click="mcqOptions.push('')"
|
||||||
|
>+ 添加选项</n-button
|
||||||
|
>
|
||||||
</n-space>
|
</n-space>
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<template v-else>
|
<template v-else-if="formType === 'sort'">
|
||||||
<n-form-item label="题目">
|
<n-form-item label="题目">
|
||||||
<n-input v-model:value="sortQuestion" type="textarea" :rows="2" placeholder="将下列代码行排列为正确顺序" />
|
<n-input
|
||||||
|
v-model:value="sortQuestion"
|
||||||
|
type="textarea"
|
||||||
|
:rows="2"
|
||||||
|
placeholder="将下列代码行排列为正确顺序"
|
||||||
|
/>
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
<n-form-item label="正确代码(每行将自动成为一道排序项)">
|
<n-form-item label="正确代码(每行将自动成为一道排序项)">
|
||||||
<n-input
|
<n-input
|
||||||
@@ -206,6 +271,26 @@ function typeName(type: string) {
|
|||||||
/>
|
/>
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
<template v-else>
|
||||||
|
<n-form-item label="题目说明">
|
||||||
|
<n-input
|
||||||
|
v-model:value="fillQuestion"
|
||||||
|
type="textarea"
|
||||||
|
:rows="2"
|
||||||
|
placeholder="例:补全下面的循环语句"
|
||||||
|
/>
|
||||||
|
</n-form-item>
|
||||||
|
<n-form-item label="含空位的代码">
|
||||||
|
<n-input
|
||||||
|
v-model:value="fillCode"
|
||||||
|
type="textarea"
|
||||||
|
:rows="10"
|
||||||
|
placeholder="用 {{答案}} 标记空位,多个合法答案用 | 分隔,例如:for {{i|idx}} in range(10):"
|
||||||
|
style="font-family: monospace"
|
||||||
|
/>
|
||||||
|
</n-form-item>
|
||||||
|
</template>
|
||||||
</n-form>
|
</n-form>
|
||||||
|
|
||||||
<template #footer>
|
<template #footer>
|
||||||
|
|||||||
@@ -64,7 +64,6 @@ async function submit() {
|
|||||||
await updateTutorial(tutorial)
|
await updateTutorial(tutorial)
|
||||||
message.success("修改已保存")
|
message.success("修改已保存")
|
||||||
}
|
}
|
||||||
router.push({ name: "admin tutorial list" })
|
|
||||||
} catch (err: any) {
|
} catch (err: any) {
|
||||||
message.error(err.data)
|
message.error(err.data)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -423,6 +423,8 @@ export function getProblemSetUserProgress(
|
|||||||
}
|
}
|
||||||
|
|
||||||
export async function getExercises(tutorialId: number): Promise<Exercise[]> {
|
export async function getExercises(tutorialId: number): Promise<Exercise[]> {
|
||||||
const res = await http.get("exercises", { params: { tutorial_id: tutorialId } })
|
const res = await http.get("exercises", {
|
||||||
|
params: { tutorial_id: tutorialId },
|
||||||
|
})
|
||||||
return res.data
|
return res.data
|
||||||
}
|
}
|
||||||
|
|||||||
160
src/oj/learn/components/ExerciseFill.vue
Normal file
160
src/oj/learn/components/ExerciseFill.vue
Normal file
@@ -0,0 +1,160 @@
|
|||||||
|
<script setup lang="ts">
|
||||||
|
import hljs from "highlight.js/lib/core"
|
||||||
|
import python from "highlight.js/lib/languages/python"
|
||||||
|
import c from "highlight.js/lib/languages/c"
|
||||||
|
import { Exercise, ExerciseFillData } from "utils/types"
|
||||||
|
|
||||||
|
hljs.registerLanguage("python", python)
|
||||||
|
hljs.registerLanguage("c", c)
|
||||||
|
|
||||||
|
const props = defineProps<{ exercise: Exercise; lang?: string }>()
|
||||||
|
const data = computed(() => props.exercise.data as ExerciseFillData)
|
||||||
|
|
||||||
|
type CodeSeg = { type: "code"; html: string }
|
||||||
|
type BlankSeg = { type: "blank"; answers: string[]; index: number }
|
||||||
|
type Segment = CodeSeg | BlankSeg
|
||||||
|
|
||||||
|
function escapeHtml(s: string): string {
|
||||||
|
return s.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">")
|
||||||
|
}
|
||||||
|
|
||||||
|
const segments = computed<Segment[]>(() => {
|
||||||
|
const blanks: string[][] = []
|
||||||
|
const markedCode = data.value.code.replace(/\{\{([^}]+)\}\}/g, (_, inner) => {
|
||||||
|
blanks.push(inner.split("|"))
|
||||||
|
return `____${blanks.length - 1}____`
|
||||||
|
})
|
||||||
|
|
||||||
|
const lang =
|
||||||
|
props.lang === "python" ? "python" : props.lang === "c" ? "c" : null
|
||||||
|
let highlighted: string
|
||||||
|
if (lang) {
|
||||||
|
try {
|
||||||
|
highlighted = hljs.highlight(markedCode, { language: lang }).value
|
||||||
|
} catch {
|
||||||
|
highlighted = escapeHtml(markedCode)
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
highlighted = escapeHtml(markedCode)
|
||||||
|
}
|
||||||
|
|
||||||
|
const parts = highlighted.split(/____(\d+)____/)
|
||||||
|
const result: Segment[] = []
|
||||||
|
for (let i = 0; i < parts.length; i++) {
|
||||||
|
if (i % 2 === 0) {
|
||||||
|
if (parts[i]) result.push({ type: "code", html: parts[i] })
|
||||||
|
} else {
|
||||||
|
const idx = parseInt(parts[i])
|
||||||
|
result.push({ type: "blank", answers: blanks[idx], index: idx })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result
|
||||||
|
})
|
||||||
|
|
||||||
|
const blankCount = computed(
|
||||||
|
() => segments.value.filter((s) => s.type === "blank").length,
|
||||||
|
)
|
||||||
|
const userInputs = ref<string[]>([])
|
||||||
|
const wrongBlanks = ref<Set<number>>(new Set())
|
||||||
|
const allCorrect = ref(false)
|
||||||
|
|
||||||
|
watch(() => props.exercise.id, reset, { immediate: true })
|
||||||
|
|
||||||
|
function reset() {
|
||||||
|
userInputs.value = Array(blankCount.value).fill("")
|
||||||
|
wrongBlanks.value = new Set()
|
||||||
|
allCorrect.value = false
|
||||||
|
}
|
||||||
|
|
||||||
|
function submit() {
|
||||||
|
if (allCorrect.value) return
|
||||||
|
const wrong = new Set<number>()
|
||||||
|
for (const seg of segments.value) {
|
||||||
|
if (seg.type !== "blank") continue
|
||||||
|
if (!seg.answers.includes(userInputs.value[seg.index]?.trim() ?? "")) {
|
||||||
|
wrong.add(seg.index)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
wrongBlanks.value = wrong
|
||||||
|
allCorrect.value = wrong.size === 0
|
||||||
|
}
|
||||||
|
|
||||||
|
function inputWidth(idx: number): string {
|
||||||
|
return Math.max(4, (userInputs.value[idx]?.length ?? 0) + 2) + "ch"
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<template>
|
||||||
|
<n-card
|
||||||
|
size="small"
|
||||||
|
style="margin: 16px 0; border: 1.5px solid var(--n-border-color)"
|
||||||
|
>
|
||||||
|
<template #header>
|
||||||
|
<n-tag type="warning" size="small" :bordered="false"
|
||||||
|
>练一练 · 代码填空</n-tag
|
||||||
|
>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<p style="font-weight: 500; margin-bottom: 12px">{{ data.question }}</p>
|
||||||
|
|
||||||
|
<pre
|
||||||
|
:style="{
|
||||||
|
fontFamily: 'monospace',
|
||||||
|
lineHeight: '1.6',
|
||||||
|
background: 'var(--n-color)',
|
||||||
|
border: '1px solid var(--n-border-color)',
|
||||||
|
borderRadius: '6px',
|
||||||
|
padding: '12px',
|
||||||
|
overflowX: 'auto',
|
||||||
|
whiteSpace: 'pre-wrap',
|
||||||
|
margin: 0,
|
||||||
|
}"
|
||||||
|
><template v-for="(seg, i) in segments" :key="i"
|
||||||
|
><span v-if="seg.type === 'code'" v-html="seg.html" /><input
|
||||||
|
v-else
|
||||||
|
:value="userInputs[seg.index]"
|
||||||
|
:disabled="allCorrect"
|
||||||
|
:style="{
|
||||||
|
width: inputWidth(seg.index),
|
||||||
|
fontFamily: 'monospace',
|
||||||
|
padding: '1px 4px',
|
||||||
|
borderRadius: '3px',
|
||||||
|
border: `1.5px solid ${
|
||||||
|
allCorrect
|
||||||
|
? '#18a058'
|
||||||
|
: wrongBlanks.has(seg.index)
|
||||||
|
? '#d03050'
|
||||||
|
: 'var(--n-border-color)'
|
||||||
|
}`,
|
||||||
|
background: allCorrect
|
||||||
|
? 'rgba(24,160,88,0.08)'
|
||||||
|
: wrongBlanks.has(seg.index)
|
||||||
|
? 'rgba(208,48,80,0.07)'
|
||||||
|
: 'transparent',
|
||||||
|
outline: 'none',
|
||||||
|
color: 'inherit',
|
||||||
|
minWidth: '4ch',
|
||||||
|
}"
|
||||||
|
@input="userInputs[seg.index] = ($event.target as HTMLInputElement).value"
|
||||||
|
/></template></pre>
|
||||||
|
|
||||||
|
<n-alert
|
||||||
|
v-if="wrongBlanks.size > 0 || allCorrect"
|
||||||
|
:type="allCorrect ? 'success' : 'error'"
|
||||||
|
:title="allCorrect ? '全部正确!' : '有填写错误,请检查红色标注的空位'"
|
||||||
|
style="margin-top: 12px"
|
||||||
|
/>
|
||||||
|
|
||||||
|
<n-space style="margin-top: 12px" :size="8">
|
||||||
|
<n-button
|
||||||
|
type="warning"
|
||||||
|
size="small"
|
||||||
|
:disabled="allCorrect"
|
||||||
|
@click="submit"
|
||||||
|
>
|
||||||
|
提交
|
||||||
|
</n-button>
|
||||||
|
<n-button size="small" @click="reset">重置</n-button>
|
||||||
|
</n-space>
|
||||||
|
</n-card>
|
||||||
|
</template>
|
||||||
@@ -39,10 +39,15 @@ function optionType(idx: number): "default" | "primary" | "success" {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<n-card size="small" style="margin: 16px 0; border: 1.5px solid var(--n-border-color)">
|
<n-card
|
||||||
|
size="small"
|
||||||
|
style="margin: 16px 0; border: 1.5px solid var(--n-border-color)"
|
||||||
|
>
|
||||||
<template #header>
|
<template #header>
|
||||||
<n-space align="center" :size="8">
|
<n-space align="center" :size="8">
|
||||||
<n-tag type="success" size="small" :bordered="false">练一练 · 选择题</n-tag>
|
<n-tag type="success" size="small" :bordered="false"
|
||||||
|
>练一练 · 选择题</n-tag
|
||||||
|
>
|
||||||
</n-space>
|
</n-space>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
@@ -56,11 +61,17 @@ function optionType(idx: number): "default" | "primary" | "success" {
|
|||||||
:secondary="optionType(idx) !== 'default'"
|
:secondary="optionType(idx) !== 'default'"
|
||||||
:tertiary="optionType(idx) === 'default'"
|
:tertiary="optionType(idx) === 'default'"
|
||||||
:strong="idx === selected"
|
:strong="idx === selected"
|
||||||
:style="{ justifyContent: 'flex-start', width: '100%', textAlign: 'left' }"
|
:style="{
|
||||||
|
justifyContent: 'flex-start',
|
||||||
|
width: '100%',
|
||||||
|
textAlign: 'left',
|
||||||
|
}"
|
||||||
@click="select(idx)"
|
@click="select(idx)"
|
||||||
>
|
>
|
||||||
<template #icon>
|
<template #icon>
|
||||||
<span style="font-weight: 700">{{ String.fromCharCode(65 + idx) }}</span>
|
<span style="font-weight: 700">{{
|
||||||
|
String.fromCharCode(65 + idx)
|
||||||
|
}}</span>
|
||||||
</template>
|
</template>
|
||||||
{{ opt }}
|
{{ opt }}
|
||||||
</n-button>
|
</n-button>
|
||||||
|
|||||||
@@ -27,7 +27,9 @@ function shuffle(arr: LineItem[]): LineItem[] {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
lines.value = shuffle(data.value.lines.map((text, idx) => ({ originalIdx: idx, text })))
|
lines.value = shuffle(
|
||||||
|
data.value.lines.map((text, idx) => ({ originalIdx: idx, text })),
|
||||||
|
)
|
||||||
submitted.value = false
|
submitted.value = false
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -55,7 +57,9 @@ function lineStatus(idx: number): "correct" | "wrong" | "default" {
|
|||||||
return lines.value[idx].originalIdx === idx ? "correct" : "wrong"
|
return lines.value[idx].originalIdx === idx ? "correct" : "wrong"
|
||||||
}
|
}
|
||||||
|
|
||||||
const allCorrect = computed(() => lines.value.every((item, i) => item.originalIdx === i))
|
const allCorrect = computed(() =>
|
||||||
|
lines.value.every((item, i) => item.originalIdx === i),
|
||||||
|
)
|
||||||
|
|
||||||
function submit() {
|
function submit() {
|
||||||
submitted.value = true
|
submitted.value = true
|
||||||
@@ -72,11 +76,14 @@ function escapeHtml(text: string): string {
|
|||||||
const lineHtmlMap = computed<Record<number, string>>(() => {
|
const lineHtmlMap = computed<Record<number, string>>(() => {
|
||||||
const rawLines = data.value.lines
|
const rawLines = data.value.lines
|
||||||
const map: Record<number, string> = {}
|
const map: Record<number, string> = {}
|
||||||
const lang = props.lang === "python" ? "python" : props.lang === "c" ? "c" : null
|
const lang =
|
||||||
|
props.lang === "python" ? "python" : props.lang === "c" ? "c" : null
|
||||||
|
|
||||||
if (lang) {
|
if (lang) {
|
||||||
try {
|
try {
|
||||||
const result = hljs.highlight(rawLines.join("\n"), { language: lang }).value
|
const result = hljs.highlight(rawLines.join("\n"), {
|
||||||
|
language: lang,
|
||||||
|
}).value
|
||||||
result.split("\n").forEach((html, i) => {
|
result.split("\n").forEach((html, i) => {
|
||||||
map[i] = html
|
map[i] = html
|
||||||
})
|
})
|
||||||
@@ -94,9 +101,14 @@ const lineHtmlMap = computed<Record<number, string>>(() => {
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<n-card size="small" style="margin: 16px 0; border: 1.5px solid var(--n-border-color)">
|
<n-card
|
||||||
|
size="small"
|
||||||
|
style="margin: 16px 0; border: 1.5px solid var(--n-border-color)"
|
||||||
|
>
|
||||||
<template #header>
|
<template #header>
|
||||||
<n-tag type="info" size="small" :bordered="false">练一练 · 代码排序</n-tag>
|
<n-tag type="info" size="small" :bordered="false"
|
||||||
|
>练一练 · 代码排序</n-tag
|
||||||
|
>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
<p style="font-weight: 500; margin-bottom: 12px">{{ data.question }}</p>
|
<p style="font-weight: 500; margin-bottom: 12px">{{ data.question }}</p>
|
||||||
@@ -127,7 +139,6 @@ const lineHtmlMap = computed<Record<number, string>>(() => {
|
|||||||
: 'transparent',
|
: 'transparent',
|
||||||
cursor: 'grab',
|
cursor: 'grab',
|
||||||
fontFamily: 'monospace',
|
fontFamily: 'monospace',
|
||||||
fontSize: '13px',
|
|
||||||
userSelect: 'none',
|
userSelect: 'none',
|
||||||
}"
|
}"
|
||||||
@dragstart="onDragStart(idx)"
|
@dragstart="onDragStart(idx)"
|
||||||
@@ -147,7 +158,12 @@ const lineHtmlMap = computed<Record<number, string>>(() => {
|
|||||||
/>
|
/>
|
||||||
|
|
||||||
<n-space style="margin-top: 12px" :size="8">
|
<n-space style="margin-top: 12px" :size="8">
|
||||||
<n-button type="info" size="small" :disabled="submitted && allCorrect" @click="submit">
|
<n-button
|
||||||
|
type="info"
|
||||||
|
size="small"
|
||||||
|
:disabled="submitted && allCorrect"
|
||||||
|
@click="submit"
|
||||||
|
>
|
||||||
提交
|
提交
|
||||||
</n-button>
|
</n-button>
|
||||||
<n-button size="small" @click="reset">重置</n-button>
|
<n-button size="small" @click="reset">重置</n-button>
|
||||||
@@ -158,29 +174,51 @@ const lineHtmlMap = computed<Record<number, string>>(() => {
|
|||||||
<style>
|
<style>
|
||||||
.hljs-keyword,
|
.hljs-keyword,
|
||||||
.hljs-operator,
|
.hljs-operator,
|
||||||
.hljs-selector-tag { color: #d73a49; }
|
.hljs-selector-tag {
|
||||||
|
color: #d73a49;
|
||||||
|
}
|
||||||
.hljs-string,
|
.hljs-string,
|
||||||
.hljs-regexp,
|
.hljs-regexp,
|
||||||
.hljs-template-literal { color: #032f62; }
|
.hljs-template-literal {
|
||||||
|
color: #032f62;
|
||||||
|
}
|
||||||
.hljs-comment,
|
.hljs-comment,
|
||||||
.hljs-quote { color: #6a737d; font-style: italic; }
|
.hljs-quote {
|
||||||
|
color: #6a737d;
|
||||||
|
font-style: italic;
|
||||||
|
}
|
||||||
.hljs-number,
|
.hljs-number,
|
||||||
.hljs-literal { color: #005cc5; }
|
.hljs-literal {
|
||||||
|
color: #005cc5;
|
||||||
|
}
|
||||||
.hljs-built_in,
|
.hljs-built_in,
|
||||||
.hljs-title.function_,
|
.hljs-title.function_,
|
||||||
.hljs-class .hljs-title { color: #6f42c1; }
|
.hljs-class .hljs-title {
|
||||||
|
color: #6f42c1;
|
||||||
|
}
|
||||||
|
|
||||||
.dark .hljs-keyword,
|
.dark .hljs-keyword,
|
||||||
.dark .hljs-operator,
|
.dark .hljs-operator,
|
||||||
.dark .hljs-selector-tag { color: #c678dd; }
|
.dark .hljs-selector-tag {
|
||||||
|
color: #c678dd;
|
||||||
|
}
|
||||||
.dark .hljs-string,
|
.dark .hljs-string,
|
||||||
.dark .hljs-regexp,
|
.dark .hljs-regexp,
|
||||||
.dark .hljs-template-literal { color: #98c379; }
|
.dark .hljs-template-literal {
|
||||||
|
color: #98c379;
|
||||||
|
}
|
||||||
.dark .hljs-comment,
|
.dark .hljs-comment,
|
||||||
.dark .hljs-quote { color: #7f848e; font-style: italic; }
|
.dark .hljs-quote {
|
||||||
|
color: #7f848e;
|
||||||
|
font-style: italic;
|
||||||
|
}
|
||||||
.dark .hljs-number,
|
.dark .hljs-number,
|
||||||
.dark .hljs-literal { color: #e5c07b; }
|
.dark .hljs-literal {
|
||||||
|
color: #e5c07b;
|
||||||
|
}
|
||||||
.dark .hljs-built_in,
|
.dark .hljs-built_in,
|
||||||
.dark .hljs-title.function_,
|
.dark .hljs-title.function_,
|
||||||
.dark .hljs-class .hljs-title { color: #61afef; }
|
.dark .hljs-class .hljs-title {
|
||||||
|
color: #61afef;
|
||||||
|
}
|
||||||
</style>
|
</style>
|
||||||
|
|||||||
@@ -3,11 +3,21 @@ import { Exercise } from "utils/types"
|
|||||||
|
|
||||||
const ExerciseMcq = defineAsyncComponent(() => import("./ExerciseMcq.vue"))
|
const ExerciseMcq = defineAsyncComponent(() => import("./ExerciseMcq.vue"))
|
||||||
const ExerciseSort = defineAsyncComponent(() => import("./ExerciseSort.vue"))
|
const ExerciseSort = defineAsyncComponent(() => import("./ExerciseSort.vue"))
|
||||||
|
const ExerciseFill = defineAsyncComponent(() => import("./ExerciseFill.vue"))
|
||||||
|
|
||||||
defineProps<{ exercise: Exercise; lang?: string }>()
|
defineProps<{ exercise: Exercise; lang?: string }>()
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
<ExerciseMcq v-if="exercise.type === 'mcq'" :exercise="exercise" />
|
<ExerciseMcq v-if="exercise.type === 'mcq'" :exercise="exercise" />
|
||||||
<ExerciseSort v-else-if="exercise.type === 'sort'" :exercise="exercise" :lang="lang" />
|
<ExerciseSort
|
||||||
|
v-else-if="exercise.type === 'sort'"
|
||||||
|
:exercise="exercise"
|
||||||
|
:lang="lang"
|
||||||
|
/>
|
||||||
|
<ExerciseFill
|
||||||
|
v-else-if="exercise.type === 'fill'"
|
||||||
|
:exercise="exercise"
|
||||||
|
:lang="lang"
|
||||||
|
/>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -4,7 +4,10 @@ type Segment =
|
|||||||
| { type: "md"; content: string }
|
| { type: "md"; content: string }
|
||||||
| { type: "exercise"; exercise: Exercise }
|
| { type: "exercise"; exercise: Exercise }
|
||||||
|
|
||||||
export function parseExercises(content: string, exercises: Exercise[]): Segment[] {
|
export function parseExercises(
|
||||||
|
content: string,
|
||||||
|
exercises: Exercise[],
|
||||||
|
): Segment[] {
|
||||||
const exerciseMap = new Map(exercises.map((e) => [e.id, e]))
|
const exerciseMap = new Map(exercises.map((e) => [e.id, e]))
|
||||||
const segments: Segment[] = []
|
const segments: Segment[] = []
|
||||||
const regex = /\[\[exercise:(\d+)\]\]/g
|
const regex = /\[\[exercise:(\d+)\]\]/g
|
||||||
@@ -13,7 +16,10 @@ export function parseExercises(content: string, exercises: Exercise[]): Segment[
|
|||||||
|
|
||||||
while ((match = regex.exec(content)) !== null) {
|
while ((match = regex.exec(content)) !== null) {
|
||||||
if (match.index > lastIndex) {
|
if (match.index > lastIndex) {
|
||||||
segments.push({ type: "md", content: content.slice(lastIndex, match.index) })
|
segments.push({
|
||||||
|
type: "md",
|
||||||
|
content: content.slice(lastIndex, match.index),
|
||||||
|
})
|
||||||
}
|
}
|
||||||
const id = parseInt(match[1])
|
const id = parseInt(match[1])
|
||||||
const exercise = exerciseMap.get(id)
|
const exercise = exerciseMap.get(id)
|
||||||
|
|||||||
@@ -34,7 +34,11 @@
|
|||||||
:theme="isDark ? 'dark' : 'light'"
|
:theme="isDark ? 'dark' : 'light'"
|
||||||
:model-value="seg.content"
|
:model-value="seg.content"
|
||||||
/>
|
/>
|
||||||
<ExerciseWidget v-else :exercise="seg.exercise" :lang="tutorial.type" />
|
<ExerciseWidget
|
||||||
|
v-else
|
||||||
|
:exercise="seg.exercise"
|
||||||
|
:lang="tutorial.type"
|
||||||
|
/>
|
||||||
</template>
|
</template>
|
||||||
</n-card>
|
</n-card>
|
||||||
</n-gi>
|
</n-gi>
|
||||||
@@ -74,7 +78,11 @@
|
|||||||
:theme="isDark ? 'dark' : 'light'"
|
:theme="isDark ? 'dark' : 'light'"
|
||||||
:model-value="seg.content"
|
:model-value="seg.content"
|
||||||
/>
|
/>
|
||||||
<ExerciseWidget v-else :exercise="seg.exercise" :lang="tutorial.type" />
|
<ExerciseWidget
|
||||||
|
v-else
|
||||||
|
:exercise="seg.exercise"
|
||||||
|
:lang="tutorial.type"
|
||||||
|
/>
|
||||||
</template>
|
</template>
|
||||||
</n-tab-pane>
|
</n-tab-pane>
|
||||||
|
|
||||||
@@ -86,11 +94,21 @@
|
|||||||
<n-divider style="margin: 12px 0" />
|
<n-divider style="margin: 12px 0" />
|
||||||
|
|
||||||
<n-flex align="center" justify="space-between">
|
<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-button>
|
||||||
<n-text>{{ step }} / {{ titles.length }}</n-text>
|
<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-button>
|
||||||
</n-flex>
|
</n-flex>
|
||||||
@@ -106,8 +124,12 @@ import { getTutorial, getTutorials, getExercises } from "../api"
|
|||||||
import { parseExercises } from "./composables/useExerciseParse"
|
import { parseExercises } from "./composables/useExerciseParse"
|
||||||
import { useBreakpoints } from "shared/composables/breakpoints"
|
import { useBreakpoints } from "shared/composables/breakpoints"
|
||||||
|
|
||||||
const ExerciseWidget = defineAsyncComponent(() => import("./components/ExerciseWidget.vue"))
|
const ExerciseWidget = defineAsyncComponent(
|
||||||
const CodeEditor = defineAsyncComponent(() => import("shared/components/CodeEditor.vue"))
|
() => import("./components/ExerciseWidget.vue"),
|
||||||
|
)
|
||||||
|
const CodeEditor = defineAsyncComponent(
|
||||||
|
() => import("shared/components/CodeEditor.vue"),
|
||||||
|
)
|
||||||
|
|
||||||
const isDark = useDark()
|
const isDark = useDark()
|
||||||
const route = useRoute()
|
const route = useRoute()
|
||||||
@@ -119,7 +141,12 @@ const step = computed(() => {
|
|||||||
return parseInt(route.params.step[0])
|
return parseInt(route.params.step[0])
|
||||||
})
|
})
|
||||||
|
|
||||||
const tutorial = ref<Partial<Tutorial>>({ id: 0, title: "", content: "", code: "" })
|
const tutorial = ref<Partial<Tutorial>>({
|
||||||
|
id: 0,
|
||||||
|
title: "",
|
||||||
|
content: "",
|
||||||
|
code: "",
|
||||||
|
})
|
||||||
const titles = ref<{ id: number; title: string }[]>([])
|
const titles = ref<{ id: number; title: string }[]>([])
|
||||||
const exercises = ref<Exercise[]>([])
|
const exercises = ref<Exercise[]>([])
|
||||||
const activeTab = ref("content")
|
const activeTab = ref("content")
|
||||||
@@ -147,7 +174,10 @@ async function init() {
|
|||||||
titles.value = res1.data
|
titles.value = res1.data
|
||||||
if (titles.value.length === 0) return
|
if (titles.value.length === 0) return
|
||||||
const id = titles.value[step.value - 1].id
|
const id = titles.value[step.value - 1].id
|
||||||
const [res2, exs] = await Promise.allSettled([getTutorial(id), getExercises(id)])
|
const [res2, exs] = await Promise.allSettled([
|
||||||
|
getTutorial(id),
|
||||||
|
getExercises(id),
|
||||||
|
])
|
||||||
if (res2.status === "fulfilled") tutorial.value = res2.value.data
|
if (res2.status === "fulfilled") tutorial.value = res2.value.data
|
||||||
exercises.value = exs.status === "fulfilled" ? exs.value : []
|
exercises.value = exs.status === "fulfilled" ? exs.value : []
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -30,9 +30,11 @@ function toggleDark(event: MouseEvent) {
|
|||||||
isDark.value = !isDark.value
|
isDark.value = !isDark.value
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
document.startViewTransition(() => {
|
document
|
||||||
|
.startViewTransition(() => {
|
||||||
isDark.value = !isDark.value
|
isDark.value = !isDark.value
|
||||||
}).ready.then(() => {
|
})
|
||||||
|
.ready.then(() => {
|
||||||
document.documentElement.animate(
|
document.documentElement.animate(
|
||||||
{
|
{
|
||||||
clipPath: [
|
clipPath: [
|
||||||
@@ -46,7 +48,8 @@ function toggleDark(event: MouseEvent) {
|
|||||||
pseudoElement: "::view-transition-new(root)",
|
pseudoElement: "::view-transition-new(root)",
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}).catch(() => {})
|
})
|
||||||
|
.catch(() => {})
|
||||||
}
|
}
|
||||||
|
|
||||||
// 从 store 中获取屏幕模式状态
|
// 从 store 中获取屏幕模式状态
|
||||||
|
|||||||
@@ -73,7 +73,8 @@ const renderMermaid = async () => {
|
|||||||
renderSuccess.value = false
|
renderSuccess.value = false
|
||||||
|
|
||||||
const errorDiv = document.createElement("div")
|
const errorDiv = document.createElement("div")
|
||||||
errorDiv.style.cssText = "color: #ff4d4f; padding: 20px; text-align: center; border: 1px dashed #ff4d4f; border-radius: 4px;"
|
errorDiv.style.cssText =
|
||||||
|
"color: #ff4d4f; padding: 20px; text-align: center; border: 1px dashed #ff4d4f; border-radius: 4px;"
|
||||||
const titleP = document.createElement("p")
|
const titleP = document.createElement("p")
|
||||||
titleP.textContent = "Mermaid语法错误"
|
titleP.textContent = "Mermaid语法错误"
|
||||||
const detailP = document.createElement("p")
|
const detailP = document.createElement("p")
|
||||||
|
|||||||
@@ -590,10 +590,15 @@ export interface ExerciseSortData {
|
|||||||
lines: string[]
|
lines: string[]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ExerciseFillData {
|
||||||
|
question: string
|
||||||
|
code: string
|
||||||
|
}
|
||||||
|
|
||||||
export interface Exercise {
|
export interface Exercise {
|
||||||
id: number
|
id: number
|
||||||
type: 'mcq' | 'sort'
|
type: "mcq" | "sort" | "fill"
|
||||||
data: ExerciseMcqData | ExerciseSortData
|
data: ExerciseMcqData | ExerciseSortData | ExerciseFillData
|
||||||
order: number
|
order: number
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user