Skip to content

Commit 5bc026f

Browse files
committed
update SkMoustTrap service interface: add, remove
... bindings
1 parent 409f2bb commit 5bc026f

File tree

2 files changed

+59
-34
lines changed

2 files changed

+59
-34
lines changed

packages/common-ui/src/components/SkMouseTrapToolTip.vue

Lines changed: 19 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,19 +3,19 @@
33
class="sk-mousetrap-tooltip-wrapper"
44
ref="wrapperElement"
55
:class="[
6-
isControlKeyPressed && !disabled && highlightEffect !== 'none' ? `sk-mousetrap-highlight-${highlightEffect}` : ''
6+
isControlKeyPressed && !disabled && highlightEffect !== 'none' ? `sk-mousetrap-highlight-${highlightEffect}` : '',
77
]"
88
>
99
<slot></slot>
1010
<transition name="fade">
1111
<div
1212
v-if="showTooltip && isControlKeyPressed && !disabled"
1313
class="sk-mousetrap-tooltip"
14-
:class="{
15-
'sk-mt-tooltip-top': position === 'top',
14+
:class="{
15+
'sk-mt-tooltip-top': position === 'top',
1616
'sk-mt-tooltip-bottom': position === 'bottom',
1717
'sk-mt-tooltip-left': position === 'left',
18-
'sk-mt-tooltip-right': position === 'right'
18+
'sk-mt-tooltip-right': position === 'right',
1919
}"
2020
>
2121
{{ formattedHotkey }}
@@ -70,11 +70,11 @@ export default defineComponent({
7070
const hotkey = Array.isArray(props.hotkey) ? props.hotkey[0] : props.hotkey;
7171
// Check if this is a sequence (has spaces) or a combination (has +)
7272
if (hotkey.includes(' ')) {
73-
// For sequences like "g h", display as "G, H"
73+
// For sequences like "g h", display as "g, h"
7474
return hotkey
7575
.toLowerCase()
7676
.split(' ')
77-
.map((part) => part.charAt(0).toUpperCase() + part.slice(1))
77+
.map((part) => part.charAt(0) + part.slice(1))
7878
.join(', ');
7979
} else {
8080
// For combinations like "ctrl+s", display as "Ctrl + S"
@@ -139,11 +139,12 @@ export default defineComponent({
139139
}
140140
141141
// If still no element found, try the wrapper itself - it might be clickable
142-
if (!clickableElement && (
143-
wrapperElement.value.hasAttribute('to') ||
144-
wrapperElement.value.tagName === 'A' ||
145-
wrapperElement.value.classList.contains('v-list-item')
146-
)) {
142+
if (
143+
!clickableElement &&
144+
(wrapperElement.value.hasAttribute('to') ||
145+
wrapperElement.value.tagName === 'A' ||
146+
wrapperElement.value.classList.contains('v-list-item'))
147+
) {
147148
clickableElement = wrapperElement.value;
148149
}
149150
@@ -185,32 +186,17 @@ export default defineComponent({
185186
// Register/unregister the hotkey binding
186187
const registerHotkey = () => {
187188
if (!props.disabled) {
188-
SkldrMouseTrap.bind([
189-
{
190-
hotkey: props.hotkey,
191-
command: props.command,
192-
callback: handleHotkeyPress,
193-
},
194-
]);
189+
SkldrMouseTrap.addBinding({
190+
hotkey: props.hotkey,
191+
command: props.command,
192+
callback: handleHotkeyPress,
193+
});
195194
}
196195
};
197196
198197
const unregisterHotkey = () => {
199-
// Currently, SkldrMouseTrap only supports full reset
200-
// To avoid affecting other hotkeys, we need to track all active hotkeys
201-
// and rebind the ones we want to keep
202-
const currentCommands = [...SkldrMouseTrap.commands];
203-
const filteredCommands = currentCommands.filter(
204-
(cmd) => JSON.stringify(cmd.hotkey) !== JSON.stringify(props.hotkey)
205-
);
206-
207-
if (filteredCommands.length !== currentCommands.length) {
208-
// There was a match - we need to reset and rebind the remaining hotkeys
209-
SkldrMouseTrap.reset();
210-
211-
// Rebind all the other hotkeys
212-
// Note: this approach has limitations since we don't have access to the callbacks
213-
// In a future enhancement, SkldrMouseTrap should be updated to support selective unbinding
198+
if (!props.disabled) {
199+
SkldrMouseTrap.removeBinding(props.hotkey);
214200
}
215201
};
216202

packages/common-ui/src/utils/SkldrMouseTrap.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,49 @@ export class SkldrMouseTrap {
3636
SkldrMouseTrap.instance().hotkeys = hk;
3737

3838
hk.forEach((k) => {
39-
Mousetrap.bindGlobal(k.hotkey, k.callback);
39+
Mousetrap.bindGlobal(k.hotkey, (a, b) => {
40+
console.log(`Running ${k.hotkey}`);
41+
k.callback(a, b);
42+
});
4043
});
4144
}
4245

46+
/**
47+
* Add bindings without resetting existing ones
48+
*/
49+
public static addBinding(hk: HotKey | HotKey[]) {
50+
const hotkeys = Array.isArray(hk) ? hk : [hk];
51+
const instance = SkldrMouseTrap.instance();
52+
53+
// Add to internal registry
54+
instance.hotkeys = [...instance.hotkeys, ...hotkeys];
55+
56+
// Bind each hotkey
57+
hotkeys.forEach((k) => {
58+
Mousetrap.bindGlobal(k.hotkey, (a, b) => {
59+
console.log(`Running ${k.hotkey}`);
60+
k.callback(a, b);
61+
});
62+
});
63+
}
64+
65+
/**
66+
* Remove a specific binding without affecting others
67+
*/
68+
public static removeBinding(hotkey: string | string[]) {
69+
const instance = SkldrMouseTrap.instance();
70+
const currentHotkeys = [...instance.hotkeys];
71+
72+
// Remove from internal registry
73+
instance.hotkeys = currentHotkeys.filter(k => {
74+
// Convert both to JSON for comparison to handle arrays correctly
75+
return JSON.stringify(k.hotkey) !== JSON.stringify(hotkey);
76+
});
77+
78+
// Unbind from Mousetrap
79+
Mousetrap.unbind(hotkey);
80+
}
81+
4382
public static reset() {
4483
Mousetrap.reset();
4584
SkldrMouseTrap.instance().mouseTrap.reset();

0 commit comments

Comments
 (0)