-
Notifications
You must be signed in to change notification settings - Fork 150
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
ThatDeparted2061
wants to merge
9
commits into
CircuitVerse:main
Choose a base branch
from
ThatDeparted2061:keybinder.js
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
02fff18
refactor
ThatDeparted2061 13310fa
Merge branch 'main' into keybinder.js
ThatDeparted2061 787043d
Merge branch 'main' into keybinder.js
ThatDeparted2061 19d0f55
resolve
ThatDeparted2061 78edda4
refactor
ThatDeparted2061 f9c989d
resolve
ThatDeparted2061 d411d62
resolve
ThatDeparted2061 fc4b26d
Merge branch 'main' into keybinder.js
ThatDeparted2061 9276f21
Merge branch 'main' into keybinder.js
ThatDeparted2061 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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>( | ||
+ (() => { | ||
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) | ||
} | ||
ThatDeparted2061 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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.
📝 Committable suggestion