fix
This commit is contained in:
@@ -58,8 +58,10 @@ provide("hljs", hljsInstance)
|
|||||||
:date-locale="dateZhCN"
|
:date-locale="dateZhCN"
|
||||||
:hljs="hljsInstance"
|
:hljs="hljsInstance"
|
||||||
>
|
>
|
||||||
|
<n-dialog-provider>
|
||||||
<n-message-provider>
|
<n-message-provider>
|
||||||
<router-view></router-view>
|
<router-view></router-view>
|
||||||
</n-message-provider>
|
</n-message-provider>
|
||||||
|
</n-dialog-provider>
|
||||||
</n-config-provider>
|
</n-config-provider>
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -21,8 +21,7 @@ const mcqQuestion = ref("")
|
|||||||
const mcqOptions = ref(["", ""])
|
const mcqOptions = ref(["", ""])
|
||||||
const mcqAnswer = ref(0)
|
const mcqAnswer = ref(0)
|
||||||
|
|
||||||
const sortQuestion = ref("")
|
const sortCode = ref("")
|
||||||
const sortLines = ref(["", ""])
|
|
||||||
|
|
||||||
async function load() {
|
async function load() {
|
||||||
exercises.value = await getAdminExercises(props.tutorialId)
|
exercises.value = await getAdminExercises(props.tutorialId)
|
||||||
@@ -37,8 +36,7 @@ function openCreate() {
|
|||||||
mcqQuestion.value = ""
|
mcqQuestion.value = ""
|
||||||
mcqOptions.value = ["", ""]
|
mcqOptions.value = ["", ""]
|
||||||
mcqAnswer.value = 0
|
mcqAnswer.value = 0
|
||||||
sortQuestion.value = ""
|
sortCode.value = ""
|
||||||
sortLines.value = ["", ""]
|
|
||||||
showForm.value = true
|
showForm.value = true
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,8 +51,7 @@ function openEdit(ex: Exercise) {
|
|||||||
mcqAnswer.value = d.answer
|
mcqAnswer.value = d.answer
|
||||||
} else {
|
} else {
|
||||||
const d = ex.data as ExerciseSortData
|
const d = ex.data as ExerciseSortData
|
||||||
sortQuestion.value = d.question
|
sortCode.value = d.lines.join("\n")
|
||||||
sortLines.value = [...d.lines]
|
|
||||||
}
|
}
|
||||||
showForm.value = true
|
showForm.value = true
|
||||||
}
|
}
|
||||||
@@ -63,7 +60,10 @@ async function save() {
|
|||||||
const data =
|
const data =
|
||||||
formType.value === "mcq"
|
formType.value === "mcq"
|
||||||
? { question: mcqQuestion.value, options: mcqOptions.value, answer: mcqAnswer.value }
|
? { question: mcqQuestion.value, options: mcqOptions.value, answer: mcqAnswer.value }
|
||||||
: { question: sortQuestion.value, lines: sortLines.value }
|
: {
|
||||||
|
question: "将下列代码行排列为正确顺序",
|
||||||
|
lines: sortCode.value.split("\n").filter((l) => l.trim() !== ""),
|
||||||
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
if (editingId.value) {
|
if (editingId.value) {
|
||||||
@@ -169,7 +169,7 @@ function typeName(type: string) {
|
|||||||
<n-radio
|
<n-radio
|
||||||
:value="i"
|
:value="i"
|
||||||
:checked="mcqAnswer === i"
|
:checked="mcqAnswer === i"
|
||||||
@update:checked="if ($event) mcqAnswer = i"
|
@update:checked="$event && (mcqAnswer = i)"
|
||||||
/>
|
/>
|
||||||
<n-input
|
<n-input
|
||||||
v-model:value="mcqOptions[i]"
|
v-model:value="mcqOptions[i]"
|
||||||
@@ -190,27 +190,14 @@ function typeName(type: string) {
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<template v-else>
|
<template v-else>
|
||||||
<n-form-item label="题目">
|
<n-form-item label="正确代码(每行将自动成为一道排序项)">
|
||||||
<n-input v-model:value="sortQuestion" type="textarea" :rows="2" />
|
|
||||||
</n-form-item>
|
|
||||||
<n-form-item label="代码行(按正确顺序输入)">
|
|
||||||
<n-space vertical style="width: 100%">
|
|
||||||
<n-flex v-for="(line, i) in sortLines" :key="i" align="center" :size="8">
|
|
||||||
<n-input
|
<n-input
|
||||||
v-model:value="sortLines[i]"
|
v-model:value="sortCode"
|
||||||
:placeholder="`第 ${i + 1} 行`"
|
type="textarea"
|
||||||
style="flex: 1; font-family: monospace"
|
:rows="10"
|
||||||
|
placeholder="在此粘贴正确的代码,保存后将自动按行拆分并乱序"
|
||||||
|
style="font-family: monospace"
|
||||||
/>
|
/>
|
||||||
<n-button
|
|
||||||
size="small"
|
|
||||||
:disabled="sortLines.length <= 2"
|
|
||||||
@click="sortLines.splice(i, 1)"
|
|
||||||
>
|
|
||||||
✕
|
|
||||||
</n-button>
|
|
||||||
</n-flex>
|
|
||||||
<n-button size="small" @click="sortLines.push('')">+ 添加行</n-button>
|
|
||||||
</n-space>
|
|
||||||
</n-form-item>
|
</n-form-item>
|
||||||
</template>
|
</template>
|
||||||
</n-form>
|
</n-form>
|
||||||
|
|||||||
@@ -5,26 +5,35 @@ const props = defineProps<{ exercise: Exercise }>()
|
|||||||
const data = computed(() => props.exercise.data as ExerciseMcqData)
|
const data = computed(() => props.exercise.data as ExerciseMcqData)
|
||||||
|
|
||||||
const selected = ref<number | null>(null)
|
const selected = ref<number | null>(null)
|
||||||
const submitted = ref(false)
|
const correct = ref(false)
|
||||||
|
const wrong = ref(false)
|
||||||
|
|
||||||
function select(idx: number) {
|
function select(idx: number) {
|
||||||
if (!submitted.value) selected.value = idx
|
if (correct.value) return
|
||||||
|
selected.value = idx
|
||||||
|
wrong.value = false
|
||||||
}
|
}
|
||||||
|
|
||||||
function submit() {
|
function submit() {
|
||||||
if (selected.value === null) return
|
if (selected.value === null || correct.value) return
|
||||||
submitted.value = true
|
if (selected.value === data.value.answer) {
|
||||||
|
correct.value = true
|
||||||
|
wrong.value = false
|
||||||
|
} else {
|
||||||
|
wrong.value = true
|
||||||
|
selected.value = null
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function reset() {
|
function reset() {
|
||||||
selected.value = null
|
selected.value = null
|
||||||
submitted.value = false
|
correct.value = false
|
||||||
|
wrong.value = false
|
||||||
}
|
}
|
||||||
|
|
||||||
function optionType(idx: number): "default" | "success" | "error" {
|
function optionType(idx: number): "default" | "primary" | "success" {
|
||||||
if (!submitted.value) return "default"
|
if (correct.value && idx === data.value.answer) return "success"
|
||||||
if (idx === data.value.answer) return "success"
|
if (idx === selected.value) return "primary"
|
||||||
if (idx === selected.value) return "error"
|
|
||||||
return "default"
|
return "default"
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
@@ -46,6 +55,7 @@ function optionType(idx: number): "default" | "success" | "error" {
|
|||||||
:type="optionType(idx)"
|
:type="optionType(idx)"
|
||||||
:secondary="optionType(idx) !== 'default'"
|
:secondary="optionType(idx) !== 'default'"
|
||||||
:tertiary="optionType(idx) === 'default'"
|
:tertiary="optionType(idx) === 'default'"
|
||||||
|
: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)"
|
||||||
>
|
>
|
||||||
@@ -57,9 +67,9 @@ function optionType(idx: number): "default" | "success" | "error" {
|
|||||||
</n-space>
|
</n-space>
|
||||||
|
|
||||||
<n-alert
|
<n-alert
|
||||||
v-if="submitted"
|
v-if="correct || wrong"
|
||||||
:type="selected === data.answer ? 'success' : 'error'"
|
:type="correct ? 'success' : 'error'"
|
||||||
:title="selected === data.answer ? '正确!' : '不对,请看正确答案(绿色)'"
|
:title="correct ? '正确!' : '选择有误,请重试'"
|
||||||
style="margin-top: 12px"
|
style="margin-top: 12px"
|
||||||
/>
|
/>
|
||||||
|
|
||||||
@@ -67,7 +77,7 @@ function optionType(idx: number): "default" | "success" | "error" {
|
|||||||
<n-button
|
<n-button
|
||||||
type="primary"
|
type="primary"
|
||||||
size="small"
|
size="small"
|
||||||
:disabled="selected === null || submitted"
|
:disabled="selected === null || correct"
|
||||||
@click="submit"
|
@click="submit"
|
||||||
>
|
>
|
||||||
提交
|
提交
|
||||||
|
|||||||
@@ -1,7 +1,13 @@
|
|||||||
<script setup lang="ts">
|
<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, ExerciseSortData } from "utils/types"
|
import { Exercise, ExerciseSortData } from "utils/types"
|
||||||
|
|
||||||
const props = defineProps<{ exercise: Exercise }>()
|
hljs.registerLanguage("python", python)
|
||||||
|
hljs.registerLanguage("c", c)
|
||||||
|
|
||||||
|
const props = defineProps<{ exercise: Exercise; lang?: string }>()
|
||||||
const data = computed(() => props.exercise.data as ExerciseSortData)
|
const data = computed(() => props.exercise.data as ExerciseSortData)
|
||||||
|
|
||||||
type LineItem = { originalIdx: number; text: string }
|
type LineItem = { originalIdx: number; text: string }
|
||||||
@@ -40,6 +46,7 @@ function onDrop(targetIdx: number) {
|
|||||||
newLines.splice(targetIdx, 0, moved)
|
newLines.splice(targetIdx, 0, moved)
|
||||||
lines.value = newLines
|
lines.value = newLines
|
||||||
dragIdx.value = null
|
dragIdx.value = null
|
||||||
|
submitted.value = false
|
||||||
}
|
}
|
||||||
|
|
||||||
function lineStatus(idx: number): "correct" | "wrong" | "default" {
|
function lineStatus(idx: number): "correct" | "wrong" | "default" {
|
||||||
@@ -56,6 +63,33 @@ function submit() {
|
|||||||
function reset() {
|
function reset() {
|
||||||
init()
|
init()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function escapeHtml(text: string): string {
|
||||||
|
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">")
|
||||||
|
}
|
||||||
|
|
||||||
|
const lineHtmlMap = computed<Record<number, string>>(() => {
|
||||||
|
const rawLines = data.value.lines
|
||||||
|
const map: Record<number, string> = {}
|
||||||
|
const lang = props.lang === "python" ? "python" : props.lang === "c" ? "c" : null
|
||||||
|
|
||||||
|
if (lang) {
|
||||||
|
try {
|
||||||
|
const result = hljs.highlight(rawLines.join("\n"), { language: lang }).value
|
||||||
|
result.split("\n").forEach((html, i) => {
|
||||||
|
map[i] = html
|
||||||
|
})
|
||||||
|
} catch {
|
||||||
|
// fall through
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
rawLines.forEach((text, i) => {
|
||||||
|
if (map[i] === undefined) map[i] = escapeHtml(text)
|
||||||
|
})
|
||||||
|
|
||||||
|
return map
|
||||||
|
})
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -100,7 +134,7 @@ function reset() {
|
|||||||
@drop="onDrop(idx)"
|
@drop="onDrop(idx)"
|
||||||
>
|
>
|
||||||
<span style="color: #bbb; cursor: grab">⠿</span>
|
<span style="color: #bbb; cursor: grab">⠿</span>
|
||||||
<span>{{ line.text }}</span>
|
<span v-html="lineHtmlMap[line.originalIdx]" style="white-space: pre" />
|
||||||
</div>
|
</div>
|
||||||
</n-space>
|
</n-space>
|
||||||
|
|
||||||
@@ -112,10 +146,40 @@ function reset() {
|
|||||||
/>
|
/>
|
||||||
|
|
||||||
<n-space style="margin-top: 12px" :size="8">
|
<n-space style="margin-top: 12px" :size="8">
|
||||||
<n-button type="info" size="small" :disabled="submitted" @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>
|
||||||
</n-space>
|
</n-space>
|
||||||
</n-card>
|
</n-card>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
<style>
|
||||||
|
.hljs-keyword,
|
||||||
|
.hljs-operator,
|
||||||
|
.hljs-selector-tag { color: #d73a49; }
|
||||||
|
.hljs-string,
|
||||||
|
.hljs-regexp,
|
||||||
|
.hljs-template-literal { color: #032f62; }
|
||||||
|
.hljs-comment,
|
||||||
|
.hljs-quote { color: #6a737d; font-style: italic; }
|
||||||
|
.hljs-number,
|
||||||
|
.hljs-literal { color: #005cc5; }
|
||||||
|
.hljs-built_in,
|
||||||
|
.hljs-title.function_,
|
||||||
|
.hljs-class .hljs-title { color: #6f42c1; }
|
||||||
|
|
||||||
|
.dark .hljs-keyword,
|
||||||
|
.dark .hljs-operator,
|
||||||
|
.dark .hljs-selector-tag { color: #c678dd; }
|
||||||
|
.dark .hljs-string,
|
||||||
|
.dark .hljs-regexp,
|
||||||
|
.dark .hljs-template-literal { color: #98c379; }
|
||||||
|
.dark .hljs-comment,
|
||||||
|
.dark .hljs-quote { color: #7f848e; font-style: italic; }
|
||||||
|
.dark .hljs-number,
|
||||||
|
.dark .hljs-literal { color: #e5c07b; }
|
||||||
|
.dark .hljs-built_in,
|
||||||
|
.dark .hljs-title.function_,
|
||||||
|
.dark .hljs-class .hljs-title { color: #61afef; }
|
||||||
|
</style>
|
||||||
|
|||||||
@@ -4,10 +4,10 @@ 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"))
|
||||||
|
|
||||||
defineProps<{ exercise: Exercise }>()
|
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" />
|
<ExerciseSort v-else-if="exercise.type === 'sort'" :exercise="exercise" :lang="lang" />
|
||||||
</template>
|
</template>
|
||||||
|
|||||||
@@ -34,7 +34,7 @@
|
|||||||
:theme="isDark ? 'dark' : 'light'"
|
:theme="isDark ? 'dark' : 'light'"
|
||||||
:model-value="seg.content"
|
:model-value="seg.content"
|
||||||
/>
|
/>
|
||||||
<ExerciseWidget v-else :exercise="seg.exercise" />
|
<ExerciseWidget v-else :exercise="seg.exercise" :lang="tutorial.type" />
|
||||||
</template>
|
</template>
|
||||||
</n-card>
|
</n-card>
|
||||||
</n-gi>
|
</n-gi>
|
||||||
@@ -74,7 +74,7 @@
|
|||||||
:theme="isDark ? 'dark' : 'light'"
|
:theme="isDark ? 'dark' : 'light'"
|
||||||
:model-value="seg.content"
|
:model-value="seg.content"
|
||||||
/>
|
/>
|
||||||
<ExerciseWidget v-else :exercise="seg.exercise" />
|
<ExerciseWidget v-else :exercise="seg.exercise" :lang="tutorial.type" />
|
||||||
</template>
|
</template>
|
||||||
</n-tab-pane>
|
</n-tab-pane>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user