|
| 1 | +<template> |
| 2 | + <div class="text-5xl text-center mb-20 mt-10"> |
| 3 | + <div class="text-fuchsia-500 dark:text-gray-50"> |
| 4 | + {{ currentStatement?.chinese }} |
| 5 | + </div> |
| 6 | + <div class="code-box"> |
| 7 | + <template v-for="i in lineNum" :key="i"> |
| 8 | + <div |
| 9 | + :class="[ |
| 10 | + 'code-item', |
| 11 | + 'border-b-2', |
| 12 | + 'border-b-solid', |
| 13 | + 'border-b-gray-300 dark:border-b-gray-500', |
| 14 | + i - 1 === activeInputIndex && focusing ? 'active' : '', |
| 15 | + 'dark:text-indigo-500 text-[rgba(32,32,32,0.6)]', |
| 16 | + ]" |
| 17 | + > |
| 18 | + {{ words[i - 1] }} |
| 19 | + </div> |
| 20 | + </template> |
| 21 | + <input |
| 22 | + class="code-input" |
| 23 | + type="text" |
| 24 | + v-model="inputValue" |
| 25 | + @keydown="handleKeyDown" |
| 26 | + @focus="handleInputFocus" |
| 27 | + @blur="handleBlur" |
| 28 | + autoFocus |
| 29 | + /> |
| 30 | + </div> |
| 31 | + </div> |
| 32 | +</template> |
| 33 | + |
| 34 | +<script setup lang="ts"> |
| 35 | +import { useCoursesStore } from "~/store/courses"; |
| 36 | +
|
| 37 | +const emit = defineEmits(["bingo"]); |
| 38 | +
|
| 39 | +const { currentStatement, checkCorrect } = useCoursesStore(); |
| 40 | +
|
| 41 | +const lineNum = ref(1); |
| 42 | +const focusing = ref(true); |
| 43 | +const inputValue = ref(""); |
| 44 | +const words = ref<string[]>([]); |
| 45 | +
|
| 46 | +const activeInputIndex = computed(() => { |
| 47 | + return Math.min(words.value.length - 1, lineNum.value - 1); |
| 48 | +}); |
| 49 | +
|
| 50 | +watchEffect(() => { |
| 51 | + lineNum.value = currentStatement?.english.split(" ").length || 1; |
| 52 | +}); |
| 53 | +
|
| 54 | +watchEffect(() => { |
| 55 | + words.value = inputValue.value.trimStart().split(" "); |
| 56 | +}); |
| 57 | +
|
| 58 | +function handleKeyDown(e: KeyboardEvent) { |
| 59 | + if (e.code === "Enter") { |
| 60 | + if (checkCorrect(inputValue.value.trim())) { |
| 61 | + emit("bingo"); |
| 62 | + } |
| 63 | + inputValue.value = ""; |
| 64 | + } |
| 65 | +} |
| 66 | +
|
| 67 | +function handleInputFocus() { |
| 68 | + focusing.value = true; |
| 69 | +} |
| 70 | +
|
| 71 | +function handleBlur() { |
| 72 | + focusing.value = false; |
| 73 | +} |
| 74 | +</script> |
| 75 | + |
| 76 | +<style scoped> |
| 77 | +.code-box { |
| 78 | + height: 10vh; |
| 79 | + display: flex; |
| 80 | + flex-wrap: wrap; |
| 81 | + justify-content: center; |
| 82 | + max-width: 80vw; |
| 83 | + position: relative; |
| 84 | + margin-top: 8px; |
| 85 | +} |
| 86 | +
|
| 87 | +.code-box .code-item { |
| 88 | + min-width: 10vw; |
| 89 | + min-height: 6vh; |
| 90 | + text-align: center; |
| 91 | + font-size: 4vw; |
| 92 | + transition: border 0.3s; |
| 93 | + box-sizing: border-box; |
| 94 | + margin-right: 8px; |
| 95 | + display: flex; |
| 96 | + justify-content: center; |
| 97 | + align-items: flex-end; |
| 98 | +} |
| 99 | +
|
| 100 | +.code-box .code-input { |
| 101 | + position: absolute; |
| 102 | + width: 100%; |
| 103 | +
|
| 104 | + height: 100%; |
| 105 | + opacity: 0; |
| 106 | +} |
| 107 | +
|
| 108 | +.active { |
| 109 | + border-bottom: 3px solid #1e80ff !important; |
| 110 | +} |
| 111 | +</style> |
0 commit comments