123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- <script setup lang="ts">
- import { store } from "@/store/store";
- import { onMounted, onUnmounted } from "vue";
- import QuestionBody from "./QuestionBody.vue";
- let isShowAnswer = $ref(false);
- function toggleShowAnswer() {
- isShowAnswer = !isShowAnswer;
- }
- const examQuestion = $computed(() => store.exam.currentQuestion);
- onMounted(() => {
- window.addEventListener("keyup", keyup);
- });
- onUnmounted(() => {
- window.removeEventListener("keyup", keyup);
- });
- function keyup(e: KeyboardEvent) {
- const { tagName = "" } = document?.activeElement || {};
- if (
- examQuestion.questionType === "TRUE_OR_FALSE" &&
- ["BODY", "A", "BUTTON", "DIV"].includes(tagName)
- ) {
- if ("KeyY" === e.code) {
- answerQuestion("true");
- }
- if ("KeyN" === e.code) {
- answerQuestion("false");
- }
- }
- }
- function answerQuestion(studentAnswer: "true" | "false") {
- store.updateExamQuestion({
- order: examQuestion.order,
- studentAnswer,
- });
- }
- const optionWithNames: Record<string, string> = {
- true: "正确",
- false: "错误",
- "": "",
- };
- </script>
- <template>
- <div class="question-view">
- <question-body :questionBody="examQuestion.body" />
- <div class="ops">
- <div class="stu-answer">
- {{ optionWithNames[examQuestion.studentAnswer ?? ""] }}
- </div>
- <div class="score">({{ examQuestion.questionScore }}分)</div>
- </div>
- <div
- :class="[
- examQuestion.studentAnswer === 'true' && 'row-selected',
- 'option',
- ]"
- class="tw-flex tw-items-center"
- @click="() => answerQuestion('true')"
- >
- <input
- type="radio"
- name="question"
- value="true"
- :checked="examQuestion.studentAnswer === 'true'"
- />
- <span class="question-options">正确</span>
- </div>
- <div
- :class="[
- examQuestion.studentAnswer === 'false' && 'row-selected',
- 'option',
- ]"
- class="tw-flex tw-items-center"
- @click="() => answerQuestion('false')"
- >
- <input
- type="radio"
- name="question"
- value="false"
- :checked="examQuestion.studentAnswer === 'false'"
- />
- <div class="question-options">错误</div>
- </div>
- <div class="reset">
- <span v-if="store.examShouldShowAnswer">
- <n-button type="success" @click="toggleShowAnswer">
- {{ isShowAnswer ? "隐藏" : "显示" }}答案
- </n-button>
- </span>
- <div v-if="isShowAnswer" class="tw-mt-2">
- 正确答案:
- <div>
- {{ optionWithNames[examQuestion.rightAnswer + ""] }}
- </div>
- </div>
- </div>
- </div>
- </template>
- <style scoped>
- .question-view {
- display: grid;
- grid-row-gap: 10px;
- }
- .ops {
- display: flex;
- align-items: flex-end;
- }
- .stu-answer {
- width: 100px;
- border-bottom: 1px solid black;
- text-align: center;
- height: 20px;
- }
- .option {
- display: flex;
- cursor: pointer;
- border-radius: 5px;
- height: 26px;
- }
- .option:hover {
- background-color: aliceblue;
- }
- .row-selected {
- background-color: aliceblue;
- width: 100%;
- }
- .question-options {
- text-align: left;
- margin-left: 10px;
- }
- </style>
|