-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathCustomShortcut.vue
237 lines (227 loc) · 8.36 KB
/
CustomShortcut.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
<template>
<v-dialog
v-model="SimulatorState.dialogBox.customshortcut_dialog"
:persistent="false"
>
<v-card class="messageBoxContent">
<v-card-text>
<p class="dialogHeader">Keybinding Preference</p>
<v-btn
size="x-small"
icon
class="dialogClose"
@click="closeDialog()"
>
<v-icon>mdi-close</v-icon>
</v-btn>
<v-text-field label="Search Command" v-model="search"></v-text-field>
<div v-if="search && searchCommand().length" id="customShortcutDialog" title="Keybinding Preference">
<!-- Edit Panel -->
<div id="edit" tabindex="0" @keydown="updateEdit($event)">
<span style="font-size: 14px">
Press Desire Key Combination & press Enter or press
ESC to cancel.
</span>
<div id="pressedKeys"></div>
<div id="warning"></div>
</div>
<!-- Heading -->
<div id="heading">
<span>Command</span>
<span>Keymapping</span>
</div>
<!-- Markup -->
<div
id="preference"
class="customScroll"
@click="updatePreference($event)"
>
<!-- Elements -->
<div
v-for="(keyOption, index) in searchCommand()"
:key="index"
>
<span id="edit-icon"></span>
<div>
<span id="command">{{ keyOption[0] }}</span>
<span id="keyword">{{keyOption[1]}}</span>
</div>
</div>
</div>
</div>
<div v-if="!search" id="customShortcutDialog" title="Keybinding Preference">
<!-- Edit Panel -->
<div id="edit" tabindex="0" @keydown="updateEdit($event)">
<span style="font-size: 14px">
Press Desire Key Combination & press Enter or press
ESC to cancel.
</span>
<div id="pressedKeys"></div>
<div id="warning"></div>
</div>
<!-- Heading -->
<div id="heading">
<span>Command</span>
<span>Keymapping</span>
</div>
<!-- Markup -->
<div
id="preference"
class="customScroll"
@click="updatePreference($event)"
>
<!-- Elements -->
<div
v-for="(keyOption, index) in keyOptions"
:key="index"
>
<span id="edit-icon"></span>
<div>
<span id="command">{{ keyOption[0] }}</span>
<span id="keyword">{{keyOption[1]}}</span>
</div>
</div>
</div>
</div>
</v-card-text>
<v-card-actions>
<v-btn class="messageBtn" block @click="resetKeybinding()">
Reset To Default
</v-btn>
<v-btn class="messageBtn" block @click="saveKeybinding()">
Save
</v-btn>
</v-card-actions>
</v-card>
</v-dialog>
</template>
<script lang="ts" setup>
import { defaultKeys } from '#/simulator/src/hotkey_binder/defaultKeys'
import {
addKeys,
checkUpdate,
setDefault,
warnOverride,
} from '#/simulator/src/hotkey_binder/model/actions'
import { KeyCode } from '#/simulator/src/hotkey_binder/model/normalize/normalizer.plugin'
import { checkRestricted } from '#/simulator/src/hotkey_binder/model/utils'
import {
closeEdit,
override,
submit,
updateHTML,
} from '#/simulator/src/hotkey_binder/view/panel.ui'
import { useState } from '#/store/SimulatorStore/state'
import { onMounted, onUpdated, ref } from '@vue/runtime-core'
const SimulatorState = useState()
const keyOptions = ref([])
const targetPref = ref(null)
const search = ref('');
onMounted(() => {
SimulatorState.dialogBox.customshortcut_dialog = false
keyOptions.value = Object.entries(defaultKeys)
})
onUpdated(() => {
if (localStorage.userKeys) {
checkUpdate()
addKeys('user')
} else setDefault()
})
const searchCommand=()=>{
if(!search.value) return [];
const searchResult = keyOptions.value.filter((target)=>{
return target[0].toLowerCase().includes(search.value.toLowerCase());
})
return searchResult;
}
function updatePreference(e) {
$('#pressedKeys').text('')
$('#warning').text('')
$('#edit').css('border', 'none')
$('#edit').css('display', 'block')
$($('#edit')).focus()
;[, targetPref.value] = e.target.closest('div').children
}
function updateEdit(e) {
e = e || window.event
e.stopPropagation()
e.preventDefault()
var k = KeyCode
let modifiers = ['CTRL', 'ALT', 'SHIFT', 'META']
$('#edit').css('animation', 'none')
$('#warning').text('')
if (e.keyCode === 27) closeEdit()
if (e.keyCode === 13) {
if ($('#pressedKeys').text() === '') {
$('#warning').text('Please enter some key(s)')
$('#edit').css('animation', 'shake .3s linear')
return
}
if (!checkRestricted($('#pressedKeys').text())) {
override($('#pressedKeys').text())
targetPref.value.innerText = $('#pressedKeys').text()
$('#pressedKeys').text('')
$('#edit').css('display', 'none')
} else {
$('#warning').text('Please enter different key(s).')
$('#edit').css('animation', 'shake .3s linear')
$('#pressedKeys').text('')
}
}
const currentKey =
k.hot_key(k.translate_event(e)).split('+').join(' + ') !== 'Enter'
? k.hot_key(k.translate_event(e)).split('+').join(' + ')
: ''
if (
$('#pressedKeys').text().split(' + ').length === 2 &&
!modifiers.includes(currentKey) &&
modifiers.includes($('#pressedKeys').text().split(' + ')[1])
) {
$('#pressedKeys').append(` + ${currentKey}`)
} else if (modifiers.includes($('#pressedKeys').text())) {
modifiers = modifiers.filter((mod) => mod === $('#pressedKeys').text())
if (!modifiers.includes(currentKey)) {
$('#pressedKeys').append(` + ${currentKey}`)
}
} else {
$('#pressedKeys').text('')
$('#pressedKeys').text(currentKey)
}
if (!$('#pressedKeys').text()) {
$('#pressedKeys').text(currentKey)
}
if (
($('#pressedKeys').text().split(' + ').length === 2 &&
['Ctrl', 'Meta'].includes(
$('#pressedKeys').text().split(' + ')[1]
)) ||
($('#pressedKeys').text().split(' + ')[0] === 'Alt' &&
$('#pressedKeys').text().split(' + ')[1] === 'Shift')
) {
$('#pressedKeys').text(
$('#pressedKeys').text().split(' + ').reverse().join(' + ')
)
}
warnOverride($('#pressedKeys').text(), targetPref.value)
if (checkRestricted($('#pressedKeys').text())) {
$('#warning').text(
'The above combination is a system default shortcut & cannot be set.'
)
}
}
function resetKeybinding() {
console.log('Reset Keybinding')
if (confirm('Remove all custom keys & set the default keys?')) setDefault()
}
function saveKeybinding() {
console.log('Save Keybinding')
submit()
SimulatorState.dialogBox.customshortcut_dialog = false
}
function closeDialog() {
if (localStorage.userKeys) {
updateHTML('user')
} else updateHTML('default')
SimulatorState.dialogBox.customshortcut_dialog = false
}
</script>