You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dianshang-qianduan/src/views/components/StepOneChoiceQuestionEditor...

128 lines
6.9 KiB
Vue

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<template>
<el-dialog
:model-value="visible"
:title="`${taskName || '实训任务'} · 第一步选择题`"
width="min(900px, 94vw)"
append-to-body
class="step-one-question-editor"
@update:model-value="close"
>
<div class="choice-editor-intro">
<div>
<span>STEP 01 · KNOWLEDGE CHECK</span>
<p>每次新增一题;填写题干、至少两个选项,并标记正确答案。</p>
</div>
<el-button type="primary" @click="addQuestion">新增题目</el-button>
</div>
<el-empty v-if="!draft.length" description="暂未设置选择题,可点击“新增题目”开始配置。" />
<div v-else class="choice-question-list">
<article v-for="(question, questionIndex) in draft" :key="question.id" class="choice-question-card">
<header>
<span class="choice-question-number">{{ String(questionIndex + 1).padStart(2, '0') }}</span>
<strong>单选题</strong>
<button type="button" class="choice-question-remove" @click="removeQuestion(questionIndex)">删除本题</button>
</header>
<el-input v-model="question.title" maxlength="300" show-word-limit placeholder="请输入题目内容" aria-label="题目内容" />
<div class="choice-options">
<div v-for="(option, optionIndex) in question.options" :key="option.key" class="choice-option-row">
<el-radio v-model="question.answer" :label="option.key" :aria-label="`设置 ${option.key} 为正确答案`">
正确答案
</el-radio>
<span class="choice-option-key">{{ option.key }}</span>
<el-input v-model="option.content" maxlength="200" show-word-limit :placeholder="`请输入 ${option.key} 选项`" />
<el-button text type="danger" :disabled="question.options.length <= 2" @click="removeOption(question, optionIndex)">删除</el-button>
</div>
</div>
<el-button v-if="question.options.length < optionKeys.length" text type="primary" @click="addOption(question)">+ 添加选项</el-button>
</article>
</div>
<template #footer>
<el-button @click="close(false)">取消</el-button>
<el-button type="primary" @click="save">保存选择题</el-button>
</template>
</el-dialog>
</template>
<script setup>
import { ref, watch } from "vue";
import { ElMessage } from "element-plus";
import { QUESTION_OPTION_KEYS, createStepOneChoiceQuestion, parseStepOneChoiceQuestions } from "@/utils/stepOneChoiceQuestions";
const props = defineProps({
visible: { type: Boolean, default: false },
modelValue: { type: Array, default: () => [] },
taskName: { type: String, default: "" },
});
const emit = defineEmits(["update:visible", "update:modelValue"]);
const draft = ref([]);
const optionKeys = QUESTION_OPTION_KEYS;
watch(() => props.visible, (opened) => {
if (opened) draft.value = structuredClone(parseStepOneChoiceQuestions(props.modelValue));
}, { immediate: true });
function addQuestion() {
draft.value.push(createStepOneChoiceQuestion(draft.value.length));
}
function addOption(question) {
const key = optionKeys[question.options.length];
if (key) question.options.push({ key, content: "" });
}
function removeOption(question, index) {
if (question.options.length <= 2) return;
const removed = question.options.splice(index, 1)[0];
question.options.forEach((option, optionIndex) => { option.key = optionKeys[optionIndex]; });
if (question.answer === removed.key) question.answer = question.options[0].key;
}
function removeQuestion(index) {
draft.value.splice(index, 1);
}
function normalizeAndValidate() {
return draft.value.map((question, questionIndex) => {
const title = String(question.title || "").trim();
const options = question.options
.map((option, optionIndex) => ({ key: optionKeys[optionIndex], content: String(option.content || "").trim() }))
.filter((option) => option.content);
if (!title) throw new Error(`请填写第 ${questionIndex + 1} 题题干`);
if (options.length < 2) throw new Error(`第 ${questionIndex + 1} 题至少需要两个选项`);
const answer = String(question.answer || "").trim().toUpperCase();
if (!options.some((option) => option.key === answer)) throw new Error(`请设置第 ${questionIndex + 1} 题的正确答案`);
return { id: question.id || `step-one-question-${questionIndex + 1}`, title, options, answer };
});
}
function save() {
try {
emit("update:modelValue", normalizeAndValidate());
emit("update:visible", false);
ElMessage.success("选择题已保存到当前任务配置,点击“保存配置”后对学生生效");
} catch (error) {
ElMessage.warning(error?.message || "请选择正确答案");
}
}
function close(value = false) {
emit("update:visible", value);
}
</script>
<style lang="scss">
.step-one-question-editor {
.el-dialog { overflow: hidden; border: 1px solid rgba(64, 211, 255, .6); background: #061a2a; box-shadow: 0 24px 80px rgba(0, 0, 0, .46), inset 0 1px rgba(200, 247, 255, .14); }
.el-dialog__header { margin: 0; padding: 20px 24px; border-bottom: 1px solid rgba(72, 201, 255, .25); }
.el-dialog__title { color: #e9fbff; font-size: 18px; font-weight: 800; }
.el-dialog__body { max-height: min(66vh, 720px); overflow-y: auto; padding: 20px 24px; }
.el-dialog__footer { padding: 16px 24px; border-top: 1px solid rgba(72, 201, 255, .25); }
}
.choice-editor-intro { display:flex; align-items:center; justify-content:space-between; gap:20px; margin-bottom:18px; padding:15px 17px; border-left:3px solid #3be2ff; background:linear-gradient(90deg,rgba(18,151,211,.2),transparent); }
.choice-editor-intro span { color:#59dcff; font-size:12px; font-weight:800; letter-spacing:.12em; }.choice-editor-intro p { margin:5px 0 0; color:#b6d9e7; line-height:1.6; }
.choice-question-list { display:grid; gap:16px; }.choice-question-card { padding:16px; border:1px solid rgba(82,204,250,.32); border-radius:10px; background:rgba(4,37,57,.7); }.choice-question-card header { display:flex; align-items:center; gap:9px; margin-bottom:13px; color:#dff8ff; }.choice-question-number { display:grid; place-items:center; width:28px; height:28px; border-radius:50%; color:#032335; background:#60e6e1; font-size:12px; font-weight:900; }.choice-question-remove { margin-left:auto; color:#ff92a5; cursor:pointer; border:0; background:transparent; }.choice-options { display:grid; gap:9px; margin:13px 0 7px; }.choice-option-row { display:grid; grid-template-columns:94px 28px minmax(0,1fr) 52px; align-items:center; gap:8px; }.choice-option-row .el-radio { margin-right:0; color:#bde9f8; }.choice-option-key { display:grid; place-items:center; width:25px; height:25px; color:#64e7ff; font-weight:900; border:1px solid rgba(91,222,255,.5); border-radius:5px; }
@media (max-width:640px) { .choice-editor-intro { align-items:flex-start; flex-direction:column; }.choice-option-row { grid-template-columns:78px 25px minmax(0,1fr); }.choice-option-row > .el-button { grid-column:3; justify-self:end; } }
</style>