Skip to content
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

refactor: TS integration, jquery Dom removal : src/simulator/src/hotkey_binder/keyBinder.vue #457

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
151 changes: 0 additions & 151 deletions src/simulator/src/hotkey_binder/keyBinder.js

This file was deleted.

169 changes: 169 additions & 0 deletions src/simulator/src/hotkey_binder/keyBinder.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<template>
<div v-if="showDialog" class="custom-shortcut-dialog">
<div class="dialog-content">
<h2>Custom Shortcuts</h2>

<div class="key-bindings">
<div v-for="(binding, key) in keyBindings" :key="key" class="binding-row">
<label>{{ key }}</label>
<div class="preference" @click="startEditing(key.toString())">
{{ binding.custom || binding.default }}
</div>
</div>
</div>

<div v-if="editingKey" class="edit-panel">
<p>Press your desired key combination</p>
<div class="key-display">
{{ pressedKeys.join(' + ') }}
<span v-if="warning" class="warning">{{ warning }}</span>
</div>
<div class="dialog-actions">
<button @click="cancelEdit">Cancel</button>
<button @click="saveBinding">Save</button>
</div>
</div>

<div class="dialog-footer">
<button @click="resetToDefault">Reset to Default</button>
<button @click="closeDialog">Close</button>
</div>
</div>
</div>
</template>

<script lang="ts">
import { defineComponent, ref, reactive, watch, Ref, onUnmounted } from 'vue'
import { checkRestricted } from './model/utils'
import { KeyCode } from './model/normalize/normalizer.plugin.js'
import { KeyBindings } from './keyBinding.types';

export default defineComponent({
name: 'KeyBinder',
setup() {
const showDialog = ref<boolean>(false)
const editingKey = ref<string | null>(null)
const pressedKeys = ref<string[]>([])
const warning = ref<string>('')

const keyBindings = reactive<KeyBindings>(
+ (() => {
Comment on lines +49 to +50
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Remove the extraneous unary plus.
A unary plus operator before the immediately-invoked function expression causes the returned object to be cast to a number (resulting in NaN). Removing it will ensure the function’s result is used as intended.

49     const keyBindings = reactive<KeyBindings>(
50-      +  (() => {
50+      (() => {
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const keyBindings = reactive<KeyBindings>(
+ (() => {
const keyBindings = reactive<KeyBindings>(
(() => {

try {
const stored = localStorage.getItem('userKeys');
return stored ? JSON.parse(stored) : {
togglePanel: { default: 'Ctrl+P', custom: '' },
saveFile: { default: 'Ctrl+S', custom: '' }
};
} catch (e) {
console.error('Failed to parse stored key bindings:', e);
return {
togglePanel: { default: 'Ctrl+P', custom: '' },
saveFile: { default: 'Ctrl+S', custom: '' }
};
}
})()
)

watch(keyBindings, (newVal: KeyBindings) => {
localStorage.setItem('userKeys', JSON.stringify(newVal))
})

const startEditing = (key: string): void => {
editingKey.value = key
pressedKeys.value = []
warning.value = ''
window.addEventListener('keydown', handleKeyDown)
}

const handleKeyDown = (e: KeyboardEvent): void => {
e.preventDefault()
const key = KeyCode.hot_key(KeyCode.translate_event(e))

if (key === 'Escape') {
cancelEdit()
return
}

if (key === 'Enter') {
saveBinding()
return
}

if (checkRestricted(key)) {
warning.value = 'Restricted system shortcut'
return
}

const keys = [...new Set([...pressedKeys.value, key])]
.sort((a, b) => a.localeCompare(b))

pressedKeys.value = keys
}

const saveBinding = (): void => {
if (pressedKeys.value.length === 0) {
warning.value = 'Please enter some keys'
return
}

if (editingKey.value) {
keyBindings[editingKey.value].custom = pressedKeys.value.join(' + ')
}
cancelEdit()
}

const cancelEdit = (): void => {
editingKey.value = null
pressedKeys.value = []
window.removeEventListener('keydown', handleKeyDown)
}

onUnmounted(() => {
window.removeEventListener('keydown', handleKeyDown);
})

const resetToDefault = (): void => {
if (confirm('Reset all to default?')) {
Object.keys(keyBindings).forEach(key => {
keyBindings[key].custom = ''
})
}
}

const closeDialog = (): void => {
showDialog.value = false
}

return {
showDialog,
editingKey,
pressedKeys,
warning,
keyBindings,
startEditing,
saveBinding,
cancelEdit,
resetToDefault,
closeDialog
}
}
})
</script>

<style scoped>
.custom-shortcut-dialog {
position: fixed;
/* Add more dialog styling */
}

.key-display {
border: 1px solid #ccc;
padding: 8px;
margin: 10px 0;
}

.warning {
color: red;
margin-left: 10px;
}
</style>
8 changes: 8 additions & 0 deletions src/simulator/src/hotkey_binder/keyBinding.types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export interface KeyBinding {
custom?: string;
default: string;
}

export interface KeyBindings {
[key: string]: KeyBinding;
}
Loading