|
4 | 4 |
|
5 | 5 | import options from 'virtual:svelte-inspector-options';
|
6 | 6 | 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 | +
|
9 | 11 | let enabled = false;
|
10 | 12 | let has_opened = false;
|
11 | 13 |
|
|
34 | 36 | // eslint-disable-next-line svelte/valid-compile
|
35 | 37 | options.showToggleButton === 'always' || (options.showToggleButton === 'active' && enabled);
|
36 | 38 |
|
37 |
| - function mousemove(event) { |
38 |
| - x = event.x; |
39 |
| - y = event.y; |
| 39 | + function mousemove(e) { |
| 40 | + x = e.x; |
| 41 | + y = e.y; |
40 | 42 | }
|
41 | 43 |
|
42 | 44 | function find_selectable_parent(el, include_self = false) {
|
|
123 | 125 | }
|
124 | 126 | }
|
125 | 127 |
|
126 |
| - function open_editor(event) { |
| 128 | + function open_editor(e) { |
127 | 129 | if (file_loc) {
|
128 |
| - stop(event); |
| 130 | + stop(e); |
129 | 131 | fetch(`${options.__internal.base}/__open-in-editor?file=${encodeURIComponent(file_loc)}`);
|
130 | 132 | has_opened = true;
|
131 | 133 | if (options.holdMode && is_holding()) {
|
|
134 | 136 | }
|
135 | 137 | }
|
136 | 138 |
|
137 |
| - function is_key_active(key, event) { |
| 139 | + function is_active(key, e) { |
138 | 140 | switch (key) {
|
139 | 141 | case 'shift':
|
140 | 142 | case 'control':
|
141 | 143 | case 'alt':
|
142 | 144 | case 'meta':
|
143 |
| - return event.getModifierState(key.charAt(0).toUpperCase() + key.slice(1)); |
| 145 | + return e.getModifierState(key.charAt(0).toUpperCase() + key.slice(1)); |
144 | 146 | default:
|
145 |
| - return key === event.key.toLowerCase(); |
| 147 | + return key === e.code.replace(/^Key/, '').toLowerCase() || key === e.key.toLowerCase(); |
146 | 148 | }
|
147 | 149 | }
|
148 | 150 |
|
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)); |
151 | 153 | }
|
152 | 154 |
|
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)); |
155 | 157 | }
|
156 | 158 |
|
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)); |
159 | 161 | }
|
160 | 162 |
|
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)); |
163 | 165 | }
|
164 | 166 |
|
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); |
167 | 169 | }
|
168 | 170 |
|
169 | 171 | function is_holding() {
|
170 | 172 | return hold_start_ts && Date.now() - hold_start_ts > 250;
|
171 | 173 | }
|
172 | 174 |
|
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(); |
177 | 179 | }
|
178 | 180 |
|
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))) { |
181 | 183 | return;
|
182 | 184 | }
|
183 |
| - if (is_combo(event)) { |
| 185 | + if (is_combo(e)) { |
184 | 186 | toggle();
|
185 | 187 | if (options.holdMode && enabled) {
|
186 | 188 | hold_start_ts = Date.now();
|
187 | 189 | }
|
188 | 190 | } 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); |
191 | 193 | if (el) {
|
192 | 194 | activate(el);
|
193 |
| - stop(event); |
| 195 | + stop(e); |
194 | 196 | }
|
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)) { |
198 | 200 | // is_holding() checks for unhandled additional key pressed
|
199 | 201 | // while holding the toggle keys, which is possibly another
|
200 | 202 | // shortcut (e.g. 'meta-shift-x'), so disable again.
|
|
204 | 206 | }
|
205 | 207 | }
|
206 | 208 |
|
207 |
| - function keyup(event) { |
208 |
| - if (event.repeat || event.key == null || !enabled) { |
| 209 | + function keyup(e) { |
| 210 | + if (e.repeat || e.key == null || !enabled) { |
209 | 211 | return;
|
210 | 212 | }
|
211 |
| - if (is_toggle(event)) { |
| 213 | + if (is_toggle(e)) { |
212 | 214 | if (is_holding()) {
|
213 | 215 | disable();
|
214 | 216 | } else {
|
|
0 commit comments