-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathKeyboard.vue
172 lines (147 loc) · 4.43 KB
/
Keyboard.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
<script setup lang="ts">
import { computed } from "vue";
import { storeToRefs } from "pinia";
import { ref, onActivated, onDeactivated } from "vue";
import { useStore } from "../store";
import { mapConfigToLayout } from "../utils/keyboard";
const store = useStore();
const settings = storeToRefs(store).settings;
const props = defineProps<{
hints?: string[];
validSeq?: (_: [string?, string?]) => boolean;
}>();
const pressingKeys = ref(new Set<string>());
const keySeq = ref<string[]>([]);
const scale = ref(1);
const onPressKey = (e: KeyboardEvent) => pressKey(e.key);
const onReleaseKey = (e: KeyboardEvent) => releaseKey(e.key);
onActivated(() => {
document.addEventListener("keydown", onPressKey);
document.addEventListener("keyup", onReleaseKey);
window.addEventListener("resize", resizeKeyboard);
resizeKeyboard();
});
onDeactivated(() => {
document.removeEventListener("keydown", onPressKey);
document.removeEventListener("keyup", onReleaseKey);
window.removeEventListener("resize", resizeKeyboard);
});
function resizeKeyboard() {
const screenWidth = document.getElementById("app")?.clientWidth ?? 920;
const keyboardWidth = document.getElementById("keyboard")?.clientWidth ?? 920;
scale.value = screenWidth < 576 ? (screenWidth / keyboardWidth) * 1.1 : 1;
}
function pressKey(key: string) {
pressingKeys.value.add(key);
navigator.vibrate(100);
}
function send() {
if (props.validSeq?.([keySeq.value.at(0), keySeq.value.at(1)])) {
keySeq.value = [];
}
}
function releaseKey(key: string, shouldSend = true) {
pressingKeys.value.delete(key);
if (key === "Backspace") {
keySeq.value.pop();
return send();
}
if (!shouldSend || !store.mode().groupByKey.has(key as Char)) {
return;
}
if (keySeq.value.length <= 2) {
keySeq.value.push(key);
}
if (keySeq.value.length > 2) {
if (settings.value.enableAutoClear) {
keySeq.value = [key];
} else {
keySeq.value.pop();
}
}
send();
}
const keyLayout = computed(() => {
return mapConfigToLayout(store.mode());
});
function keyItemClass(key: string) {
let classNames = [];
if (pressingKeys.value.has(key)) {
classNames.push("pressing");
}
if (props.hints?.includes(key) && settings.value.enableKeyHint) {
classNames.push("hint-key");
}
return classNames.join(" ");
}
</script>
<template>
<div class="keyboard" :style="`transform: scale(${scale})`" id="keyboard">
<div v-for="(line, li) in keyLayout" :key="li" class="key-row">
<div
v-for="(keyItem, ki) in line"
:key="ki"
class="key-item"
:class="keyItemClass(keyItem.main)"
@mousedown="pressKey(keyItem.main)"
@touchstart.stop.prevent="pressKey(keyItem.main)"
@mouseup="releaseKey(keyItem.main)"
@mouseout="releaseKey(keyItem.main, false)"
@touchend.stop.prevent="releaseKey(keyItem.main)"
>
<div class="main-content">
<div class="main-key">
{{ keyItem.main.toUpperCase() }}
</div>
<div v-if="keyItem.lead.length > 0" class="lead-key">
{{ keyItem.lead }}
</div>
</div>
<div class="bottom-content">
<div class="follow-key">
{{ keyItem.follow }}
</div>
</div>
</div>
<div
v-if="li === keyLayout.length - 1"
class="key-item backspace"
:class="keyItemClass('Backspace')"
@mousedown="pressKey('Backspace')"
@touchstart.stop.prevent="pressKey('Backspace')"
@mouseup="releaseKey('Backspace')"
@mouseout="releaseKey('Backspace', false)"
@touchend.stop.prevent="releaseKey('Backspace')"
>
<svg width="16" height="16" viewBox="0 0 48 48" fill="none">
<path
d="M14 11L4 24L14 37H44V11H14Z"
fill="none"
stroke="#333"
stroke-width="3"
stroke-linecap="round"
stroke-linejoin="round"
/>
<path
d="M21 19L31 29"
stroke="#333"
stroke-width="3"
stroke-linecap="round"
stroke-linejoin="round"
/>
<path
d="M31 19L21 29"
stroke="#333"
stroke-width="3"
stroke-linecap="round"
stroke-linejoin="round"
/>
</svg>
</div>
</div>
</div>
</template>
<style lang="less">
@import "../styles/color.less";
@import "../styles/keyboard.less";
</style>