-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathKeyPractice.vue
255 lines (223 loc) · 5.85 KB
/
KeyPractice.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<script setup lang="ts">
import Keyboard from "../components/Keyboard.vue";
import PhonemeComponent from "../components/PhonemeList.vue";
import TypeSummary from "../components/TypeSummary.vue";
import MenuList from "../components/MenuList.vue";
import { ref, computed } from "vue";
import { useRoute, useRouter } from "vue-router";
import { useStore } from "../store";
import { courseNames, courses, courseTitles } from "../utils/courseConfig.gen";
import { keyboardLayoutWithPunctuation } from "../utils/keyboard";
import {
allMenuChangeKeys,
createWholeSeq,
currentMenuChangeKeys,
passRequire,
punctuationMapper,
resultMap,
} from "../utils/keyPosition";
import type { Phoneme, KeyPracticeProps } from "../utils/phoneme";
const props = defineProps<KeyPracticeProps>();
const store = useStore();
const route = useRoute();
const router = useRouter();
const index = ref(0);
const menuIndex = ref(0);
const phonemeSeq = ref<Phoneme[]>([]);
const summary = ref({
inputCharNum: 0,
correctCharNum: 0,
wrongCharList: {} as Record<string, number | undefined>, // TODO: show wrong char list summary
startTime: Date.now(),
});
const extraInfos = ref<string[]>([]);
const summaryResult = computed(() => {
const { startTime, inputCharNum, correctCharNum } = summary.value;
const now = Date.now();
const timeDiffer = now - startTime;
const speed = timeDiffer && (inputCharNum / timeDiffer) * 1000 * 60;
const accuracy = inputCharNum && correctCharNum / inputCharNum;
return {
speed,
accuracy,
};
});
init(Number(route.query.course ?? 0));
function init(_menuIndex: number) {
router.push({ query: { ...route.query, course: _menuIndex } });
index.value = 0;
menuIndex.value = _menuIndex;
phonemeSeq.value = createWholeSeq(
courses[_menuIndex],
store.mode(),
menuIndex.value,
props.mode
);
summary.value = {
inputCharNum: 0,
correctCharNum: 0,
wrongCharList: {},
startTime: Date.now(),
};
}
function onFinish() {
const { accuracy } = summaryResult.value;
const pass = accuracy >= passRequire;
extraInfos.value = pass ? [] : ["失败率过高, 请重新完成课程"];
const newMenuIndex = pass
? (menuIndex.value + 1) % courseNames.length
: menuIndex.value;
init(newMenuIndex);
}
function updatePhonemeStatus(
index: number,
option: { type: "curt"; key: string } | { type: "next" }
): boolean {
if (index < 0 || index >= phonemeSeq.value.length) {
return false;
}
const phoneme = phonemeSeq.value[index];
console.log("opt", option);
switch (option.type) {
case "curt": {
const _key = option.key;
const key = _key in punctuationMapper ? punctuationMapper[_key] : _key;
const result = Boolean(key === phoneme.char);
const status = resultMap[`${option.type}_${Number(result) as 0 | 1}`];
phoneme.status = status;
return result;
}
case "next": {
const status = resultMap[option.type];
phoneme.status = status;
console.log("stas", status, phoneme);
return true;
}
}
}
function onSeq([key]: [string?, string?]) {
// TODO: support backspace
if (!key) return false;
extraInfos.value = [];
if (key === "Backspace" || allMenuChangeKeys.includes(key as any)) {
return false;
}
const valid = updateSeq({ key, index: index.value });
if (valid) {
summary.value.correctCharNum++;
} else {
const prev = summary.value.wrongCharList[key];
summary.value.wrongCharList[key] = (prev ?? 0) + 1;
}
summary.value.inputCharNum++;
return valid;
}
function updateSeq(option: { key: string; index: number }): boolean {
updatePhonemeStatus(option.index + 1, { type: "next" });
const result = updatePhonemeStatus(option.index, {
type: "curt",
key: option.key,
});
index.value++;
document.querySelector(".phoneme.activate")?.scrollIntoView({
inline: "nearest",
block: "center",
behavior: "smooth",
});
if (index.value >= phonemeSeq.value.length) {
onFinish();
}
return result;
}
function onMenuChange(i: number) {
if (i === menuIndex.value) return;
init(i);
}
const hints = computed(() => {
const key = phonemeSeq.value[index.value].char;
return [key];
});
</script>
<template>
<div class="home-page">
<div class="single-menu">
<menu-list
enable-arrow
:menu-change-keys="currentMenuChangeKeys"
:items="courseNames"
:index="menuIndex"
@menu-change="onMenuChange"
/>
</div>
<div class="input-area"></div>
<div class="phoneme-list">
<PhonemeComponent
:phoneme-seq="phonemeSeq"
:title="courseTitles[menuIndex]"
/>
</div>
<div class="single-keyboard">
<Keyboard
:valid-seq="onSeq"
:hints="hints"
mode="singleKey"
:key-board-layout="keyboardLayoutWithPunctuation"
/>
</div>
<div class="summary">
<TypeSummary
hide-avgpress
:avgpress="0"
:extra-infos="extraInfos"
:speed="summaryResult.speed"
:accuracy="summaryResult.accuracy"
/>
</div>
</div>
</template>
<style lang="less">
@import "../styles/color.less";
@import "../styles/var.less";
.home-page {
display: flex;
flex-direction: column;
align-items: center;
height: 100%;
.single-menu {
position: absolute;
top: 0;
left: 100px;
}
.input-area {
margin-bottom: 32px;
height: 160px;
display: flex;
align-items: center;
@media (max-width: 576px) {
margin-top: 30vh;
}
}
.summary {
position: absolute;
right: var(--app-padding);
bottom: var(--app-padding);
@media (max-width: 576px) {
top: 36px;
}
}
.phoneme-list {
position: absolute;
top: var(--app-padding);
left: 200px;
@media (max-width: 576px) {
top: 120px;
}
}
@media (max-width: 576px) {
.single-keyboard {
position: absolute;
bottom: 1em;
}
}
}
</style>