Skip to content

refactor: remove jQuery & DOM logic from CustomShortcut.vue #582

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 32 additions & 17 deletions src/components/DialogBox/CustomShortcut.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,17 @@
</v-btn>
<div id="customShortcutDialog" title="Keybinding Preference">
<!-- Edit Panel -->
<div id="edit" tabindex="0" @keydown="updateEdit($event)">
<div
id="edit"
tabindex="0"
@keydown="updateEdit($event)"
:style="{
border: editBorder,
display: editDisplay,
animation: editAnimation
}"
ref="editRef"
>
<span style="font-size: 14px">
{{
$t(
Expand Down Expand Up @@ -122,6 +132,12 @@ const keyOptions = ref<KeyOption[]>([])
const targetPref = ref<HTMLSpanElement | null>(null)
const pressedKeys: Ref<string> = ref('')
const warning: Ref<string> = ref('')
const editRef = ref<HTMLElement | null>(null)

// Reactive properties for edit element styling
const editBorder = ref('none')
const editDisplay = ref('none')
const editAnimation = ref('none')

onMounted(() => {
if (localStorage.userKeys) {
Expand All @@ -144,9 +160,12 @@ onUpdated(() => {
function updatePreference(e: MouseEvent) {
pressedKeys.value = ''
warning.value = ''
document.getElementById('edit')!.style.border = 'none'
document.getElementById('edit')!.style.display = 'block'
document.getElementById('edit')!.focus()
editBorder.value = 'none'
editDisplay.value = 'block'
// Focus the edit element using Vue ref
if (editRef.value) {
editRef.value.focus()
}
;[, targetPref.value] = e.target!.closest('div')!.children as [
HTMLSpanElement,
HTMLSpanElement
Expand All @@ -159,26 +178,24 @@ function updateEdit(e: KeyboardEvent) {
e.preventDefault()
const k = KeyCode
let modifiers = ['CTRL', 'ALT', 'SHIFT', 'META']
document.getElementById('edit')!.style.animation = 'none'
editAnimation.value = 'none'
warning.value = ''
if (e.keyCode === 27) closeEdit()
if (e.keyCode === 13) {
if (pressedKeys.value === '') {
warning.value = 'Please enter some key(s)'
document.getElementById('edit')!.style.animation =
'shake .3s linear'
editAnimation.value = 'shake .3s linear'
return
}

if (!checkRestricted(pressedKeys.value)) {
override(pressedKeys.value)
targetPref.value!.innerText = pressedKeys.value
pressedKeys.value = ''
document.getElementById('edit')!.style.display = 'none'
editDisplay.value = 'none'
} else {
warning.value = 'Please enter different key(s).'
document.getElementById('edit')!.style.animation =
'shake .3s linear'
editAnimation.value = 'shake .3s linear'
pressedKeys.value = ''
return
}
Expand Down Expand Up @@ -232,16 +249,14 @@ function saveKeybinding() {
}

function closeDialog() {
const editDialogState = document.getElementById('edit')!.style.display
if (editDialogState === 'block') {
document.getElementById('edit')!.style.display = 'none'
if (editDisplay.value === 'block') {
editDisplay.value = 'none'
}
}

function closeAllDialog() {
const editDialogState = document.getElementById('edit')!.style.display
if (editDialogState === 'block') {
document.getElementById('edit')!.style.display = 'none'
if (editDisplay.value === 'block') {
editDisplay.value = 'none'
}
if (localStorage.userKeys) {
updateHTML('user')
Expand Down Expand Up @@ -272,4 +287,4 @@ function closeAllDialog() {
width: 100%;
}
}
</style>
</style>