Skip to content

Commit

Permalink
Escape keys (#880)
Browse files Browse the repository at this point in the history
* Implements `escapeKeys` to close the inspector

* `inspector` is no longer experimental

* Changelog entry for `escapeKeys` enhancement

* Generate types for `escapeKeys`
  • Loading branch information
ryangclark committed Apr 8, 2024
1 parent 826dfc6 commit 8ae3dc8
Show file tree
Hide file tree
Showing 8 changed files with 37 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/witty-games-shout.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte-inspector': minor
---

Implements `escapeKeys` to close the inspector via hotkey (default keys are `Backspace` and `Escape`).
7 changes: 7 additions & 0 deletions docs/inspector.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ SVELTE_INSPECTOR_OPTIONS=true

Define key to open the editor for the currently selected dom node.

### escapeKeys

- **Type:** `string[]`
- **Default:** `['Backspace', 'Escape']`

Define keys to close the inspector.

### holdMode

- **Type:** `boolean`
Expand Down
5 changes: 1 addition & 4 deletions packages/playground/kit-demo-app/svelte.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@ const config = {
adapter: adapter()
},
vitePlugin: {
experimental: {
inspector: true
}
inspector: true
}
};

export default config;
1 change: 1 addition & 0 deletions packages/vite-plugin-svelte-inspector/src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { debug } from './debug.js';
export const defaultInspectorOptions = {
toggleKeyCombo: process.platform === 'darwin' ? 'meta-shift' : 'control-shift',
navKeys: { parent: 'ArrowUp', child: 'ArrowDown', next: 'ArrowRight', prev: 'ArrowLeft' },
escapeKeys: ['Backspace', 'Escape'],
openKey: 'Enter',
holdMode: true,
showToggleButton: 'active',
Expand Down
6 changes: 6 additions & 0 deletions packages/vite-plugin-svelte-inspector/src/public.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export interface Options {
*/
openKey?: string;

/**
* define keys to close the inspector
* @default ['Backspace', 'Escape']
*/
escapeKeys?: string[];

/**
* inspector is automatically disabled when releasing toggleKeyCombo after holding it for a longpress
* @default true
Expand Down
12 changes: 10 additions & 2 deletions packages/vite-plugin-svelte-inspector/src/runtime/Inspector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import options from 'virtual:svelte-inspector-options';
const toggle_combo = options.toggleKeyCombo?.toLowerCase().split('-');
const escape_keys = options.escapeKeys?.map((key) => key?.toLowerCase());
const nav_keys = Object.values(options.navKeys).map((k) => k.toLowerCase());
let enabled = false;
let has_opened = false;
Expand Down Expand Up @@ -148,6 +149,10 @@
return is_toggle(event) && toggle_combo?.every((key) => is_key_active(key, event));
}
function is_escape(event) {
return escape_keys?.includes(event.key.toLowerCase());
}
function is_toggle(event) {
return toggle_combo?.includes(event.key.toLowerCase());
}
Expand Down Expand Up @@ -188,8 +193,11 @@
}
} else if (is_open(event)) {
open_editor(event);
} else if (is_holding()) {
// unhandled additional key pressed while holding, possibly another shortcut, disable again
} else if (is_holding() || is_escape(event)) {
// is_holding() checks for unhandled additional key pressed
// while holding the toggle keys, which is possibly another
// shortcut (e.g. 'meta-shift-x'), so disable again.
disable();
}
}
Expand Down
6 changes: 6 additions & 0 deletions packages/vite-plugin-svelte-inspector/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,12 @@ declare module '@sveltejs/vite-plugin-svelte-inspector' {
*/
openKey?: string;

/**
* define keys to close the inspector
* @default ['Backspace', 'Escape']
*/
escapeKeys?: string[];

/**
* inspector is automatically disabled when releasing toggleKeyCombo after holding it for a longpress
* @default true
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin-svelte-inspector/types/index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
null,
null
],
"mappings": ";kBAAiBA,OAAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCoBRC,eAAeA"
"mappings": ";kBAAiBA,OAAOA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;iBCoBRC,eAAeA"
}

0 comments on commit 8ae3dc8

Please sign in to comment.