Skip to content

Commit 213fedd

Browse files
authoredOct 17, 2024··
breaking(options): change default key-combo for svelte-inspector to alt-x (#995)
* breaking(options): change default key-combo for svelte-inspector to alt-x * fix(inspector): consistently handle key identification with event.code and event.key

File tree

4 files changed

+49
-42
lines changed

4 files changed

+49
-42
lines changed
 

‎.changeset/tall-monkeys-shout.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte-inspector': major
3+
---
4+
5+
Change default key-combo to `alt-x` to avoid conflicts with other combos that started with the previous defaults.

‎docs/inspector.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ export default {
2424
export default {
2525
vitePlugin: {
2626
inspector: {
27-
toggleKeyCombo: 'meta-shift',
27+
toggleKeyCombo: 'alt-x',
2828
showToggleButton: 'always',
2929
toggleButtonPos: 'bottom-right'
3030
}
@@ -39,7 +39,7 @@ To allow you to use your own setup, svelte inspector can be configured via envir
3939

4040
```shell
4141
# just keycombo, unquoted string
42-
SVELTE_INSPECTOR_TOGGLE=control-shift
42+
SVELTE_INSPECTOR_TOGGLE=alt-x
4343

4444
# options object as json
4545
SVELTE_INSPECTOR_OPTIONS='{"holdMode": false, "toggleButtonPos": "bottom-left"}'
@@ -58,13 +58,13 @@ SVELTE_INSPECTOR_OPTIONS=true
5858
### toggleKeyCombo
5959

6060
- **Type:** `string`
61-
- **Default:** `'meta-shift'` on mac, `'control-shift'` on other os
61+
- **Default:** `'alt-x'`
6262

6363
Define a key combo to toggle inspector.
6464

65-
The value is recommended to be any number of modifiers (e.g. `control`, `shift`, `alt`, `meta`) followed by zero or one regular key, separated by `-`. This helps avoid conflicts or accidentally typing into inputs. Note that some keys have native behavior (e.g. `alt-s` opens history menu on firefox).
65+
The value is recommended to be any number of modifiers (e.g. `control`, `shift`, `alt`, `meta`) followed by zero or one regular key, separated by `-`. Note that some keys have native behavior (e.g. `alt-s` opens history menu on firefox).
6666

67-
Examples: `control-shift`, `control-o`, `control-alt-s`, `meta-x`, `control-meta`.
67+
Examples: `control-o`, `control-alt-s`, `meta-x`, `alt-i`.
6868

6969
### navKeys
7070

‎packages/vite-plugin-svelte-inspector/src/options.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { debug } from './debug.js';
44

55
/** @type {import('./public.d.ts').Options} */
66
export const defaultInspectorOptions = {
7-
toggleKeyCombo: process.platform === 'darwin' ? 'meta-shift' : 'control-shift',
7+
toggleKeyCombo: 'alt-x',
88
navKeys: { parent: 'ArrowUp', child: 'ArrowDown', next: 'ArrowRight', prev: 'ArrowLeft' },
99
escapeKeys: ['Backspace', 'Escape'],
1010
openKey: 'Enter',

‎packages/vite-plugin-svelte-inspector/src/runtime/Inspector.svelte

+38-36
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,10 @@
44
55
import options from 'virtual:svelte-inspector-options';
66
const toggle_combo = options.toggleKeyCombo?.toLowerCase().split('-');
7-
const escape_keys = options.escapeKeys?.map((key) => key?.toLowerCase());
8-
const nav_keys = Object.values(options.navKeys).map((k) => k.toLowerCase());
7+
const escape_keys = options.escapeKeys?.map((k) => k.toLowerCase());
8+
const nav_keys = Object.values(options.navKeys).map((k) => k?.toLowerCase());
9+
const open_key = options.openKey?.toLowerCase();
10+
911
let enabled = false;
1012
let has_opened = false;
1113
@@ -34,9 +36,9 @@
3436
// eslint-disable-next-line svelte/valid-compile
3537
options.showToggleButton === 'always' || (options.showToggleButton === 'active' && enabled);
3638
37-
function mousemove(event) {
38-
x = event.x;
39-
y = event.y;
39+
function mousemove(e) {
40+
x = e.x;
41+
y = e.y;
4042
}
4143
4244
function find_selectable_parent(el, include_self = false) {
@@ -123,9 +125,9 @@
123125
}
124126
}
125127
126-
function open_editor(event) {
128+
function open_editor(e) {
127129
if (file_loc) {
128-
stop(event);
130+
stop(e);
129131
fetch(`${options.__internal.base}/__open-in-editor?file=${encodeURIComponent(file_loc)}`);
130132
has_opened = true;
131133
if (options.holdMode && is_holding()) {
@@ -134,67 +136,67 @@
134136
}
135137
}
136138
137-
function is_key_active(key, event) {
139+
function is_active(key, e) {
138140
switch (key) {
139141
case 'shift':
140142
case 'control':
141143
case 'alt':
142144
case 'meta':
143-
return event.getModifierState(key.charAt(0).toUpperCase() + key.slice(1));
145+
return e.getModifierState(key.charAt(0).toUpperCase() + key.slice(1));
144146
default:
145-
return key === event.key.toLowerCase();
147+
return key === e.code.replace(/^Key/, '').toLowerCase() || key === e.key.toLowerCase();
146148
}
147149
}
148150
149-
function is_combo(event) {
150-
return is_toggle(event) && toggle_combo?.every((key) => is_key_active(key, event));
151+
function is_combo(e) {
152+
return toggle_combo?.every((k) => is_active(k, e));
151153
}
152154
153-
function is_escape(event) {
154-
return escape_keys?.includes(event.key.toLowerCase());
155+
function is_escape(e) {
156+
return escape_keys?.some((k) => is_active(k, e));
155157
}
156158
157-
function is_toggle(event) {
158-
return toggle_combo?.includes(event.key.toLowerCase());
159+
function is_toggle(e) {
160+
return toggle_combo?.some((k) => is_active(k, e));
159161
}
160162
161-
function is_nav(event) {
162-
return nav_keys?.some((key) => is_key_active(key, event));
163+
function is_nav(e) {
164+
return nav_keys?.some((k) => is_active(k, e));
163165
}
164166
165-
function is_open(event) {
166-
return options.openKey && options.openKey.toLowerCase() === event.key.toLowerCase();
167+
function is_open(e) {
168+
return open_key && is_active(open_key, e);
167169
}
168170
169171
function is_holding() {
170172
return hold_start_ts && Date.now() - hold_start_ts > 250;
171173
}
172174
173-
function stop(event) {
174-
event.preventDefault();
175-
event.stopPropagation();
176-
event.stopImmediatePropagation();
175+
function stop(e) {
176+
e.preventDefault();
177+
e.stopPropagation();
178+
e.stopImmediatePropagation();
177179
}
178180
179-
function keydown(event) {
180-
if (event.repeat || event.key == null || (!enabled && !is_toggle(event))) {
181+
function keydown(e) {
182+
if (e.repeat || e.key == null || (!enabled && !is_toggle(e))) {
181183
return;
182184
}
183-
if (is_combo(event)) {
185+
if (is_combo(e)) {
184186
toggle();
185187
if (options.holdMode && enabled) {
186188
hold_start_ts = Date.now();
187189
}
188190
} else if (enabled) {
189-
if (is_nav(event)) {
190-
const el = find_selectable_for_nav(event.key);
191+
if (is_nav(e)) {
192+
const el = find_selectable_for_nav(e.key);
191193
if (el) {
192194
activate(el);
193-
stop(event);
195+
stop(e);
194196
}
195-
} else if (is_open(event)) {
196-
open_editor(event);
197-
} else if (is_holding() || is_escape(event)) {
197+
} else if (is_open(e)) {
198+
open_editor(e);
199+
} else if (is_holding() || is_escape(e)) {
198200
// is_holding() checks for unhandled additional key pressed
199201
// while holding the toggle keys, which is possibly another
200202
// shortcut (e.g. 'meta-shift-x'), so disable again.
@@ -204,11 +206,11 @@
204206
}
205207
}
206208
207-
function keyup(event) {
208-
if (event.repeat || event.key == null || !enabled) {
209+
function keyup(e) {
210+
if (e.repeat || e.key == null || !enabled) {
209211
return;
210212
}
211-
if (is_toggle(event)) {
213+
if (is_toggle(e)) {
212214
if (is_holding()) {
213215
disable();
214216
} else {

0 commit comments

Comments
 (0)
Please sign in to comment.